Bigzhu's den

CFUN 相关

bigzhu

CFun是一款搭建在Qtum量子链上的DAPP,旨在通过CFun平台汇集来自全球范围内的中小微创作者。通过自组织的协同创作,并依靠区块链记录确权及整个过程来计算用户创作贡献,基于专利算法预测IP市场价值并匹配需求方,最后结合智能合约完成交易,希望能够最大程度的实现社交化的内容创作及个人数字资产的价值传递。

Qtum

浏览器钱包: https://explorer.qtum.org/address/QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR

没有什么资料, 相关逻辑只能看这个 php 代码: https://github.com/qtumproject/qrc20-wrapper/blob/master/Qrc20.php

git clone https://github.com/qtumproject/qrc20-wrapper

开始研究吧

转账

先要验证地址类型

$result=json_decode(trim(sendCmd('sendtocontract '.TOKEN_CONTRACT_ADDRESS.' '.'a9059cbb'.to32bytesArg(addressToHash160($userAddress)).to32bytesArg(addDecimals($amount)).' 0 '.DEFAULT_GAS_LIMIT.' '.DEFAULT_GAS_PRICE.' '.MAIN_QRC_ADDRESS)),true)["txid"];

就一行 php 代码.

拆分成这个样子

    json_decode(
        trim(
            sendCmd(
                'sendtocontract '.
                TOKEN_CONTRACT_ADDRESS.
                ' '.
                'a9059cbb'.
                to32bytesArg(addressToHash160($userAddress)).
                to32bytesArg(addDecimals($amount)).
                ' 0 '.
                DEFAULT_GAS_LIMIT.
                ' '.
                DEFAULT_GAS_PRICE.
                ' '.
                MAIN_QRC_ADDRESS
            )
        )
        ,true)
        ["txid"];

主要是 sendCmd 里面:

sendtocontract 命令

TOKEN_CONTRACT_ADDRESS 是合约地址

拼接上 a9059cbb 不知道这个是个什么鬼 函数地址, 用来定位合约函数

to32bytesArg是一个函数

function to32bytesArg($arg){
    return str_pad($arg, 64, "0", STR_PAD_LEFT);
}

addressToHash160 取 16 进制地址

str_pad 把地址左侧填充0直到64位

addDecimals

function addDecimals($amount){
    $decimalPos=getNumberOfDecimals($amount);
    $amount= gmp_init(str_replace(".","",$amount));
    return gmp_strval(gmp_mul($amount,gmp_pow(10,(TOKEN_DECIMALS-$decimalPos))),16);
}

把金额转为16进制后同样填充到64位

中间 0 这个参数干什么用不清楚

DEFAULT_GAS_LIMIT 250000

DEFAULT_GAS_PRICE 0.00000040

MAIN_QRC_ADDRESS 主地址? qdyNS7WwaNQELNRKZFoUVkNRttyM61u356 应该是转出地址

rpc 问题

同样的串, 在 cli 运行没问题, rpc 就报 500

panchao@W166:~/bin$ ./qtum-cli sendtocontract 8b9500e2b789e002c1d0e744bd0ac7aa60dbffcc a9059cbb00000000000000000000000039b7bca4df628b0730ce9a5d82a008623f2bb47500000000000000000000000000000000000000000000000000000000000939f4 0 250000 0.0000004 QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR
{
  "txid": "b7eeee51f98242b9a7eeb01e03f959b31e9a9ef0a9323eec2c63dc09eeb9775c",
  "sender": "QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR",
  "hash160": "c47a355ac537f2476dd60e448d223af818f3ee49"
}

被 golang 的 json 转换坑死了

INFO[0001] {"method":"sendtocontract","params":["8b9500e2b789e002c1d0e744bd0ac7aa60dbffcc","a9059cbb00000000000000000000000007124cd6e3dd0f5137936f2a2a0e0a56934e71b000000000000000000000000000000000000000000000000000000000000939f4",0,250000,4e-7,"QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR"],"id":1519957337464250000,"jsonrpc":"1.0"}

0.0000004 被转成了 4e-7, 简直坑

https://github.com/golang/go/issues/14135

定义类型的时候, 用 json.Number 类型来解决

精度

var dTokenDecimal = decimal.NewFromFloat(1000000000)

cfun 竟然是10位

交易确认

根据 tax_id : d740b9a2a356379be2b16bce92e32c4271dfab3f30e10c1b2772253d11fddf9e

查到的结果是:

qtum-cli gettransaction d740b9a2a356379be2b16bce92e32c4271dfab3f30e10c1b2772253d11fddf9e                                                                   [8/85]
{
  "amount": 0.00000000,
  "fee": -0.10125113,
  "confirmations": 106,
  "blockhash": "f820d0f944261be4907bc28d9a2f93e82df9553d0bddf0a607998c91080d2e3f",
  "blockindex": 37,
  "blocktime": 1519958928,
  "txid": "d740b9a2a356379be2b16bce92e32c4271dfab3f30e10c1b2772253d11fddf9e",
  "walletconflicts": [
  ],
  "time": 1519958601,
  "timereceived": 1519958601,
  "bip125-replaceable": "no",
  "details": [
    {
      "account": "",
      "category": "send",
      "amount": 0.00000000,
      "vout": 0,
      "fee": -0.10125113,
      "abandoned": false
    },
    {
      "account": "",
      "address": "QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR",
      "category": "send",
      "amount": -0.59624640,
      "label": "xstar",
      "vout": 1,
      "fee": -0.10125113,
      "abandoned": false
    },
    {
      "account": "xstar",
      "address": "QeWrx61qxU1AttPivfSDqCPnMrsWhXBpSR",
      "category": "receive",
      "amount": 0.59624640,
      "label": "xstar",
      "vout": 1
    }
  ],
    "hex": "0200000001fcff69d3e914abd84be9bbfe3f2726907e3c2a25df3a44d3c4df374e736b9e73000000006b483045022100c56b57343bd995440796e6f0c028e0b8825d8860e718220208aa6a7163957b9d02202e97311
c09b0a6df286e3c1fc5e794796667011f239157266f48897f8a9b2a03012102bac483e9d322fe582e5375724975a836ffb92604ea296628ec1db72a06ce5a55feffffff0200000000000000006301040390d003012844a9059cbb
000000000000000000000000670126645b19ea748aa2b9ace7ecfdc46800f6a800000000000000000000000000000000000000000000000000000000000939f4148b9500e2b789e002c1d0e744bd0ac7aa60dbffccc2c0cc8d030
00000001976a914c47a355ac537f2476dd60e448d223af818f3ee4988ac01ac0100"
}

余额问题

qtum 发送合约导致同个钱包帐户余额差异的问题

bigzhu
Everything is learnable