Conflux实现了一种赞助机制以补贴用户使用智能合约。因此,一个账户余额为0的新用户,只要执行赞助(通常由Dapps的运营商赞助),就能够直接调用智能合约。 通过引入内置的 SponsorControl 合约已记录对智能合约的赞助信息。
一、代付功能
在书写代付合约时只要引入内置合约中的代付合约就能实现代付功能下面是代付合约地址
内建的合约地址为 0x8ad036480160591706c831f0da19d1a424e39469
pragma solidity ^0.5.0;
import "github.com/Conflux-Chain/conflux-rust/blob/master/internal_contract/contracts/SponsorWhitelistControl.sol";
contract CommissionPrivilegeTest {
mapping(uint => uint) public ss;
function add(address account) public payable {
SponsorWhitelistControl cpc = SponsorWhitelistControl(0x8ad036480160591706c831f0DA19D1a424e39469);
address[] memory a = new address[](1);
a[0] = account;
cpc.add_privilege(a);
}
function remove(address account) public payable {
SponsorWhitelistControl cpc = SponsorWhitelistControl(0x8ad036480160591706c831f0DA19D1a424e39469);
address[] memory a = new address[](1);
a[0] = account;
cpc.remove_privilege(a);
}
function foo() public payable {
}
}
复制代码
上面的合约需要添加白名单,然后白名单账户进行调用该合约中的foo将不需要支付,而是由赞助者支付,前提是赞助者需要进行赞助
二、赞助合约
赞助需要进行调用 sponsor_contract.set_sponsor_for_gas(contract_addr, your_upper_bound) 方法进行赞助,示例代码如下:
const PRIVATE_KEY = '0xxxxxxx';
const cfx = new Conflux({
url: 'http://testnet-jsonrpc.conflux-chain.org:12537',
defaultGasPrice: 100,
defaultGas: 1000000,
logger: console,
});
const account = cfx.Account(PRIVATE_KEY); // create account instance
const sponsor_contract_addr = '0x8ad036480160591706c831f0da19d1a424e39469';
const sponsor_contract = cfx.Contract({
abi: require('./contracts/sponsor.abi.json'),
address: sponsor_contract_addr,
});
sponsor_contract.set_sponsor_for_gas(contract_addr, your_upper_bound).sendTransaction({
from: account,
value: your_sponsor_value
}).confirmed();
作者:悠悠_15832013094
链接:https://juejin.im/post/5ef56446e51d453474165e22
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。