Skip to content

Commit edab522

Browse files
committed
update basics rent program
1 parent c290346 commit edab522

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
// Disclaimer: This library provides functions for working with storage rent. Although it is production ready,
4+
// it has not been audited for security, so use it at your own risk.
5+
6+
// This is the Solidity version of the rust module rent:
7+
// https://github.com/solana-labs/solana/blob/master/sdk/program/src/rent.rs
8+
// As rent is currently not implemented on Solana, only the minimum balance is required.
9+
10+
/// Default rental rate in lamports/byte-year.
11+
///
12+
/// This calculation is based on:
13+
/// - 10^9 lamports per SOL
14+
/// - $1 per SOL
15+
/// - $0.01 per megabyte day
16+
/// - $3.65 per megabyte year
17+
uint64 constant DEFAULT_LAMPORTS_PER_BYTE_YEAR = 1_000_000_000 / 100 * 365 / (1024 * 1024);
18+
19+
/// Default amount of time (in years) the balance has to include rent for the
20+
/// account to be rent exempt.
21+
uint64 constant DEFAULT_EXEMPTION_THRESHOLD = 2;
22+
23+
/// Account storage overhead for calculation of base rent.
24+
///
25+
/// This is the number of bytes required to store an account with no data. It is
26+
/// added to an accounts data length when calculating [`Rent::minimum_balance`].
27+
uint64 constant ACCOUNT_STORAGE_OVERHEAD = 128;
28+
29+
/// Minimum balance due for rent-exemption of a given account data size.
30+
function minimum_balance(uint64 data_len) pure returns (uint64) {
31+
return ((ACCOUNT_STORAGE_OVERHEAD + data_len) * DEFAULT_LAMPORTS_PER_BYTE_YEAR)
32+
* DEFAULT_EXEMPTION_THRESHOLD;
33+
}

basics/rent/solang/solidity/rent.sol

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11

2-
32
import "./system_instruction.sol";
3+
import 'minimum_balance.sol';
44

55
@program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
66
contract rent {
77

88
@payer(payer) // The "payer" pays for the data account creation
99
constructor(address payer) {}
1010

11-
function createSystemAccount(address payer, address newAccount, uint64 lamports, uint64 space) public view {
11+
function createSystemAccount(address payer, address newAccount, uint64 space) public view {
12+
// The minimum lamports required for the amount of space allocated to the account
13+
uint64 lamports = minimum_balance(space);
14+
1215
SystemInstruction.create_account(
1316
payer, // lamports sent from this account (payer)
1417
newAccount, // lamports sent to this account (account to be created)

basics/rent/solang/tests/rent.ts

-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ describe("rent", () => {
2929
const newAccount = anchor.web3.Keypair.generate()
3030
// Number of bytes of space to allocate for the account
3131
const space = 100
32-
// Get the minimum balance required for the account for the given space
33-
const lamports = await connection.getMinimumBalanceForRentExemption(space)
3432

3533
// Create a new account via a Cross Program Invocation to the system program
3634
const tx = await program.methods
3735
.createSystemAccount(
3836
wallet.publicKey, // payer
3937
newAccount.publicKey, // new account
40-
new anchor.BN(lamports), // lamports
4138
new anchor.BN(space) // space
4239
)
4340
.accounts({ dataAccount: dataAccount.publicKey })

0 commit comments

Comments
 (0)