Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose context.Context #128

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/e

# Unreleased

- [`Breaking`] Exposed `context.Context` parameter, enabling explicit request cancellation and timeout management.

# v1.5.0 (2/10/2024)

- [`Fix`] Make NodeClient match AptosRpcClient interface
Expand Down
178 changes: 86 additions & 92 deletions client.go

Large diffs are not rendered by default.

115 changes: 58 additions & 57 deletions client_test.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions client_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aptos

import (
"context"
"fmt"
"net/url"
"runtime/debug"
Expand Down Expand Up @@ -53,13 +54,13 @@ func init() {
//
// options may be: MaxGasAmount, GasUnitPrice, ExpirationSeconds, ValidUntil, SequenceNumber, ChainIdOption
// deprecated, please use the EntryFunction APIs
func APTTransferTransaction(client *Client, sender TransactionSigner, dest AccountAddress, amount uint64, options ...any) (rawTxn *RawTransaction, err error) {
func APTTransferTransaction(ctx context.Context, client *Client, sender TransactionSigner, dest AccountAddress, amount uint64, options ...any) (rawTxn *RawTransaction, err error) {
entryFunction, err := CoinTransferPayload(nil, dest, amount)
if err != nil {
return nil, err
}

rawTxn, err = client.BuildTransaction(sender.AccountAddress(),
rawTxn, err = client.BuildTransaction(ctx, sender.AccountAddress(),
TransactionPayload{Payload: entryFunction}, options...)
return
}
10 changes: 6 additions & 4 deletions examples/alternative_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package main

import (
"context"
"fmt"

"github.com/aptos-labs/aptos-go-sdk"
"github.com/aptos-labs/aptos-go-sdk/crypto"
"golang.org/x/crypto/ed25519"
Expand Down Expand Up @@ -90,7 +92,7 @@ func example(network aptos.NetworkConfig) {
}

// Fund the sender with the faucet to create it on-chain
err = client.Fund(sender.Address, 100_000_000)
err = client.Fund(context.Background(), sender.Address, 100_000_000)
fmt.Printf("We fund the signer account %s with the faucet\n", sender.Address.String())

// Prep arguments
Expand All @@ -102,7 +104,7 @@ func example(network aptos.NetworkConfig) {
amount := uint64(100)

// Build transaction
rawTxn, err := aptos.APTTransferTransaction(client, sender, receiver, amount)
rawTxn, err := aptos.APTTransferTransaction(context.Background(), client, sender, receiver, amount)
if err != nil {
panic("Failed to build transaction:" + err.Error())
}
Expand All @@ -115,15 +117,15 @@ func example(network aptos.NetworkConfig) {
fmt.Printf("Submit a coin transfer to address %s\n", receiver.String())

// Submit and wait for it to complete
submitResult, err := client.SubmitTransaction(signedTxn)
submitResult, err := client.SubmitTransaction(context.Background(), signedTxn)
if err != nil {
panic("Failed to submit transaction:" + err.Error())
}
txnHash := submitResult.Hash

// Wait for the transaction
fmt.Printf("And we wait for the transaction %s to complete...\n", txnHash)
userTxn, err := client.WaitForTransaction(txnHash)
userTxn, err := client.WaitForTransaction(context.Background(), txnHash)
if err != nil {
panic("Failed to wait for transaction:" + err.Error())
}
Expand Down
10 changes: 6 additions & 4 deletions examples/external_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package main

import (
"context"
"fmt"

"github.com/aptos-labs/aptos-go-sdk"
"github.com/aptos-labs/aptos-go-sdk/crypto"
"golang.org/x/crypto/ed25519"
Expand Down Expand Up @@ -91,7 +93,7 @@ func example(networkConfig aptos.NetworkConfig) {
}

// Fund the sender with the faucet to create it on-chain
err = client.Fund(sender.Address, 100_000_000)
err = client.Fund(context.Background(), sender.Address, 100_000_000)
fmt.Printf("We fund the signer account %s with the faucet\n", sender.Address.String())

// Prep arguments
Expand All @@ -108,7 +110,7 @@ func example(networkConfig aptos.NetworkConfig) {

// Sign transaction
fmt.Printf("Submit a coin transfer to address %s\n", receiver.String())
rawTxn, err := client.BuildTransaction(sender.Address,
rawTxn, err := client.BuildTransaction(context.Background(), sender.Address,
aptos.TransactionPayload{Payload: payload},
)
if err != nil {
Expand Down Expand Up @@ -139,15 +141,15 @@ func example(networkConfig aptos.NetworkConfig) {
// TODO: Show how to send over a wire with an encoding

// Submit and wait for it to complete
submitResult, err := client.SubmitTransaction(signedTxn)
submitResult, err := client.SubmitTransaction(context.Background(), signedTxn)
if err != nil {
panic("Failed to submit transaction:" + err.Error())
}
txnHash := submitResult.Hash

// Wait for the transaction
fmt.Printf("And we wait for the transaction %s to complete...\n", txnHash)
userTxn, err := client.WaitForTransaction(txnHash)
userTxn, err := client.WaitForTransaction(context.Background(), txnHash)
if err != nil {
panic("Failed to wait for transaction:" + err.Error())
}
Expand Down
38 changes: 20 additions & 18 deletions examples/fungible_asset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/aptos-labs/aptos-go-sdk"
"github.com/aptos-labs/aptos-go-sdk/bcs"
"github.com/aptos-labs/aptos-go-sdk/crypto"
Expand Down Expand Up @@ -38,7 +40,7 @@ func example(networkConfig aptos.NetworkConfig) {

// Fund the sender with the faucet to create it on-chain
println("SENDER: ", sender.Address.String())
err = client.Fund(sender.Address, FundAmount)
err = client.Fund(context.Background(), sender.Address, FundAmount)
if err != nil {
panic("Failed to fund sender:" + err.Error())
}
Expand All @@ -50,11 +52,11 @@ func example(networkConfig aptos.NetworkConfig) {
if err != nil {
panic("Failed to create publish payload:" + err.Error())
}
response, err := client.BuildSignAndSubmitTransaction(sender, *payload)
response, err := client.BuildSignAndSubmitTransaction(context.Background(), sender, *payload)
if err != nil {
panic("Failed to build sign and submit publish transaction:" + err.Error())
}
waitResponse, err := client.WaitForTransaction(response.Hash)
waitResponse, err := client.WaitForTransaction(context.Background(), response.Hash)
if err != nil {
panic("Failed to wait for publish transaction:" + err.Error())
}
Expand All @@ -66,7 +68,7 @@ func example(networkConfig aptos.NetworkConfig) {
// Get the fungible asset address by view function
rupeeModule := aptos.ModuleId{Address: sender.Address, Name: "rupee"}
var noTypeTags []aptos.TypeTag
viewResponse, err := client.View(&aptos.ViewPayload{
viewResponse, err := client.View(context.Background(), &aptos.ViewPayload{
Module: rupeeModule,
Function: "fa_address",
ArgTypes: noTypeTags,
Expand All @@ -85,7 +87,7 @@ func example(networkConfig aptos.NetworkConfig) {
panic("Failed to create fungible asset client:" + err.Error())
}

beforeBalance, err := faClient.PrimaryBalance(&sender.Address)
beforeBalance, err := faClient.PrimaryBalance(context.Background(), &sender.Address)
if err != nil {
panic("Failed to get balance:" + err.Error())
}
Expand All @@ -96,7 +98,7 @@ func example(networkConfig aptos.NetworkConfig) {
panic("Failed to serialize amount:" + err.Error())
}
serializedSenderAddress, _ := bcs.Serialize(&sender.Address) // This can't fail
response, err = client.BuildSignAndSubmitTransaction(sender, aptos.TransactionPayload{
response, err = client.BuildSignAndSubmitTransaction(context.Background(), sender, aptos.TransactionPayload{
Payload: &aptos.EntryFunction{
Module: rupeeModule,
Function: "mint",
Expand All @@ -108,12 +110,12 @@ func example(networkConfig aptos.NetworkConfig) {
panic("Failed to build sign and submit mint transaction:" + err.Error())
}
fmt.Printf("Submitted mint as: %s\n", response.Hash)
_, err = client.WaitForTransaction(response.Hash)
_, err = client.WaitForTransaction(context.Background(), response.Hash)
if err != nil {
panic("Failed to wait for publish transaction:" + err.Error())
}

afterBalance, err := faClient.PrimaryBalance(&sender.Address)
afterBalance, err := faClient.PrimaryBalance(context.Background(), &sender.Address)
if err != nil {
panic("Failed to get balance:" + err.Error())
}
Expand All @@ -127,24 +129,24 @@ func example(networkConfig aptos.NetworkConfig) {
}

// Transfer some to 0xCAFE
receiverBeforeBalance, err := faClient.PrimaryBalance(receiver)
receiverBeforeBalance, err := faClient.PrimaryBalance(context.Background(), receiver)
if err != nil {
panic("Failed to get balance:" + err.Error())
}
transferTxn, err := faClient.TransferPrimaryStore(sender, *receiver, TransferAmount)
transferTxn, err := faClient.TransferPrimaryStore(context.Background(), sender, *receiver, TransferAmount)
if err != nil {
panic("Failed to create primary store transfer transaction:" + err.Error())
}
response, err = client.SubmitTransaction(transferTxn)
response, err = client.SubmitTransaction(context.Background(), transferTxn)
if err != nil {
panic("Failed to submit transaction:" + err.Error())
}
fmt.Printf("Submitted transfer as: %s\n", response.Hash)
err = client.PollForTransactions([]string{response.Hash})
err = client.PollForTransactions(context.Background(), []string{response.Hash})
if err != nil {
panic("Failed to wait for transaction:" + err.Error())
}
receiverAfterBalance, err := faClient.PrimaryBalance(receiver)
receiverAfterBalance, err := faClient.PrimaryBalance(context.Background(), receiver)
if err != nil {
panic("Failed to get store balance:" + err.Error())
}
Expand All @@ -155,7 +157,7 @@ func example(networkConfig aptos.NetworkConfig) {
fmt.Printf("\n== Now running script version ==\n")
runScript(client, sender, receiver, faMetadataAddress)

receiverAfterAfterBalance, err := faClient.PrimaryBalance(receiver)
receiverAfterAfterBalance, err := faClient.PrimaryBalance(context.Background(), receiver)
if err != nil {
panic("Failed to get store balance:" + err.Error())
}
Expand All @@ -171,7 +173,7 @@ func runScript(client *aptos.Client, alice *aptos.Account, bob *aptos.AccountAdd
}

// 1. Build transaction
rawTxn, err := client.BuildTransaction(alice.AccountAddress(), aptos.TransactionPayload{
rawTxn, err := client.BuildTransaction(context.Background(), alice.AccountAddress(), aptos.TransactionPayload{
Payload: &aptos.Script{
Code: scriptBytes,
ArgTypes: []aptos.TypeTag{},
Expand All @@ -191,7 +193,7 @@ func runScript(client *aptos.Client, alice *aptos.Account, bob *aptos.AccountAdd
// This is useful for understanding how much the transaction will cost
// and to ensure that the transaction is valid before sending it to the network
// This is optional, but recommended
simulationResult, err := client.SimulateTransaction(rawTxn, alice)
simulationResult, err := client.SimulateTransaction(context.Background(), rawTxn, alice)
if err != nil {
panic("Failed to simulate transaction:" + err.Error())
}
Expand All @@ -208,14 +210,14 @@ func runScript(client *aptos.Client, alice *aptos.Account, bob *aptos.AccountAdd
}

// 4. Submit transaction
submitResult, err := client.SubmitTransaction(signedTxn)
submitResult, err := client.SubmitTransaction(context.Background(), signedTxn)
if err != nil {
panic("Failed to submit transaction:" + err.Error())
}
txnHash := submitResult.Hash

// 5. Wait for the transaction to complete
_, err = client.WaitForTransaction(txnHash)
_, err = client.WaitForTransaction(context.Background(), txnHash)
if err != nil {
panic("Failed to wait for transaction:" + err.Error())
}
Expand Down
20 changes: 11 additions & 9 deletions examples/multi_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
package main

import (
"context"
"fmt"

"github.com/aptos-labs/aptos-go-sdk/crypto"
"github.com/aptos-labs/aptos-go-sdk/internal/util"

Expand Down Expand Up @@ -37,19 +39,19 @@ func example(networkConfig aptos.NetworkConfig) {
fmt.Printf("Bob:%s\n", bob.Address.String())

// Fund the sender with the faucet to create it on-chain
err = client.Fund(alice.Address, FundAmount)
err = client.Fund(context.Background(), alice.Address, FundAmount)
if err != nil {
panic("Failed to fund alice:" + err.Error())
}
err = client.Fund(bob.Address, FundAmount)
err = client.Fund(context.Background(), bob.Address, FundAmount)
if err != nil {
panic("Failed to fund bob:" + err.Error())
}
aliceBalance, err := client.AccountAPTBalance(alice.Address)
aliceBalance, err := client.AccountAPTBalance(context.Background(), alice.Address)
if err != nil {
panic("Failed to retrieve alice balance:" + err.Error())
}
bobBalance, err := client.AccountAPTBalance(bob.Address)
bobBalance, err := client.AccountAPTBalance(context.Background(), bob.Address)
if err != nil {
panic("Failed to retrieve bob balance:" + err.Error())
}
Expand All @@ -62,7 +64,7 @@ func example(networkConfig aptos.NetworkConfig) {
if err != nil {
panic("Failed to deserialize script:" + err.Error())
}
rawTxn, err := client.BuildTransactionMultiAgent(alice.AccountAddress(), aptos.TransactionPayload{
rawTxn, err := client.BuildTransactionMultiAgent(context.Background(), alice.AccountAddress(), aptos.TransactionPayload{
Payload: &aptos.Script{
Code: script,
ArgTypes: []aptos.TypeTag{aptos.AptosCoinTypeTag, aptos.AptosCoinTypeTag},
Expand Down Expand Up @@ -113,24 +115,24 @@ func example(networkConfig aptos.NetworkConfig) {
}

// 4. Submit transaction
submitResult, err := client.SubmitTransaction(signedTxn)
submitResult, err := client.SubmitTransaction(context.Background(), signedTxn)
if err != nil {
panic("Failed to submit transaction:" + err.Error())
}
txnHash := submitResult.Hash

// 5. Wait for the transaction to complete
_, err = client.WaitForTransaction(txnHash)
_, err = client.WaitForTransaction(context.Background(), txnHash)
if err != nil {
panic("Failed to wait for transaction:" + err.Error())
}

// Check balances
aliceBalance, err = client.AccountAPTBalance(alice.Address)
aliceBalance, err = client.AccountAPTBalance(context.Background(), alice.Address)
if err != nil {
panic("Failed to retrieve alice balance:" + err.Error())
}
bobBalance, err = client.AccountAPTBalance(bob.Address)
bobBalance, err = client.AccountAPTBalance(context.Background(), bob.Address)
if err != nil {
panic("Failed to retrieve bob balance:" + err.Error())
}
Expand Down
Loading