Skip to content

Feature idea: a function to calculate minimum rent required without an RPC call #777

@mcintyre94

Description

@mcintyre94

Motivation

If you'd like to create an account on chain to store some data, you need to fund it with a balance that makes it rent-exempt. To find out the required balance you can use the getMinimumBalanceForRentExemption RPC call.

But the logic for this RPC call is stateless, and can be computed without making an RPC call. We could add a function to Kit to do this, which would save consumers making the RPC call.

Example use case

// we want to create an account and store some data structure
const spaceRequired = getMyThingCodec().fixedSize; 
// no need to make an RPC call
const balanceRequired = getMinimumBalanceForRentExemption(spaceRequired);

// Create an account with the required balance
const message = pipe(
  createTransactionMessage({ version: 0}),
  m => appendTransactionMessageInstruction(
    getCreateAccountInstruction({
      space: spaceRequired,
      lamports: balanceRequired,
      // ...
   })
)

Details

Gill has an implementation: https://github.com/DecalLabs/gill/blob/9d30e69d5faaf34e773def8cde17a444ad6fd7f1/packages/gill/src/core/accounts.ts#L4

The values and rent calculation come from: https://github.com/anza-xyz/solana-sdk/blob/c07f692e41d757057c8700211a9300cdcd6d33b1/rent/src/lib.rs#L93-L97

While this could in theory change, it is not just an RPC value since it is enforced when new accounts are created on-chain. So I think the risk of it changing often or quickly is very low, and I don't think this would be a significant maintenance burden for Kit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions