github.com/algorand/go-algorand-sdk@v1.24.0/test/algodclientv2_test.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/base64"
     7  	"fmt"
     8  
     9  	"github.com/algorand/go-algorand-sdk/client/v2/algod"
    10  	modelsV2 "github.com/algorand/go-algorand-sdk/client/v2/common/models"
    11  	"github.com/algorand/go-algorand-sdk/types"
    12  
    13  	"github.com/cucumber/godog"
    14  )
    15  
    16  func AlgodClientV2Context(s *godog.Suite) {
    17  	s.Step(`^mock http responses in "([^"]*)" loaded from "([^"]*)"$`, mockHttpResponsesInLoadedFrom)
    18  	s.Step(`^expect error string to contain "([^"]*)"$`, expectErrorStringToContain)
    19  	s.Step(`^we make any Pending Transaction Information call$`, weMakeAnyPendingTransactionInformationCall)
    20  	s.Step(`^the parsed Pending Transaction Information response should have sender "([^"]*)"$`, theParsedResponseShouldEqualTheMockResponse)
    21  	s.Step(`^we make any Pending Transactions Information call$`, weMakeAnyPendingTransactionsInformationCall)
    22  	s.Step(`^we make any Send Raw Transaction call$`, weMakeAnySendRawTransactionCall)
    23  	s.Step(`^the parsed Send Raw Transaction response should have txid "([^"]*)"$`, theParsedResponseShouldEqualTheMockResponse)
    24  	s.Step(`^we make any Pending Transactions By Address call$`, weMakeAnyPendingTransactionsByAddressCall)
    25  	s.Step(`^the parsed Pending Transactions By Address response should contain an array of len (\d+) and element number (\d+) should have sender "([^"]*)"$`, theParsedResponseShouldEqualTheMockResponse)
    26  	s.Step(`^we make any Node Status call$`, weMakeAnyNodeStatusCall)
    27  	s.Step(`^the parsed Node Status response should have a last round of (\d+)$`, theParsedResponseShouldEqualTheMockResponse)
    28  	s.Step(`^we make any Ledger Supply call$`, weMakeAnyLedgerSupplyCall)
    29  	s.Step(`^the parsed Ledger Supply response should have totalMoney (\d+) onlineMoney (\d+) on round (\d+)$`, theParsedResponseShouldEqualTheMockResponse)
    30  	s.Step(`^we make any Status After Block call$`, weMakeAnyStatusAfterBlockCall)
    31  	s.Step(`^the parsed Status After Block response should have a last round of (\d+)$`, theParsedResponseShouldEqualTheMockResponse)
    32  	s.Step(`^we make any Account Information call$`, weMakeAnyAccountInformationCall)
    33  	s.Step(`^the parsed Account Information response should have address "([^"]*)"$`, theParsedResponseShouldEqualTheMockResponse)
    34  	s.Step(`^we make any Get Block call$`, weMakeAnyGetBlockCall)
    35  	s.Step(`^the parsed Get Block response should have rewards pool "([^"]*)"$`, theParsedGetBlockResponseShouldHaveRewardsPool)
    36  	s.Step(`^we make any Suggested Transaction Parameters call$`, weMakeAnySuggestedTransactionParametersCall)
    37  	s.Step(`^the parsed Suggested Transaction Parameters response should have first round valid of (\d+)$`, theParsedResponseShouldEqualTheMockResponse)
    38  	s.Step(`^expect the path used to be "([^"]*)"$`, expectThePathUsedToBe)
    39  	s.Step(`^we make a Status after Block call with round (\d+)$`, weMakeAStatusAfterBlockCallWithRound)
    40  	s.Step(`^we make an Account Information call against account "([^"]*)"$`, weMakeAnAccountInformationCallAgainstAccount)
    41  	s.Step(`^the parsed Pending Transactions Information response should contain an array of len (\d+) and element number (\d+) should have sender "([^"]*)"$`, theParsedResponseShouldEqualTheMockResponse)
    42  	s.Step(`^we make a Pending Transaction Information against txid "([^"]*)" with format "([^"]*)"$`, weMakeAPendingTransactionInformationAgainstTxidWithFormat)
    43  	s.Step(`^we make a Pending Transaction Information with max (\d+) and format "([^"]*)"$`, weMakeAPendingTransactionInformationWithMaxAndFormat)
    44  	s.Step(`^we make a Pending Transactions By Address call against account "([^"]*)" and max (\d+) and format "([^"]*)"$`, weMakeAPendingTransactionsByAddressCallAgainstAccountAndMaxAndFormat)
    45  	s.Step(`^we make a Get Block call against block number (\d+) with format "([^"]*)"$`, weMakeAGetBlockCallAgainstBlockNumberWithFormat)
    46  	s.Step(`^we make any Dryrun call$`, weMakeAnyDryrunCall)
    47  	s.Step(`^the parsed Dryrun Response should have global delta "([^"]*)" with (\d+)$`, parsedDryrunResponseShouldHave)
    48  	s.Step(`^we make an Account Information call against account "([^"]*)" with exclude "([^"]*)"$`, weMakeAnAccountInformationCallAgainstAccountWithExclude)
    49  	s.Step(`^we make an Account Asset Information call against account "([^"]*)" assetID (\d+)$`, weMakeAnAccountAssetInformationCallAgainstAccountAssetID)
    50  	s.Step(`^we make an Account Application Information call against account "([^"]*)" applicationID (\d+)$`, weMakeAnAccountApplicationInformationCallAgainstAccountApplicationID)
    51  	s.Step(`^we make a GetApplicationBoxByName call for applicationID (\d+) with encoded box name "([^"]*)"$`, weMakeAGetApplicationBoxByNameCall)
    52  	s.Step(`^we make a GetApplicationBoxes call for applicationID (\d+) with max (\d+)$`, weMakeAGetApplicationBoxesCall)
    53  	s.Step(`^we make a GetLightBlockHeaderProof call for round (\d+)$`, weMakeAGetLightBlockHeaderProofCallForRound)
    54  	s.Step(`^we make a GetStateProof call for round (\d+)$`, weMakeAGetStateProofCallForRound)
    55  	s.Step(`^we make a GetTransactionProof call for round (\d+) txid "([^"]*)" and hashtype "([^"]*)"$`, weMakeAGetTransactionProofCallForRoundTxidAndHashtype)
    56  	s.Step(`^we make a Lookup Block Hash call against round (\d+)$`, weMakeALookupBlockHashCallAgainstRound)
    57  
    58  	s.BeforeScenario(func(interface{}) {
    59  		globalErrForExamination = nil
    60  	})
    61  }
    62  
    63  func weMakeAnyPendingTransactionInformationCall() error {
    64  	return weMakeAnyCallTo("algod", "PendingTransactionInformation")
    65  }
    66  
    67  func weMakeAnyPendingTransactionsInformationCall() error {
    68  	return weMakeAnyCallTo("algod", "GetPendingTransactions")
    69  }
    70  
    71  func weMakeAnySendRawTransactionCall() error {
    72  	return weMakeAnyCallTo("algod", "RawTransaction")
    73  }
    74  
    75  func weMakeAnyPendingTransactionsByAddressCall() error {
    76  	return weMakeAnyCallTo("algod", "GetPendingTransactionsByAddress")
    77  }
    78  
    79  func weMakeAnyNodeStatusCall() error {
    80  	return weMakeAnyCallTo("algod", "GetStatus")
    81  }
    82  
    83  func weMakeAnyLedgerSupplyCall() error {
    84  	return weMakeAnyCallTo("algod", "GetSupply")
    85  }
    86  
    87  func weMakeAnyStatusAfterBlockCall() error {
    88  	return weMakeAnyCallTo("algod", "WaitForBlock")
    89  }
    90  
    91  func weMakeAnyAccountInformationCall() error {
    92  	return weMakeAnyCallTo("algod", "AccountInformation")
    93  }
    94  
    95  var blockResponse types.Block
    96  
    97  func weMakeAnyGetBlockCall() error {
    98  	// This endpoint requires some sort of base64 decoding for the verification step and it isn't working properly.
    99  	//return weMakeAnyCallTo("algod", "GetBlock")
   100  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   101  	if err != nil {
   102  		return err
   103  	}
   104  	blockResponse, globalErrForExamination = algodClient.Block(0).Do(context.Background())
   105  	return nil
   106  }
   107  
   108  func theParsedGetBlockResponseShouldHaveRewardsPool(pool string) error {
   109  	blockResponseRewardsPoolBytes := [32]byte(blockResponse.RewardsPool)
   110  	poolBytes, err := base64.StdEncoding.DecodeString(pool)
   111  	if err != nil {
   112  		return err
   113  	}
   114  	if !bytes.Equal(poolBytes, blockResponseRewardsPoolBytes[:]) {
   115  		return fmt.Errorf("response pool %v mismatched expected pool %v", blockResponseRewardsPoolBytes, poolBytes)
   116  	}
   117  	return nil
   118  }
   119  
   120  func weMakeAnySuggestedTransactionParametersCall() error {
   121  	return weMakeAnyCallTo("algod", "TransactionParams")
   122  }
   123  
   124  func weMakeAStatusAfterBlockCallWithRound(round int) error {
   125  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   126  	if err != nil {
   127  		return err
   128  	}
   129  	_, globalErrForExamination = algodClient.StatusAfterBlock(uint64(round)).Do(context.Background())
   130  	return nil
   131  }
   132  
   133  func weMakeAnAccountInformationCallAgainstAccount(account string) error {
   134  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   135  	if err != nil {
   136  		return err
   137  	}
   138  	_, globalErrForExamination = algodClient.AccountInformation(account).Do(context.Background())
   139  	return nil
   140  }
   141  
   142  func weMakeAPendingTransactionInformationAgainstTxidWithFormat(txid, format string) error {
   143  	if format != "msgpack" {
   144  		return fmt.Errorf("this sdk does not support format %s", format)
   145  	}
   146  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   147  	if err != nil {
   148  		return err
   149  	}
   150  	_, _, globalErrForExamination = algodClient.PendingTransactionInformation(txid).Do(context.Background())
   151  	return nil
   152  }
   153  
   154  func weMakeAPendingTransactionInformationWithMaxAndFormat(max int, format string) error {
   155  	if format != "msgpack" {
   156  		return fmt.Errorf("this sdk does not support format %s", format)
   157  	}
   158  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   159  	if err != nil {
   160  		return err
   161  	}
   162  	_, _, globalErrForExamination = algodClient.PendingTransactions().Max(uint64(max)).Do(context.Background())
   163  	return nil
   164  }
   165  
   166  func weMakeAPendingTransactionsByAddressCallAgainstAccountAndMaxAndFormat(account string, max int, format string) error {
   167  	if format != "msgpack" {
   168  		return fmt.Errorf("this sdk does not support format %s", format)
   169  	}
   170  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   171  	if err != nil {
   172  		return err
   173  	}
   174  	_, _, globalErrForExamination = algodClient.PendingTransactionsByAddress(account).Max(uint64(max)).Do(context.Background())
   175  	return nil
   176  }
   177  
   178  func weMakeAGetBlockCallAgainstBlockNumberWithFormat(blocknum int, format string) error {
   179  	if format != "msgpack" {
   180  		return fmt.Errorf("this sdk does not support format %s", format)
   181  	}
   182  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   183  	if err != nil {
   184  		return err
   185  	}
   186  	_, globalErrForExamination = algodClient.Block(uint64(blocknum)).Do(context.Background())
   187  	return nil
   188  }
   189  
   190  var dryrunResponse modelsV2.DryrunResponse
   191  
   192  func weMakeAnyDryrunCall() (err error) {
   193  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   194  	if err != nil {
   195  		return
   196  	}
   197  
   198  	dryrunResponse, err = algodClient.TealDryrun(modelsV2.DryrunRequest{}).Do(context.Background())
   199  	return
   200  }
   201  
   202  func parsedDryrunResponseShouldHave(key string, action int) error {
   203  	if len(dryrunResponse.Txns) != 1 {
   204  		return fmt.Errorf("expected 1 txn in result but got %d", len(dryrunResponse.Txns))
   205  	}
   206  	gd := dryrunResponse.Txns[0].GlobalDelta
   207  	if len(gd) != 1 {
   208  		return fmt.Errorf("expected 1 global delta in result but got %d", len(gd))
   209  	}
   210  	if gd[0].Key != key {
   211  		return fmt.Errorf("key %s != %s", key, gd[0].Key)
   212  	}
   213  	if int(gd[0].Value.Action) != action {
   214  		return fmt.Errorf("action %d != %d", action, int(gd[0].Value.Action))
   215  	}
   216  	return nil
   217  }
   218  
   219  func mockHttpResponsesInLoadedFrom(jsonfiles, directory string) error {
   220  	return mockHttpResponsesInLoadedFromWithStatus(jsonfiles, directory, 200)
   221  }
   222  
   223  func weMakeAnAccountInformationCallAgainstAccountWithExclude(account, exclude string) error {
   224  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   225  	if err != nil {
   226  		return err
   227  	}
   228  	algodClient.AccountInformation(account).Exclude(exclude).Do(context.Background())
   229  	return nil
   230  }
   231  
   232  func weMakeAnAccountAssetInformationCallAgainstAccountAssetID(account string, assetID int) error {
   233  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   234  	if err != nil {
   235  		return err
   236  	}
   237  	algodClient.AccountAssetInformation(account, uint64(assetID)).Do(context.Background())
   238  	return nil
   239  }
   240  
   241  func weMakeAnAccountApplicationInformationCallAgainstAccountApplicationID(account string, appID int) error {
   242  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   243  	if err != nil {
   244  		return err
   245  	}
   246  	algodClient.AccountApplicationInformation(account, uint64(appID)).Do(context.Background())
   247  	return nil
   248  }
   249  
   250  func weMakeAGetApplicationBoxByNameCall(appId int, encodedBoxName string) error {
   251  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   252  	if err != nil {
   253  		return err
   254  	}
   255  	decodedBoxNames, err := parseAppArgs(encodedBoxName)
   256  	if err != nil {
   257  		return err
   258  	}
   259  	algodClient.GetApplicationBoxByName(uint64(appId), decodedBoxNames[0]).Do(context.Background())
   260  	return nil
   261  }
   262  
   263  func weMakeAGetApplicationBoxesCall(appId int, max int) error {
   264  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   265  	if err != nil {
   266  		return err
   267  	}
   268  	algodClient.GetApplicationBoxes(uint64(appId)).Max(uint64(max)).Do(context.Background())
   269  	return nil
   270  }
   271  
   272  func weMakeAGetLightBlockHeaderProofCallForRound(round int) error {
   273  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   274  	if err != nil {
   275  		return err
   276  	}
   277  	algodClient.GetLightBlockHeaderProof(uint64(round)).Do(context.Background())
   278  	return nil
   279  }
   280  
   281  func weMakeAGetStateProofCallForRound(round int) error {
   282  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   283  	if err != nil {
   284  		return err
   285  	}
   286  	algodClient.GetStateProof(uint64(round)).Do(context.Background())
   287  	return nil
   288  }
   289  
   290  func weMakeAGetTransactionProofCallForRoundTxidAndHashtype(round int, txid, hashtype string) error {
   291  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   292  	if err != nil {
   293  		return err
   294  	}
   295  	algodClient.GetTransactionProof(uint64(round), txid).Hashtype(hashtype).Do(context.Background())
   296  	return nil
   297  }
   298  
   299  func weMakeALookupBlockHashCallAgainstRound(round int) error {
   300  	algodClient, err := algod.MakeClient(mockServer.URL, "")
   301  	if err != nil {
   302  		return err
   303  	}
   304  	algodClient.GetBlockHash(uint64(round)).Do(context.Background())
   305  	return nil
   306  }