aptos

package module
v0.0.0-...-270a13f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 18 Imported by: 0

README

aptos-sdk-go

Aptos SDK for Golang

Aptos RPC Go Build statusTest status SDK Documentation MIT License

aptos-sdk-go is a golang sdk for Aptos. Which contains all RPC API with Client and some operation for Account object such as Transfer, Balance etc...

Getting started

Add SDK Dependencies

$ go get -u  github.com/0x6368616e67/aptos-sdk-go

A "Alice and Bob" example demonstrate two user Alice and Bob. each of them faucet 10000 coin first. and then Alice transfer 5000 to Bob.

Here is the code

package main

import (
    "context"
    "fmt"
    "net/http"
    "time"

    aptos "github.com/0x6368616e67/aptos-sdk-go"
)

var (
    faucetURLFmt = "https://tap.devnet.prod.gcp.aptosdev.com/mint?address=%s&amount=%d"
)

func faucet(addr string, amount uint64) (err error) {
    faucetURL := fmt.Sprintf(faucetURLFmt, addr, amount)
    fmt.Printf("Get faucet:%s \n", faucetURL)
    _, err = http.Post(faucetURL, "", nil)
    return err

}

func main() {
    cli, err := aptos.DialContext(context.Background(), aptos.Devnet)
    if err != nil {
        panic(err.Error())
    }
    alice := aptos.NewAccount()
    bob := aptos.NewAccount()
    fmt.Printf("faucet first \n")
    err = faucet(alice.Address().String(), 1000000)
    if err != nil {
        panic(err.Error())
    }
    err = faucet(bob.Address().String(), 1000000)
    if err != nil {
        panic(err.Error())
    }

    fmt.Printf("wait 10 second ...")
    time.Sleep(10 * time.Second) // wait for stat
    aliceBalance, err := alice.Balance(cli)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Alice balance:%d\n", aliceBalance)

    bobBalance, err := bob.Balance(cli)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Bob balance:%d\n", bobBalance)

    hash, err := alice.Transfer(cli, bob.Address(), 5000)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Alice transfer %d to bob with hash:%s\n", 5000, hash)
    fmt.Printf("====================================================\n")
    fmt.Printf("wait 10 second ...\n")
    time.Sleep(10 * time.Second) // wait for stat
    aliceBalance, err = alice.Balance(cli)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Alice balance:%d\n", aliceBalance)

    bobBalance, err = bob.Balance(cli)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Bob balance:%d\n", bobBalance)

}

when run we got:

alice_and_bob % go run main.go 
faucet first 
Get faucet:https://tap.devnet.prod.gcp.aptosdev.com/mint?address=0x62c0ce0d9d2d6bea852abd6f4feb6f88dea0a4a4eabc9edf295a4949f3f47870&amount=10000 
Get faucet:https://tap.devnet.prod.gcp.aptosdev.com/mint?address=0x0b74ceb77162d79c5d61a546e8a2e47c29e35f0fced9e961e8c7a919802ed0de&amount=10000 
wait 10 second ...
Alice balance:10000
Bob balance:10000
Alice transfer 5000 to bob with hash:0x6566c80b00cb66a79cbe153ed96827fc62a8b57d4675dd260813edf644fe3939
====================================================
wait 10 second ...
Alice balance:4999
Bob balance:15000

Then in the explorer , we can see what happend 0x6566c80b00cb66a79cbe153ed96827fc62a8b57d4675dd260813edf644fe3939 (as devnet will be reload, this version for the hash will change, use your own please.)

Support RPC Status

Accounts: Access to account resources and modules
  • Get account
  • Get account resources
  • Get account modules
  • Get specific account resource
  • Get specific account module
Blocks: Access to blocks
  • Get blocks by height
  • Get blocks by version
Events: Access to events
  • Get events by event key
  • Get events by event handle
General: General information
  • Show OpenAPI explorer
  • Check basic node health
  • Get ledger info
Tables: Access to tables
  • Get table item
  • Get raw table item
Transactions: Access to transactions
  • Get transactions
  • Submit transaction
  • Get transaction by hash
  • Get transaction by version
  • Get account transactions
  • Submit batch transactions
  • Simulate transaction
  • Encode submission
  • Estimate gas price
View
  • [] Execute view function of a module

TODO

  • Add wait status for sending a transaction
  • Add non native token operation for 0x3::token
  • Add support for x25519
  • Add more examples
  • Add more document
  • Add support for tables
  • Add support multi signer

Documentation

Index

Constants

View Source
const (
	Devnet  string = "https://fullnode.devnet.aptoslabs.com"
	Mainnet string = "https://fullnode.mainnet.aptoslabs.com"
)

Variables

View Source
var (
	Endpoint     string
	MaxGasAmount = 10000
	GasUnitPrice = 100
)
View Source
var (
	ErrNilClient = errors.New("client is nil")
)

Functions

This section is empty.

Types

type Account

type Account struct {
	// contains filtered or unexported fields
}

func NewAccount

func NewAccount() *Account

func NewAccountWithHexSeed

func NewAccountWithHexSeed(seed string) *Account

func NewAccountWithPrivateKey

func NewAccountWithPrivateKey(key string) *Account

func (*Account) Address

func (acc *Account) Address() types.Address

func (*Account) Balance

func (acc *Account) Balance(cli *Client) (balance uint64, err error)

func (*Account) CreateAptosAccount

func (acc *Account) CreateAptosAccount(cli *Client) (account *framework.AptosAccount, err error)

func (*Account) PrivateKey

func (acc *Account) PrivateKey() string

func (*Account) SendTransaction

func (acc *Account) SendTransaction(cli *Client, tx *types.Transaction) (hash string, err error)

func (*Account) Sequence

func (acc *Account) Sequence() uint64

func (*Account) Sign

func (acc *Account) Sign(msg []byte) ([]byte, error)

func (*Account) SignTx

func (acc *Account) SignTx(cli *Client, tx *types.Transaction) (err error)

func (*Account) SignTxWithBCSPayload

func (acc *Account) SignTxWithBCSPayload(tx *types.Transaction, payload types.RawTransactionPayload) (err error)

func (*Account) SimulateTransaction

func (acc *Account) SimulateTransaction(cli *Client, tx *types.Transaction) (err error)

func (*Account) SyncSequence

func (acc *Account) SyncSequence(cli *Client) error

func (*Account) Transfer

func (acc *Account) Transfer(cli *Client, to types.Address, amount uint64) (hash string, err error)

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represent a RPC Client

func Dial

func Dial(rpcurl string) (*Client, error)

Dial connects a client to the given URL.

func DialContext

func DialContext(ctx context.Context, rpcurl string) (client *Client, err error)

Dial connects a client to the given URL and given context

func (*Client) EstimateGasPrice

func (cli *Client) EstimateGasPrice(ctx context.Context) (info *v1.EstimateGasPrice, err error)

func (*Client) GetAccount

func (cli *Client) GetAccount(ctx context.Context, address string, ledger uint64) (info *v1.AccountInfo, err error)

func (*Client) GetAccountModule

func (cli *Client) GetAccountModule(ctx context.Context, address string, ledger uint64) (infos []*v1.AccountModuleInfo, err error)

func (*Client) GetAccountModuleWithName

func (cli *Client) GetAccountModuleWithName(ctx context.Context, address string, name string, ledger uint64) (info *v1.AccountModuleInfo, err error)

func (*Client) GetAccountResource

func (cli *Client) GetAccountResource(ctx context.Context, address string, ledger uint64) (infos []*v1.AccountResourceInfo, err error)

func (*Client) GetAccountResourceWithType

func (cli *Client) GetAccountResourceWithType(ctx context.Context, address string, resType string, ledger uint64) (info *v1.AccountResourceInfo, err error)

func (*Client) GetBlockByHeight

func (cli *Client) GetBlockByHeight(ctx context.Context, height uint64, withTransactions bool) (info *v1.BlockInfo, err error)

func (*Client) GetBlockByVersion

func (cli *Client) GetBlockByVersion(ctx context.Context, version uint64, withTransactions bool) (info *v1.BlockInfo, err error)

func (*Client) GetEventByCreationNumber

func (cli *Client) GetEventByCreationNumber(ctx context.Context, address string, number string, limit uint16, start uint64) (infos []*v1.EventInfo, err error)

func (*Client) GetEventByHandler

func (cli *Client) GetEventByHandler(ctx context.Context, address string, handler string, field string, limit uint16, start uint64) (infos []*v1.EventInfo, err error)

func (*Client) GetTransactionByHash

func (cli *Client) GetTransactionByHash(ctx context.Context, hash string) (info *v1.TransactionInfo, err error)

func (*Client) GetTransactionByVersion

func (cli *Client) GetTransactionByVersion(ctx context.Context, version uint64) (info *v1.TransactionInfo, err error)

func (*Client) GetTransactionEncoding

func (cli *Client) GetTransactionEncoding(ctx context.Context, tx *types.Transaction) (code string, err error)

func (*Client) GetTransactions

func (cli *Client) GetTransactions(ctx context.Context, start uint64, limit uint16) (infos []*v1.TransactionInfo, err error)

func (*Client) GetTransactionsOfAccount

func (cli *Client) GetTransactionsOfAccount(ctx context.Context, address string, start uint64, limit uint16) (infos []*v1.TransactionInfo, err error)

func (*Client) Healthy

func (cli *Client) Healthy(ctx context.Context, duration uint32) error

func (*Client) LedgerInfo

func (cli *Client) LedgerInfo(ctx context.Context) (info *v1.LedgerInfo, err error)

func (*Client) SimulateTransaction

func (cli *Client) SimulateTransaction(ctx context.Context, tx *types.Transaction) (result []*v1.TransactionInfo, err error)

func (*Client) SubmitTransaction

func (cli *Client) SubmitTransaction(ctx context.Context, tx *types.Transaction) (result *v1.TransactionInfo, err error)

type HTTPError

type HTTPError struct {
	StatusCode int
	Status     string
	Body       []byte
}

HTTPError is a wrap for HTTP's not 2xx codes

func (HTTPError) Error

func (err HTTPError) Error() string

Directories

Path Synopsis
api
v1
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL