使用智能合约实现自动分帐

自动分帐是不少平台都会用到的支付功能。不少互联网内容售卖平台都会跟内容提供者分帐。好比:Apple 的 App Store 跟 App 开发者三七分红。不少平台都使用了支付宝、微信支付做为支付手段,可是要同时实现给内容提供者分帐,倒是一件不太容易的事。使用 FIBOS 智能合约能够很容易实现这个需求。javascript

文中代码已在 GitHub 上开源。github.com/fengluo/fib…java

设计思路

在 FIBOS 转帐是经过 token 合约的extransfer方法来实现的。extransfer方法在执行的时候会给转帐方帐户和入帐方帐户发送通知。因此用户给平台方帐户转帐的时候,平台帐户就会收到通知。因此总体业务逻辑以下:git

quantity: 10 FO
        memo: 内容提供者帐户           quantity: 8 FO
用户帐户 -------------------> 平台帐户 ----------------> 内容提供者帐户
            extransfer      2/8 分红   extransfer
复制代码
  1. 用户给平台方帐户转帐,memo 中填写内容提供者的帐户名。
  2. 平台方的帐户合约监听 extransfer 方法的通知,而后作出分帐计算,给对应内容提供者的帐户转帐对应金额。

总体逻辑很简单,整个合约代码逻辑差很少用20行就能够写完。github

编写合约

FIBOS 的智能合约分为 ABI 文件和 JS 合约两部分。ABI 至关于合约接口,JS 合约则是功能实现。本案例目前没有接口设计需求,不过 ABI 文件仍是合约不可缺乏的部分。因此咱们简单建立一下就好。json

咱们先建立一个 contracts 文件夹,合约文件都会放在这里。而后在此文件夹下,建立 subaccount.abi 文件,内容为:bash

{
    "version": "eosio::abi/1.0"
}
复制代码

JS 合约部分也没有太复杂。在 contracts 文件夹下建立 subaccount.js 文件,代码为:微信

exports.on_extransfer = (from, to, quantity, memo) => {
    // 须要在开头作一些判断
    if (to === action.receiver && action.is_account(memo)) {
        const num = parseInt(quantity.quantity.split(' ')[0])
        // 假设咱们约定平台方跟内容提供者是2/8分红。
        const subnum = (num * 0.8).toFixed(4);
        trans.send_inline('eosio.token', 'extransfer', {
            from: to,
            to: memo,
            quantity: {
                quantity: `${subnum} ${quantity.quantity.split(' ')[1]}`,
                contract: quantity.contract
            },
            memo: 'sub account'
        },
        [
            {
                // 须要提供合约帐户的 active 权限
                actor: action.receiver,
                permission: 'active'
            }
        ]);
    }
}
复制代码

合约代码开头咱们须要作一些验证。ide

  1. 收款方的帐户为合约帐户,不然由于下面代码执行给内容提供者转帐时,由于转账方也是合约帐号会再次收到通知,形成无限递归,超出最大 send_inline 层数而报错。
  2. 咱们用 memo 参数来放内容提供者的帐户,因此咱们须要对此参数校验一下该帐户是否存在防止打错。

合约代码中咱们使用 send_inline 调用 eosio.token 合约来执行转账操做。转账操做须要对应帐户的 active 权限才能执行。为了解决权限滥用问题,FIBOS 定义了一个特殊权限 eosio.code。咱们须要在平台合约帐户中配置权限,在 active 权限下添加该合约帐户的 eosio.code 受权。具体的配置操做会在下面说明。函数

在 FIBOS TestNet 上注册帐号

为方便测试,咱们在测试网 testnet.fibos.fo 上注册三个帐户。测试

  • 用户帐号 helloworld11
  • 内容提供者帐号 helloworld22
  • 平台合约帐号 helloworld33

咱们须要记录这三个帐号的帐户名以及公私钥。以便下面的开发使用。建立一个统一的配置文件来记录这些数据:

const config = {
    // 平台合约帐户的客户端配置
    client: {
        chainId: '68cee14f598d88d340b50940b6ddfba28c444b46cd5f33201ace82c78896793a',
        httpEndpoint: 'http://testnet.fibos.fo',
        keyProvider: 'PRIVATE_KEY_OF_helloworld33'
    },
    // 用户帐户的客户端配置
    callClient:{
        chainId: '68cee14f598d88d340b50940b6ddfba28c444b46cd5f33201ace82c78896793a',
        httpEndpoint: 'http://testnet.fibos.fo',
        keyProvider: 'PRIVATE_KEY_OF_helloworld11'
    },
    // 平台合约帐户信息
    contractAccount: {
        name: 'helloworld33',
        publicKey: 'PUBLIC_KEY_OF_helloworld33',
        privateKey: 'PRIVATE_KEY_OF_helloworld33'
    },
    // 用户帐户信息
    account1: {
        name: 'helloworld11',
        publicKey: 'PUBLIC_KEY_OF_helloworld11',
        privateKey: 'PRIVATE_KEY_OF_helloworld11'
    },
    // 内容提供者帐户信息
    account2: {
        name: 'helloworld22',
        publicKey: 'PUBLIC_KEY_OF_helloworld22',
        privateKey: 'PRIVATE_KEY_OF_helloworld22'
    }
}

module.exports = config
复制代码

配置权限

在合约代码中,咱们调用了 trans.send_inline 函数调用合约 eosio.token 来实现转账操做,可是转账操做是须要帐户的 active 权限。因此咱们须要更新一下合约帐户的权限,须要添加调用者的 eosio.code 受权到它的 active 权限。这个调用者天然也是这个合约帐户。

const FIBOS = require('fibos.js');
const config = require('./config');

const fibosClient = FIBOS(config.client);

let ctx = fibosClient.contractSync('eosio');

var r = ctx.updateauthSync({
  account: config.contractAccount.name,
  permission: 'active',
  parent: 'owner',
  auth: {
    threshold: 1,
    keys: [{
      key: config.contractAccount.publicKey,
      weight: 1
    }],
    accounts: [{
        permission: {
            // 将调用者帐号的 eosio.code 受权添加到它的 active 权限下。
            actor: config.contractAccount.name,
            permission: 'eosio.code'
        },
        weight: 1
    }]
  }
},{
    authorization: `${config.contractAccount.name}@owner` //更改帐户权限须要使用 owner 权限
});
console.log(r);
复制代码

部署合约

const FIBOS = require('fibos.js');
const config = require('./config');

const fibosClient = FIBOS(config.client);
const fs = require('fs');

// setcode
const jsCode = fs.readTextFile(`${__dirname}/contracts/subaccount.js`);
fibosClient.setcodeSync(config.contractAccount.name, 0, 0, fibosClient.compileCode(jsCode));

// getcode
const code = fibosClient.getCodeSync(config.contractAccount.name, true);
console.log('code:', code);

// setabi
const abi = JSON.parse(fs.readTextFile(`${__dirname}/contracts/subaccount.abi`));
fibosClient.setabiSync(config.contractAccount.name, abi);
复制代码

转帐测试

咱们先来写一个脚本 account.js 来查看三个帐户的余额。

const FIBOS = require('fibos.js');
const config = require('./config');

const fibosClient = FIBOS(config.callClient);

const account1 = fibosClient.getTableRowsSync(true, 'eosio.token', config.account1.name, 'accounts');
console.log(config.account1.name);
console.log(account1);

const account2 = fibosClient.getTableRowsSync(true, 'eosio.token', config.account2.name, 'accounts');
console.log(config.account2.name);
console.log(account2);

const contractAccount = fibosClient.getTableRowsSync(true, 'eosio.token', config.contractAccount.name, 'accounts');
console.log(config.contractAccount.name);
console.log(contractAccount);
复制代码

执行 fibos account.js 来查看三个帐户信息。 目前咱们的帐户尚未 FO,因此大体状况是这样的:

  • 用户帐户:helloworld11 金额:0.0000 FO
  • 内容提供者帐户:helloworld22 金额:0.0000 FO
  • 平台合约帐户:helloworld33 金额:0.0000 FO

测试网会自动给每一个帐户发放10 EOS 的通证用以测试使用。帐户中还并无 FO 通证。因此咱们再来写一个兑换脚本,用1 EOS 换一点 FO 通证。

const FIBOS = require('fibos.js');
const config = require('./config');

const fibosClient = FIBOS(config.callClient);

let ctx = fibosClient.contractSync('eosio.token');

const r = ctx.exchangeSync(
    config.account1.name,
    '1.0000 EOS@eosio',
    '0.0000 FO@eosio',
    'exchange FO to EOS',
    {
        authorization: config.account1.name
    }
);
console.log(r)
复制代码

再次执行 fibos account.js 来查看帐户信息。目前咱们的帐户金额大体是这样的:

  • 用户帐户:helloworld11 金额:146.4245 FO
  • 内容提供者帐户:helloworld22 金额:0.0000 FO
  • 平台合约帐户:helloworld33 金额:0.0000 FO

下面写个脚本 transfer.js 来执行转账操做。

const FIBOS = require('fibos.js');
const config = require('./config');

const fibosClient = FIBOS(config.callClient);

let ctx = fibosClient.contractSync('eosio.token');

const r = ctx.extransferSync(
    config.account1.name, // 用户帐户
    config.contractAccount.name, // 平台合约帐户
    '10.0000 FO@eosio', // 转账金额
    config.account2.name, // 附言填写内容提供者的帐户名,平台合约会给它分帐
    {
        authorization: config.account1.name //提供用户帐户的受权
    }
)

console.log(r)
复制代码

咱们要从用户帐户 account1 给平台合约帐户 account3 转账 10 FO。memo 参数为要分红的内容提供者帐户 account2。根据合约中定的2/8分红,平台合约帐户 account3 将会分得2 FO,而内容提供者帐户 account2 将会得到8 FO。

使用命令 fibos transfer.js 执行该脚本完成转账操做。

下面咱们再来看一下目前三个帐户状况。执行命令 fibos account.js。三个帐户金额大体以下。

  • 用户帐户:helloworld11 金额:136.4245 FO
  • 内容提供者帐户:helloworld22 金额:8.0000 FO
  • 平台合约帐户:helloworld33 金额:2.0000 FO

结果显示,分帐帐户和平台合约帐户如预期那样得到8 FO2 FO

综上,咱们成功使用了智能合约实现了自动分帐。平台方还能够继续根据本身业务须要定制本身的合约。

文中的代码请参考:github.com/fengluo/fib…