Introduction of the new TVM instructions “freeze/unfreeze”

In order to make contract accounts have the ability to freeze TRX to obtain resources, community developers have proposed a discussion about introducing freeze/unfreeze instructions to TVM (Please refer to https://github.com/tronprotocol/tips/issues/157 ). With the joint efforts from the community developers, the discussion finally formed TIP-157 and was implemented in the GreatVoyage-4.2.0(Plato) version. (The corresponding Solidity version is: Solidity 0.5.17 and Solidity 0.6.2)

The limitation of the existing resource model

In the existing TRON resource model, the freeze/unfreeze instructions of the system contract only apply to normal accounts, and contract accounts cannot freeze TRX to obtain resources. DAPP developers cannot use the TRX in the contract to obtain resources. They can only obtain resources by freezing additional TRX or burning TRX. This greatly increases the operating cost of DAPP, and the amount of freezing TRX in the entire network is also limited by this.

The new TVM instructions

TVM freeze related function introduces a total of 3 TVM instructions, the detailed instructions are as follows:

0xd5: FREEZE- Description:Freeze TRX in the contract account to obtain resources
- Parameters description:
- "receiverAddress": The address for receiving resource
- "freezeAmount": amount to freeze in SUN.
- "resourceType": 0 means bandwidth, 1 means energy
- Return value: push 1 to stack if the operation succeeds, otherwise push 0 to stack.
0xd6: UNFREEZE- Description: Unfreeze the TRX that has ended the freeze period
- Parameters description:
- "receiverAddress": The address which received resource
- "resourceType": 0 means bandwidth, 1 means energy
- Return value:push 1 to stack if the operation succeeds, otherwise push 0 to stack.
0xd7: FREEZEEXPIRETIME- Description:Query the time when the 'unfreeze' can be executed.
- Parameters description:
- "targetAddress": The address which received resource
- "resourceType": 0 means bandwidth, 1 means energy
- Return value: Return the time in seconds when the 'unfreeze' can be executed.
  • The TRON Nile test network has enabled TIP-157 on April 27, 2021, and supports the freeze/unfreeze instructions. For details, please refer to https://nile.tronscan.org/#/proposal/8191, all interested developers are welcome to the Nile to Experience the brand-new TVM instructions.

Contract sample code:

In TIP-157, the author provides a small piece of example code to illustrate the usage of the new instruction, the details are as follows:

contract TestFreeze {function freeze(address payable receiver, uint amount, uint res) external returns(uint) {
receiver.freeze(amount, res);
return this.getExpireTime(receiver, res) - now;
}

function unfreeze(address payable receiver, uint res) external returns(uint) {
receiver.unfreeze(res);
return 1;
}

function getExpireTime(address payable target, uint res) external view returns(uint) {
return target.freezeExpireTime(res);
}
}

The difference between TVM freeze instructions and system contract freeze instructions

The new TVM freeze/unfreeze instructions make the contract accounts have the ability to freeze TRX to obtain resources, however, there are still certain differences between TVM freeze/unfreeze instructions and system contract freeze/unfreeze instruction. Here we enumerate the most frequently encountered differences among developers for your reference.

  • For the freeze instruction, the number of freezing TRX must be greater than or equal to 1 TRX = 10e6 SUN.
  • For the TVM freeze/unfreeze instructions, if receiverAddress != contractAddress , the receiverAddress must not be a contract address type.
  • When the freeze instruction is executed, if the target address is a non-existent account, it will automatically activate the target address and deduct an additional 25,000 energy as the account activation cost.
  • When there are freezing TRXs in the contract account and the resource receiver is the contract account itself, executing the selfdestruct will automatically unfreeze the freezing TRX and transferred it to the target account specified in the selfdestruct instruction.

The positive impact of TIP-157

The new TVM instructions are undoubtedly very good news for DAPP developers and TRON users. Take the energy banking system as an example. At present, such systems are implemented through centralized services. After the freezing-related instructions were enabled in TVM, developers can fully implement the energy bank system through smart contracts, so that such systems can also be more decentralized, thereby reducing the security risks caused by centralized services.

Summary

This article introduces the background, usage, and potential application scenarios of the TVM freeze/unfreeze instructions. I hope readers will have a deeper understanding of TVM freeze/unfreeze instructions through this article. We will release more technical articles related to TVM in the follow-up. Please look forward to it.

References

https://github.com/tronprotocol/tips/blob/master/tip-157.md

For more information

Github: https://github.com/tronprotocol