Skip to content

MoonBaZZe/znn-sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zenon Go SDK implementation

Go Report Card GoDoc PRs Welcome GitHub license

It follows the official Dart SDK code structure. Tested with Go v1.18. It is 100% compatible with go-zenon.

The Go SDK features a client that will connect to a Zenon full node via websockets. We recommend setting up and running a local Zenon full node.

Usage

Install Go v1.18 and start the latest znnd node version on your local host. By default the RPC calls are enabled, otherwise please check the config.json.

Some RPC calls require the keyFile to modify the state of the ledger for example moving funds, while others only read the on-chain data such as getting the latest momentum or a particular account-block.

Commands with keyFile

  • Note that there must be a valid keyFile with the specified name in DefaultWalletDir. The name of the keyFile can be specified by the user, otherwise by default it is the baseAddress.
// Load your keyFile (wallet file)
z, err := zenon.NewZenon("keyfile-sdk")
if err != nil {
    zenon.CommonLogger.Error("", err)
}

// Connect to local node and decrypt your wallet from defaultKeyFilePath
if err := z.Start("123456", "ws://127.0.0.1:35998", 0); err != nil {
    zenon.CommonLogger.Error("", err)
}

// Issue RPC calls to the node

// Stop the client
if err := z.Stop(); err != nil {
    zenon.CommonLogger.Error("", err)
}

Commands without keyFile

  • Calls that can be issued without a keyFile. Note that calls that require a keyFile cannot be used.
// Initialize zenon client (without keyFile)
z, err := zenon.NewZenon("")
if err != nil {
    zenon.CommonLogger.Error("", err)
}

// Connect to local node
if err := z.Start("", "ws://127.0.0.1:35998", 0); err != nil {
    zenon.CommonLogger.Error("", err)
}

// Issue RPC calls to the node

// Stop the client
if err := z.Stop(); err != nil {
    zenon.CommonLogger.Error("", err)
}

Wallet commands

Read existing keyFile

keyFile, err := wallet.ReadKeyFile("keyfile-sdk", "123456")
if err != nil {
    fmt.Printf("err: %s\n", err)
} else {
    fmt.Printf("baseAddress: %s\n", keyFile.BaseAddress)
    _, kp, err := keyFile.DeriveForIndexPath(0)
    if err != nil {
        fmt.Printf("err: %s\n", err)
    } else {
        fmt.Printf("kp address: %s\n", kp.Address.String())
    }
}

Create a new keyFile

kf, err := wallet.NewKeyFile()
if err != nil {
    zenon.WalletLogger.Error("", err)
} else {
    if err := wallet.WriteKeyFile(kf, "keyfile-sdk", "123456"); err != nil {
        zenon.WalletLogger.Error("", "wallet", err)
    }
}

Send and receive commands

  • Note that you must fill in the ZnnTokenStandard accordingly for ZNN, QSR or ZTS
if err := z.Send(z.Client.LedgerApi.SendTemplate(toAddress, types.ZnnTokenStandard, amount, []byte{})); err != nil {
      fmt.Println(err)
}
if err := z.Send(z.Client.LedgerApi.ReceiveTemplate(types.HexToHashPanic("HASH"))); err != nil {
    fmt.Println(err)
}

Pillar commands

Register Pillar

if err := z.Send(z.Client.PillarApi.Register("keyfile-sdk", z.Address(), z.Address(), 0, 50)); err != nil {
    fmt.Println(err)
}

Update Pillar

if err := z.Send(z.Client.PillarApi.UpdatePillar("keyfile-sdk", types.ParseAddressPanic("z1qqmqp78duzxhpvg7dwxph7724mqu2t3mru297p"), z.Address(), 10, 10)); err != nil {
    fmt.Println(err)
}

Delegate commands

Delegate to Pillar

z.Send(z.Client.PillarApi.Delegate("Pillar"))

Undelegate

z.Send(z.Client.PillarApi.Undelegate())

Sentinel

Deposit QSR for Sentinel slot setup

amount := big.NewInt(50000 * constants.Decimals)
if err := z.Send(z.Client.SentinelApi.DepositQsr(amount)); err != nil {
    fmt.Println(err)
}

Withdraw QSR from Sentinel slot setup

if err := z.Send(z.Client.SentinelApi.WithdrawQsr()); err != nil {
    fmt.Println(err)
}

Register Sentinel

if err := z.Send(z.Client.SentinelApi.Register()); err != nil {
    fmt.Println(err)
}

Revoke Sentinel

if err := z.Send(z.Client.SentinelApi.Revoke()); err != nil {
    fmt.Println(err)
}

Plasma commands

Fuse Plasma for address

if err := z.Send(z.Client.PlasmaApi.Fuse(toAddress, amount)); err != nil {
    fmt.Println(err)
}

Staking commands

Stake for 6 months

amount := big.NewInt(15000 * constants.Decimals)
if err := z.Send(z.Client.StakeApi.Stake(6*30*24*60*60, amount)); err != nil {
    fmt.Println(err)
}

Token commands

Issue token

totalSupply := big.NewInt(15000 * constants.Decimals)
maxSupply := big.NewInt(20000 * constants.Decimals)
if err := z.Send(z.Client.TokenApi.IssueToken("keyfile-sdk", "SDK test", "sdk-test.com", totalSupply, maxSupply, 8, true, true, true)); err != nil {
    fmt.Println(err)
}

Mint token

tokenZts, _ := types.ParseZTS("ZTS")
if err := z.Send(z.Client.TokenApi.Mint(tokenZts, amount, z.Address())); err != nil {
    fmt.Println(err)
}

Burn token

amount := big.NewInt(1250 * constants.Decimals)
tokenZts, _ := types.ParseZTS("ZTS")
if err := z.Send(z.Client.TokenApi.Burn(tokenZts, amount)); err != nil {
    fmt.Println(err)
}

Update token

tokenZts, _ := types.ParseZTS("ZTS")
if err := z.Send(z.Client.TokenApi.UpdateToken(tokenZts, z.Address(), false, false)); err != nil {
    fmt.Println(err)
}

Accelerator commands

Create project

amountZnn := big.NewInt(5000 * constants.Decimals)
amountQsr := big.NewInt(50000 * constants.Decimals)
if err := z.Send(z.Client.AcceleratorApi.CreateProject("sdk-project-test", "sdk test description", "github.com/sdk/test", amountZnn, amountQsr)); err != nil {
    fmt.Println(err)
}