In Conflux world you may often need to interact with contracts, with JS SDK this can be done very easy.
How to deploy a contract
One contract must be created before interacting with it. To create a contract you can develop it with solidity. Then compile it with solidity compiler or cfxtruffle, you will get bytecode and abi. With bytecode, abi you can deploy it by send a transaction.
const { Conflux } =require('js-conflux-sdk');const { abi,bytecode } =MINI_ERC20; // see https://github.com/Conflux-Chain/js-conflux-sdk/tree/master/example/contract/miniERC20.jsonconstPRIVATE_KEY='0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; // sender private keyasyncfunctionmain() {constconflux=newConflux({ url:'https://test.confluxrpc.com', networkId:1, });constaccount=conflux.wallet.addPrivateKey(PRIVATE_KEY);// 1. initialize a contract with abi and bytecodeconstcontract=conflux.Contract({ abi, bytecode });// 2. specify constructor's parameter, if constructor need no parameter leave it emptyconstreceipt=awaitcontract.constructor('MiniERC20',18,'MC',10000)// 3. send transaction to deploy the contract, you can specify any transaction parameter here .sendTransaction({ from: account }) .executed();console.log(receipt);// 4. If your transaction executed successfully then you have deploy a new contract// 5. The receipt.contractCreated is the address of the new deployed contract/* { "index": 0, "epochNumber": 318456, "outcomeStatus": 0, "gasUsed": 1054531n, "gasFee": 1054531000000000n, "blockHash": "0x4a8b07e2694e358af075f7a9e96e78842b77ac2d511e2ab33f6acfff34a5846c", "contractCreated": "CFXTEST:TYPE.CONTRACT:ACFK2K2SDMP6A1FKB52TAAENV7WEKX24W6KKF7RF0E", "from": "cfxtest:aar7x4r8mkrnw39ggs8rz40j1znwh5mrrpufpr2u76", "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"stateRoot": "0x0940d4870e25bae1e7a5e5d7c19411b41922c025aa3de61aea2be17759673b1a", "to": null, "transactionHash": "0x6f55e67b486b5ef0c658c6d50cb5b89a2a2ddfecc1a1f2e414bbbefe36ef8dd5" } */// created contract address: "CFXTEST:TYPE.CONTRACT:ACFK2K2SDMP6A1FKB52TAAENV7WEKX24W6KKF7RF0E"}main().catch(console.log);
Check the transaction you will find the tx data is the contract bytecode and constructor's encoded signature.
How to get and update contract's state
After you got the contract address, you can interact with it. The Conflux network makes a distinction between writing data to the network and reading data from it, and this distinction plays a significant part in how you write your application, and this behavior is very like Ethereum network. In general, writing data is called a transaction whereas reading data is called a call. Transactions and calls are treated very differently, and have the following characteristics.
const { Conflux } =require('js-conflux-sdk');const { abi } =MINI_ERC20;asyncfunctionmain() {constconflux=newConflux({ url:'https://test.confluxrpc.com', networkId:1, });constaccount=conflux.wallet.addPrivateKey(PRIVATE_KEY);// 1. initialize contract with abi and addressconstcontract=conflux.Contract({ abi, address:'cfxtest:acfk2k2sdmp6a1fkb52taaenv7wekx24w6kkf7rf0e' });// 2. call method to get contract stateconstname=awaitcontract.name(); console.log(name); // MiniERC20// 3. user can set options by `contract.name().call({ from: account, ... })`// 4. call method with argumentsconstbalance=awaitcontract.balanceOf(account.address); console.log(balance); // 10000n// 5. change contract state by send a transactionconsttransactionHash=awaitcontract.transfer(ADDRESS,10).sendTransaction({ from: account }); console.log(transactionHash); // 0xb31eb095b62bed1ef6fee6b7b4ee43d4127e4b42411e95f761b1fdab89780f1a// 6. estimate contract method gas and storageconstestimated=awaitcontract.transfer(ADDRESS,10).estimateGasAndCollateral({from: account}, epochNumber);// 7. get contract method dataconsttransferMethodData=contract.transfer(ADDRESS,10).data;}main();
How to play with InternalContract
Conflux network has provide Internal Contracts AdminControl, SponsorWhitelistControl, Staking, these internal contract are very helpful to contract developer, for detail documentation check official doc. This SDK have fully support for Internal Contract, you can use them like this.
const { Conflux } =require('js-conflux-sdk');asyncfunctionmain() {constconflux=newConflux({ url:'https://test.confluxrpc.com', networkId:1, });// 1. get internal contract through InternalContract method and pass the internal contract nameconstsponsor=conflux.InternalContract('SponsorWhitelistControl');constgasSponsor=awaitsponsor.getSponsorForGas('cfxtest:acg6rb7s9h1be63zjrhbyc5mc4w3jhk5p6eempe9hk');}main();
Available internal contracts:
AdminControl
SponsorWhitelistControl
Staking
PoSRegister
CrossSpaceCall
ParamsControl
How to get log
Get log through tranction receipt
If an transaction emit some logs, you can find them in transaction receipt's logs field. Which is an log array, each log will have three fields:
// initialize a contract instance with abi and addresslet fc =conflux.Contract({ abi:CRC20_ABI,});// This example will use ERC20's "Transfer" event as example// Get event signatureconsole.log('Event signature: ',fc.Transfer.signature);// Get event topics by invoke encodeTopics method with parameters as arrayconsole.log(fc.Transfer.encodeTopics([account.address, targetAddress,100]));// Get an event's log filter, which can be used as console.log(fc.Transfer(account.address, targetAddress,100));// Get "Transfer" logslet logs =awaitfc.Transfer(account.address, targetAddress,100).getLogs();// Subscribe to "Transfer" logslet sub =awaitfc.Transfer(account.address, targetAddress,100).subscribeLogs();sub.on('data',console.log);
Subscribe logs with websocket
With websocket's advantage, logs can be subscribed:
let logFilter = { address: token_address,// other filter options};let subers =cfx.subscribeLogs(logs);subers.on("data",console.log);
How to decode log
With contract's abi, you can decode the event log data:
constabi= [// your contract ABI];let contract =cfx.Contract({abi});let decoded =contract.abi.decodeLog(log);console.log(decoded);
MISC
BigNumber
Note: when interacting with contract and if your parameter is bigger than Number.MAX_SAFE_INTEGER, you should use string representation of the number or BigInt.
// use stringawaitcontract.deposit('90071992547409910').sendTransaction({from:'cfxtest:aar7x4r8mkrnw39ggs8rz40j1znwh5mrrpufpr2u76'});// or use hex stringawaitcontract.deposit('0x13ffffffffffff0').sendTransaction({from:'cfxtest:aar7x4r8mkrnw39ggs8rz40j1znwh5mrrpufpr2u76'});// not use number// await contract.deposit(Number.MAX_SAFE_INTEGER * 10);
MethodOverride
If there are several methods that have same name in one contract. In most situation SDK can choose the right method through arguments. But sometimes you will encounter with error Error: can not match override "xxxx" with args for example Error: can not match override "placeBid(uint256,address)|placeBid(uint256)" with args, this is because SDK can not determine invoke which method through args.
For this situation user can invoke method through whole method signature