Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-dragons-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/kit': minor
---

Add local rent exemption calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Lamports } from '@solana/rpc-types';

import { getMinimumBalanceForRentExemption } from '../get-minimum-balance-for-rent-exemption';

describe('getMinimumBalanceForRentExemption', () => {
it.each`
space | lamports
${0n} | ${890_880n}
${100n} | ${890_880n + 100n * 3_480n * 2n}
${1_024n} | ${890_880n + 1_024n * 3_480n * 2n}
`('calculates the correct rent for an account with $space bytes of space allocated', ({ space, lamports }) => {
expect.assertions(1);
expect(getMinimumBalanceForRentExemption(space)).toBe(lamports as unknown as Lamports);
});
});
26 changes: 26 additions & 0 deletions packages/kit/src/get-minimum-balance-for-rent-exemption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Lamports } from '@solana/rpc-types';

/**
* Calculates the minimum {@link Lamports | lamports} required to make an account rent exempt for a
* given data size, without performing an RPC call.
*
* Values are sourced from the on-chain rent parameters in the Solana runtime:
* https://github.com/anza-xyz/solana-sdk/blob/c07f692e41d757057c8700211a9300cdcd6d33b1/rent/src/lib.rs#L93-L97
*
* Note that this logic may change, or be incorrect depending on the cluster you are connected to.
* You can always use the RPC method `getMinimumBalanceForRentExemption` to get the current value.
*
* @param space The number of bytes of account data.
*/
export function getMinimumBalanceForRentExemption(space: bigint): Lamports {
const RENT = {
ACCOUNT_STORAGE_OVERHEAD: 128n,
DEFAULT_EXEMPTION_THRESHOLD: 2n,
DEFAULT_LAMPORTS_PER_BYTE_YEAR: 3_480n,
} as const;
const requiredLamports =
(RENT.ACCOUNT_STORAGE_OVERHEAD + space) *
RENT.DEFAULT_LAMPORTS_PER_BYTE_YEAR *
RENT.DEFAULT_EXEMPTION_THRESHOLD;
return requiredLamports as Lamports;
}
1 change: 1 addition & 0 deletions packages/kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from '@solana/transactions';
export * from './airdrop';
export * from './decompile-transaction-message-fetching-lookup-tables';
export * from './fetch-lookup-tables';
export * from './get-minimum-balance-for-rent-exemption';
export * from './send-and-confirm-durable-nonce-transaction';
export * from './send-and-confirm-transaction';
export * from './send-transaction-without-confirming';
Expand Down
Loading