Skip to content

Commit

Permalink
feat(sdk-coin-sol): fetch prio fees from WP
Browse files Browse the repository at this point in the history
Ticket: CR-1224
  • Loading branch information
Vijay-Jagannathan committed Jan 21, 2025
1 parent 19476e3 commit cf1678b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion modules/sdk-coin-sol/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ export interface StakingAuthorize {

export interface SetPriorityFee {
type: InstructionBuilderTypes.SetPriorityFee;
params: Record<string, never>;
params: {
fee: number | bigint;
};
}

export interface AtaInit {
Expand Down
6 changes: 5 additions & 1 deletion modules/sdk-coin-sol/src/lib/instructionParamsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
StakeProgram,
SystemInstruction,
TransactionInstruction,
ComputeBudgetInstruction,
} from '@solana/web3.js';

import { NotSupported, TransactionType } from '@bitgo/sdk-core';
Expand Down Expand Up @@ -195,9 +196,12 @@ function parseSendInstructions(
instructionData.push(ataClose);
break;
case ValidInstructionTypesEnum.SetPriorityFee:
const setComputeUnitPriceParams = ComputeBudgetInstruction.decodeSetComputeUnitPrice(instruction);
const setPriorityFee: SetPriorityFee = {
type: InstructionBuilderTypes.SetPriorityFee,
params: {},
params: {
fee: setComputeUnitPriceParams.microLamports,
},
};
instructionData.push(setPriorityFee);
break;
Expand Down
4 changes: 1 addition & 3 deletions modules/sdk-coin-sol/src/lib/solInstructionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ function advanceNonceInstruction(data: Nonce): TransactionInstruction[] {
}

function fetchPriorityFeeInstruction(instructionToBuild: SetPriorityFee): TransactionInstruction[] {
// 200k * 10000000 microlamports => prio fee
// https://www.quicknode.com/gas-tracker/solana
const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 10000000,
microLamports: instructionToBuild.params.fee,
});

return [addPriorityFee];
Expand Down
4 changes: 3 additions & 1 deletion modules/sdk-coin-sol/src/lib/tokenTransferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ export class TokenTransferBuilder extends TransactionBuilder {
);
const addPriorityFeeInstruction: SetPriorityFee = {
type: InstructionBuilderTypes.SetPriorityFee,
params: {},
params: {
fee: this._priorityFee,
},
};
// order is important, createAtaInstructions must be before sendInstructions
this._instructionsData = [addPriorityFeeInstruction, ...createAtaInstructions, ...sendInstructions];
Expand Down
6 changes: 6 additions & 0 deletions modules/sdk-coin-sol/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
protected _signers: KeyPair[] = [];
protected _memo?: string;
protected _feePayer?: string;
protected _priorityFee: number;

constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
Expand Down Expand Up @@ -245,6 +246,11 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
return this;
}

setPriorityFee(feeOptions: FeeOptions): this {
this._priorityFee = Number(feeOptions.amount);
return this;
}

feePayer(feePayer: string): this {
this._feePayer = feePayer;
return this;
Expand Down
4 changes: 3 additions & 1 deletion modules/sdk-coin-sol/src/lib/transferBuilderV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ export class TransferBuilderV2 extends TransactionBuilder {
) {
addPriorityFeeInstruction = {
type: InstructionBuilderTypes.SetPriorityFee,
params: {},
params: {
fee: this._priorityFee,
},
};
this._instructionsData = [addPriorityFeeInstruction, ...createAtaInstructions, ...sendInstructions];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getBuilderFactory } from '../getBuilderFactory';
import { KeyPair, Utils } from '../../../src';
import should from 'should';
import * as testData from '../../resources/sol';
import { FeeOptions } from '@bitgo/sdk-core';

describe('Sol Token Transfer Builder', () => {
let ataAddress;
Expand All @@ -26,6 +27,10 @@ describe('Sol Token Transfer Builder', () => {
const owner = testData.tokenTransfers.owner;
const walletPK = testData.associatedTokenAccounts.accounts[0].pub;
const walletSK = testData.associatedTokenAccounts.accounts[0].prv;
const prioFeeMicroLamports = '10000000';
const priorityFee: FeeOptions = {
amount: prioFeeMicroLamports,
};
describe('Succeed', () => {
before(async () => {
ataAddress = await Utils.getAssociatedTokenAccountAddress(mintUSDC, otherAccount.pub);
Expand All @@ -37,6 +42,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand All @@ -60,6 +66,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.nonce(recentBlockHash, { walletNonceAddress: nonceAccount.pub, authWalletAddress: walletPK });
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -89,6 +96,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand All @@ -112,6 +120,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.nonce(recentBlockHash);
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -140,6 +149,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.sign({ key: walletSK });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.id.should.not.equal(undefined);
tx.inputs.length.should.equal(1);
Expand Down Expand Up @@ -177,6 +187,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.send({ address: account5.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.sign({ key: authAccount.prv });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(6);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -259,6 +270,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.send({ address: account1.pub, amount, tokenName: nameUSDC });
txBuilder.send({ address: account2.pub, amount, tokenName: nameSRM });
txBuilder.send({ address: account3.pub, amount, tokenName: nameRAY });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(4);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -325,6 +337,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.nonce(recentBlockHash);
txBuilder.sender(owner);
txBuilder.send({ address: account1.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();

tx.outputs.should.deepEqual([
Expand All @@ -343,6 +356,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -399,6 +413,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.createAssociatedTokenAccount({ ownerAddress: account1.pub, tokenName: nameUSDC });
txBuilder.createAssociatedTokenAccount({ ownerAddress: account2.pub, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(3);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -503,6 +518,7 @@ describe('Sol Token Transfer Builder', () => {
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(3);
tx.inputs[0].should.deepEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getBuilderFactory } from '../getBuilderFactory';
import { KeyPair, Utils } from '../../../src';
import * as testData from '../../resources/sol';
import should from 'should';
import { FeeOptions } from '@bitgo/sdk-core';

describe('Sol Transfer Builder V2', () => {
let ataAddress;
Expand All @@ -20,7 +21,10 @@ describe('Sol Transfer Builder V2', () => {
const owner = testData.tokenTransfers.owner;
const walletPK = testData.associatedTokenAccounts.accounts[0].pub;
const walletSK = testData.associatedTokenAccounts.accounts[0].prv;

const prioFeeMicroLamports = '10000000';
const priorityFee: FeeOptions = {
amount: prioFeeMicroLamports,
};
const transferBuilderV2 = () => {
const txBuilder = factory.getTransferBuilderV2();
txBuilder.nonce(recentBlockHash);
Expand Down Expand Up @@ -287,6 +291,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand All @@ -311,6 +316,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.feePayer(feePayerAccount.pub);
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -341,6 +347,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand All @@ -365,6 +372,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.feePayer(feePayerAccount.pub);
txBuilder.sender(walletPK);
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(1);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -394,6 +402,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.sign({ key: walletSK });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.id.should.not.equal(undefined);
tx.inputs.length.should.equal(1);
Expand Down Expand Up @@ -425,6 +434,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.memo(memo);
txBuilder.createAssociatedTokenAccount({ ownerAddress: otherAccount.pub, tokenName: nameUSDC });
txBuilder.sign({ key: walletSK });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.id.should.not.equal(undefined);
tx.inputs.length.should.equal(1);
Expand Down Expand Up @@ -485,6 +495,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.send({ address: account5.pub, amount, tokenName: nameUSDC });
txBuilder.memo(memo);
txBuilder.sign({ key: authAccount.prv });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(6);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -568,6 +579,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.send({ address: account1.pub, amount, tokenName: nameUSDC });
txBuilder.send({ address: account2.pub, amount, tokenName: nameSRM });
txBuilder.send({ address: account3.pub, amount, tokenName: nameRAY });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(4);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -658,6 +670,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.sender(authAccount.pub);
txBuilder.send({ address: otherAccount.pub, amount });
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(2);
tx.inputs[0].should.deepEqual({
Expand Down Expand Up @@ -694,6 +707,7 @@ describe('Sol Transfer Builder V2', () => {
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.send({ address: otherAccount.pub, amount, tokenName: nameUSDC });
txBuilder.setPriorityFee(priorityFee);
const tx = await txBuilder.build();
tx.inputs.length.should.equal(4);
tx.inputs[0].should.deepEqual({
Expand Down

0 comments on commit cf1678b

Please sign in to comment.