Skip to content

Commit

Permalink
refactor: no need for SpendTxnormal vs SpendTxLarge anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed May 27, 2019
1 parent ac88116 commit 0fe777e
Showing 1 changed file with 7 additions and 65 deletions.
72 changes: 7 additions & 65 deletions integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integration_test

import (
"fmt"
"math/big"
"os"
"strings"
"testing"
Expand All @@ -18,6 +17,8 @@ var recipientPrivateKey = os.Getenv("INTEGRATION_TEST_RECEIVER_PRIVATE_KEY")
var nodeURL = "http://localhost:3013"
var networkID = "ae_docker"

// Tests for 2 things: sending an amount that is max uint64, and that the node accepts the minimum fee
// that is calculated via tx.EstimateFee().
func TestSpendTxWithNode(t *testing.T) {
senderAccount, err := aeternity.AccountFromHexString(senderPrivateKey)
if err != nil {
Expand All @@ -30,70 +31,6 @@ func TestSpendTxWithNode(t *testing.T) {
aeternity.Config.Node.NetworkID = networkID
aeCli := aeternity.NewCli(aeternity.Config.Node.URL, false)

// In case this test has been run before, get recipient's account info. If it exists, expectedAmount = amount + 10
var expectedAmount big.Int
recipientAccount, err := aeCli.APIGetAccount(recipient)
if err != nil {
expectedAmount.SetInt64(10)
} else {
expectedAmount.Add(recipientAccount.Balance.Int, big.NewInt(10))
fmt.Printf("Recipient already exists with balance %v, expectedAmount after test is %s\n", recipientAccount.Balance.String(), expectedAmount.String())
}

amount := utils.NewBigInt()
amount.SetInt64(10)
fee := utils.NewBigInt()
fee.SetUint64(uint64(2e13))
ttl, nonce, err := aeCli.GetTTLNonce(sender, aeternity.Config.Client.TTL)
if err != nil {
t.Fatalf("Error in GetTTLNonce(): %v", err)
}

// create the SpendTransaction
tx := aeternity.NewSpendTx(sender, recipient, *amount, *fee, message, ttl, nonce)
base64TxMsg, err := aeternity.BaseEncodeTx(&tx)
if err != nil {
t.Fatalf("Base64 encoding errored out: %v", err)
}
fmt.Println(base64TxMsg)

// sign the transaction, output params for debugging
signedBase64TxMsg, hash, signature, err := aeternity.SignEncodeTxStr(senderAccount, base64TxMsg, aeternity.Config.Node.NetworkID)
if err != nil {
t.Error(err)
}
fmt.Println(signedBase64TxMsg, hash, signature)

// send the signed transaction to the node
err = aeCli.BroadcastTransaction(signedBase64TxMsg)
if err != nil {
t.Fatalf("Error while broadcasting transaction: %v", err)
}

// check the recipient's balance
recipientAccount, err = aeCli.APIGetAccount(recipient)
if err != nil {
t.Fatalf("Couldn't get recipient's account data: %v", err)
}

if recipientAccount.Balance.Cmp(&expectedAmount) != 0 {
t.Fatalf("Recipient should have %v, but has %v instead", expectedAmount.String(), recipientAccount.Balance.String())
}
}

func TestSpendTxLargeWithNode(t *testing.T) {
// This is a separate test because the account may not have enough funds for this test when the node has just started.
senderAccount, err := aeternity.AccountFromHexString(senderPrivateKey)
if err != nil {
t.Fatal(err)
}
recipient := "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v"
message := "Hello World"

aeternity.Config.Node.URL = nodeURL
aeternity.Config.Node.NetworkID = "ae_docker"
aeCli := aeternity.NewCli(aeternity.Config.Node.URL, false)

amount := utils.RequireBigIntFromString("18446744073709551615") // max uint64
fee := utils.NewBigIntFromUint64(uint64(2e13))
var expectedAmount = utils.NewBigInt()
Expand All @@ -113,6 +50,11 @@ func TestSpendTxLargeWithNode(t *testing.T) {

// create the SpendTransaction
tx := aeternity.NewSpendTx(sender, recipient, *amount, *fee, message, ttl, nonce)
// minimize the fee to save money!
estimatedFee, _ := tx.FeeEstimate()
fmt.Println("Estimated vs Actual Fee:", estimatedFee, tx.Fee)
tx.Fee = *estimatedFee

base64TxMsg, err := aeternity.BaseEncodeTx(&tx)
if err != nil {
t.Fatalf("Base64 encoding errored out: %v", err)
Expand Down

0 comments on commit 0fe777e

Please sign in to comment.