✏️
go-conflux-sdk
  • README
  • Quickstart
  • Getting Started
    • Run a Node locally
    • Interacting with a Node
    • Deploy and Interact with Smart Contracts
    • Subscription
  • Smart Contracts
    • Smart Contract Overview
    • Getting Started with Solidity
    • Compiling Solidity source code
    • Interacting with Smart Contracts
    • Application Binary Interface
    • Contracts Supported
  • Transactions
    • Conflux Transactions
    • Obtaining CFX
    • Gas & Storage Collateral
    • Account Manager
    • Transfer CFX
    • Transaction Nonce
    • Batch Call snd Send Transactions
  • Other
    • Call/BatchCall RPC Hook
    • Conflux Addreess
  • Conflux-ABIGEN
  • ChangeLog
  • References
Powered by GitBook
On this page
  • Working with smart contracts and conflux-abigen
  • Install
  • Usage
  • Smart contract examples

Was this helpful?

  1. Getting Started

Deploy and Interact with Smart Contracts

PreviousInteracting with a NodeNextSubscription

Last updated 3 years ago

Was this helpful?

Working with smart contracts and conflux-abigen

conflux-abigen can auto-generate smart contract binding to deploy and interact with smart contracts.

Install

$ go install github.com/Conflux-Chain/conflux-abigen/cmd/cfxabigen

Usage

To generate the contract binding, compile your smart contract:

$ solc <contract>.sol --bin --abi --optimize -o <output-dir>/

Then generate the binding code using the :

$ cfxabigen --abi /path/to/<smart-contract>.abi --bin /path/to/<smart-contract>.bin --pkg main  --out <smart-contract>.go

The generated code will be like:

// DeployMyERC20Token deploys a new Conflux contract, binding an instance of MyERC20Token to it.
func DeployMyERC20Token(auth *bind.TransactOpts, ...) (*types.UnsignedTransaction, *types.Hash, *MyERC20Token, error) {
        ...
}

// NewMyERC20Token creates a new instance of MyERC20Token, bound to a specific deployed contract.
func NewMyERC20Token(address types.Address, backend bind.ContractBackend) (*MyERC20Token, error) {
        ...
}


// Transfer is a paid mutator transaction binding the contract method 0xa9059cbb.
//
// Solidity: function transfer(address _to, uint256 _value) returns()
func (_MyERC20Token *MyERC20TokenTransactor) Transfer(opts *bind.TransactOpts, _to common.Address, _value *big.Int) (*types.UnsignedTransaction, *types.Hash, error) {
	return _MyERC20Token.contract.Transact(opts, "transfer", _to, _value)
}


// BalanceOf is a free data retrieval call binding the contract method 0x70a08231.
//
// Solidity: function balanceOf(address ) view returns(uint256)
func (_MyERC20Token *MyERC20TokenCaller) BalanceOf(opts *bind.CallOpts, arg0 common.Address) (*big.Int, error) {
        ...
}

Now you can create and deploy your smart contract use method Deploy<YourContractName>:

client, err := sdk.NewClient("https://test.confluxrpc.com", sdk.ClientOption{
	KeystorePath: "../keystore",
})
if err != nil {
	log.Fatal(err)
}       
err = client.AccountManager.UnlockDefault("hello")
if err != nil {
	log.Fatal(err)
}       
tx, hash, yourContract, err := DeployYourContract(<*bind.TransactOpts>, client,<PARAM1>,...,<PARAMN>)
if err != nil {
	panic(err)
}
// yourContract is deployed contract instance

Or use an existing contract:

yourContract, err := NewYourContract(<YourContractAddress>, client)

To transact with a smart contract:

unsignedTx, txHash, err := yourContract.someMethod(<*bind.TransactOpts>,<param1>,...)

To call a smart contract:

result,err := yourContract.someMethod(<*bind.CallOpts>, <param1>, ...);

Smart contract examples

For more information refer to

conflux-abigen applies a very simple smart contract exmaple at

conflux-abigen
conflux-abigen-example
conflux-abigen