incognitoclient

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package go-incognito-sdk is a tutorial that integrate with Incognito Chain.

Notice that this doc is written in godoc itself as package documentation. The defined types are just for making the table of contents at the head of the page; they have no meanings as types.

If you have any suggestion or comment, please feel free to open an issue on this tutorial's GitHub page!

By Incognito.

Installation

To download the specific tagged release, run:

go get github.com/incognitochain/go-incognito-sdk@V0.0.1

It requires Go 1.13 or later due to usage of Go Modules.

Usage

Initialize object Blockchain and play rock

Example:

package main

import (
	"fmt"
	"github.com/incognitochain/go-incognito-sdk/incognitoclient"
	"github.com/incognitochain/go-incognito-sdk/incognitoclient/entity"
	"net/http"
)

func main() {
	client := &http.Client{}
	publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")

	blockInfo := NewBlockInfo(publicIncognito)
	wallet := NewWallet(publicIncognito, blockInfo)

	//create new a wallet
	paymentAddress, pubkey, readonlyKey, privateKey, validatorKey, shardId , _ := wallet.CreateWallet()

	fmt.Println("payment adresss", paymentAddress)
	fmt.Println("public key", pubkey)
	fmt.Println("readonly key", readonlyKey)
	fmt.Println("private key", privateKey)
	fmt.Println("validator key", validatorKey)
	fmt.Println("shard id", shardId)

	//send prv
	listPaymentAddresses := entity.WalletSend{
		Type: 0,
		PaymentAddresses: map[string]uint64{
			"12Rsf3wFnThr3T8dMafmaw4b3CzUatNao61dkj8KyoHfH5VWr4ravL32sunA2z9UhbNnyijzWFaVDvacJPSRFAq66HU7YBWjwfWR7Ff": 500000000000,
		},
	}

	tx, err := wallet.CreateAndSendConstantTransaction(
		"112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5",
		listPaymentAddresses,
	)

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Println(tx)
}

Index

Constants

View Source
const (
	ChargeFeeViaAutoCalculatePrvFee = -1
	ChargeFeeViaPrv                 = 0
	ChargeFeeViaToken               = 1
)
View Source
const (
	RoleNodeStatusNotStake  = -1
	RoleNodeStatusCandidate = 0
	RoleNodeStatusCommittee = 1
)

Status of node validator

View Source
const (
	MintCentralizedStatusNotFound = 0
	MintCentralizedStatusPending  = 1
	MintCentralizedStatusSuccess  = 2
	MintCentralizedStatusReject   = 3
)

Status of mint centralized token

View Source
const PRVToken = "0000000000000000000000000000000000000000000000000000000000000004"

Variables

View Source
var BurnCentralizedTokenMetadata = map[string]interface{}{
	"TokenID":     "ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854",
	"Privacy":     true,
	"TokenTxType": 1,
	"TokenName":   "ETH",
	"TokenSymbol": "pETH",
	"TokenAmount": uint64(1000000000),
	"TokenReceivers": map[string]uint64{
		"12RxahVABnAVCGP3LGwCn8jkQxgw7z1x14wztHzn455TTVpi1wBq9YGwkRMQg3J4e657AbAnCvYCJSdA9czBUNuCKwGSRQt55Xwz8WA": uint64(1000000000),
	},
	"TokenFee": uint64(0),
}

Metadata input for BurnCentralizedToken

View Source
var MintDecentralizedTokenMetadata = map[string]interface{}{
	"BlockHash":  "block hash of deposit coin",
	"IncTokenID": "ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854",
	"ProofStrs":  "proof of deposit coin",
	"TxIndex":    "tx index of of deposit coin",
}

Metadata input for MintDecentralizedToken

Functions

This section is empty.

Types

type BlockInfo

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

func NewBlockInfo

func NewBlockInfo(public *PublicIncognito) *BlockInfo

Creat new BlockInfo

Example:

client := &http.Client{}
publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")
blockInfo := BlockInfo(publicIncognito)

func (*BlockInfo) GetBeaconBestStateDetail

func (b *BlockInfo) GetBeaconBestStateDetail() (res *entity.BeaconBestStateResp, err error)

GetBeaconBestStateDetail return beacon stage detail current

func (*BlockInfo) GetBeaconHeight

func (b *BlockInfo) GetBeaconHeight() (int32, error)

GetBeaconHeight return beacon height current

func (*BlockInfo) GetBestBlockHeight

func (b *BlockInfo) GetBestBlockHeight(shardID int) (uint64, error)

GetBestBlockHeight return block height current of any shard id

func (*BlockInfo) GetBlockInfo

func (b *BlockInfo) GetBlockInfo(blockHeight int32, shardID int) (*entity.GetBlockInfo, error)

GetBlockInfo return info of that block

func (*BlockInfo) GetBurningAddress

func (b *BlockInfo) GetBurningAddress() (string, error)

GetBurningAddress return burn address of chain, burn address has burned token

func (*BlockInfo) GetChainInfo

func (b *BlockInfo) GetChainInfo() (*entity.GetBlockChainInfoResult, error)

GetChainInfo return info of Incognito Chain

type PDex

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

func NewPDex

func NewPDex(public *PublicIncognito, block *BlockInfo) *PDex

Creat new PDex

Example:

client := &http.Client{}
publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")
blockInfo := NewBlockInfo(publicIncognito)

wallet := NewWallet(publicIncognito, blockInfo)
pdex := NewPDex(publicIncognito, blockInfo)

func (*PDex) GetPDexState

func (b *PDex) GetPDexState(beaconHeight int32) (map[string]interface{}, error)

GetPDexState return all pair pdex by beacon height

func (*PDex) GetPDexTradeStatus

func (b *PDex) GetPDexTradeStatus(txId string) (constant.PDexTradeStatus, error)

GetPDexTradeStatus return status of trade tx

func (*PDex) TradePDex

func (b *PDex) TradePDex(privateKey string, buyTokenId string, tradingFee uint64, sellTokenId string, sellTokenAmount uint64, minimumAmount uint64, traderAddress string, networkFeeTokenID string, networkFee uint64) (string, error)

TradePDex will trade pair token, sell this token and buy that token

Input:

  • privateKey: private key of trader (string)
  • buyTokenId: token id of buy coin (string)
  • tradingFee: amount trading fee to pay for trade if have (uint64)
  • sellTokenId: token id of sell coin (string)
  • sellTokenAmount: amount to sell (uint64)
  • minimumAmount: minimum amount can receive (uint64)
  • traderAddress: address of trader (string)
  • networkFeeTokenID: amount network fee to pay for trade (string)
  • networkFee: amount of network fee (uint64)

Output:

  • result: tx hash (string)
  • err: err (error)

Example:

//trade 1 PRV -> pDai
tx, err := pdex.TradePDex(
	"112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5",
	"c7545459764224a000a9b323850648acf271186238210ce474b505cd17cc93a0",
	uint64(100),
	t.client.GetPRVToken(),
	uint64(1000000000),
	uint64(0),
	"12RwamF5njyL5cqpiMZ3SrqGHMqDaEDLyQexeaHYjYn2LDMzKZzgPZHnbQ75iLBKxm4md4kiyLxrPrFRNRNNktmAMjmfD4ktmcptgiX",
	t.client.GetPRVToken(),
	uint64(100))

type PublicIncognito

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

Public of incognito

func NewPublicIncognito

func NewPublicIncognito(c *http.Client, endpointUri string) *PublicIncognito

Creat new PublicIncognito

Input:

  • c: init http client module (*http.Client)
  • endpointUri: endpoint chain url (string)

Output:

  • result: return an object (*PublicIncognito)

Example:

client := &http.Client{}
publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")

func (*PublicIncognito) GetPRVToken

func (i *PublicIncognito) GetPRVToken() string

GetPRVToken return token id of PRV

type Stake

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

func NewStake

func NewStake(public *PublicIncognito) *Stake

Creat new Stake

Example:

client := &http.Client{}
publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")
stake := NewStake(publicIncognito)

func (*Stake) GetRewardAmount

func (b *Stake) GetRewardAmount(paymentAddress string) ([]entity.RewardItems, error)

GetRewardAmount return list amount reward each token

Input:

  • paymentAddress: payment address of staker (string)

Output:

  • result: List reward item ([]entity.RewardItems)
  • err: err

func (*Stake) GetStatusNodeValidator

func (b *Stake) GetStatusNodeValidator(validatorKey string) (float64, error)

GetStatusNodeValidator return status of node validator

Input:

  • validatorKey: validator key of node (string)

Output:

  • result: status of node (RoleNodeStatusNotStake | RoleNodeStatusCandidate | RoleNodeStatusCommittee) (float)
  • err: err

func (*Stake) GetTotalStaker

func (b *Stake) GetTotalStaker() (float64, error)

GetTotalStaker return total staker

Output:

  • result: Total staker (float)
  • err: err

func (*Stake) ListRewardAmounts

func (b *Stake) ListRewardAmounts() ([]entity.RewardAmount, error)

func (*Stake) ListUnstake

func (b *Stake) ListUnstake() ([]entity.Unstake, error)

ListUnstake return all node unstake

func (*Stake) Staking

func (b *Stake) Staking(receiveRewardAddress, privateKey, userPaymentAddress, userValidatorKey, burnTokenAddress string) (string, error)

Staking is action to stake a node validator

Input:

  • receiveRewardAddress: payment address of staker, address receive reward (string)
  • privateKey: private key of staker (string)
  • userPaymentAddress: payment address of node (string)
  • userValidatorKey: validator key of node (string)
  • burnTokenAddress: burn address (string)

Output:

  • result: tx hash (string)
  • err: err (error)

func (*Stake) Unstaking

func (b *Stake) Unstaking(privateKey, userPaymentAddress, userValidatorKey, burnTokenAddress string) (string, error)

Unstaking is action to unstake node validator

Input:

  • privateKey: private key of staker (string)
  • userPaymentAddress: payment address of node (string)
  • userValidatorKey: validator key of node (string)
  • burnTokenAddress: burn address (string)

Output:

  • result: tx hash (string)
  • err: err (error)

func (*Stake) WithDrawReward

func (b *Stake) WithDrawReward(privateKey, paymentAddress, tokenId string) (string, error)

WithDrawReward is action to withdraw all reward earn of node validator

Input:

  • privateKey: private key of staker (string)
  • paymentAddress: payment address of staker (string)
  • tokenId: token id (string)

Output:

  • result: tx hash (string)
  • err: err (error)

type Wallet

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

func NewWallet

func NewWallet(public *PublicIncognito, block *BlockInfo) *Wallet

Creat new Wallet

Example:

client := &http.Client{}
publicIncognito := NewPublicIncognito(client, "https://testnet.incognito.org/fullnode")
block = NewBlockInfo(publicIncognito)
wallet := NewWallet(publicIncognito, block)

func (*Wallet) BurnCentralizedToken

func (b *Wallet) BurnCentralizedToken(privateKey string, autoChargePRVFee int, metadata map[string]interface{}) (string, error)

BurnCentralizedToken is action to burn token

Input:

  • privateKey: private key hold token is burned (string)
  • autoChargePRVFee: method to calculate fee (ChargeFeeViaAutoCalculatePrvFee | ChargeFeeViaPrv | ChargeFeeViaToken) (int)
  • metadata: info to burn (BurnCentralizedTokenMetadata) (map[string]interface{}) TokenFee: it is must more than 0 when charge fee via token else equal 0 with charge fee via Prv

Output:

  • result: tx hash (string)
  • error: error (error)

Example:

masterPrivKey := "112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5"
autoChargePRVFee := -1
burnAddress, _ := block.GetBurningAddress()

txID, err := wallet.BurnCentralizedToken(
	masterPrivKey,
	autoChargePRVFee,
	map[string]interface{}{
		"TokenID":     "4584d5e9b2fc0337dfb17f4b5bb025e5b82c38cfa4f54e8a3d4fcdd03954ff82",
		"Privacy":     true,
		"TokenTxType": 1,
		"TokenName":   "BTC",
		"TokenSymbol": "pBTC,
		"TokenAmount": 1000000000,
		"TokenReceivers": map[string]uint64{
			burnAddress: 1000000000,
		},
		"TokenFee": 0,
})

func (*Wallet) BurnDecentralizedToken

func (b *Wallet) BurnDecentralizedToken(incPrivateKey string, amount *big.Int, receiverAddress string, tokenId string) (*entity.BurningForDepositToSCRes, error)

func (*Wallet) CreateWallet

func (b *Wallet) CreateWallet() (paymentAddress, pubkey, readonlyKey, privateKey, validatorKey string, shardId int, err error)

CreateWallet return all info of a wallet

Output:

  • paymentAddress: payment address (string)
  • pubkey: public key (string)
  • readonlyKey: readonly key (string)
  • privateKey: private key (string)
  • validatorKey: validator key (string)
  • shardId: shard id (int)
  • err: err (error)

func (*Wallet) CreateWalletByShardId

func (b *Wallet) CreateWalletByShardId(byShardId int) (paymentAddress, pubkey, readonlyKey, privateKey, validatorKey string, shardId int, err error)

CreateWallet return all info of a wallet

Input:

  • byShardId: shard id (int)

Output:

  • paymentAddress: payment address (string)
  • pubkey: public key (string)
  • readonlyKey: readonly key (string)
  • privateKey: private key (string)
  • validatorKey: validator key (string)
  • shardId: shard id (int)
  • err: err (error)

func (*Wallet) Defragmentation

func (b *Wallet) Defragmentation(privateKey string, maxValue int64, tokenId string) (string, error)

Defragmentation is action to merge utxo of wallet

Input:

  • privateKey: incognito private key (string)
  • maxValue: Max value (int64) maxValue: it is useful with Prv token
  • tokenId: token (string)

Output:

  • result: tx hash (string)
  • error: error (error)

Example:

//merge all utxo of Prv token
tx, err := wallet.Defragmentation("112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5", int64(500000*1e9), PRVToken)

//merge all utxo of token id
tx1, err1 := t.wallet.Defragmentation("112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5", 0, "ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854")

func (*Wallet) GenerateTokenID

func (b *Wallet) GenerateTokenID(symbol, pSymbol string) (string, error)

GenerateTokenID return new token id, input token info defined by yourself. Note you shouldn't generate new token which is exist token

Input:

  • symbol: symbol of new token (string)
  • pSymbol: pSymbol of new token (string)

Output:

  • result: new token id (string)
  • error: error (error)

Example:

tokenId, err := wallet.GenerateTokenID("ABC", "pABC")

func (*Wallet) GetBalance

func (b *Wallet) GetBalance(privateKey string, tokenId string) (uint64, error)

func (*Wallet) GetMintStatusCentralized

func (b *Wallet) GetMintStatusCentralized(txHash string) (int, error)

GetMintStatusCentralized return status tx of mint action

Input:

  • txHash: tx hash (string)

Output:

  • result: status tx (MintCentralizedStatusNotFound | MintCentralizedStatusPending | MintCentralizedStatusSuccess | MintCentralizedStatusReject) (int)
  • error: error (error)

func (*Wallet) GetPublicKeyFromPaymentAddress

func (b *Wallet) GetPublicKeyFromPaymentAddress(paymentAddress string) (string, error)

GetPublicKeyFromPaymentAddress return public key of payment address

Input:

  • paymentAddress: payment address (string)

Output:

  • result: public key (string)
  • error: error (error)

func (*Wallet) GetTransactionAmount

func (b *Wallet) GetTransactionAmount(txId string, walletAddress string, readOnlyKey string) (uint64, error)

func (*Wallet) GetTransactionByReceiversAddress

func (b *Wallet) GetTransactionByReceiversAddress(paymentAddress, readonlyKey string) (*entity.ReceivedTransactions, error)

GetTransactionByReceiversAddress return list transaction detail of payment address

Input:

  • paymentAddress: payment address (string)
  • readonlyKey: read only key (string)

Output:

  • result: transaction info (*entity.ReceivedTransactions)
  • error: error (error)

func (*Wallet) GetTransactionDetailByTxHash

func (b *Wallet) GetTransactionDetailByTxHash(txHash string) (*entity.TransactionDetail, error)

GetTransactionDetailByTxHash return info detail of a tx

Input:

  • txHash: transaction (string)

Output:

  • result: transaction detail (entity.TransactionDetail)
  • error: error (error)

func (*Wallet) GetUTXO

func (b *Wallet) GetUTXO(privateKey string, tokenId string) ([]*entity.Utxo, error)

GetUTXO return all unspent output coin except spending of wallet

Input:

  • privateKey: incognito private key (string)
  • tokenId: token (string)

Output:

  • result: list utxo ([]*entity.Utxo)
  • error: error (error)

func (*Wallet) ListPrivacyCustomToken

func (b *Wallet) ListPrivacyCustomToken() ([]entity.PCustomToken, error)

ListPrivacyCustomToken return all token info of chain as token id, logo, name, symbol

Output:

  • result: list reward item ([]entity.PCustomToken)
  • error: error (error)

func (*Wallet) MintCentralizedToken

func (b *Wallet) MintCentralizedToken(privateKey, receiveAddress string, depositedAmount *big.Int, tokenId string, tokenName string) (string, error)

func (*Wallet) MintDecentralizedToken

func (b *Wallet) MintDecentralizedToken(privateKey, burnerAddress string, metadata map[string]interface{}) (txHash string, res []byte, err error)

MintDecentralizedToken is action to mint decentralized token as ETH, Erc20. To done this action, first you must deposit coin to Ethereum chain after that you need get proof deposit which to mint token

Input:

Output:

  • txHash: tx hash (string)
  • res: response data ([]byte)
  • error: error (error)

Example:

privateKey := "112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5"
burningAddress, _ := block.GetBurningAddress()

txID, body, err := wallet.MintDecentralizedToken(
	privateKey,
	burningAddress,
	map[string]interface{}{
		"BlockHash":  "test",
		"IncTokenID": "ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854",
		"ProofStrs":  "test",
		"TxIndex":    "test",
	})

func (*Wallet) SendToken

func (b *Wallet) SendToken(privateKey string, receiverAddress string, tokenId string, amount uint64, fee uint64, feeTokenId string) (string, error)

SendToken is action to send token or prv to receiver address

Input:

  • privateKey: incognito private key (string)
  • receiverAddress: address of receiver (string)
  • tokenId: token (string)
  • amount: amount to send (uint64)
  • fee: amount fee (uint64)
  • feeTokenId: token id of fee (string)

if you send Prv: you ignore fee and feeTokenId.

if you send Token:

  • charge fee via Prv: fill prv token id and fee
  • charge fee via Token: fill token id and fee

Output:

  • result: tx hash (string)
  • error: error (error)

Example:

	//send prv with fee is prv
	tx, err := wallet.SendToken(
		"112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5",
		"12Rsf3wFnThr3T8dMafmaw4b3CzUatNao61dkj8KyoHfH5VWr4ravL32sunA2z9UhbNnyijzWFaVDvacJPSRFAq66HU7YBWjwfWR7Ff",
		PRVToken,
		500000000000,
		0,
		",
	)

	//send ptoken with fee is prv
    tx1, err1 := wallet.SendToken(
		"112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5",
		"12RqaTLErSnN88pGgXaKmw1PSQEaG86FA4uJsm32RZetAy7e5yEncqjTC6QJcMRjMfTSc48tcWRTyy8FoB9VkCHu56Vd9b86gd8Pq8k",
		"ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854",
		9800000,
		5,
		"0000000000000000000000000000000000000000000000000000000000000004")

	//send ptoken with fee is ptoken
	tx2, err2 := t.wallet.SendToken(
		"112t8s4Pdng512MhHmLVJNYqzoEJQ1TG4XZduvjfwYZFJhmuNtGPhUYRko4jSPFBFmeRg6bumKQuhAEMriQ72cpp5SKAkRuXfLCv5xeZx3f5",
		"12RqaTLErSnN88pGgXaKmw1PSQEaG86FA4uJsm32RZetAy7e5yEncqjTC6QJcMRjMfTSc48tcWRTyy8FoB9VkCHu56Vd9b86gd8Pq8k",
		"ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854",
		9800000,
		100,
		"ffd8d42dc40a8d166ea4848baf8b5f6e9fe0e9c30d60062eb7d44a8df9e00854")

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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