From 8171043c3a21c465971aabf496c34b6f7c0cdf7b Mon Sep 17 00:00:00 2001 From: Rishi Jat Date: Sat, 21 Jun 2025 19:51:51 +0530 Subject: [PATCH 1/2] docs: add instructions for setting additional fees using ComputeBudgetProgram --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index c60bb14..0ac079a 100644 --- a/README.md +++ b/README.md @@ -42,5 +42,36 @@ balance = client.get_balance(public_key) print(balance) ``` +## ⏱ Handling Congestion with Compute Budget Instructions + +On Solana, transaction fees are based on compute units consumed. During high network load, your transaction may fail or be delayed unless additional compute resources are allocated. + +You can manually increase the compute unit limit and the price per unit using Solathon's `ComputeBudgetProgram`. This is especially useful when the network is congested. + +```python +from solathon import Client, Transaction, PublicKey, Keypair +from solathon.core.instructions import transfer +from solathon.core import ComputeBudgetProgram + +client = Client("https://api.devnet.solana.com") +sender = Keypair() +receiver = PublicKey("DESTINATION_PUBLIC_KEY") + +# Optional: Increase compute resources +compute_limit_ix = ComputeBudgetProgram.set_compute_unit_limit(1_000_000) +compute_price_ix = ComputeBudgetProgram.set_compute_unit_price(1) # micro-lamports per unit + +# Transfer instruction +transfer_ix = transfer(from_public_key=sender.public_key, to_public_key=receiver, lamports=10_000) + +# Create and send transaction +tx = Transaction( + instructions=[compute_limit_ix, compute_price_ix, transfer_ix], + signers=[sender] +) +result = client.send_transaction(tx) +print("Transaction signature:", result) +``` + # 🗃️ Contribution Drop a pull request for anything which seems wrong or can be improved, could be a small typo or an entirely new feature! Checkout [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to proceed. From 92e474ff78d338a5a721cc341918787a16353b5d Mon Sep 17 00:00:00 2001 From: Rishi Jat Date: Sat, 21 Jun 2025 23:46:52 +0530 Subject: [PATCH 2/2] feat: implement ComputeBudgetProgram class with tests and update exports --- solathon/core/__init__.py | 2 ++ solathon/core/compute_budget.py | 24 ++++++++++++++++++++++++ tests/utils/test_compute_budget.py | 13 +++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 solathon/core/compute_budget.py create mode 100644 tests/utils/test_compute_budget.py diff --git a/solathon/core/__init__.py b/solathon/core/__init__.py index cde71e7..fd9ac75 100644 --- a/solathon/core/__init__.py +++ b/solathon/core/__init__.py @@ -1 +1,3 @@ """Solathon core""" + +from .compute_budget import ComputeBudgetProgram \ No newline at end of file diff --git a/solathon/core/compute_budget.py b/solathon/core/compute_budget.py new file mode 100644 index 0000000..9217f7e --- /dev/null +++ b/solathon/core/compute_budget.py @@ -0,0 +1,24 @@ +from solathon.core.instructions import Instruction + +COMPUTE_BUDGET_PROGRAM_ID = "ComputeBudget111111111111111111111111111111" + +class ComputeBudgetProgram: + @staticmethod + def set_compute_unit_limit(units: int) -> Instruction: + # 0 = tag for set compute unit limit + data = bytes([0]) + units.to_bytes(4, "little") + return Instruction( + program_id=COMPUTE_BUDGET_PROGRAM_ID, + accounts=[], + data=data + ) + + @staticmethod + def set_compute_unit_price(micro_lamports: int) -> Instruction: + # 3 = tag for set compute unit price + data = bytes([3]) + micro_lamports.to_bytes(8, "little") + return Instruction( + program_id=COMPUTE_BUDGET_PROGRAM_ID, + accounts=[], + data=data + ) \ No newline at end of file diff --git a/tests/utils/test_compute_budget.py b/tests/utils/test_compute_budget.py new file mode 100644 index 0000000..8bc21a3 --- /dev/null +++ b/tests/utils/test_compute_budget.py @@ -0,0 +1,13 @@ +from solathon.core.compute_budget import ComputeBudgetProgram + +def test_set_compute_unit_limit(): + instruction = ComputeBudgetProgram.set_compute_unit_limit(1000) + assert instruction.program_id == "ComputeBudget111111111111111111111111111111" + assert instruction.data[0] == 0 # Tag for set_compute_unit_limit + assert int.from_bytes(instruction.data[1:5], "little") == 1000 + +def test_set_compute_unit_price(): + instruction = ComputeBudgetProgram.set_compute_unit_price(10) + assert instruction.program_id == "ComputeBudget111111111111111111111111111111" + assert instruction.data[0] == 3 # Tag for set_compute_unit_price + assert int.from_bytes(instruction.data[1:9], "little") == 10 \ No newline at end of file