(与web3的对比)
注: privateKey是钱包私钥, address是钱包地址
一,在前端项目中引入js-conflux-sdk
web3:
npm: npm install web3
js-conflux-sdk:
npm: npm install js-conflux-sdk
二,导入和初始化
web3:
ar Web3 = require('web3');
// "Web3.providers.givenProvider" will be set if in an Ethereum supported browser.
var web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');
// or
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
js-conflux-sdk:
const { Conflux } = require('js-conflux-sdk');
const cfx = new Conflux({url:'http://test.confluxrpc.org'});
三,调用客户端的RPC方法
web3:
// get balance
await web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
// get nonce
await web3.eth.getTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1");
// get block
await web3.eth.getBlock('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
// get transaction
await web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
// get tx receipt
await web3.eth.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
js-conflux-sdk:
// get balance
await conflux.cfx.getBalance('address');
// get nonce
await conflux.cfx.getNextNonce('address');
// get block
await conflux.cfx.getBlockByHash('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b')
// get transaction
await conflux.cfx.getTransactionByHash('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
// get tx receipt
await conflux.cfx.getTransactionReceipt('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
四,账户管理
web3:
// create
const account = web3.eth.accounts.create();
/*
{
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey:privateKey,
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
*/
// import private key
web3.eth.accounts.privateKeyToAccount(privateKey);
// signTransaction
const txInfo = {
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
};
account.signTransaction(txInfo)
web3.eth.accounts.signTransaction(txInfo, account.privateKey);
js-conflux-sdk:
// create 账户
// 通过私钥创建
const account = window.confluxJS.wallet.addPrivateKey(privateKey);
// 通过钱包创建
// const account = window.confluxJS.wallet.addRandom();
// 通过私钥账户创建
const { PrivateKeyAccount } = require('js-conflux-sdk');
// const randomSeed = "0xfffff"; // any random buffer
// const account = PrivateKeyAccount.random(randomSeed, 1);
// import private key
const account2 = conflux.wallet.addPrivateKey();
// signTransaction
const txInfo = {
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
};
五,发送交易
web3:
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// signTransaction
const txInfo = {
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
};
const tx = await account.signTransaction(txInfo);
const hash = await web3.eth.sendRawTransaction(tx.rawTransaction);
js-conflux-sdk:
const account = conflux.wallet.addPrivateKey(privateKey);
const hash = await conflux.cfx.sendTransaction({
from: address,
to: 'cfxtest:aaawgvnhveawgvnhveawgvnhveawgvnhvey1umfzwp',
value: '1000000000',
gas: 2000000
});
六,与合约交互
一,部署合约
web3:
const contract = new web3.eth.Contract(abi);
await contract.deploy({
data: bytecode,
arguments: [arg1, arg2]
});
js-conflux-sdk:
const contract = conflux.Contract({
abi,
bytecode,
});
const hash = await contract.constructor(arg1, arg2).sendTransaction({
from: account
});
// wait tx packed and executed
const receipt = await conflux.cfx.getTransactionReceipt(hash);
console.log('Contract address: ', receipt.contractCreated);
二,调用合约
web3:
const contract = new web3.eth.Contract(abi, address);
let result = await contract.methods.myMethod(123).call({
from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
});
js-conflux-sdk:
const contract = conflux.Contract({
abi,
contractaddress // 合约地址
});
const result = await contract.myMethod(123).call({
from: address,
});
// 读取合约
const nftInfo = await contract.myMethod(123)
// 写入合约
const result = await contract.myMethod(123)
.sendTransaction({
from: address,
})
.confirmed();
提示: 从 ConfluxPortal 迁移至 Fluent之后,一些方法有变化,可以查看《参考文档》第3篇
参考文档
js-conflux-sdk API文档
Web3.js 与 js-conflux-sdk的对比
从 ConfluxPortal 迁移至 Fluent
网友评论