将给定的二进制 Wasm 代码存储到链上,同时可以获取它的 codeHash
。只能通过已存储的代码实例化合约。
const { compactAddLength } = require('@chainx/util');
const code = compactAddLength(fs.readFileSync(path.resolve(__dirname, './erc20.wasm')));
const extrinsic = chainx.api.tx.xContracts.putCode(500000, code);
let codeHash;
ex.signAndSend(Test, (error, result) => {
console.log(error, result);
if (result && result.result === 'ExtrinsicSuccess') {
// 获取 codeHash
codeHash = result.events.find(e => e.method === 'CodeStored').event.data;
}
});
从 putCode
生成的' codehash '实例化一个新合约,同时也可以转移一些余额"。
// 合约 abi
const erc20 = require('./erc20');
// 解析 abi
const Abi = require('@chainx/api-contract');
const abi = new Abi(erc20);
// 5GE7vwvDmKCCPrVLc9XZJAiAspM9LhQWbQjPvZ3QxzBUbhT7
const extrinsic = chainx.api.tx.xContracts.instantiate(
1000,
500000,
'0x5e71dc66c1527bf4047942c5ada9c5c59941bff8eb8b2d1a6d849306bfd52e93',
abi.constructors[0](...), // 合约的构造函数
);
let contractAddress
ex.signAndSend(Test, (error, result) => {
if (result && result.result === 'ExtrinsicSuccess') {
// 获取 contractAddress
contractAddress = result.events.find(e => e.method === 'Instantiated').event.data[1];
}
});
调用合约,同时也可以传输一些余额。
const abi = new Abi(erc20);
const extrinsic = chainx.api.tx.xContracts.call(
'5GE7vwvDmKCCPrVLc9XZJAiAspM9LhQWbQjPvZ3QxzBUbhT7', // contract address
0, // value
500000, // gas
// 调用的函数
abi.messages.transfer('5FvHGYk44FHZXznrhoskVyr2zGPYn5CpUXphRKM8eGRJZMtX', 10)
);
extrinsic.signAndSend(Alice, (error, result) => {
console.log(error, result);
});
转化成 XRC 资产
const extrinsic = chainx.api.tx.xContracts.convertToXrc20(
'BTC', // token
1000, // value
500000 // gas
);
extrinsic.signAndSend(Alice, (error, result) => {
console.log(error, result);
});
将 xrc 资产转为余额
chainx.chain.convertToAsset('BTC', 100000, 0, 500000).then(extrinsic => {
extrinsic.signAndSend(Alice, (error, result) => {
console.log(error, result);
});
});
chainx.api.rpc.chainx.contractcall({ "dest": 5SS..., "gasLimit": 500000, "inputData": 0x..., "origin": 5HH... })
参见 https://github.com/chainx-org/ChainX/wiki/RPC#chainx_contractcall
参见 https://github.com/chainx-org/ChainX/wiki/RPC#chainx_contractxrctokeninfo
chainx.api.rpc.chainx.contractXRC20Call({ "token": "BTC", "selector": "BalanceOf", "inputData": 0x...})
https://github.com/chainx-org/ChainX/wiki/RPC#chainx_contractxrc20call