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

     1  package integrationtest
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/aeternity/aepp-sdk-go/aeternity"
    10  )
    11  
    12  var sender = "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi"
    13  var senderPrivateKey = os.Getenv("INTEGRATION_TEST_SENDER_PRIVATE_KEY")
    14  var recipientPrivateKey = os.Getenv("INTEGRATION_TEST_RECEIVER_PRIVATE_KEY")
    15  var nodeURL = "http://localhost:3013"
    16  var networkID = "ae_docker"
    17  
    18  func setupNetwork(t *testing.T) *aeternity.Client {
    19  	aeternity.Config.Node.NetworkID = networkID
    20  	client := aeternity.NewClient(nodeURL, false)
    21  	t.Logf("nodeURL: %s, networkID: %s", nodeURL, aeternity.Config.Node.NetworkID)
    22  	return client
    23  }
    24  
    25  func setupAccounts(t *testing.T) (*aeternity.Account, *aeternity.Account) {
    26  	alice, err := aeternity.AccountFromHexString(senderPrivateKey)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	bob, err := aeternity.AccountFromHexString(recipientPrivateKey)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	t.Logf("Alice: %s, Bob: %s", alice.Address, bob.Address)
    36  	return alice, bob
    37  }
    38  
    39  func signBroadcast(t *testing.T, tx aeternity.Tx, acc *aeternity.Account, aeClient *aeternity.Client) (hash string) {
    40  	txB64, _ := aeternity.BaseEncodeTx(tx)
    41  	// t.Log(txB64)
    42  
    43  	signedTxStr, hash, _, err := aeternity.SignEncodeTxStr(acc, txB64, aeternity.Config.Node.NetworkID)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	err = aeClient.BroadcastTransaction(signedTxStr)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	return hash
    54  
    55  }
    56  
    57  type delayableCode func()
    58  
    59  func delay(f delayableCode) {
    60  	time.Sleep(2000 * time.Millisecond)
    61  	f()
    62  }
    63  
    64  func getHeight(aeClient *aeternity.Client) (h uint64) {
    65  	h, err := aeClient.APIGetHeight()
    66  	if err != nil {
    67  		fmt.Println("Could not retrieve chain height")
    68  		return
    69  	}
    70  	// fmt.Println("Current Height:", h)
    71  	return
    72  }
    73  
    74  func waitForTransaction(aeClient *aeternity.Client, hash string) (err error) {
    75  	height := getHeight(aeClient)
    76  	// fmt.Println("Waiting for", hash)
    77  	height, _, _, _, err = aeClient.WaitForTransactionUntilHeight(height+10, hash)
    78  	if err != nil {
    79  		// Sometimes, the tests want the tx to fail. Return the err to let them know.
    80  		return err
    81  	}
    82  	// fmt.Println("Transaction was found at", height, "blockhash", blockHash, "microBlockHash", microBlockHash, "err", err)
    83  	return nil
    84  }