Skip to content

Commit

Permalink
feat: use pool delegation endpoint for constructing drep tx no matter…
Browse files Browse the repository at this point in the history
… the wallet type
  • Loading branch information
szymonmaslowski committed Dec 12, 2024
1 parent 0b5c7ea commit cd787d7
Showing 1 changed file with 31 additions and 55 deletions.
86 changes: 31 additions & 55 deletions source/renderer/app/stores/VotingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,29 +278,29 @@ export default class VotingStore extends Store {
chosenOption: string;
wallet: Wallet;
}) => {
if (wallet.isHardwareWallet) {
let poolId: string;

if (wallet.isDelegating) {
const { lastDelegatedStakePoolId, delegatedStakePoolId } = wallet;
const currentPoolId = lastDelegatedStakePoolId || delegatedStakePoolId;
poolId = this.stores.staking.stakePools.find(
(stakePool) => stakePool.id !== currentPoolId
).id;
} else {
const [{ id }] = this.stores.staking.stakePools;
poolId = id;
}
let poolId: string;

if (wallet.isDelegating) {
const { lastDelegatedStakePoolId, delegatedStakePoolId } = wallet;
const currentPoolId = lastDelegatedStakePoolId || delegatedStakePoolId;
poolId = this.stores.staking.stakePools.find(
(stakePool) => stakePool.id !== currentPoolId
).id;
} else {
const [{ id }] = this.stores.staking.stakePools;
poolId = id;
}

try {
const initialCoinSelection = await this.stores.hardwareWallets.selectDelegationCoins(
{
walletId: wallet.id,
delegationAction: 'join',
poolId,
}
);
try {
let coinSelection = await this.stores.hardwareWallets.selectDelegationCoins(
{
walletId: wallet.id,
delegationAction: 'join',
poolId,
}
);

if (wallet.isHardwareWallet) {
let certificates: object[] = [
{
certificateType: 'cast_vote',
Expand All @@ -309,7 +309,7 @@ export default class VotingStore extends Store {
},
];

const walletNeedsRegisteringRewardAccount = initialCoinSelection.certificates.some(
const walletNeedsRegisteringRewardAccount = coinSelection.certificates.some(
(c) => c.certificateType === 'register_reward_account'
);
if (walletNeedsRegisteringRewardAccount) {
Expand All @@ -322,52 +322,28 @@ export default class VotingStore extends Store {
];
}

const coinSelection = {
...initialCoinSelection,
coinSelection = {
...coinSelection,
certificates,
};

this.stores.hardwareWallets.updateTxSignRequest(coinSelection);
this.stores.hardwareWallets.initiateTransaction({
walletId: wallet.id,
});

return {
success: true,
fees: coinSelection.fee,
};
} catch (error) {
logger.error(
'VotingStore: error while initializing VP delegation TX with HW',
{
error,
}
);
return {
success: false,
errorCode: parseApiCode(
expectedInitializeVPDelegationTxErrors,
error
),
};
}
}

this.constructTxRequest.reset();
try {
const constructedTx = await this.constructTxRequest.execute({
walletId: wallet.id,
data: { vote: chosenOption },
}).promise;

return {
success: true,
fees: constructedTx.fee,
fees: coinSelection.fee,
};
} catch (error) {
logger.error('VotingStore: error while initializing VP delegation TX', {
error,
});
logger.error(
'VotingStore: error while initializing VP delegation TX with HW',
{
error,
}
);
return {
success: false,
errorCode: parseApiCode(expectedInitializeVPDelegationTxErrors, error),
Expand Down

0 comments on commit cd787d7

Please sign in to comment.