Skip to content

创建免手续费交易

QM edited this page Dec 21, 2020 · 1 revision

1. 什么是免手续费交易

众所周知,所有的交易都是需要手续费的,这不仅是对矿工的奖励,也是防系统攻击的手段,必要的手续费可以防止大量无代价的交易占用系统流量,影响系统运行。

如果这个地址下只有token,但是缺乏必要的手续费,在很多区块链中式无法交易的,典型的区块链是ETH,其token的交易必然伴随token拥有者账户有一定的gas,在普通场景应用中,问题不大,但是如果放在交易所中,我们需要批量操作大量地址下的token时,不得不为他们批量充值手续费。Chain33 正式看到这样的弊端,创新的支持代扣手续费交易。

1.1 免收费的步骤:

1.1.1 构造一笔正常的普通交易:

构造交易 单笔 构造 BTY的token交易

curl --data '{
    "jsonrpc":"2.0",
    "id":1,
    "method":"Chain33.CreateRawTransaction",
    "params":[
        {
            "to":"135YwNqSgiEQxuwXfPbmRmH2Y53Edg8d48",
            "amount":10000000,
            "fee":100000,
            "note":"test transfer",
            "isToken":true,
            "tokenSymbol":"CCNY"
        }
    ]
}' -H "Content-Type:application/json" -X POST http://localhost:9801

返回结果:

      
{
    "id":1,
    "result":"0a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d063087ede7928f8ab9a50b3a2231333559774e715367694551787577586650626d526d483259353345646738643438",
    "error":null
}
1.1.2 构建一个代扣手续费的交易

这一步需要在1.1.1的基础上,把上述操作返回的结果作为参数,进行构建 需要说明的是,txHex可以作为任何chain33的构造交易参数,包括平行链的构造交易,

特别说明:这个步骤中指定的privkey必须要足够的手续费来支撑txHex的交易。如果我发连接的rpc节点中有私钥对应的地址,我们可以不用把privkey作为参数,直接用payAddr:具体代扣的地址 来替换

curl --data '{
    "jsonrpc":"2.0",
    "id":1,
    "method":"Chain33.CreateNoBalanceTransaction",
    "params":[
        {
            "txHex":"0a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d063087ede7928f8ab9a50b3a2231333559774e715367694551787577586650626d526d483259353345646738643438",
            "privkey":"xxxxxxx43a928e789f341087c65d779e1173ac81df8c9467097373a052b3xxxx",
            "expire":"120"
        }
    ]
}' -H "Content-Type:application/json" -X POST http://localhost:9801

或者

curl --data '{
    "jsonrpc":"2.0",
    "id":1,
    "method":"Chain33.CreateNoBalanceTransaction",
    "params":[
        {
            "txHex":"0a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d063087ede7928f8ab9a50b3a2231333559774e715367694551787577586650626d526d483259353345646738643438",
            "paryAddr":"14TaZDqdzzmt8FcLrSJuWQux62JXmJynR9",
            "expire":"120"
        }
    ]
}' -H "Content-Type:application/json" -X POST http://localhost:9801

返回结果

{
    "id":1,
    "result":"0a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402201f079cf50e2a000ffe29f459fb87a8d90cbd78737acc361b2dcc465c6a29c83d02201aaec2c324a8412488057e07db3b4ece7237fe9d6612f57b8700503c317f66e420c09a0c30e0aead86d7f5bae92b3a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024aa4030a81020a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402201f079cf50e2a000ffe29f459fb87a8d90cbd78737acc361b2dcc465c6a29c83d02201aaec2c324a8412488057e07db3b4ece7237fe9d6612f57b8700503c317f66e420c09a0c30e0aead86d7f5bae92b3a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024a2042ed2954e955e3514966436b49126d5ebfb8812dcaa3c8fe53d10d04f141dcb65220e231eef1bec5c27b039d280b9a1a51a2c90fbeef8cad57ca4d9da9824ab40fed0a9d010a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d4832593533456467386434383087ede7928f8ab9a50b3a2231333559774e715367694551787577586650626d526d48325935334564673864343840024a2042ed2954e955e3514966436b49126d5ebfb8812dcaa3c8fe53d10d04f141dcb65220e231eef1bec5c27b039d280b9a1a51a2c90fbeef8cad57ca4d9da9824ab40fed",
    "error":null
}

1.1.3 对1.1.2 交易进行签名
 curl --data '{
    "jsonrpc":"2.0",
    "id":1,
    "method":"Chain33.SignRawTx",
    "params":[
        {
            "txHex":"0a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402201f079cf50e2a000ffe29f459fb87a8d90cbd78737acc361b2dcc465c6a29c83d02201aaec2c324a8412488057e07db3b4ece7237fe9d6612f57b8700503c317f66e420c09a0c30e0aead86d7f5bae92b3a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024aa4030a81020a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402201f079cf50e2a000ffe29f459fb87a8d90cbd78737acc361b2dcc465c6a29c83d02201aaec2c324a8412488057e07db3b4ece7237fe9d6612f57b8700503c317f66e420c09a0c30e0aead86d7f5bae92b3a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024a2042ed2954e955e3514966436b49126d5ebfb8812dcaa3c8fe53d10d04f141dcb65220e231eef1bec5c27b039d280b9a1a51a2c90fbeef8cad57ca4d9da9824ab40fed0a9d010a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d4832593533456467386434383087ede7928f8ab9a50b3a2231333559774e715367694551787577586650626d526d48325935334564673864343840024a2042ed2954e955e3514966436b49126d5ebfb8812dcaa3c8fe53d10d04f141dcb65220e231eef1bec5c27b039d280b9a1a51a2c90fbeef8cad57ca4d9da9824ab40fed",
            "privkey":"xxxxxxf9b523f266a9545b69bd41c0251483a93e21e348e85118afe17a5e2",
            "index":2,
            "expire":"1h"
        }
    ]
}' -H "Content-Type:application/json" -X POST http://localhost:9801

注意:index:2 代表我们对组交易中的第二笔交易进行签名,为什么是2,而不是1,因为第一笔交易在CreateNoBalance中已经签过了,所以我们不需要在对其进行签名了

返回结果

      
{
    "id":1,
    "result":"0a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402206bc48c5393ed808e97672ad9ebe9c997b535f637e9d6e8627032646c390b4ab802201d6740c89fb6a3a738bfffa1a074af2dc7c49fc6afad0c9a7fecc08e72194aa920c09a0c30bc9c97bff09df684353a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024a93040a81020a046e6f6e6512126e6f2d6665652d7472616e73616374696f6e1a6d080112210312738105fdbc114077420431dd3f676abd0490e82c98ec52734a62547ddd7aa21a46304402206bc48c5393ed808e97672ad9ebe9c997b535f637e9d6e8627032646c390b4ab802201d6740c89fb6a3a738bfffa1a074af2dc7c49fc6afad0c9a7fecc08e72194aa920c09a0c30bc9c97bff09df684353a2231447a5464544c61354a50704c644e4e50325072563161364a4374554c413747735440024a20d3b037ad5a27ef7ccac478f0fa49887d9599e463faa43635ba459a1338f4cd3352201d20d01e278315075e496321826bbb8411a732457f298d57f8f0f4518b3a5b930a8c020a05746f6b656e12423804223e0a0443434e591080ade2041a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d4832593533456467386434381a6d0801122103bfb792eb89fe7103b599252896f6817657dcb50a37bd89865671bac8f42cae981a4630440220194ca30a8e4604b4fe7c7c20a0f5fdc6ee1643324e656186479134643789b943022042ba49564865d1a79a1846a69de0334b8b0e3e7156b8c2beff879d304b2c31fc30f6eba3cff2bbabeb3e3a2231333559774e715367694551787577586650626d526d48325935334564673864343840024a20d3b037ad5a27ef7ccac478f0fa49887d9599e463faa43635ba459a1338f4cd3352201d20d01e278315075e496321826bbb8411a732457f298d57f8f0f4518b3a5b93",
    "error":null
}

发送后 返回交易哈希

这个交易组会有两个哈希:

  1. 签名后发送返回的代扣手续费哈希 0xd3b037ad5a27ef7ccac478f0fa49887d9599e463faa43635ba459a1338f4cd33

  2. 第二笔真实的交易哈希 0x1d20d01e278315075e496321826bbb8411a732457f298d57f8f0f4518b3a5b93