Skip to content

Commit

Permalink
feature: Helper{} functions to lookup Accounts, Oracles, Contracts, C…
Browse files Browse the repository at this point in the history
…hannels with AENS
  • Loading branch information
randomshinichi committed Jul 26, 2019
1 parent b15da47 commit b00ea77
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
51 changes: 30 additions & 21 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aeternity

import (
"errors"
"fmt"
"io/ioutil"
"math/big"
Expand Down Expand Up @@ -32,7 +31,10 @@ type HelpersInterface interface {
GetTTL(offset uint64) (ttl uint64, err error)
GetNextNonce(accountID string) (nextNonce uint64, err error)
GetTTLNonce(accountID string, offset uint64) (height uint64, nonce uint64, err error)
GetName(name string) (accountID string, err error)
GetAccountsByName(name string) (addresses []string, err error)
GetOraclesByName(name string) (oracleIDs []string, err error)
GetContractsByName(name string) (contracts []string, err error)
GetChannelsByName(name string) (channels []string, err error)
}

// Helpers is a struct to contain the GetTTLNonce helper functions and feed them
Expand Down Expand Up @@ -77,19 +79,38 @@ func (h Helpers) GetTTLNonce(accountID string, offset uint64) (height uint64, no
return
}

// GetName returns the first account_pubkey entry that it finds in a name's Pointers.
func (h Helpers) GetName(name string) (address string, err error) {
// getAnythingByName is the underlying implementation of Get*ByName
func (h Helpers) getAnythingByName(name string, key string) (results []string, err error) {
n, err := h.node.GetNameEntryByName(name)
if err != nil {
return "", err
return []string{}, err
}
for _, p := range n.Pointers {
if *p.Key == "account_pubkey" {
return *p.ID, nil
if *p.Key == key {
results = append(results, *p.ID)
}
}
s := fmt.Sprintf("No account_pubkey entry found for %s", name)
return "", errors.New(s)
return results, nil
}

// GetAccountsByName returns any account_pubkey entries that it finds in a name's Pointers.
func (h Helpers) GetAccountsByName(name string) (addresses []string, err error) {
return h.getAnythingByName(name, "account_pubkey")
}

// GetOraclesByName returns any oracle_pubkey entries that it finds in a name's Pointers.
func (h Helpers) GetOraclesByName(name string) (oracleIDs []string, err error) {
return h.getAnythingByName(name, "oracle_pubkey")
}

// GetContractsByName returns any contract_pubkey entries that it finds in a name's Pointers.
func (h Helpers) GetContractsByName(name string) (contracts []string, err error) {
return h.getAnythingByName(name, "contract_pubkey")
}

// GetChannelsByName returns any channel entries that it finds in a name's Pointers.
func (h Helpers) GetChannelsByName(name string) (channels []string, err error) {
return h.getAnythingByName(name, "channel")
}

// Context stores relevant context (node connection, account address) that one might not want to spell out each time one creates a transaction
Expand Down Expand Up @@ -262,18 +283,6 @@ func (c *Context) ContractCallTx(ContractID, CallData string, AbiVersion uint16,
return tx, nil
}

// ContractCallTxByName returns a transaction for calling a contract on the chain
func (c *Context) ContractCallTxByName(Name, CallData string, AbiVersion uint16, Amount, Gas, GasPrice, Fee big.Int) (tx ContractCallTx, err error) {
ttl, nonce, err := c.Helpers.GetTTLNonce(c.Address, Config.Client.TTL)
if err != nil {
return ContractCallTx{}, err
}
contractID, err := c.Helpers.GetName(Name)

tx = NewContractCallTx(c.Address, nonce, contractID, Amount, Gas, GasPrice, AbiVersion, CallData, Fee, ttl)
return tx, nil
}

// StoreAccountToKeyStoreFile store an account to a json file
func StoreAccountToKeyStoreFile(account *Account, password, walletName string) (filePath string, err error) {
// keystore will be saved in current directory
Expand Down
24 changes: 24 additions & 0 deletions cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ import (
models "github.com/aeternity/aepp-sdk-go/swagguard/node/models"
)

type mockHelpers struct{}

func (mh mockHelpers) GetTTL(uint64) (uint64, error) {
return 500, nil
}
func (mh mockHelpers) GetNextNonce(string) (uint64, error) {
return 1337, nil
}
func (mh mockHelpers) GetTTLNonce(string, uint64) (uint64, uint64, error) {
return 500, 1337, nil
}
func (mh mockHelpers) GetAccountsByName(string) ([]string, error) {
return []string{"ak_address"}, nil
}
func (mh mockHelpers) GetOraclesByName(string) ([]string, error) {
return []string{"ok_address"}, nil
}
func (mh mockHelpers) GetContractsByName(string) ([]string, error) {
return []string{"ct_address"}, nil
}
func (mh mockHelpers) GetChannelsByName(string) ([]string, error) {
return []string{"channel address?"}, nil
}

type mockGetHeighter struct {
h uint64
}
Expand Down

0 comments on commit b00ea77

Please sign in to comment.