Skip to content

Commit

Permalink
refactor: Client{} helper struct renamed to Node. So, Node is a conne…
Browse files Browse the repository at this point in the history
…ction to a node. Compiler is a connection to a compiler.
  • Loading branch information
randomshinichi committed Jul 4, 2019
1 parent fe8c046 commit 568853e
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 79 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ if err != nil {
fmt.Println(err)
return
}
aeClient := aeternity.NewCli("http://localhost:3013", false).WithAccount(acc)
aeNode := aeternity.NewCli("http://localhost:3013", false).WithAccount(acc)
```

Most parameters are set by modifying the variables in `config.go` in this manner:
`aeternity.Config.Client.Fee = *utils.RequireBigIntFromString("100000000000000")`

When using the `Ae/Aens/Contract/Oracle` struct helper functions in `helpers.go`, chores like getting the TTL, Account Nonce, encoding of the AENS claim etc are done automatically.
```
preclaimTx, salt, err := aeClient.Aens.NamePreclaimTx("fdsa.test", aeternity.Config.Client.Fee)
preclaimTx, salt, err := aeNode.Aens.NamePreclaimTx("fdsa.test", aeternity.Config.Client.Fee)
if err != nil {
fmt.Println(err)
return
Expand All @@ -38,7 +38,7 @@ if err != nil {
return
}
err = aeClient.BroadcastTransaction(signedTxStr)
err = aeNode.BroadcastTransaction(signedTxStr)
if err != nil {
panic(err)
}
Expand Down
30 changes: 15 additions & 15 deletions aeternity/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// GetStatus post transaction
func (c *Client) GetStatus() (status *models.Status, err error) {
func (c *Node) GetStatus() (status *models.Status, err error) {
r, err := c.External.GetStatus(nil)
if err != nil {
return
Expand All @@ -18,7 +18,7 @@ func (c *Client) GetStatus() (status *models.Status, err error) {
}

// PostTransaction post transaction
func (c *Client) PostTransaction(signedEncodedTx, signedEncodedTxHash string) (err error) {
func (c *Node) PostTransaction(signedEncodedTx, signedEncodedTxHash string) (err error) {
p := external.NewPostTransactionParams().WithBody(&models.Tx{
Tx: &signedEncodedTx,
})
Expand All @@ -33,7 +33,7 @@ func (c *Client) PostTransaction(signedEncodedTx, signedEncodedTxHash string) (e
}

// GetTopBlock get the top block of the chain
func (c *Client) GetTopBlock() (kb *models.KeyBlockOrMicroBlockHeader, err error) {
func (c *Node) GetTopBlock() (kb *models.KeyBlockOrMicroBlockHeader, err error) {
r, err := c.External.GetTopBlock(nil)
if err != nil {
return
Expand All @@ -43,7 +43,7 @@ func (c *Client) GetTopBlock() (kb *models.KeyBlockOrMicroBlockHeader, err error
}

// GetHeight get the height of the chain
func (c *Client) GetHeight() (height uint64, err error) {
func (c *Node) GetHeight() (height uint64, err error) {
tb, err := c.GetTopBlock()
if err != nil {
return
Expand All @@ -57,7 +57,7 @@ func (c *Client) GetHeight() (height uint64, err error) {
}

// GetCurrentKeyBlock get current key block
func (c *Client) GetCurrentKeyBlock() (kb *models.KeyBlock, err error) {
func (c *Node) GetCurrentKeyBlock() (kb *models.KeyBlock, err error) {
r, err := c.External.GetCurrentKeyBlock(nil)
if err != nil {
err = fmt.Errorf("Error: %v", getErrorReason(err))
Expand All @@ -69,7 +69,7 @@ func (c *Client) GetCurrentKeyBlock() (kb *models.KeyBlock, err error) {

// GetAccount retrieve an account by its address (public key)
// it is particularly useful to obtain the nonce for spending transactions
func (c *Client) GetAccount(accountID string) (account *models.Account, err error) {
func (c *Node) GetAccount(accountID string) (account *models.Account, err error) {
p := external.NewGetAccountByPubkeyParams().WithPubkey(accountID)
r, err := c.External.GetAccountByPubkey(p)
if err != nil {
Expand All @@ -81,7 +81,7 @@ func (c *Client) GetAccount(accountID string) (account *models.Account, err erro
}

// GetNameEntryByName return the name entry
func (c *Client) GetNameEntryByName(name string) (nameEntry *models.NameEntry, err error) {
func (c *Node) GetNameEntryByName(name string) (nameEntry *models.NameEntry, err error) {
p := external.NewGetNameEntryByNameParams().WithName(name)
r, err := c.External.GetNameEntryByName(p)

Expand All @@ -95,7 +95,7 @@ func (c *Client) GetNameEntryByName(name string) (nameEntry *models.NameEntry, e
}

// GetGenerationByHeight gets the keyblock and all its microblocks
func (c *Client) GetGenerationByHeight(height uint64) (g *models.Generation, err error) {
func (c *Node) GetGenerationByHeight(height uint64) (g *models.Generation, err error) {
p := external.NewGetGenerationByHeightParams().WithHeight(height)
r, err := c.External.GetGenerationByHeight(p)
if err != nil {
Expand All @@ -107,7 +107,7 @@ func (c *Client) GetGenerationByHeight(height uint64) (g *models.Generation, err
}

// GetMicroBlockTransactionsByHash get the transactions of a microblock
func (c *Client) GetMicroBlockTransactionsByHash(microBlockID string) (txs *models.GenericTxs, err error) {
func (c *Node) GetMicroBlockTransactionsByHash(microBlockID string) (txs *models.GenericTxs, err error) {
p := external.NewGetMicroBlockTransactionsByHashParams().WithHash(microBlockID)
r, err := c.External.GetMicroBlockTransactionsByHash(p)
if err != nil {
Expand All @@ -119,7 +119,7 @@ func (c *Client) GetMicroBlockTransactionsByHash(microBlockID string) (txs *mode
}

// GetMicroBlockHeaderByHash get the header of a micro block
func (c *Client) GetMicroBlockHeaderByHash(microBlockID string) (txs *models.MicroBlockHeader, err error) {
func (c *Node) GetMicroBlockHeaderByHash(microBlockID string) (txs *models.MicroBlockHeader, err error) {
p := external.NewGetMicroBlockHeaderByHashParams().WithHash(microBlockID)
r, err := c.External.GetMicroBlockHeaderByHash(p)
if err != nil {
Expand All @@ -131,7 +131,7 @@ func (c *Client) GetMicroBlockHeaderByHash(microBlockID string) (txs *models.Mic
}

// GetKeyBlockByHash get a key block by its hash
func (c *Client) GetKeyBlockByHash(keyBlockID string) (txs *models.KeyBlock, err error) {
func (c *Node) GetKeyBlockByHash(keyBlockID string) (txs *models.KeyBlock, err error) {
p := external.NewGetKeyBlockByHashParams().WithHash(keyBlockID)
r, err := c.External.GetKeyBlockByHash(p)
if err != nil {
Expand All @@ -143,7 +143,7 @@ func (c *Client) GetKeyBlockByHash(keyBlockID string) (txs *models.KeyBlock, err
}

// GetTransactionByHash get a transaction by it's hash
func (c *Client) GetTransactionByHash(txHash string) (tx *models.GenericSignedTx, err error) {
func (c *Node) GetTransactionByHash(txHash string) (tx *models.GenericSignedTx, err error) {
p := external.NewGetTransactionByHashParams().WithHash(txHash)
r, err := c.External.GetTransactionByHash(p)
if err != nil {
Expand All @@ -155,7 +155,7 @@ func (c *Client) GetTransactionByHash(txHash string) (tx *models.GenericSignedTx
}

// GetOracleByPubkey get an oracle by it's public key
func (c *Client) GetOracleByPubkey(pubkey string) (oracle *models.RegisteredOracle, err error) {
func (c *Node) GetOracleByPubkey(pubkey string) (oracle *models.RegisteredOracle, err error) {
p := external.NewGetOracleByPubkeyParams().WithPubkey(pubkey)
r, err := c.External.GetOracleByPubkey(p)
if err != nil {
Expand All @@ -167,7 +167,7 @@ func (c *Client) GetOracleByPubkey(pubkey string) (oracle *models.RegisteredOrac
}

// GetOracleQueriesByPubkey get a list of queries made to a particular oracle
func (c *Client) GetOracleQueriesByPubkey(pubkey string) (oracleQueries *models.OracleQueries, err error) {
func (c *Node) GetOracleQueriesByPubkey(pubkey string) (oracleQueries *models.OracleQueries, err error) {
p := external.NewGetOracleQueriesByPubkeyParams().WithPubkey(pubkey)
r, err := c.External.GetOracleQueriesByPubkey(p)
if err != nil {
Expand All @@ -179,7 +179,7 @@ func (c *Client) GetOracleQueriesByPubkey(pubkey string) (oracleQueries *models.
}

// GetContractByID gets a contract by ct_ ID
func (c *Client) GetContractByID(ctID string) (contract *models.ContractObject, err error) {
func (c *Node) GetContractByID(ctID string) (contract *models.ContractObject, err error) {
p := external.NewGetContractParams().WithPubkey(ctID)
r, err := c.External.GetContract(p)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func BroadcastTransaction(c transactionPoster, txSignedBase64 string) (err error

// PrintGenerationByHeight utility function to print a generation by it's height
// TODO this belongs in cmd and needs to be tested with error cases
func (c *Client) PrintGenerationByHeight(height uint64) {
func (c *Node) PrintGenerationByHeight(height uint64) {
r, err := c.GetGenerationByHeight(height)
if err == nil {
PrintObject("generation", r)
Expand Down
28 changes: 14 additions & 14 deletions aeternity/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ import (
"github.com/go-openapi/strfmt"
)

// Client is the HTTP connection to the aeternity node
type Client struct {
// Node is the HTTP connection to the aeternity node
type Node struct {
*apiclient.Node
}

// Wallet is a account-specific helper that stores state relevant to spending operations
type Wallet struct {
Client *Client
Client *Node
Account *Account
}

// Aens ais a account-specific helper that stores state relevant to AENS operations
type Aens struct {
Client *Client
Client *Node
Account *Account
name string
preClaimSalt []byte
}

// Oracle is a account-specific helper that stores state relevant to oracles
type Oracle struct {
Client *Node
Account *Account
}

// Contract is a account-specific helper that stores state relevant to smtart contract execution
type Contract struct {
Client *Client
Client *Node
Compiler *Compiler
Owner string
}

// Oracle is a account-specific helper that stores state relevant to oracles
type Oracle struct {
Client *Client
Account *Account
}

func urlComponents(url string) (host string, schemas []string) {
p := strings.Split(url, "://")
if len(p) == 1 {
Expand All @@ -52,15 +52,15 @@ func urlComponents(url string) (host string, schemas []string) {
return
}

// NewClient obtain a new nodeClient instance
func NewClient(nodeURL string, debug bool) *Client {
// NewNode obtain a new swagger HTTP client to a aeternity node
func NewNode(nodeURL string, debug bool) *Node {
// create the transport
host, schemas := urlComponents(nodeURL)
transport := httptransport.New(host, "/v2", schemas)
transport.SetDebug(debug)
// create the API client, with the transport
openAPIClient := apiclient.New(transport, strfmt.Default)
aecli := &Client{
aecli := &Node{
Node: openAPIClient,
}
return aecli
Expand Down
4 changes: 2 additions & 2 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var balanceCmd = &cobra.Command{
}

func balanceFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
aeNode := NewAeNode()
p, err := getPassword()

// load the account
Expand All @@ -136,7 +136,7 @@ func balanceFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

a, err := aeCli.GetAccount(account.Address)
a, err := aeNode.GetAccount(account.Address)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var topCmd = &cobra.Command{
}

func topFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
v, err := aeCli.GetTopBlock()
aeNode := NewAeNode()
v, err := aeNode.GetTopBlock()
if err != nil {
return err
}
Expand All @@ -54,8 +54,8 @@ var statusCmd = &cobra.Command{
}

func statusFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
v, err := aeCli.GetStatus()
aeNode := NewAeNode()
v, err := aeNode.GetStatus()
if err != nil {
return err
}
Expand All @@ -72,8 +72,8 @@ var playCmd = &cobra.Command{
}

func playFunc(cmd *cobra.Command, args []string) (err error) {
aeCli := NewAeCli()
blockHeight, err := aeCli.GetHeight()
aeNode := NewAeNode()
blockHeight, err := aeNode.GetHeight()
if err != nil {
return err
}
Expand All @@ -97,7 +97,7 @@ func playFunc(cmd *cobra.Command, args []string) (err error) {
}
// run the play
for ; blockHeight > targetHeight; blockHeight-- {
aeCli.PrintGenerationByHeight(blockHeight)
aeNode.PrintGenerationByHeight(blockHeight)
fmt.Println("")
}

Expand All @@ -121,8 +121,8 @@ func broadcastFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeCli := NewAeCli()
err = aeternity.BroadcastTransaction(aeCli, txSignedBase64)
aeNode := NewAeNode()
err = aeternity.BroadcastTransaction(aeNode, txSignedBase64)
if err != nil {
errFinal := fmt.Errorf("Error while broadcasting transaction: %v", err)
return errFinal
Expand All @@ -140,7 +140,7 @@ var ttlCmd = &cobra.Command{
}

func ttlFunc(cmd *cobra.Command, args []string) (err error) {
ae := NewAeCli()
ae := NewAeNode()
height, err := ae.GetHeight()
if err != nil {
errFinal := fmt.Errorf("Error getting height from the node: %v", err)
Expand All @@ -159,7 +159,7 @@ var networkIDCmd = &cobra.Command{
}

func networkIDFunc(cmd *cobra.Command, args []string) (err error) {
ae := NewAeCli()
ae := NewAeNode()
resp, err := ae.GetStatus()
if err != nil {
errFinal := fmt.Errorf("Error getting status information from the node: %v", err)
Expand Down
Loading

0 comments on commit 568853e

Please sign in to comment.