github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/integration_test/spend_test.go (about)

     1  package integrationtest
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/aeternity/aepp-sdk-go/aeternity"
     9  	"github.com/aeternity/aepp-sdk-go/utils"
    10  )
    11  
    12  // Tests for 2 things: sending an amount that is max uint64, and that the node accepts the minimum fee
    13  // that is calculated via tx.EstimateFee().
    14  func TestSpendTx(t *testing.T) {
    15  	node := setupNetwork(t)
    16  	alice, bob := setupAccounts(t)
    17  
    18  	amount := utils.RequireIntFromString("18446744073709551615") // max uint64
    19  	fee := utils.NewIntFromUint64(uint64(2e13))
    20  	msg := "Hello World"
    21  
    22  	// In case the recipient account already has funds, get recipient's account info. If it exists, expectedAmount = existing balance + amount + fee
    23  	expected := new(big.Int)
    24  	bobState, err := node.APIGetAccount(bob.Address)
    25  	if err != nil {
    26  		expected.Set(amount)
    27  	} else {
    28  		bS := big.Int(bobState.Balance)
    29  		expected.Add(&bS, amount)
    30  	}
    31  
    32  	ttl, nonce, err := node.GetTTLNonce(sender, aeternity.Config.Client.TTL)
    33  	if err != nil {
    34  		t.Fatalf("Error in GetTTLNonce(): %v", err)
    35  	}
    36  
    37  	// create the SpendTransaction
    38  	tx := aeternity.NewSpendTx(sender, bob.Address, *amount, *fee, msg, ttl, nonce)
    39  	// minimize the fee to save money!
    40  	est, _ := tx.FeeEstimate()
    41  	fmt.Println("Estimated vs Actual Fee:", est, tx.Fee)
    42  	tx.Fee = *est
    43  
    44  	// sign the transaction, output params for debugging
    45  	_ = signBroadcast(t, &tx, alice, node)
    46  	// check the recipient's balance
    47  
    48  	getBobsAccount := func() {
    49  		bobState, err = node.APIGetAccount(bob.Address)
    50  		if err != nil {
    51  			t.Fatalf("Couldn't get Bob's account data: %v", err)
    52  		}
    53  	}
    54  	delay(getBobsAccount)
    55  	b := big.Int(bobState.Balance)
    56  
    57  	if expected.Cmp(&b) != 0 {
    58  		t.Fatalf("Bob should have %v, but has %v instead", expected.String(), bobState.Balance.String())
    59  	}
    60  }