Skip to content

Commit

Permalink
claim and undelegate sequentially (#812)
Browse files Browse the repository at this point in the history
* claim and undelegate sequentially

* tweak logic
  • Loading branch information
bryzettler authored Oct 3, 2024
1 parent 7e39029 commit dc07a23
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/features/governance/PositionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ export const PositionCard = ({
message,
instructions,
sigs = [],
sequentially = false,
}: {
header: string
message: string
instructions: TransactionInstruction[]
sigs?: Keypair[]
sequentially?: boolean
}) => {
if (!anchorProvider || !walletSignBottomSheetRef) return

Expand All @@ -174,33 +176,35 @@ export const PositionCard = ({
basePriorityFee: await getBasePriorityFee(),
},
)

const populatedDrafts = await Promise.all(
transactions.map((tx) =>
populateMissingDraftInfo(anchorProvider.connection, tx),
),
)
const txs = populatedDrafts.map((transaction) => toVersionedTx(transaction))

const asVersionedTx = populatedDrafts.map(toVersionedTx)
const decision = await walletSignBottomSheetRef.show({
type: WalletStandardMessageTypes.signTransaction,
url: '',
header,
renderer: () => <MessagePreview message={message} />,
serializedTxs: txs.map((t) => Buffer.from(t.serialize())),
suppressWarnings: sequentially,
serializedTxs: asVersionedTx.map((transaction) =>
Buffer.from(transaction.serialize()),
),
})

if (decision) {
if (sigs.length) {
if (transactions.length > 1 && sequentially) {
let i = 0
// eslint-disable-next-line no-restricted-syntax
for (const tx of await anchorProvider.wallet.signAllTransactions(txs)) {
// eslint-disable-next-line @typescript-eslint/no-loop-func
for (const tx of await anchorProvider.wallet.signAllTransactions(
asVersionedTx,
)) {
const draft = transactions[i]
sigs.forEach((sig) => {
if (
transactions[i].signers?.some((s) =>
s.publicKey.equals(sig.publicKey),
)
) {
if (draft.signers?.some((s) => s.publicKey.equals(sig.publicKey))) {
tx.sign([sig])
}
})
Expand All @@ -209,7 +213,7 @@ export const PositionCard = ({
anchorProvider.connection,
Buffer.from(tx.serialize()),
{
skipPreflight: true,
skipPreflight: false,
},
'confirmed',
)
Expand Down Expand Up @@ -507,21 +511,15 @@ export const PositionCard = ({
onInstructions: async (ixs) => {
const undelegate = ixs[ixs.length - 1]
const claims = ixs.slice(0, ixs.length - 1)
if (claims.length > 0) {
await decideAndExecute({
header: t('gov.transactions.claimRewards'),
message: t('gov.transactions.claimRewards'),
instructions: claims,
})
}

const hasClaims = claims.length > 0
await decideAndExecute({
header: t('gov.transactions.undelegatePosition'),
message: t('gov.positions.undelegateMessage', {
amount: lockedTokens,
symbol,
}),
instructions: [undelegate],
sequentially: hasClaims,
instructions: hasClaims ? [...claims, undelegate] : [undelegate],
})

if (!undelegatingError) {
Expand Down

0 comments on commit dc07a23

Please sign in to comment.