Interact with Contract
How to deploy a contract
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);How to get and update contract's state
How to play with InternalContract
How to get log
Get log through tranction receipt
Get log with cfx_getLogs method
cfx_getLogs methodSubscribe logs with websocket
How to decode log
MISC
BigNumber
MethodOverride
Last updated
Was this helpful?