典型合约模板解析

如果合约contract有参数的话,在调用编译合约的API接口compile的时候需要加上相关参数进行实例化,编译后的返回结果中program字段才是需要部署的正常合约程序

单签验证合约

LockWithPublicKey源代码如下:

contract LockWithPublicKey(publicKey: PublicKey) locks locked {
  clause unlockWithSig(sig: Signature) {
    verify checkTxSig(publicKey, sig)
    unlock locked
  }
}
  • 合约编译之后的字节码为:ae7cac
  • 合约对应的指令码为:TXSIGHASH SWAP CHECKSIG

假如合约参数如下:

  • publicKey : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78

添加合约参数后的合约程序参考结果如下:20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0

对应的解锁合约的参数如下:

  • sig :
{
  "type": "raw_tx_signature",
  "raw_data": {
    "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
    "derivation_path": [
      "010100000000000000",
      "0100000000000000"
    ]
  }
}

其对应的解锁合约交易模板示例如下:

{
  "base_transaction": null,
  "actions": [
    {
      "output_id": "913d941faf5a19501ab4c06747fe1eb38c5ae76b74d0f5af524fc40ee6bf7116",
      "arguments": [
        {
          "type": "raw_tx_signature",
          "raw_data": {
            "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
            "derivation_path": [
              "010100000000000000",
              "0100000000000000"
            ]
          }
        }
      ],
      "type": "spend_account_unspent_output"
    },
    {
      "amount": 100000000,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 20000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "type": "spend_account"
    }
  ],
  "ttl": 0,
  "time_range": 1521625823
}

多签验证合约

LockWithMultiSig源代码如下:

contract LockWithMultiSig(publicKey1: PublicKey,
                        publicKey2: PublicKey,
                        publicKey3: PublicKey) locks value {
  clause spend(sig1: Signature, sig2: Signature) {
    verify checkTxMultiSig([publicKey1, publicKey2, publicKey3], [sig1, sig2])
    unlock value
  }
}
  • 合约编译之后的字节码为:537a547a526bae71557a536c7cad
  • 合约对应的指令码为:3 ROLL 4 ROLL 2 TOALTSTACK TXSIGHASH 2ROT 5 ROLL 3 FROMALTSTACK SWAP CHECKMULTISIG

假如合约参数如下:

  • publicKey1 : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78
  • publicKey2 : 1f51c25decab2168835f1292a5432707ed94b90be8f5ec0d62aca1c6daa1ec55
  • publicKey3 : e386c85178418fc72f8182111aa818ac736f3f7f1eee75ccdd7e5a057abe8fe0

添加合约参数后的合约程序参考结果如下:(注意添加合约参数的顺序与虚拟机入栈的顺序是相反的)20e386c85178418fc72f8182111aa818ac736f3f7f1eee75ccdd7e5a057abe8fe0201f51c25decab2168835f1292a5432707ed94b90be8f5ec0d62aca1c6daa1ec5520e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740e537a547a526bae71557a536c7cad00c0

对应的解锁合约的参数如下:(注意解锁合约参数的顺序,否则会执行VM失败)

  • sig1 :
{
  "type": "raw_tx_signature",
  "raw_data": {
    "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
    "derivation_path": [
      "010100000000000000",
      "0100000000000000"
    ]
  }
}
  • sig2 :
{
  "type": "raw_tx_signature",
  "raw_data": {
    "xpub": "e0f470a91bcd99497b4804d76264a293e257ca906938a43e34a6a0e619bbf5f1606a2866eca2ceec9f93348c577b052f30bb7ef41b4b09fff28eb1004ca1f8d5",
    "derivation_path": [
      "010200000000000000",
      "0100000000000000"
    ]
  }
}

其对应的解锁合约交易模板示例如下:

{
  "base_transaction": null,
  "actions": [
    {
      "output_id": "bf6a3999cfbd25cef24ff1b19c6f2e0988de2e8ef3d40ed7c15402f2a7327bfa",
      "arguments": [
        {
          "type": "raw_tx_signature",
          "raw_data": {
            "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
            "derivation_path": [
              "010100000000000000",
              "0100000000000000"
            ]
          }
        },
        {
          "type": "raw_tx_signature",
          "raw_data": {
            "xpub": "e0f470a91bcd99497b4804d76264a293e257ca906938a43e34a6a0e619bbf5f1606a2866eca2ceec9f93348c577b052f30bb7ef41b4b09fff28eb1004ca1f8d5",
            "derivation_path": [
              "010200000000000000",
              "0100000000000000"
            ]
          }
        }
      ],
      "type": "spend_account_unspent_output"
    },
    {
      "amount": 200000000,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 20000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "type": "spend_account"
    }
  ],
  "ttl": 0,
  "time_range": 1521625823
}

pubkey哈希校验和单签验证合约

LockWithPublicKeyHash源代码如下:

contract LockWithPublicKeyHash(pubKeyHash: Hash) locks value {
  clause spend(pubKey: PublicKey, sig: Signature) {
    verify sha3(pubKey) == pubKeyHash
    verify checkTxSig(pubKey, sig)
    unlock value
  }
}
  • 合约编译之后的字节码为:5279aa887cae7cac
  • 合约对应的指令码为:2 PICK SHA3 EQUALVERIFY SWAP TXSIGHASH SWAP CHECKSIG

假如合约参数如下:

  • pubKeyHash : b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e89

添加合约参数后的合约程序参考结果如下:20b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e8974085279aa887cae7cac00c0

对应的解锁合约的参数如下:(注意解锁合约参数的顺序,否则会执行VM失败)

  • pubKey :
{
  "type": "data",
  "raw_data": {
    "value": "b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e89"
  }
}
  • sig :
{
  "type": "raw_tx_signature",
  "raw_data": {
    "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
    "derivation_path": [
      "010100000000000000",
      "0100000000000000"
    ]
  }
}

其对应的解锁合约交易模板示例如下:

{
  "base_transaction": null,
  "actions": [
    {
      "output_id": "f98e0aa007dec76a827e924c25678e8c04b922fb28da9f5513a92787ac53725b",
      "arguments": [
        {
          "type": "data",
          "raw_data": {
            "value": "b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e89"
          }
        },
        {
          "type": "raw_tx_signature",
          "raw_data": {
            "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
            "derivation_path": [
              "010100000000000000",
              "0100000000000000"
            ]
          }
        }
      ],
      "type": "spend_account_unspent_output"
    },
    {
      "amount": 300000000,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 20000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "type": "spend_account"
    }
  ],
  "ttl": 0,
  "time_range": 1521625823
}

字符串哈希校验合约

RevealPreimage源代码如下:

contract RevealPreimage(hash: Hash) locks value {
  clause reveal(string: String) {
    verify sha3(string) == hash
    unlock value
  }
}
  • 合约编译之后的字节码为:7caa87
  • 合约对应的指令码为:SWAP SHA3 EQUAL

假如合约参数如下:

  • hash : 22e829107201c6b975b1dc60b928117916285ceb4aa5c6d7b4b8cc48038083e0

添加合约参数后的合约程序参考结果如下:2022e829107201c6b975b1dc60b928117916285ceb4aa5c6d7b4b8cc48038083e074037caa8700c0

对应的解锁合约的参数如下:(注意解锁合约参数的顺序,否则会执行VM失败)

  • string :
{
  "type": "data",
  "raw_data": {
    "value": "737472696e67"
  }
}

其对应的解锁合约交易模板示例如下:

{
  "base_transaction": null,
  "actions": [
    {
      "output_id": "35fe216572bea7d81effd2fed2db1bc257f977dfa492299742a0dee2a9ae1c8e",
      "arguments": [
        {
          "type": "data",
          "raw_data": {
            "value": "737472696e67"
          }
        }
      ],
      "type": "spend_account_unspent_output"
    },
    {
      "amount": 400000000,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 20000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "type": "spend_account"
    }
  ],
  "ttl": 0,
  "time_range": 1521625823
}

币币交易合约

TradeOffer源代码如下:

contract TradeOffer(assetRequested: Asset,
                  amountRequested: Amount,
                  seller: Program,
                  cancelKey: PublicKey) locks offered {
  clause trade() requires payment: amountRequested of assetRequested {
    lock payment with seller
    unlock offered
  }
  clause cancel(sellerSig: Signature) {
    verify checkTxSig(cancelKey, sellerSig)
    unlock offered
  }
}
  • 合约编译之后的字节码为:547a6413000000007b7b51547ac1631a000000547a547aae7cac
  • 合约对应的指令码为:4 ROLL JUMPIF:$cancel $trade FALSE ROT ROT 1 4 ROLL CHECKOUTPUT JUMP:$_end $cancel 4 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG $_end

假如合约参数如下:

  • assetRequested : 1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22
  • amountRequested : 99
  • seller : 0014c5a5b563c4623018557fb299259542b8739f6bc2
  • cancelKey : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78

添加合约参数后的合约程序参考结果如下:20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78160014c5a5b563c4623018557fb299259542b8739f6bc20163201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22741a547a6413000000007b7b51547ac1631a000000547a547aae7cac00c0

对应的解锁合约的参数如下:(注意该合约包含两个clause,在解锁合约的时候任选其一即可,其中clause的选择是通过合约代码位置偏移来决定的,通过调用API接口decode-program可以查看对应的偏移量; 此外还需注意解锁合约参数的顺序,否则会执行VM失败)

  • clause trade 解锁参数 :

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    
    • payment : (合约中requires的字段表示解锁账户需要支付对应的amountRequested数量的assetRequested资产到对应的接收对象seller,注意上述参数必须严格匹配,否则合约执行将失败)
    {
      "amount": 99,
      "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JJF0KG0A06",
      "amount": 99,
      "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
      "type": "spend_account"
    }
    

    其对应的解锁合约交易模板示例如下:

    // clause trade
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "9f3a63d2f8352a6891dadd8a8337268873c84a852594f35b0b9815a4b9d56d86",
          "arguments": [
            {
              "type": "data",
              "raw_data": {
                "value": "00000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 99,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 99,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "type": "spend_account"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        },
        {
          "amount": 500000000,
          "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    
  • clause cancel 解锁参数 :(注意参数顺序)

    • sellerSig :
    {
      "type": "raw_tx_signature",
      "raw_data": {
        "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
        "derivation_path": [
          "010100000000000000",
          "0100000000000000"
        ]
      }
    }
    
    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "13000000"
      }
    }
    

    其对应的解锁合约交易模板示例如下:

    // clause cancel
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "9f3a63d2f8352a6891dadd8a8337268873c84a852594f35b0b9815a4b9d56d86",
          "arguments": [
            {
              "type": "raw_tx_signature",
              "raw_data": {
                "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
                "derivation_path": [
                  "010100000000000000",
                  "0100000000000000"
                ]
              }
            },
            {
              "type": "data",
              "raw_data": {
                "value": "13000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 500000000,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014929ec7d92f89d74716ba9591eaea588aa1867f75",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    

第三方信任机构托管合约

Escrow源代码如下:

contract Escrow(agent: PublicKey,
              sender: Program,
              recipient: Program) locks value {
  clause approve(sig: Signature) {
    verify checkTxSig(agent, sig)
    lock value with recipient
  }
  clause reject(sig: Signature) {
    verify checkTxSig(agent, sig)
    lock value with sender
  }
}
  • 合约编译之后的字节码为:537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac1
  • 合约对应的指令码为:3 ROLL JUMPIF:$reject $approve 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY FALSE AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $reject 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY FALSE AMOUNT ASSET 1 4 ROLL CHECKOUTPUT $_end

假如合约参数如下:

  • agent : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78
  • sender : 0014905df16bc248790676744bab063a1ae810803bd7
  • recipient : 0014929ec7d92f89d74716ba9591eaea588aa1867f75

添加合约参数后的合约程序参考结果如下:160014929ec7d92f89d74716ba9591eaea588aa1867f75160014905df16bc248790676744bab063a1ae810803bd720e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787428537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac100c0

对应的解锁合约的参数如下:(注意该合约包含两个clause,在解锁合约的时候任选其一即可,其中clause的选择是通过合约代码位置偏移来决定的,通过调用API接口decode-program可以查看对应的偏移量; 此外还需注意解锁合约参数的顺序,否则会执行VM失败)

  • clause approve 解锁参数 :

    • sellerSig :
    {
      "type": "raw_tx_signature",
      "raw_data": {
        "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
        "derivation_path": [
          "010100000000000000",
          "0100000000000000"
        ]
      }
    }
    
    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    

    其对应的解锁合约交易模板示例如下:(注意接收control_program字段必须为recipient,否则执行失败)

    // clause approve
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "15ee3368e77a0699eadaf99bbc096a48595ac975d05b10b198d902dde808e120",
          "arguments": [
            {
              "type": "raw_tx_signature",
              "raw_data": {
                "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
                "derivation_path": [
                  "010100000000000000",
                  "0100000000000000"
                ]
              }
            },
            {
              "type": "data",
              "raw_data": {
                "value": "00000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 600000000,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014929ec7d92f89d74716ba9591eaea588aa1867f75",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    
  • clause reject 解锁参数 :

    {
      "type": "raw_tx_signature",
      "raw_data": {
        "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
        "derivation_path": [
          "010100000000000000",
          "0100000000000000"
        ]
      }
    }
    
    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "1a000000"
      }
    }
    

    其对应的解锁合约交易模板示例如下:(注意接收control_program字段必须为sender,否则执行失败)

    // clause reject
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "15ee3368e77a0699eadaf99bbc096a48595ac975d05b10b198d902dde808e120",
          "arguments": [
            {
              "type": "raw_tx_signature",
              "raw_data": {
                "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
                "derivation_path": [
                  "010100000000000000",
                  "0100000000000000"
                ]
              }
            },
            {
              "type": "data",
              "raw_data": {
                "value": "1a000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 600000000,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    

抵押贷款合约

LoanCollateral源代码如下:

contract LoanCollateral(assetLoaned: Asset,
                      amountLoaned: Amount,
                      blockHeight: Integer,
                      lender: Program,
                      borrower: Program) locks collateral {
  clause repay() requires payment: amountLoaned of assetLoaned {
    lock payment with lender
    lock collateral with borrower
  }
  clause default() {
    verify above(blockHeight)
    lock collateral with lender
  }
}
  • 合约编译之后的字节码为:557a641b000000007b7b51557ac16951c3c251557ac163260000007bcd9f6900c3c251567ac1
  • 合约对应的指令码为:5 ROLL JUMPIF:$default $repay FALSE ROT ROT 1 5 ROLL CHECKOUTPUT VERIFY 1 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $default ROT BLOCKHEIGHT LESSTHAN VERIFY FALSE AMOUNT ASSET 1 6 ROLL CHECKOUTPUT $_end

假如合约参数如下:

  • assetLoaned : 1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22
  • amountLoaned : 88
  • blockHeight : 1920
  • lender : 0014905df16bc248790676744bab063a1ae810803bd7
  • borrower : 0014929ec7d92f89d74716ba9591eaea588aa1867f75

添加合约参数后的合约程序参考结果如下:160014929ec7d92f89d74716ba9591eaea588aa1867f75160014905df16bc248790676744bab063a1ae810803bd70280070158201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c227426557a641b000000007b7b51557ac16951c3c251557ac163260000007bcd9f6900c3c251567ac100c0

对应的解锁合约的参数如下:(注意该合约包含两个clause,在解锁合约的时候任选其一即可,其中clause的选择是通过合约代码位置偏移来决定的,通过调用API接口decode-program可以查看对应的偏移量; 此外还需注意解锁合约参数的顺序,否则会执行VM失败)

  • clause repay 解锁参数 :

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    
    • payment : (合约中requires的字段表示解锁账户需要支付对应的amountLoaned数量的assetLoaned资产到对应的接收对象lender,注意上述参数必须严格匹配,否则合约执行将失败; 在该clause中解锁对象collateral是被接收对象borrower锁定的,所以该参数也需要匹配; 此外该合约使用了两个lock语句,其解锁模板需要按下面的方式进行顺序排列)
    {
      "amount": 88,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
      "type": "control_program"
    },
    {
      "amount": 700000000,
      "asset_id": "2ee76aeaa110308fcdfb382fa02a4a35823d5a589ffcaddb23f11f8a1fae3302",
      "control_program": "0014929ec7d92f89d74716ba9591eaea588aa1867f75",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 88,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "type": "spend_account"
    }
    

    其对应的解锁合约交易模板示例如下:

    // clause repay
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "3cf74c303558b7343cfa20b32ccddfd8e66293ae1970f7612ca6cb9a006e76bc",
          "arguments": [
            {
              "type": "data",
              "raw_data": {
                "value": "00000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 88,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        },
        {
          "amount": 700000000,
          "asset_id": "2ee76aeaa110308fcdfb382fa02a4a35823d5a589ffcaddb23f11f8a1fae3302",
          "control_program": "0014929ec7d92f89d74716ba9591eaea588aa1867f75",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 88,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "type": "spend_account"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    
  • clause default 解锁参数 :(注意参数顺序)

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "1b000000"
      }
    }
    

    其对应的解锁合约交易模板示例如下:(注意接收control_program字段必须为lender,否则执行失败)

    // clause default
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "3cf74c303558b7343cfa20b32ccddfd8e66293ae1970f7612ca6cb9a006e76bc",
          "arguments": [
            {
              "type": "data",
              "raw_data": {
                "value": "1b000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 700000000,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    

看涨期权合约

CallOption源代码如下:

contract CallOption(strikePrice: Amount,
                  strikeCurrency: Asset,
                  seller: Program,
                  buyerKey: PublicKey,
                  blockHeight: Integer) locks underlying {
  clause exercise(buyerSig: Signature) requires payment: strikePrice of strikeCurrency {
    verify below(blockHeight)
    verify checkTxSig(buyerKey, buyerSig)
    lock payment with seller
    unlock underlying
  }
  clause expire() {
    verify above(blockHeight)
    lock underlying with seller
  }
}
  • 合约编译之后的字节码为:557a6420000000547acda069547a547aae7cac69007c7b51547ac1632c000000547acd9f6900c3c251567ac1
  • 合约对应的指令码为:5 ROLL JUMPIF:$expire $exercise FALSE ROT ROT 1 5 ROLL CHECKOUTPUT VERIFY 1 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $expire ROT BLOCKHEIGHT LESSTHAN VERIFY FALSE AMOUNT ASSET 1 6 ROLL CHECKOUTPUT $_end

假如合约参数如下:

  • strikePrice : 199
  • strikeCurrency : 1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22
  • seller : 0014905df16bc248790676744bab063a1ae810803bd7
  • buyerKey : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78
  • blockHeight : 3096

添加合约参数后的合约程序参考结果如下:02180c20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78160014905df16bc248790676744bab063a1ae810803bd7201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c2201c7742c557a6420000000547acda069547a547aae7cac69007c7b51547ac1632c000000547acd9f6900c3c251567ac100c0

对应的解锁合约的参数如下:(注意该合约包含两个clause,在解锁合约的时候任选其一即可,其中clause的选择是通过合约代码位置偏移来决定的,通过调用API接口decode-program可以查看对应的偏移量; 此外还需注意解锁合约参数的顺序,否则会执行VM失败)

  • clause exercise 解锁参数 :

    • buyerSig :
    {
      "type": "raw_tx_signature",
      "raw_data": {
        "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
        "derivation_path": [
          "010100000000000000",
          "0100000000000000"
        ]
      }
    }
    
    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    
    • payment : (合约中requires的字段表示解锁账户需要支付对应的strikePrice数量的strikeCurrency资产到对应的接收对象seller,注意上述参数必须严格匹配,否则合约执行将失败)
    {
      "amount": 199,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 199,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "type": "spend_account"
    }
    

    其对应的解锁合约交易模板示例如下:

    // clause exercise
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "7afa15ad39356a7e6dc363fba823146ecb0132967f067ddfa13494a34a984518",
          "arguments": [
            {
              "type": "raw_tx_signature",
              "raw_data": {
                "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
                "derivation_path": [
                  "010100000000000000",
                  "0100000000000000"
                ]
              }
            },
            {
              "type": "data",
              "raw_data": {
                "value": "00000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 199,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 199,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "type": "spend_account"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        },
        {
          "amount": 800000000,
          "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }
    
  • clause expire 解锁参数 :

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "20000000"
      }
    }
    

    其对应的解锁合约交易模板示例如下:(注意接收control_program字段必须为seller,否则执行失败)

    // clause expire
    {
      "base_transaction": null,
      "actions": [
        {
          "output_id": "7afa15ad39356a7e6dc363fba823146ecb0132967f067ddfa13494a34a984518",
          "arguments": [
            {
              "type": "data",
              "raw_data": {
                "value": "20000000"
              }
            }
          ],
          "type": "spend_account_unspent_output"
        },
        {
          "amount": 800000000,
          "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
          "control_program": "0014905df16bc248790676744bab063a1ae810803bd7",
          "type": "control_program"
        },
        {
          "account_id": "0G1JIR6400A02",
          "amount": 20000000,
          "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
          "type": "spend_account"
        }
      ],
      "ttl": 0,
      "time_range": 1521625823
    }