# Smart Contract Constructor

# Compile a Summoner Smart Contract

you need to run Summoner compiler first, refer to last chapter

write a simple Summoner smart contract, it will unlock if pass 1 as parameter

func unlockWithNumber(num int64) {
        verify(num == 1)
}


** global variable section *********************************
** function section ****************************************
*** [0] unlockWithNumber ***
void  unlockWithNumber ( num)
   0 DUP
   1 1
   2 EQUAL
   3 OVER
   4 1
   5 EQUAL
   6 VERIFY
   7 DROP
*** line number ***
*** assemble opcodes ***
7651877851876975
*** end of unlockWithNumber ***
** type section ********************************************
** toplevel ********************
*** line number ***

compile the smart contract , and get the opcodes : 7651877851876975, it will be used for transaction

# Deploy Summoner Smart Contract

Deploy smart contract by full node API

# Build transaction

Pass opcodes as an argument(control_program) to the build-transaction API (refer to API chapter)

http://localhost:9888/build-transaction (opens new window)

{
  "base_transaction": null,
  "actions": [
    {
      "account_id": "6b8bd994-960d-4658-897c-4f5fed4c7f5b",
      "amount": 100000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "type": "spend_account"
    },
    {
      "amount": 30000000,
      "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      "control_program": "7651877851876975",
      "type": "control_program"
    }
  ],
  "ttl": 0,
  "time_range": 1521625823
}


Return
{
  "status": "success",
  "data": {
    "raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b350001000201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
    "signing_instructions": [
      {
        "position": 0,
        "witness_components": [
          {
            "type": "raw_tx_signature",
            "quorum": 1,
            "keys": [
              {
                "xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
                "derivation_path": [
                  "2c000000",
                  "99000000",
                  "01000000",
                  "00000000",
                  "01000000"
                ]
              }
            ],
            "signatures": null
          },
          {
            "type": "data",
            "value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
          }
        ]
      }
    ],
    "fee": 70000000,
    "allow_additional_actions": false
  }
}

# Sign transaction

put the data as an argument to the Sign-transaction API (refer to API chapter)

{"password":"xxxxx","transaction":{
   "raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b350001000201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
    "signing_instructions": [
      {
        "position": 0,
        "witness_components": [
          {
            "type": "raw_tx_signature",
            "quorum": 1,
            "keys": [
              {
                "xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
                "derivation_path": [
                  "2c000000",
                  "99000000",
                  "01000000",
                  "00000000",
                  "01000000"
                ]
              }
            ],
            "signatures": null
          },
          {
            "type": "data",
            "value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
          }
        ]
      }
    ],
    "fee": 70000000,
    "allow_additional_actions": false
  
  }}
  
  Return
  {
  "status": "success",
  "data": {
    "transaction": {
      "raw_transaction": "0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35006302405d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c20e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df90201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000",
      "signing_instructions": [
        {
          "position": 0,
          "witness_components": [
            {
              "type": "raw_tx_signature",
              "quorum": 1,
              "keys": [
                {
                  "xpub": "25bd0d1cefc98661c51dcd0174105775bc1357a76393cf4f85c14c69716e26c02c98550eb9ea10b851e34de6b2814a447b077b57e3ee56fc6d8a29715788e9c7",
                  "derivation_path": [
                    "2c000000",
                    "99000000",
                    "01000000",
                    "00000000",
                    "01000000"
                  ]
                }
              ],
              "signatures": [
                "5d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c"
              ]
            },
            {
              "type": "data",
              "value": "e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df9"
            }
          ]
        }
      ],
      "fee": 70000000,
      "allow_additional_actions": false
    },
    "sign_complete": true
  }
}

# Submit transaction

put the raw_transaction as an argument to the Submit-transaction API (refer to API chapter)

http://localhost:9888/submit-transaction (opens new window)

{"raw_transaction":"0701dfd5c8d505010161015fe8695a60023535684313670f139d7aa6944e08175fd9a81fd4a3628d50cf624fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c6868f010001160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35006302405d90ea1c5b73e505a560dc46623e63d1bb9471ed65f7ee333d22ba2a9e6e1116fd8e98e877fd51afba81d691638aacac8babaabb606e58b65cf01205d3092a0c20e231f38c7652312e2e68b1c30eb08bff5e957995e11c5f27126a5411fd5e4df90201003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8084af5f01160014e0ac26c4ee20fa95ba1585b87414e92ddbbd2b35000001002fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e010876518778518769750000"}

Return
{
  "status": "success",
  "data": {
    "tx_id": "f9cc23f393c6a6b9c87c599586409a360123c0b0bc41f9664992b1e39ccd99bb"
  }
}

get the smart contract output id in full node wallet

id: bf3a6e44fdf2929d0923f83053d85236d3843cd1bf1ec3bd2d7299b61a050d50

# Invoke Summoner Smart Contract

invoke Summoner smart contract by BlockCenter API

# Build advanced trancation

using build-advanced-tx API , put the smart contract output as a parameter,the request body as follow

[http://bcapi.movapi.com/bycoin/bytom2/v1/merchant/build-advanced-tx?address=bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4]

{
    "fee":"0.1",
    "inputs":[
        {
            "output_id":"bf3a6e44fdf2929d0923f83053d85236d3843cd1bf1ec3bd2d7299b61a050d50",
            "type":"spend_utxo"
        }
    ]
}

Return

{
  "code": 200,
  "msg": "",
  "data": [
    {
      "tx": {
        "hash": "f54978e7bf0fd9dbdb7a6261e487e014c8ccf628706e00d0da2f5a79c1a32e9c",
        "submission_timestamp": 0,
        "inputs": [
          {
            "script": "7651877851876975",
            "address": "smart contract",
            "asset": {
              "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
              "symbol": "BTM"
            },
            "amount": "0.3",
            "type": "spend"
          }
        ],
        "outputs": [
          {
            "utxo_id": "1f1513beec9ba33e73b1cbad5804bf6a7c9760813a44cc3992f621f92a70380a",
            "script": "0014ae2c6bf48714cf0d00d77b907688ca140a6c211b",
            "address": "bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4",
            "asset": {
              "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
              "symbol": "BTM"
            },
            "amount": "0.2",
            "type": "control"
          }
        ],
        "fee": "0.1",
        "balances": [
          {
            "asset": {
              "asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
              "symbol": "BTM"
            },
            "amount": "0.2"
          }
        ],
        "types": [
          "ordinary"
        ],
        "min_veto_height": 0
      },
      "raw_transaction": "07010001015201502f5f52ba8fd87dae065112df3902d006c71ac992399620e2782051882afeda37ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e01010876518778518769750001000101003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80dac40901160014ae2c6bf48714cf0d00d77b907688ca140a6c211b0000",
      "signing_instructions": [
        {
          "derivation_path": null,
          "sign_data": [
            "3997891ced33c944d75b2795c10e1fa21caa4653c140e4f967252e8cefd6c8ba"
          ]
        }
      ]
    }
  ]
}

# Submit Transaction

using submit-payment API , put the raw_transaction as an argument,put the smart contract parameter as signatures ,you need to pass 1 to unlock this contract,,the request body as follow:

[http://bcapi.movapi.com/bycoin/bytom2/v1/merchant/submit-payment?address=bn1q4ckxhay8zn8s6qxh0wg8dzx2zs9xcggmru3fl4]

{
 "raw_transaction": "07010001015201502f5f52ba8fd87dae065112df3902d006c71ac992399620e2782051882afeda37ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8087a70e01010876518778518769750001000101003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80dac40901160014ae2c6bf48714cf0d00d77b907688ca140a6c211b0000",
 "signatures": [["1"]]
}

Return

{
  "code": 200,
  "msg": "",
  "data": {
    "tx_hash": "f54978e7bf0fd9dbdb7a6261e487e014c8ccf628706e00d0da2f5a79c1a32e9c"
  }
}

unlock the smart contract

Last Updated: 11/2/2021, 6:22:59 PM