Skip to content

Commit

Permalink
feat(LLC): calc and possibly append priority fee to solana instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhd committed Apr 5, 2024
1 parent 175fe10 commit 9376144
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 85 deletions.
5 changes: 5 additions & 0 deletions libs/ledger-live-common/src/families/solana/api/cached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export function cached(api: ChainAPI): ChainAPI {
sendRawTransaction: api.sendRawTransaction,

getEpochInfo: makeLRUCache(api.getEpochInfo, cacheKeyEmpty, minutes(1)),
getRecentPrioritizationFees: makeLRUCache(
api.getRecentPrioritizationFees,
cacheKeyEmpty,
seconds(30),
),

config: api.config,
};
Expand Down
11 changes: 10 additions & 1 deletion libs/ledger-live-common/src/families/solana/api/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
sendAndConfirmRawTransaction,
SignaturesForAddressOptions,
StakeProgram,
GetRecentPrioritizationFeesConfig,
} from "@solana/web3.js";
import { getEnv } from "@ledgerhq/live-env";
import { Awaited } from "../../logic";
Expand Down Expand Up @@ -68,6 +69,10 @@ export type ChainAPI = Readonly<{

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

getRecentPrioritizationFees: (
config?: GetRecentPrioritizationFeesConfig,
) => ReturnType<Connection["getRecentPrioritizationFees"]>;

config: Config;
}>;

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

Expand Down Expand Up @@ -196,6 +201,10 @@ export function getChainAPI(

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

getRecentPrioritizationFees: (config?: GetRecentPrioritizationFeesConfig) => {
return connection().getRecentPrioritizationFees(config).catch(remapErrors);
},

config,
};
}
168 changes: 97 additions & 71 deletions libs/ledger-live-common/src/families/solana/api/chain/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
StakeProgram,
SystemProgram,
TransactionInstruction,
ComputeBudgetProgram,
} from "@solana/web3.js";
import chunk from "lodash/chunk";
import uniqBy from "lodash/uniqBy";
import { ChainAPI } from ".";
import { Awaited } from "../../logic";
import {
Expand All @@ -25,7 +27,7 @@ import {
TokenTransferCommand,
TransferCommand,
} from "../../types";
import { drainSeqAsyncGen } from "../../utils";
import { drainSeqAsyncGen, median } from "../../utils";
import { parseTokenAccountInfo, tryParseAsTokenAccount, tryParseAsVoteAccount } from "./account";
import { parseStakeAccountInfo } from "./account/parser";
import { StakeAccountInfo } from "./account/stake";
Expand Down Expand Up @@ -127,12 +129,10 @@ export function getTransactions(
return drainSeqAsyncGen(getTransactionsGen(address, untilTxSignature, api));
}

export const buildTransferInstructions = ({
sender,
recipient,
amount,
memo,
}: TransferCommand): TransactionInstruction[] => {
export const buildTransferInstructions = async (
api: ChainAPI,
{ sender, recipient, amount, memo }: TransferCommand,
): Promise<TransactionInstruction[]> => {
const fromPublicKey = new PublicKey(sender);
const toPublicKey = new PublicKey(recipient);

Expand All @@ -153,12 +153,13 @@ export const buildTransferInstructions = ({
instructions.push(memoIx);
}

return instructions;
return appendMaybePriorityFeeInstruction(api, [fromPublicKey, toPublicKey], instructions);
};

export const buildTokenTransferInstructions = (
export const buildTokenTransferInstructions = async (
api: ChainAPI,
command: TokenTransferCommand,
): TransactionInstruction[] => {
): Promise<TransactionInstruction[]> => {
const {
ownerAddress,
ownerAssociatedTokenAccountAddress,
Expand Down Expand Up @@ -271,6 +272,36 @@ export async function getStakeAccountAddressWithSeed({
return pubkey.toBase58();
}

export async function getPriorityFee(api: ChainAPI, accounts: PublicKey[]): Promise<number> {
const uniqAccs = uniqBy(accounts, acc => acc.toBase58());
const recentFees = await api.getRecentPrioritizationFees({
lockedWritableAccounts: uniqAccs,
});

return median(recentFees.map(item => item.prioritizationFee));
}

export async function buildMaybePriorityFeeInstruction(
api: ChainAPI,
accounts: PublicKey[],
): Promise<TransactionInstruction | null> {
const priorityFee = await getPriorityFee(api, accounts);
if (priorityFee === 0) return null;

return ComputeBudgetProgram.setComputeUnitPrice({
microLamports: priorityFee,
});
}

export async function appendMaybePriorityFeeInstruction(
api: ChainAPI,
accounts: PublicKey[],
ixs: TransactionInstruction[],
): Promise<TransactionInstruction[]> {
const priorityFeeIx = await buildMaybePriorityFeeInstruction(api, accounts);
return priorityFeeIx ? [priorityFeeIx, ...ixs] : ixs;
}

export function buildCreateAssociatedTokenAccountInstruction({
mint,
owner,
Expand All @@ -292,86 +323,82 @@ export function buildCreateAssociatedTokenAccountInstruction({
return instructions;
}

export function buildStakeDelegateInstructions({
authorizedAccAddr,
stakeAccAddr,
voteAccAddr,
}: StakeDelegateCommand): TransactionInstruction[] {
export async function buildStakeDelegateInstructions(
api: ChainAPI,
{ authorizedAccAddr, stakeAccAddr, voteAccAddr }: StakeDelegateCommand,
): Promise<TransactionInstruction[]> {
const withdrawAuthority = new PublicKey(authorizedAccAddr);
const stakeAcc = new PublicKey(stakeAccAddr);
const voteAcc = new PublicKey(voteAccAddr);
const tx = StakeProgram.delegate({
authorizedPubkey: new PublicKey(authorizedAccAddr),
stakePubkey: new PublicKey(stakeAccAddr),
votePubkey: new PublicKey(voteAccAddr),
authorizedPubkey: withdrawAuthority,
stakePubkey: stakeAcc,
votePubkey: voteAcc,
});

return tx.instructions;
return appendMaybePriorityFeeInstruction(api, [withdrawAuthority, stakeAcc], tx.instructions);
}

export function buildStakeUndelegateInstructions({
authorizedAccAddr,
stakeAccAddr,
}: StakeUndelegateCommand): TransactionInstruction[] {
export async function buildStakeUndelegateInstructions(
api: ChainAPI,
{ authorizedAccAddr, stakeAccAddr }: StakeUndelegateCommand,
): Promise<TransactionInstruction[]> {
const withdrawAuthority = new PublicKey(authorizedAccAddr);
const stakeAcc = new PublicKey(stakeAccAddr);
const tx = StakeProgram.deactivate({
authorizedPubkey: new PublicKey(authorizedAccAddr),
stakePubkey: new PublicKey(stakeAccAddr),
authorizedPubkey: withdrawAuthority,
stakePubkey: stakeAcc,
});

return tx.instructions;
return appendMaybePriorityFeeInstruction(api, [withdrawAuthority, stakeAcc], tx.instructions);
}

export function buildStakeWithdrawInstructions({
authorizedAccAddr,
stakeAccAddr,
amount,
toAccAddr,
}: StakeWithdrawCommand): TransactionInstruction[] {
export async function buildStakeWithdrawInstructions(
api: ChainAPI,
{ authorizedAccAddr, stakeAccAddr, amount, toAccAddr }: StakeWithdrawCommand,
): Promise<TransactionInstruction[]> {
const withdrawAuthority = new PublicKey(authorizedAccAddr);
const stakeAcc = new PublicKey(stakeAccAddr);
const recipient = new PublicKey(toAccAddr);
const tx = StakeProgram.withdraw({
authorizedPubkey: new PublicKey(authorizedAccAddr),
stakePubkey: new PublicKey(stakeAccAddr),
authorizedPubkey: withdrawAuthority,
stakePubkey: stakeAcc,
lamports: amount,
toPubkey: new PublicKey(toAccAddr),
toPubkey: recipient,
});

return tx.instructions;
return appendMaybePriorityFeeInstruction(api, [withdrawAuthority, stakeAcc], tx.instructions);
}

export function buildStakeSplitInstructions({
authorizedAccAddr,
stakeAccAddr,
seed,
amount,
splitStakeAccAddr,
}: StakeSplitCommand): TransactionInstruction[] {
// HACK: switch to split_with_seed when supported by @solana/web3.js
const splitIx = StakeProgram.split({
authorizedPubkey: new PublicKey(authorizedAccAddr),
export async function buildStakeSplitInstructions(
api: ChainAPI,
{ authorizedAccAddr, stakeAccAddr, seed, amount, splitStakeAccAddr }: StakeSplitCommand,
): Promise<TransactionInstruction[]> {
const basePk = new PublicKey(authorizedAccAddr);
const stakePk = new PublicKey(stakeAccAddr);
const splitStakePk = new PublicKey(splitStakeAccAddr);
const splitIx = StakeProgram.splitWithSeed({
authorizedPubkey: basePk,
lamports: amount,
stakePubkey: new PublicKey(stakeAccAddr),
splitStakePubkey: new PublicKey(splitStakeAccAddr),
}).instructions[1];

if (splitIx === undefined) {
throw new Error("expected split instruction");
}

const allocateIx = SystemProgram.allocate({
accountPubkey: new PublicKey(splitStakeAccAddr),
basePubkey: new PublicKey(authorizedAccAddr),
programId: StakeProgram.programId,
stakePubkey: stakePk,
splitStakePubkey: splitStakePk,
basePubkey: basePk,
seed,
space: StakeProgram.space,
});

return [allocateIx, splitIx];
return appendMaybePriorityFeeInstruction(api, [basePk, stakePk], splitIx.instructions);
}

export function buildStakeCreateAccountInstructions({
fromAccAddress,
stakeAccAddress,
seed,
amount,
stakeAccRentExemptAmount,
delegate,
}: StakeCreateAccountCommand): TransactionInstruction[] {
export async function buildStakeCreateAccountInstructions(
api: ChainAPI,
{
fromAccAddress,
stakeAccAddress,
seed,
amount,
stakeAccRentExemptAmount,
delegate,
}: StakeCreateAccountCommand,
): Promise<TransactionInstruction[]> {
const fromPubkey = new PublicKey(fromAccAddress);
const stakePubkey = new PublicKey(stakeAccAddress);

Expand All @@ -394,6 +421,5 @@ export function buildStakeCreateAccountInstructions({
votePubkey: new PublicKey(delegate.voteAccAddress),
}),
);

return tx.instructions;
return appendMaybePriorityFeeInstruction(api, [fromPubkey, stakePubkey], tx.instructions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,18 @@ const baseTx = {
const baseAPI = {
getLatestBlockhash: () => Promise.resolve(LATEST_BLOCKHASH_MOCK),
getFeeForMessage: (_msg: unknown) => Promise.resolve(testOnChainData.fees.lamportsPerSignature),
getRecentPrioritizationFees: () => {
return Promise.resolve([
{
slot: 122422797,
prioritizationFee: 0,
},
{
slot: 122422797,
prioritizationFee: 0,
},
]);
},
} as ChainAPI;

type StakeTestSpec = {
Expand Down
16 changes: 16 additions & 0 deletions libs/ledger-live-common/src/families/solana/bridge/mock-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,4 +892,20 @@ export const getMockedMethods = (): {
},
// manual
{ method: "getLatestBlockhash", params: [], answer: LATEST_BLOCKHASH_MOCK },
{
method: "getRecentPrioritizationFees",
params: [
["AQbkEagmPgmsdAfS4X8V8UyJnXXjVPMvjeD15etqQ3Jh"]
],
answer: [[
{
slot: 122422797,
prioritizationFee: 0,
},
{
slot: 122422797,
prioritizationFee: 0,
},
]],
},
];
28 changes: 17 additions & 11 deletions libs/ledger-live-common/src/families/solana/js-buildTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const buildTransactionWithAPI = async (
transaction: Transaction,
api: ChainAPI,
): Promise<readonly [OnChainTransaction, (signature: Buffer) => OnChainTransaction]> => {
const instructions = buildInstructions(transaction);
const instructions = await buildInstructions(api, transaction);

const recentBlockhash = await api.getLatestBlockhash();

Expand All @@ -46,35 +46,41 @@ export const buildTransactionWithAPI = async (
];
};

function buildInstructions(tx: Transaction): TransactionInstruction[] {
async function buildInstructions(
api: ChainAPI,
tx: Transaction,
): Promise<TransactionInstruction[]> {
const { commandDescriptor } = tx.model;
if (commandDescriptor === undefined) {
throw new Error("missing command descriptor");
}
if (Object.keys(commandDescriptor.errors).length > 0) {
throw new Error("can not build invalid command");
}
return buildInstructionsForCommand(commandDescriptor.command);
return buildInstructionsForCommand(api, commandDescriptor.command);
}

function buildInstructionsForCommand(command: Command): TransactionInstruction[] {
async function buildInstructionsForCommand(
api: ChainAPI,
command: Command,
): Promise<TransactionInstruction[]> {
switch (command.kind) {
case "transfer":
return buildTransferInstructions(command);
return buildTransferInstructions(api, command);
case "token.transfer":
return buildTokenTransferInstructions(command);
return buildTokenTransferInstructions(api, command);
case "token.createATA":
return buildCreateAssociatedTokenAccountInstruction(command);
case "stake.createAccount":
return buildStakeCreateAccountInstructions(command);
return buildStakeCreateAccountInstructions(api, command);
case "stake.delegate":
return buildStakeDelegateInstructions(command);
return buildStakeDelegateInstructions(api, command);
case "stake.undelegate":
return buildStakeUndelegateInstructions(command);
return buildStakeUndelegateInstructions(api, command);
case "stake.withdraw":
return buildStakeWithdrawInstructions(command);
return buildStakeWithdrawInstructions(api, command);
case "stake.split":
return buildStakeSplitInstructions(command);
return buildStakeSplitInstructions(api, command);
default:
return assertUnreachable(command);
}
Expand Down
Loading

0 comments on commit 9376144

Please sign in to comment.