Skip to content

Commit

Permalink
More reliable fee logic (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass authored Dec 21, 2023
1 parent c74ea32 commit 50ea66c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/tokens-to-rent-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
"precommit": "npx git-format-staged -f 'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"' .",
"build": "tsc -p tsconfig.json",
"start": "node lib/esm/index.js",
"dev": "npx ts-node --project tsconfig.cjs.json src/index.ts"
"start": "node lib/src/index.js",
"dev": "ts-node-dev --respawn --project tsconfig.cjs.json src/index.ts"
},
"dependencies": {
"@coral-xyz/anchor": "^0.28.0",
Expand Down
34 changes: 25 additions & 9 deletions packages/tokens-to-rent-service/src/jupiter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HNT_MINT, IOT_MINT, MOBILE_MINT, toBN } from "@helium/spl-utils";
import { HNT_MINT, IOT_MINT, MOBILE_MINT, toBN, truthy } from "@helium/spl-utils";
import {
Configuration,
DefaultApi,
Expand Down Expand Up @@ -162,14 +162,30 @@ export const fundFees = async ({
instructionDataToTransactionInstruction
);
let fee = 10000;
if (budgetInstructions.length == 2) {
const units = ComputeBudgetInstruction.decodeRequestUnits(
budgetInstructions[0]!
);
const limit = ComputeBudgetInstruction.decodeSetComputeUnitPrice(
budgetInstructions[1]!
);
fee += Math.ceil(units.units * Number(limit.microLamports)) / 1000000;
if (budgetInstructions.length >= 0) {
let units = 0;
let price = 0;
budgetInstructions.filter(truthy).forEach((instr) => {
const type = ComputeBudgetInstruction.decodeInstructionType(instr);
switch (type) {
case "RequestHeapFrame":
break;
case "SetComputeUnitLimit":
units =
ComputeBudgetInstruction.decodeSetComputeUnitLimit(instr).units;
break;
case "RequestUnits":
units = ComputeBudgetInstruction.decodeRequestUnits(instr).units;
break;
case "SetComputeUnitPrice":
price = Number(
ComputeBudgetInstruction.decodeSetComputeUnitPrice(instr)
.microLamports
);
break;
}
});
fee += Math.ceil((units * price) / 1000000);
if (fee / LAMPORTS_PER_SOL > 0.01) {
throw new Error("Priority fees are too high right now, try again later");
}
Expand Down

0 comments on commit 50ea66c

Please sign in to comment.