🚨🚨Smart contract expiry and auto renewal are currently disabled. Smart contracts will not be charged auto renewal fees or expire.🚨🚨
Contracts will pay rent on Hedera starting in March 2023. As covered in the Part 1 article, rent is the recurring payment required for contracts to remain active on the network and is comprised of auto-renewal and storage fees. This article shows how you can get your contracts ready to make rent payments and avoid expiration.
Try It Yourself
- Get a Hedera testnet account
- This portal acts like a faucet, giving you 10,000 test HBAR every 24 hours
- If you’re new to Hedera, check out these steps to setup your development environment
- Use this Codesandbox to try the example
- Fork the sandbox
- Remember to provide testnet account credentials in the .env file
- Open a new terminal to execute index.js
- Get the example code from GitHub
You Will Use These Tools
- Hedera JavaScript SDK (Documentation)
- Solidity (Documentation)
- Mirror node REST API (Learn More)
- Mirror node explorer (HashScan)
Goals:
- Understand the Rent Settings
- Define How Rent Will Be Paid
- Pay Rent for New and Existing Contracts
1. Understanding the Rent Settings
Every entity on Hedera has the fields expirationTime, autorenewPeriod, and autorenewAccount:
- The expirationTime is the date and time when the entity expires
- The autorenewPeriod is the number of seconds by which the entity expiration is extended each auto-renew event
- The autorenewAccount is the account that will pay for rent
These fields can be set during creation and changed anytime with an update transaction. The next section covers this in more detail with examples.
Check out this answer in the rent Frequently Asked Questions (FAQ) to see an overview of the renewal process.
2. Defining How Rent Will Be Paid
Smart contracts on Hedera can pay for rent in two ways: external funds or contract funds.
2.1 Paying Rent from External Funds (autorenewAccount)
To pay rent for a contract from external funds (another account or contract), specify the autorenewAccount field using .setAutoRenewAccontId(). You can specify this and other rent-relevant fields for newly deployed contracts (using ContractCreateFlow() or ContractCreateTransaction()) and for existing contracts (using ContractUpdateTransaction()).
The sample function in the first tab below deploys a new contract that will be renewed by the account specified in the variable autorenewAcc. Note that the key for that account (autorenewAccKey) must sign the contract creation transaction. If you specify an admin key for the contract, that key must sign the transaction as well (cAdminKey). You also have the option to use .setAutoRenewPeriod() if you wish to specify that property for the contract. Valid renewal windows are between 30 and 92.5 days – a default of 30 days (7,776,000 seconds) is used if a value is not specified.
The function in the second tab updates an existing contract. An admin key must be specified during contract creation for a contract to be mutable. The function updates the autorenewPeriod for the contract from the default of 30 days to the maximum possible value of 8,000,001 seconds (or 92.5 days).