Last active
October 28, 2021 10:40
-
-
Save milancermak/33641e6e94b499f8bcdb4440fa7f4b1f to your computer and use it in GitHub Desktop.
Calculate minimal rent exemption balance for a Solana account
This file contains hidden or 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
""" | |
A simple cmd line script to calculate the minimal rent exemption balance | |
of a Solana account. | |
Usage: | |
python calc_rent_exemption.py ACCONUT_ADDRESS | |
""" | |
import base64 | |
import sys | |
import requests | |
RPC_URL = "https://api.mainnet-beta.solana.com" | |
def calc_rent_exemption(address: str) -> int: | |
get_account_info = { | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "getAccountInfo", | |
"params": [address, {"encoding": "base64"}] | |
} | |
resp = requests.post(RPC_URL, json=get_account_info) | |
account_info = resp.json() | |
data = account_info["result"]["value"]["data"][0] | |
data_size = len(base64.b64decode(data)) | |
get_rent_exempt_balance = { | |
"jsonrpc": "2.0", | |
"id": 2, | |
"method": "getMinimumBalanceForRentExemption", | |
"params": [data_size] | |
} | |
resp = requests.post(RPC_URL, json=get_rent_exempt_balance) | |
return resp.json()["result"] | |
if __name__ == "__main__": | |
address = sys.argv[1] | |
rent_exemption = calc_rent_exemption(address) | |
print(f"{(rent_exemption * 1e-9):.9} SOL") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment