Skip to content

Commit

Permalink
feat: use only one flow in staking
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Apr 3, 2024
1 parent e111c8c commit 1b62747
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
.nav {
background: transparent;
border: none;
cursor: pointer;
transform: rotate(90deg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
.nav {
background: transparent;
border: none;
cursor: pointer;
transform: rotate(-90deg);
}

Expand Down
113 changes: 42 additions & 71 deletions src/screens/staking/lib/staking_sdk/wallet_operations/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
LAMPORTS_PER_SOL,
PublicKey,
StakeProgram,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import Solflare from "@solflare-wallet/sdk";
Expand Down Expand Up @@ -232,92 +231,64 @@ export const stakeAmountSolana = async ({
const accountKey = new PublicKey(account.address);
const wallet = getWalletApi(account);

const stakeAccount = (account.info?.stakeAccounts || []).find(
(a) => a.validator_address === validatorAddress,
);

if (!stakeAccount) {
const stakeKeyPair = Keypair.generate();

const amountToStake = Number(amount) * LAMPORTS_PER_SOL;
const minimumAmount = minimumStakeAmount[account.networkId] || 0;

if (amountToStake < minimumAmount) {
return {
coin: normaliseCoin({
amount: minimumAmount.toString(),
denom: "LAMPORTS",
}),
error: StakeError.MinimumAmount,
success: false,
};
}
const stakeKeyPair = Keypair.generate();

const newTx = StakeProgram.createAccount({
authorized: new Authorized(accountKey, accountKey),
fromPubkey: accountKey,
lamports: amountToStake,
stakePubkey: stakeKeyPair.publicKey,
});
const amountToStake = Number(amount) * LAMPORTS_PER_SOL;
const minimumAmount = minimumStakeAmount[account.networkId] || 0;

newTx.recentBlockhash = (info as any).blockhash;
newTx.feePayer = accountKey;

newTx.sign(stakeKeyPair);

const stakeAccountResult = await wallet.signAndSendTransaction(newTx);

// eslint-disable-next-line no-console
console.log("debug: solana.ts: result", stakeAccountResult);

await new Promise((resolve) => setTimeout(resolve, 10000));
if (amountToStake < minimumAmount) {
return {
coin: normaliseCoin({
amount: minimumAmount.toString(),
denom: "LAMPORTS",
}),
error: StakeError.MinimumAmount,
success: false,
};
}

const newInfo = await stakingClient.stake(
account.networkId,
account.address,
amount,
);
const newTx = StakeProgram.createAccount({
authorized: new Authorized(accountKey, accountKey),
fromPubkey: accountKey,
lamports: amountToStake,
stakePubkey: stakeKeyPair.publicKey,
});

const newTx2 = StakeProgram.delegate({
authorizedPubkey: accountKey,
stakePubkey: stakeKeyPair.publicKey,
votePubkey: new PublicKey(validatorAddress),
});
newTx.recentBlockhash = (info as any).blockhash;
newTx.feePayer = accountKey;

newTx2.recentBlockhash = (newInfo as any).blockhash;
newTx2.feePayer = accountKey;
newTx.sign(stakeKeyPair);

// newTx2.sign(stakeKeyPair);
const stakeAccountResult = await wallet.signAndSendTransaction(newTx);

const stakeAccountResult2 = await wallet.signAndSendTransaction(newTx2);
// eslint-disable-next-line no-console
console.log("debug: solana.ts: result", stakeAccountResult);

// eslint-disable-next-line no-console
console.log("debug: solana.ts: result", stakeAccountResult2);
await new Promise((resolve) => setTimeout(resolve, 10000));

return { success: true } as const;
}
const newInfo = await stakingClient.stake(
account.networkId,
account.address,
amount,
);

const stakePubKey = new PublicKey(stakeAccount.address);
const newTx2 = StakeProgram.delegate({
authorizedPubkey: accountKey,
stakePubkey: stakeKeyPair.publicKey,
votePubkey: new PublicKey(validatorAddress),
});

const tx = new Transaction().add(
SystemProgram.transfer({
fromPubkey: accountKey,
lamports: LAMPORTS_PER_SOL * Number(amount),
toPubkey: stakePubKey,
}),
);
newTx2.recentBlockhash = (newInfo as any).blockhash;
newTx2.feePayer = accountKey;

tx.recentBlockhash = (info as any).blockhash;
tx.feePayer = accountKey;
// newTx2.sign(stakeKeyPair);

const result = await wallet.signAndSendTransaction(tx);
const stakeAccountResult2 = await wallet.signAndSendTransaction(newTx2);

// eslint-disable-next-line no-console
console.log("debug: solana.ts: result", result);
console.log("debug: solana.ts: result", stakeAccountResult2);

return {
success: true,
} as const;
return { success: true } as const;
})
.catch((error) => {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 1b62747

Please sign in to comment.