Interact with Contract

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.json

const PRIVATE_KEY = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; // sender private key

async function main() {
  const conflux = new Conflux({ 
    url: 'https://test.confluxrpc.com',
    networkId: 1,
  });
  const account = conflux.wallet.addPrivateKey(PRIVATE_KEY);

  // 1. initialize a contract with abi and bytecode
  const contract = conflux.Contract({ abi, bytecode });

  // 2. specify constructor's parameter, if constructor need no parameter leave it empty
  const receipt = await contract.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.

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.

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:

  • address

  • data

  • topics

Get log with cfx_getLogs method

Also there is an RPC cfx_getLogs to get logs. An filter object is need to invoke this method.

How to build the filter topics

Subscribe logs with websocket

With websocket's advantage, logs can be subscribed:

How to decode log

With contract's abi, you can decode the event log data:

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.

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

Last updated

Was this helpful?