Skip to content

Commit fafa52f

Browse files
feat: Add local rent exemption calculator (#1065)
* Add local rent exemption calculator Closes #777 * Tweak docstring --------- Co-authored-by: Callum <callum.mcintyre@anza.xyz>
1 parent d8e447a commit fafa52f

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

.changeset/pink-dragons-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@solana/kit': minor
3+
---
4+
5+
Add local rent exemption calculator
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Lamports } from '@solana/rpc-types';
2+
3+
import { getMinimumBalanceForRentExemption } from '../get-minimum-balance-for-rent-exemption';
4+
5+
describe('getMinimumBalanceForRentExemption', () => {
6+
it.each`
7+
space | lamports
8+
${0n} | ${890_880n}
9+
${100n} | ${890_880n + 100n * 3_480n * 2n}
10+
${1_024n} | ${890_880n + 1_024n * 3_480n * 2n}
11+
`('calculates the correct rent for an account with $space bytes of space allocated', ({ space, lamports }) => {
12+
expect.assertions(1);
13+
expect(getMinimumBalanceForRentExemption(space)).toBe(lamports as unknown as Lamports);
14+
});
15+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Lamports } from '@solana/rpc-types';
2+
3+
/**
4+
* Calculates the minimum {@link Lamports | lamports} required to make an account rent exempt for a
5+
* given data size, without performing an RPC call.
6+
*
7+
* Values are sourced from the on-chain rent parameters in the Solana runtime:
8+
* https://github.com/anza-xyz/solana-sdk/blob/c07f692e41d757057c8700211a9300cdcd6d33b1/rent/src/lib.rs#L93-L97
9+
*
10+
* Note that this logic may change, or be incorrect depending on the cluster you are connected to.
11+
* You can always use the RPC method `getMinimumBalanceForRentExemption` to get the current value.
12+
*
13+
* @param space The number of bytes of account data.
14+
*/
15+
export function getMinimumBalanceForRentExemption(space: bigint): Lamports {
16+
const RENT = {
17+
ACCOUNT_STORAGE_OVERHEAD: 128n,
18+
DEFAULT_EXEMPTION_THRESHOLD: 2n,
19+
DEFAULT_LAMPORTS_PER_BYTE_YEAR: 3_480n,
20+
} as const;
21+
const requiredLamports =
22+
(RENT.ACCOUNT_STORAGE_OVERHEAD + space) *
23+
RENT.DEFAULT_LAMPORTS_PER_BYTE_YEAR *
24+
RENT.DEFAULT_EXEMPTION_THRESHOLD;
25+
return requiredLamports as Lamports;
26+
}

packages/kit/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * from '@solana/transactions';
2727
export * from './airdrop';
2828
export * from './decompile-transaction-message-fetching-lookup-tables';
2929
export * from './fetch-lookup-tables';
30+
export * from './get-minimum-balance-for-rent-exemption';
3031
export * from './send-and-confirm-durable-nonce-transaction';
3132
export * from './send-and-confirm-transaction';
3233
export * from './send-transaction-without-confirming';

0 commit comments

Comments
 (0)