-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
getMinimumBalanceForRentExemption
with no network call (#…
…10) * feat: added getMinimumBalanceForRentExemption * refactor: better treeshake
- Loading branch information
1 parent
5de7acb
commit 0c03cb8
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters