Skip to content

Smart Contract Template

martinbtm edited this page Sep 6, 2021 · 1 revision

Typical contract template analysis

If the contract has parameters, you need to add the relevant parameters to instantiate the API interface when compiling contract. The program field in the compiled result is the normal contract program to be deployed.

Contract of single signature

LockWithPublicKeysource code as follows:

contract LockWithPublicKey(publicKey: PublicKey) locks locked {
  clause unlockWithSig(sig: Signature) {
    verify checkTxSig(publicKey, sig)
    unlock locked
  }
}
  • The bytecode after the contract is compiled is: ae7cac
  • The instruction code corresponding to the contract is: TXSIGHASH SWAP CHECKSIG

If the contract parameters are as follows:

  • publicKey : e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78

The reference result after adding the contract parameters is as follows:20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0

The parameters corresponding to unlocking contract are as follows:

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

An example of the template of unlocking contract transaction is as follows:

{
  "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
}

Contract of multi-signature

LockWithMultiSigsource code as follows:

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
  }
}
  • The bytecode after the contract is compiled is: 537a547a526bae71557a536c7cad
  • The instruction code corresponding to the contract is:3 ROLL 4 ROLL 2 TOALTSTACK TXSIGHASH 2ROT 5 ROLL 3 FROMALTSTACK SWAP CHECKMULTISIG

If the contract parameters are as follows:

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

The reference result after adding the contract parameters is as follows:(Please note that the sequence in which the contract parameters are added is the reverse of the order in which the virtual machines are pushed onto the stack.)20e386c85178418fc72f8182111aa818ac736f3f7f1eee75ccdd7e5a057abe8fe0201f51c25decab2168835f1292a5432707ed94b90be8f5ec0d62aca1c6daa1ec5520e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740e537a547a526bae71557a536c7cad00c0

The parameters of unlocking contract are as follows:(Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

  • 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"
    ]
  }
}

An example of the unlocking contract transaction template is as follows:

{
  "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 hash checksum and single signature verification contract

LockWithPublicKeyHashsource code as follows:

contract LockWithPublicKeyHash(pubKeyHash: Hash) locks value {
  clause spend(pubKey: PublicKey, sig: Signature) {
    verify sha3(pubKey) == pubKeyHash
    verify checkTxSig(pubKey, sig)
    unlock value
  }
}
  • The bytecode after the contract is compiled is: 5279aa887cae7cac
  • The instruction code corresponding to the contract is:2 PICK SHA3 EQUALVERIFY SWAP TXSIGHASH SWAP CHECKSIG

If the contract parameters are as follows:

  • pubKeyHash : b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e89

The reference result after adding the contract parameters is as follows: 20b3f37834dfa74174e9f0d208302e77c637cfe66c3e37fe1e1574e416b3516e8974085279aa887cae7cac00c0

The parameters of unlocking contract are as follows:(Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

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

An example of the unlocking contract transaction template is as follows:

{
  "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
}

String hash verification contract

RevealPreimagesource code as follows::

contract RevealPreimage(hash: Hash) locks value {
  clause reveal(string: String) {
    verify sha3(string) == hash
    unlock value
  }
}
  • The bytecode after the contract is compiled is: 7caa87
  • The instruction code corresponding to the contract is::SWAP SHA3 EQUAL

If the contract parameters are as follows:

  • hash : 22e829107201c6b975b1dc60b928117916285ceb4aa5c6d7b4b8cc48038083e0

The reference result after adding the contract parameters is as follows:2022e829107201c6b975b1dc60b928117916285ceb4aa5c6d7b4b8cc48038083e074037caa8700c0

The parameters of unlocking contract are as follows:(Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

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

An example of the unlocking contract transaction template is as follows:

{
  "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

TradeOffersource code as follows:

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
  }
}
  • The bytecode after the contract is compiled is:547a6413000000007b7b51547ac1631a000000547a547aae7cac
  • The instruction code corresponding to the contract is: 4 ROLL JUMPIF:$cancel $trade FALSE ROT ROT 1 4 ROLL CHECKOUTPUT JUMP:$_end $cancel 4 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG $_end

If the contract parameters are as follows:

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

The reference result after adding the contract parameters is as follows:20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78160014c5a5b563c4623018557fb299259542b8739f6bc20163201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22741a547a6413000000007b7b51547ac1631a000000547a547aae7cac00c0

The parameters of the corresponding unlocking contract are as follows: (Please note that the contract contains two clauses, you can choose either one when you unlock the contract, where the choice of the clause is determined by the offset of the contract code by calling the API “decode- program” to check; Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

  • clause trade unlock parameters :

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    • payment : Payment : (The “requires” field in the contract means that unlocking account needs to pay the corresponding amount (amountRequested) of the “assetRequested” to the corresponding recipient “seller”. Please note that the above parameters must be strictly matched, otherwise the contract execution will fail)
    {
      "amount": 99,
      "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JJF0KG0A06",
      "amount": 99,
      "asset_id": "2a1861ba3a5f7bbdb98392b33289465b462fe8e9d4b9c00f78cbcb1ac20fa93f",
      "type": "spend_account"
    }

    An example of the unlocking contract transaction template is as follows:

    // 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 unlock parameter

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

    An example of the unlocking contract transaction template is as follows:

    // 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

Escrowsource code as follows:

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
  }
}
  • The bytecode after the contract is compiled is:537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac1
  • The instruction code corresponding to the contract is: 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

If the contract parameters are as follows:

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

The reference result after adding the contract parameters is as follows:160014929ec7d92f89d74716ba9591eaea588aa1867f75160014905df16bc248790676744bab063a1ae810803bd720e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787428537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac100c0

The parameters of the corresponding unlocking contract are as follows: (Please note that the contract contains two clauses, you can choose either one when you unlock the contract, where the choice of the clause is determined by the offset of the contract code by calling the API “decode- program” to check; Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

  • clause approve unlock parameters :

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

    The parameters of the corresponding unlocking contract are as follows:(please note the control_program must be recipient,otherwise will fail to execute)

    // 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 unlock parameters :

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

    The parameters of the corresponding unlocking contract are as follows:(please note the control_program must be recipient,otherwise will fail to execute)

    // 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

LoanCollateralsource code as follows:

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
  }
}
  • The bytecode after the contract is compiled is:557a641b000000007b7b51557ac16951c3c251557ac163260000007bcd9f6900c3c251567ac1
  • The instruction code corresponding to the contract is: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

If the contract parameters are as follows:

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

The reference result after adding the contract parameters is as follows:160014929ec7d92f89d74716ba9591eaea588aa1867f75160014905df16bc248790676744bab063a1ae810803bd70280070158201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c227426557a641b000000007b7b51557ac16951c3c251557ac163260000007bcd9f6900c3c251567ac100c0

The parameters of the corresponding unlocking contract are as follows: (Please note that the contract contains two clauses, you can choose either one when you unlock the contract, where the choice of the clause is determined by the offset of the contract code by calling the API “decode- program” to check; Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

  • clause repay uncl==:

    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    • payment : The “require” fields in the contract means that unlocking account needs to pay the corresponding “amountLoaned” asset to the corresponding recipient “lender”. Note that the above parameters must be strictly matched, otherwise the contract execution will fail; the object “collateral” to be unlocked in the clause is locked by the recipient “borrower” , so this parameter also needs to match; in addition, the contract uses two “lock” statements, the unlock template needs to be arranged in the following way
    {
      "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"
    }

    The parameters corresponding to unlocking contract are as follows:

    // 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 unlock parameter :(note sequence)

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

    The parameters of the corresponding unlocking contract are as follows:(please note the control_program must be lender,otherwise will fail to execute)

    // 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

CallOptionsource code as follows:

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
  }
}
  • The bytecode after the contract is compiled is: 557a6420000000547acda069547a547aae7cac69007c7b51547ac1632c000000547acd9f6900c3c251567ac1
  • The instruction code corresponding to the contract is: 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

If the contract parameters are as follows:

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

The reference result after adding the contract parameters is as follows:02180c20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78160014905df16bc248790676744bab063a1ae810803bd7201e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c2201c7742c557a6420000000547acda069547a547aae7cac69007c7b51547ac1632c000000547acd9f6900c3c251567ac100c0

The parameters of the corresponding unlocking contract are as follows: (Please note that the contract contains two clauses, you can choose either one when you unlock the contract, where the choice of the clause is determined by the offset of the contract code by calling the API “decode- program” to check; Please note the sequence of unlocking the contract parameters, otherwise the VM will fail to execute)

  • clause exercise unlock parameters :

    • buyerSig :
    {
      "type": "raw_tx_signature",
      "raw_data": {
        "xpub": "5c6145b241b1147987565719657a0506ebb417a2e110a235a42cfb40951880f447432f930ce9fd1a6b7e51b3ddbfdc7adb57d33448f93c0defb4de630703a144",
        "derivation_path": [
          "010100000000000000",
          "0100000000000000"
        ]
      }
    }
    • clause_selector :
    {
      "type": "data",
      "raw_data": {
        "value": "00000000"
      }
    }
    • payment : (the requires fields in the contract means that you must pay strikePrice amount of strikeCurrency asset to seller to unlock the account.Note that the above parameters must be strictly matched, otherwise the contract execution will fail )
    {
      "amount": 199,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "control_program": "0014c5a5b563c4623018557fb299259542b8739f6bc2",
      "type": "control_program"
    },
    {
      "account_id": "0G1JIR6400A02",
      "amount": 199,
      "asset_id": "1e074b22ed7ae8470c7ba5d8a7bc95e83431a753a17465e8673af68a82500c22",
      "type": "spend_account"
    }

    The parameters corresponding to unlocking contract are as follows:

    // 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 unlock patameters :

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

    The parameters of the corresponding unlocking contract are as follows:(please note the control_program must be seller,otherwise will fail to execute)

    // 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
    }
Clone this wiki locally