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

Run examples in CI, fix example, add node healthcheck API #60

Merged
merged 3 commits into from
Jun 21, 2024
Merged
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
5 changes: 5 additions & 0 deletions api/general.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package api

type HealthCheckResponse struct {
Message string `json:"message"`
}
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,8 @@ func (client *Client) GetProcessorStatus(processorName string) (uint64, error) {
func (client *Client) GetCoinBalances(address AccountAddress) ([]CoinBalance, error) {
return client.indexerClient.GetCoinBalances(address)
}

// NodeAPIHealthCheck checks if the node is within durationSecs of the current time, if not provided the node default is used
func (client *Client) NodeAPIHealthCheck(durationSecs ...uint64) (api.HealthCheckResponse, error) {
return client.nodeClient.NodeHealthCheck(durationSecs...)
}
19 changes: 19 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aptos

import (
"strings"
"testing"

"github.com/aptos-labs/aptos-go-sdk/api"
Expand Down Expand Up @@ -397,6 +398,7 @@ func Test_Concurrent_Submission(t *testing.T) {
go client.nodeClient.BuildSignAndSubmitTransactions(account1, payloads, results)

transferAmount, err := bcs.SerializeU64(100)
assert.NoError(t, err)

// Generate transactions
for i := uint64(0); i < numTxns; i++ {
Expand Down Expand Up @@ -452,6 +454,23 @@ func TestClient_BlockByHeight(t *testing.T) {
assert.NoError(t, err)
}

func TestClient_NodeAPIHealthCheck(t *testing.T) {
client, err := createTestClient()
assert.NoError(t, err)
response, err := client.NodeAPIHealthCheck()
assert.NoError(t, err)
assert.True(t, strings.Contains(response.Message, "ok"), "Node API health check failed"+response.Message)

// Now, check node API health check with a time that should never fail
response, err = client.NodeAPIHealthCheck(10000)
assert.NoError(t, err)
assert.True(t, strings.Contains(response.Message, "ok"), "Node API health check failed"+response.Message)

// Now, check node API health check with a time that should probably fail
response, err = client.NodeAPIHealthCheck(0)
assert.Error(t, err)
}

func submitEntryFunction(_ *testing.T, client *Client, sender TransactionSigner, options ...any) (*RawTransaction, error) {
return APTTransferTransaction(client, sender, AccountOne, 100, options...)
}
Expand Down
12 changes: 7 additions & 5 deletions examples/alternative_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"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 @@ -61,10 +60,8 @@ func (signer *AlternativeSigner) AuthKey() *crypto.AuthenticationKey {
return authKey
}

// main This example shows you how to make an alternative signer for the SDK, if you prefer a different library
func main() {
// Create a client for Aptos
client, err := aptos.NewClient(aptos.DevnetConfig)
func example(network aptos.NetworkConfig) {
client, err := aptos.NewClient(network)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -121,3 +118,8 @@ func main() {
}
fmt.Printf("The transaction completed with hash: %s and version %d\n", userTxn.Hash, userTxn.Version)
}

// main This example shows you how to make an alternative signer for the SDK, if you prefer a different library
func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/alternative_signing/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
38 changes: 19 additions & 19 deletions examples/external_signing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ func (signer *ExternalSigner) AuthKey() *crypto.AuthenticationKey {
return authKey
}

// main This example shows you how to make an alternative signer for the SDK, if you prefer a different library
func main() {
func example(networkConfig aptos.NetworkConfig) {
// Create a client for Aptos
client, err := aptos.NewClient(aptos.DevnetConfig)
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -97,10 +96,8 @@ func main() {

// Sign transaction
fmt.Printf("Submit a coin transfer to address %s\n", receiver.String())
rawTxn := &aptos.RawTransaction{
Sender: sender.Address,
SequenceNumber: 0,
Payload: aptos.TransactionPayload{Payload: &aptos.EntryFunction{
rawTxn, err := client.BuildTransaction(sender.Address,
aptos.TransactionPayload{Payload: &aptos.EntryFunction{
Module: aptos.ModuleId{
Address: aptos.AccountOne,
Name: "aptos_account",
Expand All @@ -112,11 +109,13 @@ func main() {
amountBytes[:],
},
}},
MaxGasAmount: 1000,
GasUnitPrice: 2000,
ExpirationTimestampSeconds: 1714158778,
ChainId: 4,
)
if err != nil {
panic("Failed to build raw transaction:" + err.Error())
}

// Send it to our external signer

fmt.Printf("Sign the message %s\n", receiver.String())
// Build a signing message
signingMessage, err := rawTxn.SigningMessage()
Expand All @@ -130,16 +129,12 @@ func main() {
panic("Failed to sign message:" + err.Error())
}

txnAuth := &aptos.TransactionAuthenticator{
Variant: aptos.TransactionAuthenticatorEd25519,
Auth: auth,
}

// Build a signed transaction
signedTxn := &aptos.SignedTransaction{
Transaction: rawTxn,
Authenticator: txnAuth,
signedTxn, err := rawTxn.SignedTransactionWithAuthenticator(auth)
if err != nil {
panic("Failed to convert transaction authenticator:" + err.Error())
}

// TODO: Show how to send over a wire with an encoding

// Submit and wait for it to complete
Expand All @@ -158,3 +153,8 @@ func main() {

fmt.Printf("The transaction completed with hash: %s and version %d\n", userTxn.Hash, userTxn.Version)
}

// main This example shows you how to make an alternative signer for the SDK, if you prefer a different library
func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/external_signing/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
10 changes: 7 additions & 3 deletions examples/fungible_asset/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const testEd25519PrivateKey = "0xc5338cd251c22daa8c9c9cc94f498cc8a5c7e1d2e75287a
const metadata = "0x0572757065650100000000000000004038393534453933384132434137314536433445313139434230333341363036453341333537424245353843354430304235453132354236383238423745424331e7011f8b08000000000002ff3d8ecd6ec4200c84ef3cc58a7b13427ea9d4432f7d8928aa0c7636d136100149fbf885ed764ff68cbeb167dcc1dce04a13b3b0d1e5edc2fdb1137176920fabb3d9a90a5108cee0888bf32139e3c4d808889e42a030b17be4331b19173faa1f8cac6aa5846986bac6b9afda6648c350a3b06d4cdf2aec7487aa9648526ad960db894ee81e6609c0d379a49d2c92352b85e27d8f2e7cf8d4f0dbf9dbc4ae6bcc9f9618f7f05a96492e872e8cdb4ac8e4cb17e8f0588df3542480334f670e6db05a4b498743e37a6ffc476eeea472fe7ff2883f3567bf9aa419822b010000010572757065650000000300000000000000000000000000000000000000000000000000000000000000010e4170746f734672616d65776f726b00000000000000000000000000000000000000000000000000000000000000010b4170746f735374646c696200000000000000000000000000000000000000000000000000000000000000010a4d6f76655374646c696200"
const bytecode = "0xa11ceb0b060000000c01000e020e30033e850104c3010605c901980107e102c30408a4074006e407950110f9088a010a830a120c950ae1020df60c060000010101020103010401050106000708000110060001120600011406000212060002170600011a0800021b07010001021e02000323070100000625070000080001000009010200000a030100000b030100000c000100000d040100000e05010005180302000219070200021c02090108021d0a0b010804080c0100021f0e0f000220101100022110120002221301000324011501000626161700042718010001281019000121101a000129101b00022a101c00040c1d0100042b1e0100042c1f010009080a08101403060c050300010501060c03060c050104060c050503030505050206050a02010806010b07010900020b070109000501010306080305030708030808080108050c0804080202060c0a0201080801060808010805010804010608040104010b09010900010a0201080a070608080b090104080a080a02080a080a010801010802010803010c030608010503030608020501040608020505030572757065650e66756e6769626c655f6173736574066f626a656374066f7074696f6e167072696d6172795f66756e6769626c655f73746f7265067369676e657206737472696e6709527570656552656673046275726e0a66615f616464726573730b696e69745f6d6f64756c650a696e697469616c697a65046d696e740a7365745f667265657a65087472616e73666572086d696e745f726566074d696e745265660c7472616e736665725f7265660b5472616e73666572526566086275726e5f726566074275726e526566106f626a5f7472616e736665725f7265660e6f626a5f657874656e645f72656609457874656e645265660a616464726573735f6f66156372656174655f6f626a6563745f61646472657373084d65746164617461064f626a65637411616464726573735f746f5f6f626a6563740869735f6f776e65720e436f6e7374727563746f72526566136372656174655f6e616d65645f6f626a6563741367656e65726174655f657874656e645f7265661567656e65726174655f7472616e736665725f7265661864697361626c655f756e67617465645f7472616e73666572064f7074696f6e046e6f6e6506537472696e6704757466382b6372656174655f7072696d6172795f73746f72655f656e61626c65645f66756e6769626c655f61737365741167656e65726174655f6d696e745f7265661167656e65726174655f6275726e5f7265660f67656e65726174655f7369676e65720f7365745f66726f7a656e5f666c6167117472616e736665725f776974685f726566978c213990c4833df71548df7ce49d54c759d6b6d932de22b24d56060b7af2aa0000000000000000000000000000000000000000000000000000000000000001030801000000000000000201020a02060552757065650a021413527570656573417265466f7254657374696e670a02060552555045450a023635697066733a2f2f516d585342534c6f337744426e6133314d4c56784b4a356f424a486b676f444d7845676a527a39745044665a506e0520978c213990c4833df71548df7ce49d54c759d6b6d932de22b24d56060b7af2aa0a020100126170746f733a3a6d657461646174615f7631760101000000000000000b455f4e4f545f4f574e45522a43616c6c6572206973206e6f74206f776e6572206f6620746865206d65746164617461206f626a6563740109527570656552656673010301183078313a3a6f626a6563743a3a4f626a65637447726f7570010a66615f616464726573730101000002050f08011108021308031508041608050000040100061a0b0011070c0507060c030e030703110838000b053801040d050f07002707060c040e04070311082b0010000b010b02110b0201010000020607060c000e0007031108020200000001030b00110302030000000d2d0b000703110c0c020e02110d0c040e02110e0c060e06110f0e02380207021111070411110701070511110707111111120e0211130c030e0211140c070e0211150c010e0211160c050e050b030b070b010b060b0412002d00020400040100061a0b0011070c0507060c030e030703110838000b053801040d050f07002707060c040e04070311082b0010010b010b021117020500040100061a0b0011070c0507060c030e030703110838000b053801040d050f07002707060c040e04070311082b0010020b010b021118020600040100061b0b0011070c0607060c040e040703110838000b063801040d050f07002707060c050e05070311082b0010020b010b020b0311190200020000000100"

// main This example shows how to create and transfer fungible assets
func main() {
func example(networkConfig aptos.NetworkConfig) {
// Create a client for Aptos
client, err := aptos.NewClient(aptos.DevnetConfig)
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -147,3 +146,8 @@ func main() {

fmt.Printf("Receiver Before transfer: %d, after transfer: %d\n", receiverBeforeBalance, receiverAfterBalance)
}

// main This example shows how to create and transfer fungible assets
func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/fungible_asset/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
8 changes: 6 additions & 2 deletions examples/onchain_multisig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

const TransferAmount = uint64(1_000_000)

func main() {
client, err := aptos.NewClient(aptos.DevnetConfig)
func example(networkConfig aptos.NetworkConfig) {
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client " + err.Error())
}
Expand Down Expand Up @@ -324,3 +324,7 @@ func submitAndWait(client *aptos.Client, sender *aptos.Account, payload aptos.Tr

return txn
}

func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/onchain_multisig/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
10 changes: 7 additions & 3 deletions examples/performance_transaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"time"
)

// main This example shows you how to improve performance of the transaction submission
// example This example shows you how to improve performance of the transaction submission
//
// Speed can be improved by locally handling the sequence number, gas price, and other factors
func main() {
func example(networkConfig aptos.NetworkConfig) {
start := time.Now()
before := time.Now()
// Create a client for Aptos
client, err := aptos.NewClient(aptos.DevnetConfig)
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -98,3 +98,7 @@ func main() {
txnStr, _ := json.Marshal(txn)
println(string(txnStr))
}

func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/performance_transaction/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
17 changes: 7 additions & 10 deletions examples/sponsored_transaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@ import (
const FundAmount = 100_000_000
const TransferAmount = 1_000

// main This example shows you how to make an APT transfer transaction in the simplest possible way
func main() {
// example This example shows you how to make an APT transfer transaction in the simplest possible way
func example(networkConfig aptos.NetworkConfig) {
// Create a client for Aptos
netConfig := aptos.NetworkConfig{
Name: "Local",
ChainId: 4,
NodeUrl: "http://localhost:8080/v1",
IndexerUrl: "",
FaucetUrl: "http://localhost:8081/",
}
client, err := aptos.NewClient(netConfig)
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -208,3 +201,7 @@ func main() {
fmt.Printf("Bob: %d\n", bobBalance)
fmt.Printf("Sponsor: %d\n", sponsorBalance)
}

func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/sponsored_transaction/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
10 changes: 7 additions & 3 deletions examples/transfer_coin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
const FundAmount = 100_000_000
const TransferAmount = 1_000

// main This example shows you how to make an APT transfer transaction in the simplest possible way
func main() {
// example This example shows you how to make an APT transfer transaction in the simplest possible way
func example(networkConfig aptos.NetworkConfig) {
// Create a client for Aptos
client, err := aptos.NewClient(aptos.DevnetConfig)
client, err := aptos.NewClient(networkConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
Expand Down Expand Up @@ -132,3 +132,7 @@ func main() {
fmt.Printf("Alice: %d\n", aliceBalance)
fmt.Printf("Bob:%d\n", bobBalance)
}

func main() {
example(aptos.DevnetConfig)
}
10 changes: 10 additions & 0 deletions examples/transfer_coin/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/aptos-labs/aptos-go-sdk"
"testing"
)

func Test_Main(t *testing.T) {
example(aptos.LocalnetConfig)
}
10 changes: 10 additions & 0 deletions nodeClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,16 @@ func (rc *NodeClient) BuildSignAndSubmitTransaction(sender TransactionSigner, pa
return rc.SubmitTransaction(signedTxn)
}

func (rc *NodeClient) NodeHealthCheck(durationSecs ...uint64) (api.HealthCheckResponse, error) {
au := rc.baseUrl.JoinPath("-/healthy")
if len(durationSecs) > 0 {
params := url.Values{}
params.Set("duration_secs", strconv.FormatUint(durationSecs[0], 10))
au.RawQuery = params.Encode()
}
return Get[api.HealthCheckResponse](rc, au.String())
}

func Get[T any](rc *NodeClient, getUrl string) (out T, err error) {
req, err := http.NewRequest("GET", getUrl, nil)
if err != nil {
Expand Down
Loading