Giter Site home page Giter Site logo

zkbnb-go-sdk's Introduction

ZkBNB Go SDK

The ZkBNB Go SDK provides a thin wrapper around thin all the apis provided by ZkBNB, including a simple key manager for signing txs and sending signed txs to ZkBNB.

Usage

Importing

import (
    "github.com/bnb-chain/zkbnb-go-sdk" latest
)

Key manager

The key manager is a simple interface to help sign the txs with the provided private key.

type KeyManager interface {
    Sign(message []byte, hFunc hash.Hash) ([]byte, error)
    PubKey() signature.PublicKey
    PubKeyPoint() [2][]byte
}

Currently, we only support key manager generated from seed.

Examples:

keyManager, _ := NewSeedKeyManager("you private key seed")

ZkBNB Client

type ZkBNBClient interface {
    ZkBNBQuerier
    ZkBNBTxSender
}

The ZkBNB go sdk wraps the supported apis and also provides methods to sign txs with the key manager.

Init sdk

The ZkBNB client can be initiated by an API endpoint.

client := NewZkBNBClient("The ZkBNB endpoint")

Queries

You can perform the query methods directly:

// query the layer2 basic info

layer2Info, err := client.GetLayer2BasicInfo()
...

Send txs

To send txs, you need to init the key manager first and set the key manager to client.

client.SetSetKeyManager(keyManager)

Then you can sign txs and send with the sdk:

// prepare tx info
txInfo := &OfferTxInfo{
    Type:         int64(offerType),
    OfferId:      offerId,
    AccountIndex: accountIndex,
    NftIndex:     nftIndex,
    AssetId:      0, //payment asset id
    AssetAmount:  big.NewInt(10000),
    ListedAt:     listedAt,
    ExpiredAt:    expiredAt,
    TreasuryRate: 200,
    Sig:          nil,
}

txId, err := client.Offer(txInfo)

You can also sign the raw transaction by yourself and send with the SendTx api:

txInfo := &OfferTxInfo{
    Type:         int64(offerType),
    OfferId:      offerId,
    AccountIndex: accountIndex,
    NftIndex:     nftIndex,
    AssetId:      0, //payment asset id
    AssetAmount:  big.NewInt(10000),
    ListedAt:     listedAt,
    ExpiredAt:    expiredAt,
    TreasuryRate: 200,
    Sig:          nil,
}

txInfo, err := ConstructOfferTx(keyManager, txInfo)
if err != nil {
    return "", err
}

client.SendTx(TxTypeOffer, txInfo)

ZkBNB L1 Client

The ZkBNBL1Client is used to interact with ZkBNB proxy contract in l1.

Interface

type ZkBNBL1Client interface {
	// DepositBNB will deposit specific amount bnb to l2
	DepositBNB(accountName string, amount *big.Int) (common.Hash, error)

	// DepositBEP20 will deposit specific amount of bep20 token to l2
	DepositBEP20(token common.Address, accountName string, amount *big.Int) (common.Hash, error)

	// DepositNft will deposit specific nft to l2
	DepositNft(nftL1Address common.Address, accountName string, nftL1TokenId *big.Int) (common.Hash, error)

	// RegisterZNS will register account in l2
	RegisterZNS(name string, owner common.Address, pubKeyX [32]byte, pubKeyY [32]byte) (common.Hash, error)

	// CreatePair will create swap pair in l2
	CreatePair(tokenA common.Address, tokenB common.Address) (common.Hash, error)

	// RequestFullExit will request full exit from l2
	RequestFullExit(accountName string, asset common.Address) (common.Hash, error)

	// RequestFullExitNft will request full nft exit from l2
	RequestFullExitNft(accountName string, nftIndex uint32) (common.Hash, error)

	// UpdatePairRate will update pair info in l2
	UpdatePairRate(pairInfo abi.ZkBNBPairInfo) (common.Hash, error)
}

Init

client := NewZkBNBL1Client("l1 provider", "zkbnb proxy contract address")

Send tx

Before you send tx, you need to set a private key to sign the tx:

client.SetPrivateKey("private key")

Then you can send txs.

zkbnb-go-sdk's People

Contributors

forcodedancing avatar yutianwu avatar unclezoro avatar cosinlink avatar lightning-li avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.