github.com/aeternity/aepp-sdk-go/v7@v7.0.1/naet/api.go (about)

     1  package naet
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/aeternity/aepp-sdk-go/v7/swagguard/node/client/external"
     8  	"github.com/aeternity/aepp-sdk-go/v7/swagguard/node/models"
     9  )
    10  
    11  // getErrorReason parses the external.*BadRequest structs returned by the
    12  // Swagger API endpoints and gets to the Reason field of the wrapped
    13  // models.Error struct, returning the reason why the call failed.
    14  func getErrorReason(v interface{}) (msg string) {
    15  	var p func(v reflect.Value) (msg string)
    16  	p = func(v reflect.Value) (msg string) {
    17  		switch v.Kind() {
    18  		// If it is a pointer we need to unwrap and call once again
    19  		case reflect.Ptr:
    20  			if v.IsValid() {
    21  				msg = p(v.Elem())
    22  			}
    23  		case reflect.Struct:
    24  			if v.Type() == reflect.TypeOf(models.Error{}) {
    25  				msg = fmt.Sprint(reflect.Indirect(v.FieldByName("Reason")))
    26  				break
    27  			}
    28  			for i := 0; i < v.NumField(); i++ {
    29  				msg = p(v.Field(i))
    30  			}
    31  		}
    32  		return
    33  	}
    34  	msg = p(reflect.ValueOf(v))
    35  	if len(msg) == 0 {
    36  		msg = fmt.Sprint(v)
    37  	}
    38  	return
    39  }
    40  
    41  // NodeInterface collects together all the interfaces defined in this file to
    42  // describe the capabilities of a generic connection to an aeternity (or mock)
    43  // node
    44  type NodeInterface interface {
    45  	GetAccounter
    46  	GetTopBlocker
    47  	GetStatuser
    48  	GetHeighter
    49  	GetKeyBlockByHasher
    50  	GetOracleByPubkeyer
    51  	GetGenerationByHeighter
    52  	GetTransactionByHasher
    53  	GetNameEntryByNamer
    54  	GetMicroBlockHeaderByHasher
    55  	GetMicroBlockTransactionsByHasher
    56  	PostTransactioner
    57  }
    58  
    59  // GetStatuser guarantees that one can run a GetStatus() method on the
    60  // mocked/real network connection
    61  type GetStatuser interface {
    62  	GetStatus() (*models.Status, error)
    63  }
    64  
    65  // GetStatus returns a node's Status.
    66  func (c *Node) GetStatus() (status *models.Status, err error) {
    67  	r, err := c.External.GetStatus(nil)
    68  	if err != nil {
    69  		return
    70  	}
    71  	status = r.Payload
    72  	return
    73  }
    74  
    75  // PostTransactioner guarantees that one can run a PostTransaction() method on
    76  // the mocked/real network connection
    77  type PostTransactioner interface {
    78  	PostTransaction(string, string) error
    79  }
    80  
    81  // PostTransaction post transaction
    82  func (c *Node) PostTransaction(signedEncodedTx, signedEncodedTxHash string) (err error) {
    83  	p := external.NewPostTransactionParams().WithBody(&models.Tx{
    84  		Tx: &signedEncodedTx,
    85  	})
    86  	r, err := c.External.PostTransaction(p)
    87  	if err != nil {
    88  		return
    89  	}
    90  	if *r.Payload.TxHash != signedEncodedTxHash {
    91  		err = fmt.Errorf("Transaction hash mismatch, expected %s got %s", signedEncodedTxHash, *r.Payload.TxHash)
    92  	}
    93  	return
    94  }
    95  
    96  // GetTopBlocker guarantees that one can run a GetTopBlock() method on the
    97  // mocked/real network connection
    98  type GetTopBlocker interface {
    99  	GetTopBlock() (*models.KeyBlockOrMicroBlockHeader, error)
   100  }
   101  
   102  // GetTopBlock get the top block of the chain
   103  func (c *Node) GetTopBlock() (kb *models.KeyBlockOrMicroBlockHeader, err error) {
   104  	r, err := c.External.GetTopBlock(nil)
   105  	if err != nil {
   106  		return
   107  	}
   108  	kb = r.Payload
   109  	return
   110  }
   111  
   112  // GetHeighter guarantees that one can run a GetHeight() method on the
   113  // mocked/real network connection
   114  type GetHeighter interface {
   115  	GetHeight() (uint64, error)
   116  }
   117  
   118  // GetHeight get the height of the chain
   119  func (c *Node) GetHeight() (height uint64, err error) {
   120  	tb, err := c.GetTopBlock()
   121  	if err != nil {
   122  		return
   123  	}
   124  	if tb.KeyBlock == nil {
   125  		height = *tb.MicroBlock.Height
   126  		return
   127  	}
   128  	height = *tb.KeyBlock.Height
   129  	return
   130  }
   131  
   132  // GetCurrentKeyBlock get current key block
   133  func (c *Node) GetCurrentKeyBlock() (kb *models.KeyBlock, err error) {
   134  	r, err := c.External.GetCurrentKeyBlock(nil)
   135  	if err != nil {
   136  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   137  		return
   138  	}
   139  	kb = r.Payload
   140  	return
   141  }
   142  
   143  // GetAccounter guarantees that one can run a GetAccount() method on the
   144  // mocked/real network connection
   145  type GetAccounter interface {
   146  	GetAccount(accountID string) (*models.Account, error)
   147  }
   148  
   149  // GetAccount retrieve an account by its address (public key) it is particularly
   150  // useful to obtain the nonce for spending transactions
   151  func (c *Node) GetAccount(accountID string) (account *models.Account, err error) {
   152  	p := external.NewGetAccountByPubkeyParams().WithPubkey(accountID)
   153  	r, err := c.External.GetAccountByPubkey(p)
   154  	if err != nil {
   155  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   156  		return
   157  	}
   158  	account = r.Payload
   159  	return
   160  }
   161  
   162  // GetNameEntryByNamer guarantees that one can run a GetNameEntryByName() method
   163  // on the mocked/real network connection
   164  type GetNameEntryByNamer interface {
   165  	GetNameEntryByName(string) (*models.NameEntry, error)
   166  }
   167  
   168  // GetNameEntryByName return the name entry
   169  func (c *Node) GetNameEntryByName(name string) (nameEntry *models.NameEntry, err error) {
   170  	p := external.NewGetNameEntryByNameParams().WithName(name)
   171  	r, err := c.External.GetNameEntryByName(p)
   172  
   173  	if err != nil {
   174  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   175  		return
   176  	}
   177  
   178  	nameEntry = r.Payload
   179  	return
   180  }
   181  
   182  // GetGenerationByHeighter guarantees that one can run a GetGenerationByHeight()
   183  // method on the mocked/real network connection
   184  type GetGenerationByHeighter interface {
   185  	GetGenerationByHeight(height uint64) (*models.Generation, error)
   186  }
   187  
   188  // GetGenerationByHeight gets the keyblock and all its microblocks
   189  func (c *Node) GetGenerationByHeight(height uint64) (g *models.Generation, err error) {
   190  	p := external.NewGetGenerationByHeightParams().WithHeight(height)
   191  	r, err := c.External.GetGenerationByHeight(p)
   192  	if err != nil {
   193  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   194  		return
   195  	}
   196  	g = r.Payload
   197  	return
   198  }
   199  
   200  // GetMicroBlockTransactionsByHasher guarantees that one can run a
   201  // GetMicroBlockTransactionsByHash() method on the mocked/real network
   202  // connection
   203  type GetMicroBlockTransactionsByHasher interface {
   204  	GetMicroBlockTransactionsByHash(string) (*models.GenericTxs, error)
   205  }
   206  
   207  // GetMicroBlockTransactionsByHash get the transactions of a microblock
   208  func (c *Node) GetMicroBlockTransactionsByHash(microBlockID string) (txs *models.GenericTxs, err error) {
   209  	p := external.NewGetMicroBlockTransactionsByHashParams().WithHash(microBlockID)
   210  	r, err := c.External.GetMicroBlockTransactionsByHash(p)
   211  	if err != nil {
   212  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   213  		return
   214  	}
   215  	txs = r.Payload
   216  	return
   217  }
   218  
   219  // GetMicroBlockHeaderByHasher guarantees that one can run a
   220  // GetMicroBlockHeaderByHash() method on the mocked/real network connection
   221  type GetMicroBlockHeaderByHasher interface {
   222  	GetMicroBlockHeaderByHash(string) (*models.MicroBlockHeader, error)
   223  }
   224  
   225  // GetMicroBlockHeaderByHash get the header of a micro block
   226  func (c *Node) GetMicroBlockHeaderByHash(microBlockID string) (txs *models.MicroBlockHeader, err error) {
   227  	p := external.NewGetMicroBlockHeaderByHashParams().WithHash(microBlockID)
   228  	r, err := c.External.GetMicroBlockHeaderByHash(p)
   229  	if err != nil {
   230  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   231  		return
   232  	}
   233  	txs = r.Payload
   234  	return
   235  }
   236  
   237  // GetKeyBlockByHasher guarantees that one can run a GetKeyBlockByHash() method
   238  // on the mocked/real network connection
   239  type GetKeyBlockByHasher interface {
   240  	GetKeyBlockByHash(string) (*models.KeyBlock, error)
   241  }
   242  
   243  // GetKeyBlockByHash get a key block by its hash
   244  func (c *Node) GetKeyBlockByHash(keyBlockID string) (txs *models.KeyBlock, err error) {
   245  	p := external.NewGetKeyBlockByHashParams().WithHash(keyBlockID)
   246  	r, err := c.External.GetKeyBlockByHash(p)
   247  	if err != nil {
   248  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   249  		return
   250  	}
   251  	txs = r.Payload
   252  	return
   253  }
   254  
   255  // GetTransactionByHasher guarantees that one can run a GetTransactionByHash()
   256  // method on the mocked/real network connection
   257  type GetTransactionByHasher interface {
   258  	GetTransactionByHash(string) (*models.GenericSignedTx, error)
   259  }
   260  
   261  // GetTransactionByHash get a transaction by it's hash
   262  func (c *Node) GetTransactionByHash(txHash string) (tx *models.GenericSignedTx, err error) {
   263  	p := external.NewGetTransactionByHashParams().WithHash(txHash)
   264  	r, err := c.External.GetTransactionByHash(p)
   265  	if err != nil {
   266  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   267  		return
   268  	}
   269  	tx = r.Payload
   270  	return
   271  }
   272  
   273  // GetOracleByPubkeyer guarantees that one can run a GetOracleByPubkey() method
   274  // on the mocked/real network connection
   275  type GetOracleByPubkeyer interface {
   276  	GetOracleByPubkey(string) (*models.RegisteredOracle, error)
   277  }
   278  
   279  // GetOracleByPubkey get an oracle by it's public key
   280  func (c *Node) GetOracleByPubkey(pubkey string) (oracle *models.RegisteredOracle, err error) {
   281  	p := external.NewGetOracleByPubkeyParams().WithPubkey(pubkey)
   282  	r, err := c.External.GetOracleByPubkey(p)
   283  	if err != nil {
   284  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   285  		return
   286  	}
   287  	oracle = r.Payload
   288  	return
   289  }
   290  
   291  // GetOracleQueriesByPubkey get a list of queries made to a particular oracle
   292  func (c *Node) GetOracleQueriesByPubkey(pubkey string) (oracleQueries *models.OracleQueries, err error) {
   293  	p := external.NewGetOracleQueriesByPubkeyParams().WithPubkey(pubkey)
   294  	r, err := c.External.GetOracleQueriesByPubkey(p)
   295  	if err != nil {
   296  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   297  		return
   298  	}
   299  	oracleQueries = r.Payload
   300  	return
   301  }
   302  
   303  // GetContractByID gets a contract by ct_ ID
   304  func (c *Node) GetContractByID(ctID string) (contract *models.ContractObject, err error) {
   305  	p := external.NewGetContractParams().WithPubkey(ctID)
   306  	r, err := c.External.GetContract(p)
   307  	if err != nil {
   308  		err = fmt.Errorf("Error: %v", getErrorReason(err))
   309  		return
   310  	}
   311  	contract = r.Payload
   312  	return
   313  }