XDC Go SDK

XRC20 Token Integration

How to call functions in Go SDK?

CreateAccount() β†’ string Returns the Private Key.
CheckAddress(Private_key string) β†’ string Returns the Address User.
Name(Token_address string) β†’ string Returns the name of the token.
Symbol(Token_address string) β†’ string Returns the symbol of the token, usually a shorter version of the name.
Decimals(Token_address string) β†’ uint8 Returns the number of decimals used to get its user representation. For example, if decimals equal 2, a balance of 505 tokens should be displayed to a user as 5,05 (505 / 10 ** 2).
Tokens usually opt for a value of 18, imitating the relationship between Ether and Wei.
Total_supply(Token_address string) β†’ string Returns the number of tokens in existence.
BalanceOf(Token_address string ,Owner_address string) β†’ string Returns the number of tokens owned by the account.
Allowance(Token_address string , Owner_address string , Spender_address string) β†’ string Returns the remaining number of tokens that the spender will be allowed to spend on behalf of the owner through transferFrom. It is zero by default.
Approve_(Private_key string, Token_address string,sender_address string,Gas_Price string,Gas_Limit string,Amount string) -> string Pass Private key, Token address, Spender address, Gas Price, Gas Limit. It will return a transaction hash.
XDCTransfer(Private_key string, To_Address string, Value_ string,gasLimit uint64) -> string Moves XDC from the caller’s account to recipient. It will return a transaction hash.
TokenTransfer_(Private_key string, Token_address string, sender_address string,Gas_Price string, Gas_Limit string,Tokens_ string) -> string Moves tokens from the caller’s account to the recipient. It will return a transaction hash.
FromTransfer_(Private_key string, Token_address string,sender_address string,Gas_Price string,Gas_Limit string,Amount string) -> string Pass Private key, Token address, Spender address, Gas Price, Gas Limit and Amount. It will return a transaction hash.
IncreaseAllowance_(Private_key string, Token_address string,sender_address string,Gas_Price string,Gas_Limit string,Amount string) -> string Automatically increases the allowance granted to spender by the caller.
DecreaseAllowance_(Private_key string,Token_address string,sender_address string,Gas_Price string, Gas_Limit string, Amount string) -> string Automatically decreases the allowance granted to spender by the caller.

Example to call functions in Go SDK

Step 1: Clone git repo (go-xdc) in any folder and copy it.
Step 2: Go to that directory where your Golang saves. (go to your go folder).
Step 3: Find SRC in your go folder.
Step 4: Open SRC Folder and pasted your go-xdc.
Use like this in your main package.
1
package main
2
import(
3
"go-xdc/xdcsdk"
4
"fmt"
5
)
6
func main(){
7
Private_key := "8a32103448a851b2fed3e95d2b4fbaa5e564f3b8cc42eac0d3be15a96311f355"
8
Token_address := "0xba9d6a36fbc194f5d1aa48a2892ae4bdf6939cb9"
9
Owner_address := "0xd7813e7cfdf83d6fa3469d7411b52a50ed2b867f"
10
Spender_address := "0xedb472070566e072f3bdaa50cfa076e772135f33"
11
Gas_limit :="3000000"
12
Gas_Price:= "3500000"
13
var Gaslimit_In_Uint uint64
14
Gaslimit_In_Uint=3000000
15
​
16
// for account creation
17
CreateAccount1:=xdcsdk.CreateAccount()
18
fmt.Println("CreateAccount Private key :",CreateAccount1)
19
​
20
// for check address
21
CheckAddress1:=xdcsdk.CheckAddress(CreateAccount1)
22
fmt.Println("User Address :",CheckAddress1)
23
​
24
// for Name
25
Name1:=xdcsdk.Name(Token_address)
26
fmt.Println("name :",Name1)
27
​
28
// for Symbol
29
Symbol1:=xdcsdk.Symbol(Token_address)
30
fmt.Println("symbol: ",Symbol1)
31
​
32
// for decimal
33
Decimals1:=xdcsdk.Decimals(Token_address)
34
fmt.Println("Decimals: ",Decimals1)
35
​
36
// for totalsupply
37
Total_supply1:=xdcsdk.Total_supply(Token_address)
38
fmt.Println("Total_supply: ",Total_supply1)
39
​
40
// for Balanceoff
41
BalanceOf1:=xdcsdk.BalanceOf(Token_address,Owner_address)
42
fmt.Println("BalanceOf: ",BalanceOf1)
43
​
44
// for Allowance
45
Allowance1:=xdcsdk.Allowance(Token_address,Owner_address,Spender_address)
46
fmt.Println("Allowance: ",Allowance1)
47
​
48
// for Approve
49
Approve_1:=xdcsdk.Approve_(Private_key,Token_address,Spender_address,Gas_Price,Gas_limit,"3")
50
fmt.Println("Approve_: ",Approve_1)
51
​
52
// for XDC Transfer
53
XDCTransfer1:=xdcsdk.XDCTransfer(Private_key,Spender_address,"1",Gaslimit_In_Uint)
54
fmt.Println("XDCTransfer: ",XDCTransfer1)
55
​
56
// for Token Transfer
57
TokenTransfer_1:=xdcsdk.TokenTransfer_(Private_key,Token_address,Spender_address,Gas_Price,Gas_limit,"5")
58
fmt.Println("TokenTransfer_: ",TokenTransfer_1)
59
​
60
// for From Transfer
61
FromTransfer_1:=xdcsdk.FromTransfer_(Private_key,Token_address,Spender_address,Gas_Price,Gas_limit,"1")
62
fmt.Println("FromTransfer_: ",FromTransfer_1)
63
​
64
// for Increase Allowance
65
IncreaseAllowance_1:=xdcsdk.IncreaseAllowance_(Private_key,Token_address,Spender_address,Gas_Price,Gas_limit,"1")
66
fmt.Println("IncreaseAllowance_1: ",IncreaseAllowance_1)
67
​
68
// for Decrease Allowance
69
DecreaseAllowance_1:=xdcsdk.DecreaseAllowance_(Private_key,Token_address,Spender_address,Gas_Price,Gas_limit,"1")
70
fmt.Println("DecreaseAllowance_: ",DecreaseAllowance_1)
71
​
72
}
Copied!

XRC721 Token Integration

How to call functions in Go SDK?

OwnerOff_(tokenAddress string , TokenID string) β†’ common.Address Returns the owner's address.
IsApprovedForAll_(tokenAddress string ,owneraddress string, operatoraddress string) β†’ bool Returns the True or false.
Name_(tokenAddress string) β†’ string Returns the name of the token.
Symbol_(tokenAddress string) β†’ string Returns the symbol of the token, usually a shorter version of the name.
TokenURI_(tokenAddress string, TokenID string) β†’ string Return the Token URL.
TokenOfOwnerByIndex_(tokenAddress string, IndexNO string, OwnerAddress string) β†’ string Returns the symbol of the token, usually a shorter version of the name.
GetApproved_(tokenAddress string, TokenID string) β†’ common.Address Return True or False.
TotalSupply_(tokenAddress string) β†’ string Returns the number of tokens in existence.
BalanceOff_(tokenAddress string , Owneraddress string) β†’ string Returns the number of tokens owned by the account.
SupportInterface_(tokenAddress string, InterfaceID int) β†’ bool Return True or False.
Approve_(Private_key string , Token_address string,sender_address string,Gas_Price string,Gas_Limit string,TokenId string ) -> string Pass Private key, Token address, Sender address, Gas Price, Gas Limit and Token id. It will return a transaction hash.
SafeTransfer_(Private_key string,Token_address string ,sender_address string ,Gas_Price string ,Gas_Limit string , TokenId string) -> string Used for the safe transfer of token ownership.
TransferFrom_(Private_key string ,Token_address string ,sender_address string, Gas_Price string,Gas_Limit string ,TokenId string) -> string Used to transfer token ownership.
Setapprovalforall_(Private_key string ,Token_address string,sender_address string,Gas_Price string,Gas_Limit string,boolvalue string) -> string Pass Private key, Token address, Sender address, Gas Price, Gas Limit and Bool value. It will return a transaction hash.
NOTE - pass boolvalue in only 0 or 1.
0- False
1-True

Example to call functions in Go SDK

Step1: Clone git repo (XDC) in any folder and copy it.
Step2: Go to that directory where your Golang is saved (go to your Go folder).
Step3: Find SRC in your Go folder.
Step4: Open SRC Folder and paste your XDC.
Use it in your main package as:
1
package main
2
import (
3
"fmt"
4
"XDC/XRC721"
5
)
6
func main(){
7
tokenAddress := "0x8af9e0c04f1a14d4c2d13c577d81d86b3035783c"
8
Token_ID:= "21"
9
OwnerAddress:= "0x517b6Be05e5C50df6876906909c23ad130476Cc7"
10
OperatorAddress:= "0xd7813e7cfdf83d6fa3469d7411b52a50ed2b867f"
11
Indexno:= "0"
12
var Interface_id int
13
Interface_id=0x80ac58cd
14
15
// Owner OFF
16
​
17
OwnerOff__:=XRC721.OwnerOff_(tokenAddress,Token_ID)
18
fmt.Println("Owner OFF method : ",OwnerOff__)
19
​
20
//IsApprovedForAll
21
​
22
IsApprovedForAll__:=XRC721.IsApprovedForAll_(tokenAddress,OwnerAddress,OperatorAddress)
23
fmt.Println("Is approval for all :",IsApprovedForAll__)
24
​
25
// Name
26
​
27
Name__:=XRC721.Name_(tokenAddress)
28
fmt.Println("Name :",Name__)
29
​
30
//Symbol
31
​
32
Symbol__:=XRC721.Symbol_(tokenAddress)
33
fmt.Println("Symbol :", Symbol__)
34
​
35
//TokenURI
36
​
37
TokenURI__:=XRC721.TokenURI_(tokenAddress,Token_ID)
38
fmt.Println("Token URI :",TokenURI__)
39
​
40
//TokenOfOwnerByIndex
41
​
42
TokenOfOwnerByIndex__:=XRC721.TokenOfOwnerByIndex_(tokenAddress,Indexno,OwnerAddress)
43
fmt.Println("TokenOfOwnerByIndex :",TokenOfOwnerByIndex__)
44
​
45
//GetApproved
46
​
47
GetApproved__:=XRC721.GetApproved_(tokenAddress,Token_ID)
48
fmt.Println("GetApproved :",GetApproved__)
49
​
50
//TotalSupply
51
​
52
TotalSupply__:=XRC721.TotalSupply_(tokenAddress)
53
fmt.Println("TotalSupply :",TotalSupply__)
54
​
55
//BalanceOff
56
​
57
BalanceOff__:=XRC721.BalanceOff_(tokenAddress,OwnerAddress)
58
fmt.Println("BalanceOff :",BalanceOff__)
59
​
60
//SupportInterface
61
​
62
SupportInterface__:=XRC721.SupportInterface_(tokenAddress,Interface_id)
63
fmt.Println("SupportInterface_ :",SupportInterface__)
64
​
65
//---------------------------------------------------------------------------------
66
​
67
// Write Methods
68
​
69
Private_key:="07d2fb9a1f3a912000bbf9215ee0815a969b1d49e7e4c5ec94600ae2dfcfa4ce"
70
Spender_address_for_Write_op:="0xd7813e7cfdf83d6fa3469d7411b52a50ed2b867f"
71
Token_address_for_write_op:="0x301815025bd43513ec36b6c6159ebaa8dff5e36d"
72
Token_id_for_write_op:="21"
73
Gas_Limit:="3000000"
74
Gas_Price:="3500000"
75
Set_approval:="0"
76
​
77
// Approve
78
​
79
Approve__:=XRC721.Approve_(Private_key,Token_address_for_write_op,Spender_address_for_Write_op,Gas_Price,Gas_Limit,Token_id_for_write_op)
80
fmt.Println("Approve_ :",Approve__)
81
​
82
//SafeTransfer
83
​
84
SafeTransfer__:=XRC721.SafeTransfer_(Private_key,Token_address_for_write_op,Spender_address_for_Write_op,Gas_Price,Gas_Limit,Token_id_for_write_op)
85
fmt.Println("SafeTransfer_: ",SafeTransfer__)
86
​
87
//TransferFrom
88
​
89
TransferFrom__:=XRC721.TransferFrom_(Private_key,Token_address_for_write_op,Spender_address_for_Write_op,Gas_Price,Gas_Limit,Token_id_for_write_op)
90
fmt.Println("TransferFrom_ :",TransferFrom__)
91
​
92
//setapproovalforall
93
​
94
Setapprovalforall__:=XRC721.Setapprovalforall_(Private_key,Token_address_for_write_op,Spender_address_for_Write_op,Gas_Price,Gas_Limit,Set_approval)
95
fmt.Println("setappforall__",Setapprovalforall__)
96
​
97
}
Copied!