Skip to content

Commit

Permalink
Update history (#508)
Browse files Browse the repository at this point in the history
* update acala staking history

* update
  • Loading branch information
qwer951123 authored Oct 8, 2023
1 parent 356bdc7 commit 9c4364e
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
"typescript": "^5.0.4"
},
"packageManager": "yarn@3.5.0",
"stableVersion": "4.1.9-8"
"stableVersion": "4.1.9-9"
}
188 changes: 179 additions & 9 deletions packages/sdk/src/history/fetchers/earn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,89 @@ interface FetchResult {
};
}

interface EarnFetcherConfigs extends HistoryFetcherConfig {
stakingEndpoint?: string;
}

export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
constructor(configs: HistoryFetcherConfig) {
private stakingEndpoint?: string;

constructor(configs: EarnFetcherConfigs) {
super(configs);

this.stakingEndpoint = configs.stakingEndpoint;
}

async fetchStaking(params: EarnFetchParams): Promise<HistoryRecord[]> {
const variables: Record<string, string> = {
account: params.address
};

const paramsSchema = `$account: String`;
const filterSchema = `filter: { address: { equalTo: $account } }`;

const result = await request<FetchResult>(
this.stakingEndpoint,
gql`
query(${paramsSchema}){
bondeds(
${filterSchema}
first: 20
orderBy: TIMESTAMP_DESC
) {
nodes {
amount
address
timestamp
block
extrinsic
}
}
unbondeds(
${filterSchema}
first: 20
orderBy: TIMESTAMP_DESC
) {
nodes {
amount
address
timestamp
block
extrinsic
}
}
rebondeds(
${filterSchema}
first: 20
orderBy: TIMESTAMP_DESC
) {
nodes {
amount
address
timestamp
block
extrinsic
}
}
withdrawns(
${filterSchema}
first: 20
orderBy: TIMESTAMP_DESC
) {
nodes {
amount
address
timestamp
block
extrinsic
}
}
}
`,
variables
);

return this.transformStaking(result);
}

async fetch(params: EarnFetchParams): Promise<HistoryRecord[]> {
Expand Down Expand Up @@ -129,7 +209,81 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
variables
);

return this.transform(result);
const stakingHistory = await this.fetchStaking(params);

return this.transform(result)
.concat(stakingHistory)
.sort((a, b) => (new Date(b.data.timestamp) < new Date(a.data.timestamp) ? -1 : 1))
.slice(0, 20);
}

private transformStaking(data: any): HistoryRecord[] {
const bonded = data.bondeds.nodes.map((item) => {
return {
data: item,
message: this.createMessage('aca-staking-bonded', item),
resolveLinks: resolveLinks(
getChainType(this.configs.wallet.consts.runtimeChain),
item.extrinsic,
item.blockNumber
),
method: 'earning.Bonded',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});
const unbonded = data.unbondeds.nodes.map((item) => {
return {
data: item,
message: this.createMessage('aca-staking-unbonded', item),
resolveLinks: resolveLinks(
getChainType(this.configs.wallet.consts.runtimeChain),
item.extrinsic,
item.blockNumber
),
method: 'earning.Unbonded',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});
const rebonded = data.rebondeds.nodes.map((item) => {
return {
data: item,
message: this.createMessage('aca-staking-rebonded', item),
resolveLinks: resolveLinks(
getChainType(this.configs.wallet.consts.runtimeChain),
item.extrinsic,
item.blockNumber
),
method: 'earning.Rebonded',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});
const withdrawns = data.withdrawns.nodes.map((item) => {
return {
data: item,
message: this.createMessage('aca-staking-withdrawn', item),
resolveLinks: resolveLinks(
getChainType(this.configs.wallet.consts.runtimeChain),
item.extrinsic,
item.blockNumber
),
method: 'earning.Withdrawn',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});

return bonded
.concat(unbonded)
.concat(rebonded)
.concat(withdrawns)
.sort((a, b) => (new Date(b.data.timestamp) < new Date(a.data.timestamp) ? -1 : 1));
}

private transform(data: FetchResult): HistoryRecord[] {
Expand All @@ -144,7 +298,8 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
),
method: 'incentives.ClaimRewards',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});
const payoutRewards = data.payoutRewards.nodes.map((item) => {
Expand All @@ -158,7 +313,8 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
),
method: 'incentives.PayoutRewards',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});

Expand All @@ -173,7 +329,8 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
),
method: 'incentives.DepositDexShare',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});

Expand All @@ -188,24 +345,25 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
),
method: 'incentives.WithdrawDexShare',
extrinsicHash: item.extrinsic,
blockNumber: item.blockNumber
blockNumber: item.blockNumber,
timestamp: item.timestamp
};
});

return claimRewards
.concat(payoutRewards)
.concat(withdrawDexShares)
.concat(depositDexShares)
.sort((a, b) => (new Date(b.data.timestamp) < new Date(a.data.timestamp) ? -1 : 1))
.slice(0, 20);
.sort((a, b) => (new Date(b.data.timestamp) < new Date(a.data.timestamp) ? -1 : 1));
}

private getPoolId = (pool?: string) => {
return pool?.includes('-') ? this.configs.wallet.getToken(pool.split('-')[1])?.display : pool;
};

private createMessage(type: string, data: Node) {
const token = this.configs.wallet.getToken(forceToCurrencyName(data.tokenId));
const token = data.tokenId ? this.configs.wallet.getToken(forceToCurrencyName(data.tokenId)) : undefined;
const nativeToken = this.configs.wallet.getPresetTokens()?.nativeToken;

if (type === 'claimRewards') {
const amount = FixedPointNumber.fromInner(data.actualAmount || '0', token?.decimals || 12).toNumber(6);
Expand All @@ -221,6 +379,18 @@ export class Earns extends BaseHistoryFetcher<EarnFetchParams> {
} else if (type === 'withdrawDexShares') {
const amount = FixedPointNumber.fromInner(data.amount || '0', token?.decimals || 12).toNumber(6);
return `Remove ${amount} shares from ${token?.display} earn pool`;
} else if (type === 'aca-staking-bonded') {
const amount = FixedPointNumber.fromInner(data.amount || '0', nativeToken?.decimals).toNumber(6);
return `Bond ${amount} ${nativeToken?.display} to ${nativeToken?.display} staking pool`;
} else if (type === 'aca-staking-unbonded') {
const amount = FixedPointNumber.fromInner(data.amount || '0', nativeToken?.decimals).toNumber(6);
return `Unbond ${amount} ${nativeToken?.display} from ${nativeToken?.display} staking pool`;
} else if (type === 'aca-staking-rebonded') {
const amount = FixedPointNumber.fromInner(data.amount || '0', nativeToken?.decimals).toNumber(6);
return `Rebond ${amount} ${nativeToken?.display} to ${nativeToken?.display} staking pool`;
} else if (type === 'aca-staking-withdrawn') {
const amount = FixedPointNumber.fromInner(data.amount || '0', nativeToken?.decimals).toNumber(6);
return `Withdraw ${amount} ${nativeToken?.display} from ${nativeToken?.display} staking pool`;
} else {
return 'parse history data failed';
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class History {

this.earn = new Earns({
endpoint: this.configs.fetchEndpoints.earn,
stakingEndpoint: this.configs.fetchEndpoints.staking,
poolInterval: this.configs.poolInterval,
wallet: this.configs.wallet
});
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/history/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface HistoryRecord {
message: string;
data: Record<string, any>;
extrinsicHash?: string;
blokcNumber?: number;
blockNumber?: string;
timestamp?: string;
resolveLinks: ResolveLinks;
method: string;
}
Expand All @@ -21,6 +22,7 @@ export interface HistoryConfigs {
earn: string;
loan: string;
homa: string;
staking?: string;
};
poolInterval: number;
}
Expand Down

0 comments on commit 9c4364e

Please sign in to comment.