四Conflux-智能合约代付功能

68次阅读

共计 1580 个字符,预计需要花费 4 分钟才能阅读完成。

Conflux 实现了一种资助机制以补贴用户应用智能合约。因而,一个账户余额为 0 的新用户,只有执行资助(通常由 Dapps 的运营商资助),就可能间接调用智能合约。通过引入内置的 SponsorControl 合约已记录对智能合约的资助信息。

一、代付性能

在书写代付合约时只有引入内置合约中的代付合约就能实现代付性能上面是代付合约地址

https://github.com/Conflux-Ch…

内建的合约地址为 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();

正文完
 0