Skip to content

Commit

Permalink
refactor(solana): implement use of VersionedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-pvl committed Aug 9, 2024
2 parents 934d501 + 125d597 commit b1a0bdb
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 59 deletions.
14 changes: 14 additions & 0 deletions ethereum.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ethereum.d.ts

declare module "@everstake/wallet-sdk/ethereum" {
export const selectNetwork: (
network: "mainnet" | "holesky",
url?: string
) => {
address_accounting: string;
address_pool: string;
contract_pool: any;
contract_accounting: any;
address_withdraw_treasury: string;
};
}
33 changes: 12 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// index.d.ts
declare module '@everstake/wallet-sdk' {
export const Polygon: any;
export const Cosmos: any;
export const Solana: any;
export const Aptos: any;
export const Sui: any;
export const Ethereum: {
selectNetwork: (
network: 'mainnet' | 'holesky',
url?: string,
) => {
address_accounting: string;
address_pool: string;
contract_pool: any;
contract_accounting: any;
address_withdraw_treasury: string;
};
};
export const CreateToken: any;
export const GetAssets: any;
}
import { Ethereum } from "./ethereum";

declare module "@everstake/wallet-sdk" {
export const Polygon: any;
export const Cosmos: any;
export const Solana: any;
export const Aptos: any;
export const Sui: any;
export const Ethereum: Ethereum;
export const CreateToken: any;
export const GetAssets: any;
}
65 changes: 61 additions & 4 deletions solana.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const {
Authorized,
clusterApiUrl,
ComputeBudgetProgram,
Connection,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
StakeProgram
StakeProgram,
Transaction,
TransactionMessage,
VersionedTransaction
} = require('@solana/web3.js');
const bs58 = require('bs58');

const { CheckToken, ERROR_TEXT, SetStats } = require("./utils/api");

Expand Down Expand Up @@ -43,7 +48,8 @@ async function createAccount(address, amount) {
lamports: amountToStake,
stakePubkey: stakeAccount.publicKey,
});

const blockhash = await getBlockhash();
createStakeAccountTx.recentBlockhash = await blockhash
createStakeAccountTx.sign(stakeAccount);

return { result: { createStakeAccountTx, stakeAccount: stakeAccount.publicKey.toString() } };
Expand All @@ -55,6 +61,22 @@ async function createAccount(address, amount) {
}
}

async function prepareTransaction(instructions, payer) {
const blockhash = await getBlockhash();
const messageV0 = new TransactionMessage({
payerKey: payer,
recentBlockhash: blockhash,
instructions
}).compileToV0Message();
return new VersionedTransaction(messageV0);
}

async function getBlockhash(){
return await connection
.getLatestBlockhash({ commitment: 'max' })
.then((res) => res.blockhash);
}

async function delegate(token, address, amount, stakeAccount) {
if (await CheckToken(token)) {
if (+amount >= minAmount) {
Expand Down Expand Up @@ -136,8 +158,8 @@ async function getDelegations(address) {

accounts = await connection.getParsedProgramAccounts(new PublicKey("Stake11111111111111111111111111111111111111"), {
filters: [
{dataSize: 200},
{memcmp: {offset: 44, bytes: address}},
{ dataSize: 200 },
{ memcmp: { offset: 44, bytes: address } },
],
});

Expand All @@ -147,10 +169,45 @@ async function getDelegations(address) {
}
}

async function stake(sender, lamports) {
try {
await connect();
const senderPublicKey = new PublicKey(sender);
const stakeAccount = Keypair.generate();
const validatorPubkey = new PublicKey(VALIDATOR_ADDRESS);

// Calculate how much we want to stake
const minimumRent = await connection.getMinimumBalanceForRentExemption(StakeProgram.space);

const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 50 }),
StakeProgram.createAccount({
authorized: new Authorized(senderPublicKey, senderPublicKey),
fromPubkey: senderPublicKey,
lamports: lamports + minimumRent,
stakePubkey: stakeAccount.publicKey,
}),
StakeProgram.delegate({
stakePubkey: stakeAccount.publicKey,
authorizedPubkey: senderPublicKey,
votePubkey: validatorPubkey,
})
);

let versionedTX = await prepareTransaction(tx.instructions, senderPublicKey);
versionedTX.sign([stakeAccount]);

return versionedTX;
} catch (error) {
throw new Error(error);
}
}

module.exports = {
createAccount,
delegate,
deactivate,
withdraw,
getDelegations,
stake,
};
1 change: 1 addition & 0 deletions src/solana/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const ERROR_MESSAGES = {
DEACTIVATE_ERROR: 'An error occurred while deactivating the stake',
WITHDRAW_ERROR: 'An error occurred while withdrawing the stake',
GET_DELEGATIONS_ERROR: 'An error occurred while fetching the delegations',
STAKE_ERROR: 'An error occurred while staking',
};
Loading

0 comments on commit b1a0bdb

Please sign in to comment.