Skip to content

Commit

Permalink
feat(suite): add solana unstaking and claiming methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-pvl committed Dec 18, 2024
1 parent a19f35e commit 7e7e26f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {

import { Dispatch, GetState } from 'src/types/suite';
import { selectAddressDisplayType } from 'src/reducers/suite/suiteReducer';
import { getPubKeyFromAddress, prepareStakeSolTx } from 'src/utils/suite/solanaStaking';
import {
getPubKeyFromAddress,
prepareClaimSolTx,
prepareStakeSolTx,
prepareUnstakeSolTx,
} from 'src/utils/suite/solanaStaking';

import { calculate, composeStakingTransaction } from './stakeFormActions';

Expand All @@ -30,6 +35,7 @@ const calculateTransaction = (
compareWithAmount = true,
symbol: NetworkSymbol,
): PrecomposedTransaction => {
// TODO: change to the wallet-sdk fee
const feeInLamports = new BigNumber(feeLevel.feePerTx ?? '0').toString();

const stakingParams = {
Expand Down Expand Up @@ -100,6 +106,25 @@ export const signTransaction =
});
}

if (stakeType === 'unstake') {
txData = await prepareUnstakeSolTx({
from: account.descriptor,
path: account.path,
amount: formValues.outputs[0].amount,
symbol: account.symbol,
selectedBlockchain,
});
}

if (stakeType === 'claim') {
txData = await prepareClaimSolTx({
from: account.descriptor,
path: account.path,
symbol: account.symbol,
selectedBlockchain,
});
}

if (!txData) {
dispatch(
notificationsActions.addToast({
Expand Down
68 changes: 63 additions & 5 deletions packages/suite/src/utils/suite/solanaStaking.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { VersionedTransaction, PublicKey } from '@solana/web3.js-version1';

import { NetworkSymbol } from '@suite-common/wallet-config';
import { LAMPORTS_PER_SOL, WALLET_SDK_SOURCE } from '@suite-common/wallet-constants';
import { selectSolanaWalletSdkNetwork } from '@suite-common/wallet-utils';
import { BigNumber } from '@trezor/utils';
import { WALLET_SDK_SOURCE } from '@suite-common/wallet-constants';
import {
networkAmountToSmallestUnit,
selectSolanaWalletSdkNetwork,
} from '@suite-common/wallet-utils';
import type { SolanaSignTransaction } from '@trezor/connect';
import { Blockchain } from '@suite-common/wallet-types';

Expand Down Expand Up @@ -65,8 +67,64 @@ export const prepareStakeSolTx = async ({
try {
const solanaClient = selectSolanaWalletSdkNetwork(symbol, selectedBlockchain.url);

const lamports = new BigNumber(LAMPORTS_PER_SOL).multipliedBy(amount).toNumber(); // stake method expects lamports as a number
const tx = await solanaClient.stake(from, lamports, WALLET_SDK_SOURCE);
const lamports = networkAmountToSmallestUnit(amount, symbol);
const tx = await solanaClient.stake(from, Number(lamports), WALLET_SDK_SOURCE);
const transformedTx = transformTx(tx.result, path);

return {
success: true,
tx: transformedTx,
};
} catch (e) {
console.error(e);

return {
success: false,
errorMessage: e.message,
};
}
};

export const prepareUnstakeSolTx = async ({
from,
path,
amount,
symbol,
selectedBlockchain,
}: PrepareStakeSolTxParams): Promise<PrepareStakeSolTxResponse> => {
try {
const solanaClient = selectSolanaWalletSdkNetwork(symbol, selectedBlockchain.url);

const lamports = networkAmountToSmallestUnit(amount, symbol);
const tx = await solanaClient.unstake(from, Number(lamports), WALLET_SDK_SOURCE);
const transformedTx = transformTx(tx.result, path);

return {
success: true,
tx: transformedTx,
};
} catch (e) {
console.error(e);

return {
success: false,
errorMessage: e.message,
};
}
};

type PrepareClaimSolTxParams = Omit<PrepareStakeSolTxParams, 'amount'>;

export const prepareClaimSolTx = async ({
from,
path,
symbol,
selectedBlockchain,
}: PrepareClaimSolTxParams): Promise<PrepareStakeSolTxResponse> => {
try {
const solanaClient = selectSolanaWalletSdkNetwork(symbol, selectedBlockchain.url);

const tx = await solanaClient.claim(from);
const transformedTx = transformTx(tx.result, path);

return {
Expand Down
4 changes: 2 additions & 2 deletions suite-common/wallet-core/src/stake/stakeSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { StakeRootState } from './stakeReducer';

export const selectEverstakeData = (
state: StakeRootState,
networkSymbol: NetworkSymbol,
symbol: NetworkSymbol,
endpointType: 'poolStats' | 'validatorsQueue' | 'getAssets',
) => state.wallet.stake?.data?.[networkSymbol]?.[endpointType];
) => state.wallet.stake?.data?.[symbol]?.[endpointType];

export const selectPoolStatsApyData = (state: StakeRootState, symbol?: NetworkSymbol) => {
const { data } = state.wallet.stake ?? {};
Expand Down

0 comments on commit 7e7e26f

Please sign in to comment.