Package contract simplifies working with smart contracts and FA tokens. It also allows you to batch several contract calls into the same transaction.
Deploy
Simply load a script from file or another way and deploy it as new a contract
import ("context""blockwatch.cc/tzgo/codec""blockwatch.cc/tzgo/rpc")// assuming you have an initialized RPC client and a loaded scriptcon := contract.NewEmptyContract(client).WithScript(script)// deploy in a single call (which will simulate, sign, broadcast and wait)rcpt, err := con.Deploy(ctx, nil)// then access the new addressaddr := con.Address()// and get deployment costcost := rcpt.TotalCost()
For situations where you also want to transfer tez or set an initial delegate with the origination, use con.DeployExt()
Call
Assembling calls for smart contracts is also easy. Use the micheline package to construct custom call parameters from
typeParametersstruct { Entrypoint string`json:"entrypoint"` Value Prim`json:"value"`}
then use these parameters to make a call like
// construct call argumentsargs := contract.NewTxArgs().WithParameters(micheline.Parameters{Entrypoint:"transfer", Value: prim})WithSource(sender). // optional sender (also added when signing)WithAmount(tezos.N(1_000_000)) // optional amount in tez to send with call// then execute the call with default optionsrcpt, err := con.Call(ctx, args, nil)
Its also possible to batch multiple TxArgs into a single transaction with con.CallMulti()
Tokens
If you work with token contracts that implement any of the TZIP standard FA1.2 or FA2 you can use the provided token wrappers to automatically generate call parameters. You may also add A simple FA2 transfer can be implemented as
import ("context""blockwatch.cc/tzgo/contract""blockwatch.cc/tzgo/rpc""blockwatch.cc/tzgo/signer""blockwatch.cc/tzgo/tezos")// create an RPC clientc, _ := rpc.NewClient("https://rpc.tzstats.com", nil)// use private key to signc.Signer = signer.NewFromKey(tezos.MustParsePrivateKey("SK..."))// constuct a new contractcon := contract.NewContract(tezos.MustParseAddress("KT1..."), c)// construct an FA2 token wrapper (use token id 0)fa2token := con.AsFA2(0)// construct simple transfer argumentsargs := fa2token.Transfer( tezos.MustParseAddress("tz..."), // from tezos.MustParseAddress("tz..."), // to tezos.NewZ(1_000_000), // amount)// execute the call (will complete reveal, simulate cost, add cost, sign, broadcast, wait)rcpt, _ := con.Call(ctx, args, nil)
In more complex scenarios you can add multiple transfers to the same FA2 transfer list
// append multiple calls (silly example, but you get the point)args := []contract.CallArguments{ fa2token.AddOperator(owner, operator), fa2token.Transfer(from, to, amount), fa2token.Transfer(from, to, amount), fa2token.RemoveOperator(owner, operator),}// execute all calls in a single batch transactionrcpt, _ := con.CallMulti(ctx, args, nil)