sui

package module
v0.0.0-...-d9a9773 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2022 License: MIT Imports: 13 Imported by: 0

README

sui-sdk-go

Go Build statusTest status SDK Documentation MIT License

sui-sdk-go is a golang sdk for Sui. 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/sui-sdk-go

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

Here is the code

package main

import (
    "fmt"
    "time"

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

func main() {
    alice := sui.NewAccount()
    bob := sui.NewAccount()
    fmt.Printf("faucet first \n")
    err := sui.Faucet(alice.Address().String())
    if err != nil {
        panic(err.Error())
    }
    err = sui.Faucet(bob.Address().String())
    if err != nil {
        panic(err.Error())
    }

    cli, err := sui.Dial(sui.Devnet)
    if err != nil {
        panic(err.Error())
    }

    fmt.Printf("wait 3 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:%f\n", aliceBalance)

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

    hash, err := alice.Transfer(cli, bob.Address(), 0.01)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("Alice transfer %f SUI to bob with hash:%s\n", 0.01, 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:%f\n", aliceBalance)

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

}

Then in the explorer , we can see what happend .

Support RPC Status -> v0.19.0

  • sui_batchTransaction
  • sui_dryRunTransaction
  • sui_executeTransaction
  • sui_executeTransactionSerializedSig
  • sui_getCoinMetadata
  • sui_getCommitteeInfo
  • sui_getEvents
  • sui_getMoveFunctionArgTypes
  • sui_getNormalizedMoveFunction
  • sui_getNormalizedMoveModule
  • sui_getNormalizedMoveModulesByPackage
  • sui_getNormalizedMoveStruct
  • sui_getObject
  • sui_getObjectsOwnedByAddress
  • sui_getObjectsOwnedByObject
  • sui_getRawObject
  • sui_getTotalTransactionNumber
  • sui_getTransaction
  • sui_getTransactionAuthSigners
  • sui_getTransactions
  • sui_getTransactionsInRange
  • sui_mergeCoins
  • sui_moveCall
  • sui_pay
  • sui_payAllSui
  • sui_paySui
  • sui_publish
  • sui_splitCoin
  • sui_splitCoinEqual
  • sui_subscribeEvent
  • sui_transferObject
  • sui_transferSui
  • sui_tryGetPastObject

Documentation

Index

Constants

View Source
const (
	Devnet  string = "https://fullnode.devnet.sui.io"
	Testnet string = "https://fullnode.testnet.sui.io:443"
)
View Source
const (
	ED25519   = SignatureScheme("ED25519")
	Secp256k1 = SignatureScheme("Secp256k1")
)
View Source
const (
	ImmediateReturn       = ExecuteTransactionRequestType("ImmediateReturn")
	WaitForTxCert         = ExecuteTransactionRequestType("WaitForTxCert")
	WaitForEffectsCert    = ExecuteTransactionRequestType("WaitForEffectsCert")
	WaitForLocalExecution = ExecuteTransactionRequestType("WaitForLocalExecution")
)

Variables

View Source
var (
	ErrNilClient = errors.New("client is nil")
	ErrNumber    = errors.New("error number")
)
View Source
var (
	Endpoint string
)

Functions

func Faucet

func Faucet(addr string) (err error)

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(client *Client) (balance float64, err error)

func (*Account) PrivateKey

func (acc *Account) PrivateKey() string

func (*Account) Sign

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

func (*Account) Transfer

func (acc *Account) Transfer(client *Client, recipient types.Address, amount float64) (hash string, err error)

type Client

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

Client defines typed wrappers for the Sui RPC API.

func Dial

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

Dial connects a client to the given URL.

func DialContext

func DialContext(ctx context.Context, rawurl string) (*Client, error)

func NewClient

func NewClient(c *rpc.Client) *Client

NewClient creates a client that uses the given RPC client.

func (*Client) Close

func (c *Client) Close()

Close close a client's connection

func (*Client) ExecuteTransaction

func (c *Client) ExecuteTransaction(ctx context.Context, txBytes string, signatureScheme SignatureScheme, signature string, pubkey types.PubKey, requestType ExecuteTransactionRequestType) (response types.SuiExecuteTransactionResponse, err error)

ExecuteTransaction execute the transaction and wait for results if desired. Request types: 1. ImmediateReturn: immediately returns a response to client without waiting for any execution results. Note the transaction may fail without being noticed by client in this mode.

After getting the response, the client may poll the node to check the result of the transaction.

2. WaitForTxCert: waits for TransactionCertificate and then return to client. 3. WaitForEffectsCert: waits for TransactionEffectsCert and then return to client. This mode is a proxy for transaction finality. 4. WaitForLocalExecution: waits for TransactionEffectsCert and make sure the node executed the transaction locally before returning the client. The local execution makes sure this node is aware of this transaction when client fires subsequent queries. However if the node fails to execute the transaction locally in a timely manner, a bool type in the response is set to false to indicated the case

func (*Client) ExecuteTransactionSerializedSig

func (c *Client) ExecuteTransactionSerializedSig(ctx context.Context, txBytes string, signature string, requestType ExecuteTransactionRequestType) (response types.SuiExecuteTransactionResponse, err error)

ExecuteTransactionSerializedSig execute the transaction and wait for results if desired. use signature data directly

func (*Client) GetCoinMetadata

func (c *Client) GetCoinMetadata(ctx context.Context, coinType string) (metadata types.SuiCoinMetadata, err error)

GetCoinMetadata return metadata(e.g., symbol, decimals) for a coin

func (*Client) GetCommitteeInfo

func (c *Client) GetCommitteeInfo(ctx context.Context, epoch *uint64) (info types.CommitteeInfo, err error)

GetCommitteeInfo return the committee information for the asked epoch

func (*Client) GetEvents

func (c *Client) GetEvents(ctx context.Context, query types.EventQuery, cursor *types.EventID, limit uint64, descending bool) (event *types.PaginatedEvents, err error)

func (*Client) GetGormalizedMoveModule

func (c *Client) GetGormalizedMoveModule(ctx context.Context, packagee types.ObjectID, module string) (moduleInfo types.SuiMoveNormalizedModule, err error)

GetGormalizedMoveModule return a structured representation of Move module

func (*Client) GetMoveFunctionArgTypes

func (c *Client) GetMoveFunctionArgTypes(ctx context.Context, packagee types.ObjectID, module string, function string) (argTypes types.MoveFunctionArgTypes, err error)

GetMoveFunctionArgTypes return the argument types of a Move function, based on normalized Type

func (*Client) GetNormalizedMoveFunction

func (c *Client) GetNormalizedMoveFunction(ctx context.Context, packagee types.ObjectID, module string, function string) (functionInfo types.SuiMoveNormalizedFunction, err error)

GetNormalizedMoveFunction return a structured representation of Move function

func (*Client) GetNormalizedMoveModulesByPackage

func (c *Client) GetNormalizedMoveModulesByPackage(ctx context.Context, packagee types.ObjectID) (modules map[string]*types.SuiMoveNormalizedModule, err error)

GetNormalizedMoveModulesByPackage return structured representations of all modules in the given package

func (*Client) GetNormalizedMoveStruct

func (c *Client) GetNormalizedMoveStruct(ctx context.Context, packagee types.ObjectID, module string, structt string) (structInfo types.SuiMoveNormalizedStruct, err error)

GetNormalizedMoveStruct return a structured representation of Move struct

func (*Client) GetObject

func (c *Client) GetObject(ctx context.Context, objectID types.ObjectID) (objectData types.SuiObjectData, err error)

GetObject the object information for a specified object

func (*Client) GetObjectsOwnedByAddress

func (c *Client) GetObjectsOwnedByAddress(ctx context.Context, address types.Address) (object []*types.SuiObjectInfo, err error)

GetObjectsOwnedByAddress return the list of objects owned by an address

func (*Client) GetObjectsOwnedByObject

func (c *Client) GetObjectsOwnedByObject(ctx context.Context, objectID types.ObjectID) (object []*types.SuiObjectInfo, err error)

GetObjectsOwnedByObject return the list of objects owned by an object

func (*Client) GetRawObject

func (c *Client) GetRawObject(ctx context.Context, objectID types.ObjectID) (object *types.SuiObjectData, err error)

GetRawObject return the raw BCS serialized move object bytes for a specified object

func (*Client) GetTotalTransactionNumber

func (c *Client) GetTotalTransactionNumber(ctx context.Context) (number uint64, err error)

GetTotalTransactionNumber return the total number of transactions known to the server

func (*Client) GetTransaction

func (c *Client) GetTransaction(ctx context.Context, transaction types.TransactionDigest) (response *types.SuiTransactionResponse, err error)

GetTransaction return the transaction response object

func (*Client) GetTransactionAuthSigners

func (c *Client) GetTransactionAuthSigners(ctx context.Context, transaction types.TransactionDigest) (response *types.SuiTransactionAuthSignersResponse, err error)

GetTransactionAuthSigners return the authority public keys that commits to the authority signature of the transaction

func (*Client) GetTransactions

func (c *Client) GetTransactions(ctx context.Context, query types.TransactionQuery, cursor types.TransactionDigest, limit int64, descending bool) (digests *types.PaginatedTransactionDigests, err error)

GetTransactions return list of transactions for a specified query criteria

func (*Client) GetTransactionsInRange

func (c *Client) GetTransactionsInRange(ctx context.Context, start int64, end int64) (digests []types.TransactionDigest, err error)

GetTransactionsInRange return list of transaction digests within the queried range

func (*Client) MergeCoins

func (c *Client) MergeCoins(ctx context.Context, signer types.Address, primaryCoin types.ObjectID, coinToMerge types.ObjectID, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

MergeCoins create an unsigned transaction to merge multiple coins into one coin

func (*Client) MoveCall

func (c *Client) MoveCall(ctx context.Context, signer types.Address, packageObjectID types.ObjectID, module string, function string, typeArgs []string, args []json.RawMessage, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

MoveCall create an unsigned transaction to execute a Move call on the network, by calling the specified function in the module of a given package

func (*Client) Pay

func (c *Client) Pay(ctx context.Context, signer types.Address, inputCoins []types.ObjectID, recipients []string, amounts []int64, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

Pay send Coin<T> to a list of addresses, where `T` can be any coin type, following a list of amounts, The object specified in the `gas` field will be used to pay the gas fee for the transaction. The gas object can not appear in `input_coins`. If the gas object is not specified, the RPC server will auto-select one

func (*Client) PayAllSui

func (c *Client) PayAllSui(ctx context.Context, signer types.Address, coins []types.ObjectID, recipient types.Address, gasBudget uint64) (bytes types.TransactionBytes, err error)

PayAllSui send all SUI coins to one recipient. This is for SUI coin only and does not require a separate gas coin object. Specifically, what pay_all_sui does are: 1. accumulate all SUI from input coins and deposit all SUI to the first input coin 2. transfer the updated first coin to the recipient and also use this first coin as gas coin object. 3. the balance of the first input coin after tx is sum(input_coins) - actual_gas_cost. 4. all other input coins other than the first are deleted

func (*Client) PaySui

func (c *Client) PaySui(ctx context.Context, signer types.Address, inputCoins []types.ObjectID, recipients []string, amounts []int64, gasBudget uint64) (bytes types.TransactionBytes, err error)

PaySui send SUI coins to a list of addresses, following a list of amounts. This is for SUI coin only and does not require a separate gas coin object. Specifically, what pay_sui does are: 1. debit each input_coin to create new coin following the order of amounts and assign it to the corresponding recipient. 2. accumulate all residual SUI from input coins left and deposit all SUI to the first input coin, then use the first input coin as the gas coin object. 3. the balance of the first input coin after tx is sum(input_coins) - sum(amounts) - actual_gas_cost 4. all other input coints other than the first one are deleted.

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, signer types.Address, compiledModules []string, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

Publish create an unsigned transaction to publish Move module

func (*Client) SplitCoin

func (c *Client) SplitCoin(ctx context.Context, signer types.Address, coin types.ObjectID, splitAmounts []int64, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

SplitCoin create an unsigned transaction to split a coin object into multiple coins

func (*Client) SplitCoinEqual

func (c *Client) SplitCoinEqual(ctx context.Context, signer types.Address, coin types.ObjectID, splitCount int64, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

SplitCoinEqual create an unsigned transaction to split a coin object into multiple equal-size coins

func (*Client) TransferObject

func (c *Client) TransferObject(ctx context.Context, signer types.Address, objectID types.ObjectID, recipient types.Address, gas types.ObjectID, gasBudget uint64) (bytes types.TransactionBytes, err error)

TransferObject create an unsigned transaction to transfer an object from one address to another. The object's type must allow public transfers

func (*Client) TransferSui

func (c *Client) TransferSui(ctx context.Context, signer types.Address, objectID types.ObjectID, recipient types.Address, amount int64, gasBudget uint64) (bytes types.TransactionBytes, err error)

TransferSui create an unsigned transaction to send SUI coin object to a Sui address. The SUI object is also used as the gas object

type ExecuteTransactionRequestType

type ExecuteTransactionRequestType string

type SignatureScheme

type SignatureScheme string

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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