Skip to content

Latest commit

 

History

History
192 lines (142 loc) · 19.9 KB

contract-interaction.md

File metadata and controls

192 lines (142 loc) · 19.9 KB

Contract interaction

Interact with contract is the main work of web3 development. java-conflux-sdk also support to interact with contracts in Conflux network. Including deploy contracts, query contract state through invoke contract methods, and update contract state through sending transaction to calling contract methods.

Deploy contract

To deploy a contract, we need to set it's bytecode and ABI encoded constructor parameters (if it has any) to a deployment transaction's data field. And the tx to field need to leave empty. Send the deployment transaction to blockchain network, after it was executed, we can get the new deployed contract address in transaction receipt.

The Account class has a method deploy which is used to deploy contract. It receives bytecode and constructor parameters as its parameters.

For example if we want to deploy a ERC20 token contract on Conflux network, the solidity code of which is:

// contracts/GLDToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract GLDToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("Gold", "GLD") {
        _mint(msg.sender, initialSupply);
    }
}

This contract use openzeppelin/contracts lib's standard ERC20 code to build a GLDToken.

After this code is compiled(with hardhat or truffle) we can get it's bytecode and ABI.

0x60806040523480156200001157600080fd5b50604051620018d1380380620018d183398181016040528101906200003791906200033a565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039080519060200190620000bb92919062000273565b508060049080519060200190620000d492919062000273565b505050620000e93382620000f060201b60201c565b506200052c565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141562000163576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016200015a906200039e565b60405180910390fd5b62000177600083836200026960201b60201c565b80600260008282546200018b9190620003ee565b92505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254620001e29190620003ee565b925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051620002499190620003c0565b60405180910390a362000265600083836200026e60201b60201c565b5050565b505050565b505050565b828054620002819062000455565b90600052602060002090601f016020900481019282620002a55760008555620002f1565b82601f10620002c057805160ff1916838001178555620002f1565b82800160010185558215620002f1579182015b82811115620002f0578251825591602001919060010190620002d3565b5b50905062000300919062000304565b5090565b5b808211156200031f57600081600090555060010162000305565b5090565b600081519050620003348162000512565b92915050565b6000602082840312156200034d57600080fd5b60006200035d8482850162000323565b91505092915050565b600062000375601f83620003dd565b91506200038282620004e9565b602082019050919050565b62000398816200044b565b82525050565b60006020820190508181036000830152620003b98162000366565b9050919050565b6000602082019050620003d760008301846200038d565b92915050565b600082825260208201905092915050565b6000620003fb826200044b565b915062000408836200044b565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111562000440576200043f6200048b565b5b828201905092915050565b6000819050919050565b600060028204905060018216806200046e57607f821691505b60208210811415620004855762000484620004ba565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f45524332303a206d696e7420746f20746865207a65726f206164647265737300600082015250565b6200051d816200044b565b81146200052957600080fd5b50565b611395806200053c6000396000f3fe608060405234801561001057600080fd5b50600436106100a95760003560e01c80633950935111610071578063395093511461016857806370a082311461019857806395d89b41146101c8578063a457c2d7146101e6578063a9059cbb14610216578063dd62ed3e14610246576100a9565b806306fdde03146100ae578063095ea7b3146100cc57806318160ddd146100fc57806323b872dd1461011a578063313ce5671461014a575b600080fd5b6100b6610276565b6040516100c39190610e35565b60405180910390f35b6100e660048036038101906100e19190610c83565b610308565b6040516100f39190610e1a565b60405180910390f35b610104610326565b6040516101119190610f37565b60405180910390f35b610134600480360381019061012f9190610c34565b610330565b6040516101419190610e1a565b60405180910390f35b610152610428565b60405161015f9190610f52565b60405180910390f35b610182600480360381019061017d9190610c83565b610431565b60405161018f9190610e1a565b60405180910390f35b6101b260048036038101906101ad9190610bcf565b6104dd565b6040516101bf9190610f37565b60405180910390f35b6101d0610525565b6040516101dd9190610e35565b60405180910390f35b61020060048036038101906101fb9190610c83565b6105b7565b60405161020d9190610e1a565b60405180910390f35b610230600480360381019061022b9190610c83565b6106a2565b60405161023d9190610e1a565b60405180910390f35b610260600480360381019061025b9190610bf8565b6106c0565b60405161026d9190610f37565b60405180910390f35b60606003805461028590611067565b80601f01602080910402602001604051908101604052809291908181526020018280546102b190611067565b80156102fe5780601f106102d3576101008083540402835291602001916102fe565b820191906000526020600020905b8154815290600101906020018083116102e157829003601f168201915b5050505050905090565b600061031c610315610747565b848461074f565b6001905092915050565b6000600254905090565b600061033d84848461091a565b6000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000610388610747565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905082811015610408576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103ff90610eb7565b60405180910390fd5b61041c85610414610747565b85840361074f565b60019150509392505050565b60006012905090565b60006104d361043e610747565b84846001600061044c610747565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020546104ce9190610f89565b61074f565b6001905092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60606004805461053490611067565b80601f016020809104026020016040519081016040528092919081815260200182805461056090611067565b80156105ad5780601f10610582576101008083540402835291602001916105ad565b820191906000526020600020905b81548152906001019060200180831161059057829003601f168201915b5050505050905090565b600080600160006105c6610747565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905082811015610683576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161067a90610f17565b60405180910390fd5b61069761068e610747565b8585840361074f565b600191505092915050565b60006106b66106af610747565b848461091a565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614156107bf576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107b690610ef7565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561082f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161082690610e77565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9258360405161090d9190610f37565b60405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16141561098a576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161098190610ed7565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156109fa576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109f190610e57565b60405180910390fd5b610a05838383610b9b565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610a8b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a8290610e97565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610b1e9190610f89565b925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610b829190610f37565b60405180910390a3610b95848484610ba0565b50505050565b505050565b505050565b600081359050610bb481611331565b92915050565b600081359050610bc981611348565b92915050565b600060208284031215610be157600080fd5b6000610bef84828501610ba5565b91505092915050565b60008060408385031215610c0b57600080fd5b6000610c1985828601610ba5565b9250506020610c2a85828601610ba5565b9150509250929050565b600080600060608486031215610c4957600080fd5b6000610c5786828701610ba5565b9350506020610c6886828701610ba5565b9250506040610c7986828701610bba565b9150509250925092565b60008060408385031215610c9657600080fd5b6000610ca485828601610ba5565b9250506020610cb585828601610bba565b9150509250929050565b610cc881610ff1565b82525050565b6000610cd982610f6d565b610ce38185610f78565b9350610cf3818560208601611034565b610cfc816110f7565b840191505092915050565b6000610d14602383610f78565b9150610d1f82611108565b604082019050919050565b6000610d37602283610f78565b9150610d4282611157565b604082019050919050565b6000610d5a602683610f78565b9150610d65826111a6565b604082019050919050565b6000610d7d602883610f78565b9150610d88826111f5565b604082019050919050565b6000610da0602583610f78565b9150610dab82611244565b604082019050919050565b6000610dc3602483610f78565b9150610dce82611293565b604082019050919050565b6000610de6602583610f78565b9150610df1826112e2565b604082019050919050565b610e058161101d565b82525050565b610e1481611027565b82525050565b6000602082019050610e2f6000830184610cbf565b92915050565b60006020820190508181036000830152610e4f8184610cce565b905092915050565b60006020820190508181036000830152610e7081610d07565b9050919050565b60006020820190508181036000830152610e9081610d2a565b9050919050565b60006020820190508181036000830152610eb081610d4d565b9050919050565b60006020820190508181036000830152610ed081610d70565b9050919050565b60006020820190508181036000830152610ef081610d93565b9050919050565b60006020820190508181036000830152610f1081610db6565b9050919050565b60006020820190508181036000830152610f3081610dd9565b9050919050565b6000602082019050610f4c6000830184610dfc565b92915050565b6000602082019050610f676000830184610e0b565b92915050565b600081519050919050565b600082825260208201905092915050565b6000610f948261101d565b9150610f9f8361101d565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610fd457610fd3611099565b5b828201905092915050565b6000610fea82610ffd565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600060ff82169050919050565b60005b83811015611052578082015181840152602081019050611037565b83811115611061576000848401525b50505050565b6000600282049050600182168061107f57607f821691505b60208210811415611093576110926110c8565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206160008201527f6c6c6f77616e6365000000000000000000000000000000000000000000000000602082015250565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b61133a81610fdf565b811461134557600080fd5b50565b6113518161101d565b811461135c57600080fd5b5056fea26469706673582212201364e66e1b28b158bf74a1636e1f833ac402ef2976a0b4e5ec11207f9748bce664736f6c63430008040033
[
  'function name() view returns (string)',
  'function symbol() view returns (string)',
  'function decimals() view returns (uint8)',
  'function totalSupply() view returns (uint256)',
  'function balanceOf(address) view returns (uint256)',
  'function transfer(address to, uint256 amount)',
  'function allowance(address owner, address spender) view returns (uint256)',
  'function approve(address spender, uint256 amount) returns (bool)',
  'function transferFrom(address sender, address recipient, uint256 amount) returns (bool)',
  'function increaseAllowance(address spender, uint256 amount) returns (bool)',
  'function decreaseAllowance(address spender, uint256 amount) returns (bool)',
  'event Transfer(address indexed from, address indexed to, uint256 amount)',
  'event Approval(address indexed owner, address indexed spender, uint256 amount)',
]

Then we can deploy this token contract:

import org.web3j.abi.datatypes.generated.Uint256;
import conflux.web3j.response.Receipt;
import conflux.web3j.types.Address;

String bytecode = "0x608060405234801562000011...";
BigInteger initialSupply = BigInteger.valueOf(new BigInteger("100000000000000000000")); // erc20 token default decimals is 18
// when passing constructor parameters we need wrap them with correspond ABI type, for example use Uint256 when ABI require uint256 
String hash = account.deploy(bytecode, new Uint256(initialSupply));
// After this transaction is packed and successfully executed we can get its receipt
// The execute process normally need 10 seconds
Optional<Receipt> receipt = cfx.getTransactionReceipt(hash).sendAndGet();
if (receipt.isPresent()) {
    Optional<Address> contractAddr = receipt.get().getContractCreated();
    if (contracAddr.isPresent()) {
        System.out.printf("Balance of %s is %d \n", contracAddr.get());
    }
}

Query contract state

When we got a contract's address, we can query it's state by calling its methods, for example query a ERC20 token's name, symbol, decimals, totalSupply.

import conflux.web3j.contract.ContractCall;

Address contractAddr = new Address("cfxtest:aame568esrpusxku1c449939ntrx2j0rxpmm5ge874");
// create contract call instance with cfx and contractAddress
ContractCall cc = new ContractCall(cfx, contractAddr);
// Use callAndGet method to call contract method and decode result
// For example we want query ERC20's totalSupply, we need specify use which ABI type to decode result, and the method to call
BigInteger totalSupply = cc.callAndGet(Uint256.class,"totalSupply");

Address user = new Address("cfxtest:aat9v7xdvszbj68x083kjjt52z32g94nwjaj7vdpxf");
BigInteger balance = cc.callAndGet(Uint256.class,"balanceOf", user.getABIAddress());

// Call contract's name method
String name = cc.callAndGet(Utf8String.class, "name");

Update contract state

If we want change contract's state, we need send transaction and set its data to contract's method selector and ABI encoded parameters. We can achieve this by use Account.call method.

For example, we want to transfer some ERC20 token to another address

Address contractAddr = new Address("cfxtest:aame568esrpusxku1c449939ntrx2j0rxpmm5ge874");
Address receiver = new Address("cfxtest:aat9v7xdvszbj68x083kjjt52z32g94nwjaj7vdpxf");
BigInteger amount = BigInteger.valueOf(100);
String hash = account.call(contratAddr, "transfer", receiver.getABIAddress(), new Uint256(amount));
// after this transaction is executed, the receiver's balance will increase 100 GLD

Standard Token support

The java-conflux-sdk provide builtin Class to help interact with standard ERC tokens includes:

  • ERC20
  • ERC721
  • ERC777
  • ERC1155

Developer can directly initiate these Class instance by specify address and account, then invoke their methods

import conflux.web3j.contract.ERC721;

ERC721 erc721 = new ERC721(cfx, new CfxAddress(nftAddress), account);
Address account = new Address("cfxtest:aat9v7xdvszbj68x083kjjt52z32g94nwjaj7vdpxf");
BigInteger count = erc721.balanceOf(account.getHexAddress());

Builtin Contract support

Conflux network have several builtin contracts, which play important role in the ecosystem:

  • AdminControl
  • SponsorWhitelistControl
  • Staking

The java-conflux-sdk also provide class to interact with them

import conflux.web3j.contract.internals.AdminControl;
// To query builtin contract
AdminControl ac = new AdminControl(cfx);
Address contractAdmim = ac.getAdmin(address contractAddr);

// To update
AdminControl ac = new AdminControl(account);
String hash = ac.setAdmin(contractAddr, newAdmin);

ABI encode & decode

To query or update contract's state, the main work is build the call transaction's data and decode result. Which is ABI encode and decode.

The ContractCall and Account.call method only support basic Solidity types:

  • Address
  • Bool
  • Utf8String
  • DynamicBytes
  • UintX, IntX
  • BytesX

If your contract's parameter or return value has complex types for example Tuple(Struct), DynamicArray You can manually use web3j's abi module encode or decode

ABI type wrap

To pass parameters when calling contract methods, we need to wrap java types with right web3j.abi.datatypes

For example:

Solidity type web3j.abi type Note
uint256(uint) Uint256
address Address
bool Bool
string Utf8String
bytes DynamicBytes
Fixed Array Array Fixed size array
Dynamic Array DynamicArray Dynamic size array

Chinese introduction about abi encode and decode

Event query & decode

TODO