Skip to content

Commit

Permalink
feat: added getMinimumBalanceForRentExemption with no network call (#…
Browse files Browse the repository at this point in the history
…10)

* feat: added getMinimumBalanceForRentExemption

* refactor: better treeshake
  • Loading branch information
nickfrosty authored Jan 23, 2025
1 parent 5de7acb commit 0c03cb8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-poems-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gill": minor
---

added a `getMinimumBalanceForRentExemption` function that does not make an rpc call
39 changes: 39 additions & 0 deletions packages/gill/src/__tests__/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import assert from "node:assert";

import { getMinimumBalanceForRentExemption } from "../core/accounts";

/**
* Note: Each of the values checked against were obtained directly from the
* `getMinimumBalanceForRentExemption` RPC call
*/

describe("getMinimumBalanceForRentExemption", () => {
test("default lamports (no extra space)", () => {
const lamports = getMinimumBalanceForRentExemption();
assert.equal(lamports, 890_880n);
});
test("0 bytes (explicitly passed)", () => {
const lamports = getMinimumBalanceForRentExemption(0);
assert.equal(lamports, 890_880n);
});
test("1 byte of space (as number)", () => {
const lamports = getMinimumBalanceForRentExemption(1);
assert.equal(lamports, 897_840n);
});
test("1 byte of space (as bigint)", () => {
const lamports = getMinimumBalanceForRentExemption(1n);
assert.equal(lamports, 897_840n);
});
test("50 bytes of space (as number)", () => {
const lamports = getMinimumBalanceForRentExemption(50);
assert.equal(lamports, 1_238_880n);
});
test("50 bytes of space (as bigint)", () => {
const lamports = getMinimumBalanceForRentExemption(50n);
assert.equal(lamports, 1_238_880n);
});
test("1k bytes of space", () => {
const lamports = getMinimumBalanceForRentExemption(1_000);
assert.equal(lamports, 7_850_880n);
});
});
38 changes: 38 additions & 0 deletions packages/gill/src/core/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Calculate the total rent needed for to create an account, with or without extra data stored in it
*/
export function getMinimumBalanceForRentExemption(space: bigint | number = 0) {
/**
* Default values for Rent calculations
*
* Values taken from: https://github.com/anza-xyz/agave/blob/master/sdk/rent/src/lib.rs#L93-L97
*/
const RENT = {
/**
* Account storage overhead for calculation of base rent. (aka the number of bytes required to store an account with no data.
*/
ACCOUNT_STORAGE_OVERHEAD: 128n,
/**
* Amount of time (in years) a balance must include rent for the account to
* be rent exempt.
*/
DEFAULT_EXEMPTION_THRESHOLD: BigInt(Math.floor(2.0 * 1000)) / 1000n,
/**
* Default rental rate in lamports/byte-year. This calculation is based on:
* - 10^9 lamports per SOL
* - $1 per SOL
* - $0.01 per megabyte day
* - $3.65 per megabyte year
*/
DEFAULT_LAMPORTS_PER_BYTE_YEAR: BigInt(
Math.floor(((1_000_000_000 / 100) * 365) / (1024 * 1024)),
),
};

return (
((RENT.ACCOUNT_STORAGE_OVERHEAD + BigInt(space)) *
RENT.DEFAULT_LAMPORTS_PER_BYTE_YEAR *
RENT.DEFAULT_EXEMPTION_THRESHOLD) /
1n
);
}
1 change: 1 addition & 0 deletions packages/gill/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./const";
export * from "./rpc";
export * from "./explorer";
export * from "./transactions";
export * from "./accounts";

0 comments on commit 0c03cb8

Please sign in to comment.