Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Claim issue fix #671

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/hooks/helper/claim.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiPromise } from '@polkadot/api';
import { bool, Option, Struct } from '@polkadot/types';
import { bool, Option, Struct, u64 } from '@polkadot/types';
import { EventRecord, Balance, AccountId } from '@polkadot/types/interfaces';
import { BN } from '@polkadot/util';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -35,9 +35,25 @@ export interface State {
};
}

export interface PayloadWithWeight {
payload: ExtrinsicPayload;
weight: BN;
export class PayloadWithWeight {
public isWeightV2: boolean;

constructor(public payload: ExtrinsicPayload, public weight: BN | WeightV2) {
this.isWeightV2 = 'proofSize' in weight && 'refTime' in weight;
}

public asWeightV1(): BN {
return this.weight as BN;
}

public asWeightV2(): WeightV2 {
return this.weight as WeightV2;
}
}

export interface WeightV2 {
refTime: u64;
proofSize: u64;
}

export const checkIsDappOwner = async ({
Expand Down Expand Up @@ -253,11 +269,11 @@ const getTxsForClaimDapp = async ({

if (era === e) {
const tx = api.tx.dappsStaking.claimDapp(getDappAddressEnum(dappAddress), era);
transactions.push({ payload: tx, weight: info.weight });
transactions.push(new PayloadWithWeight(tx, info.weight));
} else {
// Memo: e -> skip to the era that have been staked after unstaked
const tx = api.tx.dappsStaking.claimDapp(getDappAddressEnum(dappAddress), e);
transactions.push({ payload: tx, weight: info.weight });
transactions.push(new PayloadWithWeight(tx, info.weight));
era = e;
}
}
Expand Down Expand Up @@ -287,15 +303,15 @@ const getTxsForClaimStaker = async ({

for (let i = 0; i < numberOfUnclaimedEra; i++) {
const tx = api.tx.dappsStaking.claimStaker(getDappAddressEnum(dappAddress));
transactions.push({ payload: tx, weight: claimInfo.weight });
transactions.push(new PayloadWithWeight(tx, claimInfo.weight));
}

if (!isRegistered) {
const tx = api.tx.dappsStaking.withdrawFromUnregistered(getDappAddressEnum(dappAddress));
const withdrawalInfo = await api.tx.dappsStaking
.withdrawFromUnregistered(getDappAddressEnum(dappAddress))
.paymentInfo(senderAddress);
transactions.push({ payload: tx, weight: withdrawalInfo.weight });
transactions.push(new PayloadWithWeight(tx, withdrawalInfo.weight));
}

return transactions;
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useClaimAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ export function useClaimAll() {
let totalWeight: BN = new BN(0);
for (let i = 0; i < batchTxsRef.length; i++) {
const tx = batchTxsRef[i];
if (totalWeight.add(tx.weight).gt(MAX_BATCH_WEIGHT)) {
const weight = tx.isWeightV2 ? tx.asWeightV2().refTime.toBn() : tx.asWeightV1();
if (totalWeight.add(weight).gt(MAX_BATCH_WEIGHT)) {
break;
}

txsToExecute.push(tx.payload as ExtrinsicPayload);
totalWeight = totalWeight.add(tx.weight);
totalWeight = totalWeight.add(weight);
}

// The fix causes problems and confusion for stakers because rewards are not restaked,
Expand Down