Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/add solana priority fees #4

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/coin-modules/coin-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"@ledgerhq/logs": "workspace:^",
"@ledgerhq/types-cryptoassets": "workspace:^",
"@ledgerhq/types-live": "workspace:^",
"@solana/spl-token": "^0.3.7",
"@solana/web3.js": "1.77.3",
"@solana/spl-token": "0.3.9",
"@solana/web3.js": "1.91.6",
"bignumber.js": "^9.1.2",
"bs58": "^4.0.1",
"expect": "^27.4.6",
Expand All @@ -65,12 +65,12 @@
"superstruct": "0.14.2"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/bs58": "^4.0.1",
"@types/invariant": "^2.2.2",
"@types/jest": "^29.5.10",
"@types/lodash": "^4.14.191",
"@types/object-hash": "^2.1.0",
"@faker-js/faker": "^8.4.1",
"jest": "^29.7.0",
"ts-jest": "^29.1.1"
},
Expand Down
24 changes: 24 additions & 0 deletions libs/coin-modules/coin-solana/src/api/cached.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { makeLRUCache, minutes, seconds } from "@ledgerhq/live-network/cache";
import { PublicKey, TransactionInstruction, TransactionMessage } from "@solana/web3.js";
import hash from "object-hash";
import { ChainAPI } from "./chain";

Expand All @@ -8,6 +9,17 @@ const cacheKeyAssocTokenAccAddress = (owner: string, mint: string) => `${owner}:
const cacheKeyMinimumBalanceForRentExemption = (dataLengt: number) => dataLengt.toString();

const cacheKeyTransactions = (signatures: string[]) => hash([...signatures].sort());
const cacheKeyInstructions = (ixs: TransactionInstruction[], payer: PublicKey) => {
return hash(
new TransactionMessage({
instructions: ixs,
payerKey: payer,
recentBlockhash: payer.toString(),
})
.compileToLegacyMessage()
.serialize(),
);
};

const cacheKeyByArgs = (...args: any[]) => hash(args);

Expand Down Expand Up @@ -74,6 +86,18 @@ export function cached(api: ChainAPI): ChainAPI {

getEpochInfo: makeLRUCache(api.getEpochInfo, cacheKeyEmpty, minutes(1)),

getRecentPrioritizationFees: makeLRUCache(
api.getRecentPrioritizationFees,
cacheKeyByArgs,
seconds(30),
),

getSimulationComputeUnits: makeLRUCache(
api.getSimulationComputeUnits,
cacheKeyInstructions,
seconds(30),
),

config: api.config,
};
}
49 changes: 48 additions & 1 deletion libs/coin-modules/coin-solana/src/api/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
sendAndConfirmRawTransaction,
SignaturesForAddressOptions,
StakeProgram,
GetRecentPrioritizationFeesConfig,
TransactionInstruction,
ComputeBudgetProgram,
VersionedTransaction,
TransactionMessage,
} from "@solana/web3.js";
import { makeLRUCache, minutes } from "@ledgerhq/live-network/cache";
import { getEnv } from "@ledgerhq/live-env";
Expand Down Expand Up @@ -71,6 +76,15 @@ export type ChainAPI = Readonly<{

getEpochInfo: () => ReturnType<Connection["getEpochInfo"]>;

getRecentPrioritizationFees: (
accounts: string[],
) => ReturnType<Connection["getRecentPrioritizationFees"]>;

getSimulationComputeUnits: (
instructions: Array<TransactionInstruction>,
payer: PublicKey,
) => Promise<number | null>;

config: Config;
}>;

Expand All @@ -87,7 +101,7 @@ export function getChainAPI(
logger === undefined
? undefined
: (url, options, fetch) => {
logger(url, options);
logger(url.toString(), options);
fetch(url, options);
};

Expand Down Expand Up @@ -207,6 +221,39 @@ export function getChainAPI(

getEpochInfo: () => connection().getEpochInfo().catch(remapErrors),

getRecentPrioritizationFees: (accounts: string[]) => {
return connection()
.getRecentPrioritizationFees({
lockedWritableAccounts: accounts.map(acc => new PublicKey(acc)),
})
.catch(remapErrors);
},

getSimulationComputeUnits: async (instructions, payer) => {
// https://solana.com/developers/guides/advanced/how-to-request-optimal-compute
const testInstructions = [
// Set an arbitrarily high number in simulation
// so we can be sure the transaction will succeed
// and get the real compute units used
ComputeBudgetProgram.setComputeUnitLimit({ units: 1_400_000 }),
...instructions,
];
const testTransaction = new VersionedTransaction(
new TransactionMessage({
instructions: testInstructions,
payerKey: payer,
// RecentBlockhash can by any public key during simulation
// since 'replaceRecentBlockhash' is set to 'true' below
recentBlockhash: PublicKey.default.toString(),
}).compileToV0Message(),
);
const rpcResponse = await connection().simulateTransaction(testTransaction, {
replaceRecentBlockhash: true,
sigVerify: false,
});
return rpcResponse.value.err ? null : rpcResponse.value.unitsConsumed || null;
},

config,
};
}
Loading