Introduction to Bytom 2.0 Registered Contract

BytomDAO
BytomDAO
Published in
4 min readSep 10, 2021

The Bytom 2.0 contract system has undergone major upgrades, including but not limited to UTXO’s world state, Goland-like development language, more complete operators, contract registration, and subsequent compatibility with EVM.

The article will introduce the registered contract of Bytom 2.0.

What is registered contract?

Registered contract means writing a contract code on blockchain by destroying a UTXO, and the contract code can be used directly through the hash of the contract.

To register a contract, you can contact the contract template in Bytom 1.0, but the contract template is only a reference for developers. Developers need to deploy the contract template on blockchain by themselves. After the registration complete, the developer can directly call it.

Issue once, call forever

The advantage of the Registrated contract is that it only needs to be issued once on the chain, and it can be used for unlimited times. In addition to use by owner, it can also be provided to external users.

In addition, the registered contract is issued on the chain, which is open, transparent, and unchangeable, which eliminates the possibility of malicious personnel tampering

For the chain, the registered contract only occupies the space once after it is issued. Compared with deploying a new contract every time, it greatly saves on-chain storage, thereby reducing network cost.

Process of contract registration

1. The user creates a contract using the Bytom 2.0 contract language

2. Use full node to create a registered contract locally

3. Construct a special register-contract transaction, complete the contract, gas fee, input and output

4. Sign transaction and submit

5. The client broadcasts the transaction to the entire network

6. The consensus node obtains the transaction, verifies the format, verifies the input and output, and checks whether the gas fee is sufficient. If the verification is correct, it will be placed in the transaction pool

7. The consensus node obtains transactions from the transaction pool and packs them into blocks

8. The contract is successfully registered

Example

Preparation

The user needs to first build full node and create a wallet, tutorial: https://developer.bytom.io/guide/01_set_up_full_node.html

Prepare a contract

First write a contract, the specific Bytom 2.0 contract language is not yet fully open, here is just an example

const {
var AssetX asset = 1
var AssetY asset = 2
var btmSwap BtmSwap
}
func (b btmSwap) AddLiquidity(x int64, y int64) {
newX := b.poolX + x
newY := b.poolY + y
newM := newX * newY
Lock(newX, assetX)
Lock(newY, assetY)
Lock(newM, assetM)
}

Compile the contract into bytecode

034e61bc70a09161641500000075527963170000007578569393

Bytecode for instantiating contract parameters

207acc78faf72cc0726a0533b705d52527e804946229f673e34ba6c2e4f6a814117403ae7cac00c0

Create contract locally (not required)

Use full-node create-contract to create a contract locally (not on blockchain)

// 请求
curl -X POST create-contract -d '{"alias": "pk","contract": "207acc78faf72cc0726a0533b705d52527e804946229f673e34ba6c2e4f6a814117403ae7cac00c0"}'
// 响应
{
"status": "success",
"data": {
"id": "20924a079e5f9646c8177194bcfc22e35931a6c7bc3192ad3f266d618fe2a323",
"alias": "pk",
"contract": "207acc78faf72cc0726a0533b705d52527e804946229f673e34ba6c2e4f6a814117403ae7cac00c0",
"call_program": "04626372702020924a079e5f9646c8177194bcfc22e35931a6c7bc3192ad3f266d618fe2a323",
"register_program": "6a0462637270010128207acc78faf72cc0726a0533b705d52527e804946229f673e34ba6c2e4f6a814117403ae7cac00c0"
}
}

Put registered contract on blockchain

Adopt the basic process of putting transaction on-chain , just build-sign-submit

Construct the transaction structure of the registered contract, where the type is “register_contract”, and at the same time bring the locally created contract id into the contract

curl -X POST build-transaction -d
{
"actions": [
{
"account_id": "cdcbb36c-cbbb-4a1a-88ad-74ca780359e4",
"amount": 800000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "spend_account"
},
{
"amount": 500000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"type": "register_contract",
"contract":"207acc78faf72cc0726a0533b705d52527e804946229f673e34ba6c2e4f6a814117403ae7cac00c0"
},
{
"amount": 100000000,
"asset_id": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"address": "tn1qusqn39eh38k65wcphrl78p73ukuen6tqyk3d87",
"type": "control_address"
}
],
"ttl": 0,
"time_range": 0
}

Sign transaction

curl -X POST sign-transaction -d
{
"password": "xxxx",
"transaction": {
"raw_transaction": "070100010161015f35d57984cec4d308de61e5b6065020098262742817a40e897536dc3522b27039ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88f3ee90150001160014e40138973789edaa3b01b8ffe387d1e5b999e9600001000301003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88e3b2931201160014e40138973789edaa3b01b8ffe387d1e5b999e960000001004effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80cab5ee01012604626372702020924a079e5f9646c8177194bcfc22e35931a6c7bc3192ad3f266d618fe2a323000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f01160014e40138973789edaa3b01b8ffe387d1e5b999e9600000",
"signing_instructions": [
{
"position": 0,
"witness_components": [
{
"type": "raw_tx_signature",
"quorum": 1,
"keys": [
{
"xpub": "42c574b1790650a4acd8883c36598e75c7d0b48b7fd22e4d1827f19c85fc09dabf41f15d793d68f851c16a7ff73db1ee1620a43b1ddf224a657f69d23e9ed254",
"derivation_path": [
"2c000000",
"99000000",
"01000000",
"00000000",
"02000000"
]
}
],
"signatures": null
},
{
"type": "data",
"value": "355819e54cf61cb397e378f01fef3002246d66b4f7ed81c10f4d55996ff10035"
}
]
}
],
"fee": 200000000,
"allow_additional_actions": false
}
}

Submit transaction

curl -X POST submit-transaction -d
{
"raw_transaction": "070100010161015f35d57984cec4d308de61e5b6065020098262742817a40e897536dc3522b27039ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88f3ee90150001160014e40138973789edaa3b01b8ffe387d1e5b999e96000630240b9081702cb6fc571c86e880bfb8c54af19c6ba8f40e19139dd4b64f1f9d07a8ebd229ed544a847f5ddb3b692c31f4af5e497a63c55c1c299951daf37f470ef0e20355819e54cf61cb397e378f01fef3002246d66b4f7ed81c10f4d55996ff100350301003effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff88e3b2931201160014e40138973789edaa3b01b8ffe387d1e5b999e960000001004effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80cab5ee01012604626372702020924a079e5f9646c8177194bcfc22e35931a6c7bc3192ad3f266d618fe2a323000001003dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c2d72f01160014e40138973789edaa3b01b8ffe387d1e5b999e9600000"
}

As long as the transaction is packaged on the chain, the registration contract is completed.

Summary

The registered contract is an innovation of the Bytom 2.0 contract, which can realize one-time issuance and permanent call, which facilitates the reuse of contract code, and is open and transparent, tamper-proof, and effectively saves on-chain storage.

The following article will further introduce how to interact with the registered contract, so stay tuned.

--

--