mirror of
https://github.com/pezkuwichain/pezkuwi-subquery.git
synced 2026-04-21 23:37:56 +00:00
Migrate to native @pezkuwi packages and remove swap integrations
Replace npm aliases (@polkadot -> @pezkuwi) with direct @pezkuwi package imports. Add tsconfig path mappings to redirect @polkadot type references from @subql/types to @pezkuwi equivalents, enabling proper type resolution without installing @polkadot packages. Remove Polkadot ecosystem swap/bridge integrations (HydraDx, AssetConversion) as they're incompatible with Pezkuwi.
This commit is contained in:
+11
-9
@@ -19,15 +19,17 @@
|
||||
"author": "Pezkuwi Team",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21",
|
||||
"@polkadot/api": "npm:@pezkuwi/api@^16.5.34",
|
||||
"@polkadot/api-derive": "npm:@pezkuwi/api-derive@^16.5.34",
|
||||
"@polkadot/types": "npm:@pezkuwi/types@^16.5.34",
|
||||
"@polkadot/types-augment": "npm:@pezkuwi/types-augment@^16.5.34",
|
||||
"@polkadot/types-codec": "npm:@pezkuwi/types-codec@^16.5.34",
|
||||
"@polkadot/util": "npm:@pezkuwi/util@^14.0.23",
|
||||
"@polkadot/util-crypto": "npm:@pezkuwi/util-crypto@^14.0.23",
|
||||
"@polkadot/keyring": "npm:@pezkuwi/keyring@^14.0.23"
|
||||
"@pezkuwi/api": "^16.5.36",
|
||||
"@pezkuwi/api-augment": "^16.5.36",
|
||||
"@pezkuwi/api-derive": "^16.5.36",
|
||||
"@pezkuwi/keyring": "^14.0.25",
|
||||
"@pezkuwi/types": "^16.5.36",
|
||||
"@pezkuwi/types-augment": "^16.5.36",
|
||||
"@pezkuwi/types-codec": "^16.5.36",
|
||||
"@pezkuwi/types-create": "^16.5.36",
|
||||
"@pezkuwi/util": "^14.0.25",
|
||||
"@pezkuwi/util-crypto": "^14.0.25",
|
||||
"lodash": "^4.17.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@subql/cli": "latest",
|
||||
|
||||
+1
-2
@@ -4,5 +4,4 @@ export * from "./mappings/Rewards";
|
||||
export * from "./mappings/PoolRewards";
|
||||
export * from "./mappings/Transfers";
|
||||
export * from "./mappings/NewEra";
|
||||
export * from "./mappings/swaps";
|
||||
import "@polkadot/api-augment";
|
||||
import "@pezkuwi/api-augment";
|
||||
|
||||
+16
-31
@@ -1,16 +1,11 @@
|
||||
import "@polkadot/types-augment/lookup";
|
||||
import { SubstrateEvent } from "@subql/types";
|
||||
import { blockNumber } from "./common";
|
||||
import { AccountId } from "@polkadot/types/interfaces";
|
||||
import {
|
||||
PalletStakingRewardDestination,
|
||||
PalletNominationPoolsPoolMember,
|
||||
} from "@polkadot/types/lookup";
|
||||
import { Option } from "@polkadot/types";
|
||||
import { Option } from "@pezkuwi/types";
|
||||
import { RewardDestination, AccountId } from "@pezkuwi/types/interfaces";
|
||||
|
||||
// Due to memory consumption optimization `rewardDestinationByAddress` contains only one key
|
||||
let rewardDestinationByAddress: {
|
||||
[blockId: string]: { [address: string]: PalletStakingRewardDestination };
|
||||
[blockId: string]: { [address: string]: RewardDestination };
|
||||
} = {};
|
||||
let controllersByStash: { [blockId: string]: { [address: string]: string } } =
|
||||
{};
|
||||
@@ -18,13 +13,13 @@ let controllersByStash: { [blockId: string]: { [address: string]: string } } =
|
||||
let parachainStakingRewardEra: { [blockId: string]: number } = {};
|
||||
|
||||
let poolMembers: {
|
||||
[blockId: number]: [string, PalletNominationPoolsPoolMember][];
|
||||
[blockId: number]: [string, any][];
|
||||
} = {};
|
||||
|
||||
export async function cachedRewardDestination(
|
||||
accountAddress: string,
|
||||
event: SubstrateEvent,
|
||||
): Promise<PalletStakingRewardDestination> {
|
||||
): Promise<RewardDestination> {
|
||||
const blockId = blockNumber(event);
|
||||
let cachedBlock = rewardDestinationByAddress[blockId];
|
||||
|
||||
@@ -43,7 +38,7 @@ export async function cachedRewardDestination(
|
||||
});
|
||||
|
||||
let destinationByAddress: {
|
||||
[address: string]: PalletStakingRewardDestination;
|
||||
[address: string]: RewardDestination;
|
||||
} = {};
|
||||
|
||||
let {
|
||||
@@ -58,8 +53,7 @@ export async function cachedRewardDestination(
|
||||
},
|
||||
} = event;
|
||||
let accountAddress = accountId.toString();
|
||||
destinationByAddress[accountAddress] =
|
||||
destination as PalletStakingRewardDestination;
|
||||
destinationByAddress[accountAddress] = destination as unknown as RewardDestination;
|
||||
});
|
||||
} else {
|
||||
const allAccountsInBlock = allEventsInBlock.map((event) => {
|
||||
@@ -74,26 +68,20 @@ export async function cachedRewardDestination(
|
||||
// looks like accountAddress not related to events so just try to query payee directly
|
||||
if (allAccountsInBlock.length === 0) {
|
||||
rewardDestinationByAddress[blockId] = {};
|
||||
return (await api.query.staking.payee(
|
||||
accountAddress,
|
||||
)) as unknown as PalletStakingRewardDestination;
|
||||
return (await api.query.staking.payee(accountAddress)) as unknown as RewardDestination;
|
||||
}
|
||||
|
||||
// TODO: Commented code doesn't work now, may be fixed later
|
||||
// const payees = await api.query.staking.payee.multi(allAccountsInBlock);
|
||||
const payees = await api.queryMulti(
|
||||
allAccountsInBlock.map((account) => [api.query.staking.payee, account]),
|
||||
);
|
||||
|
||||
const rewardDestinations = payees.map((payee) => {
|
||||
return payee as PalletStakingRewardDestination;
|
||||
return payee as unknown as RewardDestination;
|
||||
});
|
||||
|
||||
// something went wrong, so just query for single accountAddress
|
||||
if (rewardDestinations.length !== allAccountsInBlock.length) {
|
||||
const payee = (await api.query.staking.payee(
|
||||
accountAddress,
|
||||
)) as unknown as PalletStakingRewardDestination;
|
||||
const payee = (await api.query.staking.payee(accountAddress)) as unknown as RewardDestination;
|
||||
destinationByAddress[accountAddress] = payee;
|
||||
rewardDestinationByAddress[blockId] = destinationByAddress;
|
||||
return payee;
|
||||
@@ -150,7 +138,7 @@ export async function cachedController(
|
||||
);
|
||||
|
||||
if (rewardDestination.isController) {
|
||||
controllerNeedAccounts.push(accountId as AccountId);
|
||||
controllerNeedAccounts.push(accountId as unknown as AccountId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +149,6 @@ export async function cachedController(
|
||||
return accountId.toString();
|
||||
}
|
||||
|
||||
// TODO: Commented code doesn't work now, may be fixed later
|
||||
// const bonded = await api.query.staking.bonded.multi(controllerNeedAccounts);
|
||||
const bonded = await api.queryMulti(
|
||||
controllerNeedAccounts.map((account) => [
|
||||
api.query.staking.bonded,
|
||||
@@ -207,7 +193,7 @@ export async function cachedStakingRewardEraIndex(
|
||||
api.consts.parachainStaking.rewardPaymentDelay.toHuman();
|
||||
// HACK: used to get data from object
|
||||
const eraIndex =
|
||||
(era.toJSON() as { current: any }).current - Number(paymentDelay);
|
||||
(era.toJSON() as { current: number }).current - Number(paymentDelay);
|
||||
|
||||
parachainStakingRewardEra = {};
|
||||
parachainStakingRewardEra[blockId] = eraIndex;
|
||||
@@ -217,22 +203,21 @@ export async function cachedStakingRewardEraIndex(
|
||||
|
||||
export async function getPoolMembers(
|
||||
blockId: number,
|
||||
): Promise<[string, PalletNominationPoolsPoolMember][]> {
|
||||
): Promise<[string, any][]> {
|
||||
const cachedMembers = poolMembers[blockId];
|
||||
if (cachedMembers != undefined) {
|
||||
return cachedMembers;
|
||||
}
|
||||
|
||||
const members: [string, PalletNominationPoolsPoolMember][] = (
|
||||
const members: [string, any][] = (
|
||||
await api.query.nominationPools.poolMembers.entries()
|
||||
)
|
||||
.filter(
|
||||
([_, member]) =>
|
||||
(member as Option<PalletNominationPoolsPoolMember>).isSome,
|
||||
([_key, member]) => (member as Option<any>).isSome,
|
||||
)
|
||||
.map(([accountId, member]) => [
|
||||
accountId.args[0].toString(),
|
||||
(member as Option<PalletNominationPoolsPoolMember>).unwrap(),
|
||||
(member as Option<any>).unwrap(),
|
||||
]);
|
||||
poolMembers = {};
|
||||
poolMembers[blockId] = members;
|
||||
|
||||
+35
-386
@@ -1,67 +1,22 @@
|
||||
import { SubstrateExtrinsic } from "@subql/types";
|
||||
import { AssetTransfer, HistoryElement, Transfer, Swap } from "../types";
|
||||
import { HistoryElement, Transfer } from "../types";
|
||||
import {
|
||||
getAssetIdFromMultilocation,
|
||||
getEventData,
|
||||
callFromProxy,
|
||||
callsFromBatch,
|
||||
calculateFeeAsString,
|
||||
extrinsicIdFromBlockAndIdx,
|
||||
eventRecordToSubstrateEvent,
|
||||
isBatch,
|
||||
isProxy,
|
||||
timestamp,
|
||||
isNativeTransfer,
|
||||
isAssetTransfer,
|
||||
isOrmlTransfer,
|
||||
isSwapExactTokensForTokens,
|
||||
isSwapTokensForExactTokens,
|
||||
isNativeTransferAll,
|
||||
isOrmlTransferAll,
|
||||
isEvmTransaction,
|
||||
isEvmExecutedEvent,
|
||||
isAssetTxFeePaidEvent,
|
||||
isEquilibriumTransfer,
|
||||
isHydraOmnipoolBuy,
|
||||
isHydraOmnipoolSell,
|
||||
isHydraRouterSell,
|
||||
isHydraRouterBuy,
|
||||
convertOrmlCurrencyIdToString,
|
||||
timestamp,
|
||||
} from "./common";
|
||||
import { CallBase } from "@polkadot/types/types/calls";
|
||||
import { AnyTuple } from "@polkadot/types/types/codec";
|
||||
import { u64 } from "@polkadot/types";
|
||||
import { ethereumEncode } from "@polkadot/util-crypto";
|
||||
import { u128, u32 } from "@polkadot/types-codec";
|
||||
import { convertHydraDxTokenIdToString, findHydraDxFeeTyped } from "./swaps";
|
||||
import { Codec } from "@polkadot/types/types";
|
||||
|
||||
type TransferData = {
|
||||
isTransferAll: boolean;
|
||||
transfer: Transfer | AssetTransfer | Swap;
|
||||
transfer: Transfer;
|
||||
};
|
||||
|
||||
type TransferCallback = (
|
||||
isTransferAll: boolean,
|
||||
address: string,
|
||||
amount: any,
|
||||
assetId?: string
|
||||
) => Array<{ isTransferAll: boolean; transfer: Transfer }>;
|
||||
|
||||
type AssetHubSwapCallback = (
|
||||
path: any,
|
||||
amountId: Codec,
|
||||
amountOut: Codec,
|
||||
receiver: Codec
|
||||
) => Array<{ isTransferAll: boolean; transfer: Swap }>;
|
||||
|
||||
type HydraDxSwapCallback = (
|
||||
assetIn: Codec,
|
||||
assetOut: Codec,
|
||||
amountIn: Codec,
|
||||
amountOut: Codec
|
||||
) => { isTransferAll: boolean; transfer: Swap };
|
||||
|
||||
export async function handleHistoryElement(
|
||||
extrinsic: SubstrateExtrinsic
|
||||
): Promise<void> {
|
||||
@@ -74,11 +29,6 @@ export async function handleHistoryElement(
|
||||
} else {
|
||||
await saveExtrinsic(extrinsic);
|
||||
}
|
||||
} else if (
|
||||
isEvmTransaction(extrinsic.extrinsic.method) &&
|
||||
extrinsic.success
|
||||
) {
|
||||
await saveEvmExtrinsic(extrinsic);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,14 +39,14 @@ function createHistoryElement(
|
||||
hash?: string
|
||||
) {
|
||||
let extrinsicHash = hash || extrinsic.extrinsic.hash.toString();
|
||||
let blockNumber = extrinsic.block.block.header.number.toNumber();
|
||||
let blockNum = extrinsic.block.block.header.number.toNumber();
|
||||
let extrinsicIdx = extrinsic.idx;
|
||||
let extrinsicId = extrinsicIdFromBlockAndIdx(blockNumber, extrinsicIdx);
|
||||
let extrinsicId = extrinsicIdFromBlockAndIdx(blockNum, extrinsicIdx);
|
||||
let blockTimestamp = timestamp(extrinsic.block);
|
||||
|
||||
const historyElement = HistoryElement.create({
|
||||
id: `${extrinsicId}${suffix}`,
|
||||
blockNumber,
|
||||
blockNumber: blockNum,
|
||||
timestamp: blockTimestamp,
|
||||
address,
|
||||
});
|
||||
@@ -107,35 +57,17 @@ function createHistoryElement(
|
||||
return historyElement;
|
||||
}
|
||||
|
||||
function addTransferToHistoryElement(
|
||||
element: HistoryElement,
|
||||
transfer: Transfer | AssetTransfer | Swap
|
||||
) {
|
||||
if ("assetIdIn" in transfer) {
|
||||
element.swap = transfer;
|
||||
} else if ("assetId" in transfer) {
|
||||
element.assetTransfer = transfer;
|
||||
} else {
|
||||
element.transfer = transfer;
|
||||
}
|
||||
}
|
||||
|
||||
async function saveFailedTransfers(
|
||||
transfers: Array<TransferData>,
|
||||
extrinsic: SubstrateExtrinsic
|
||||
): Promise<void> {
|
||||
for (const { isTransferAll, transfer } of transfers) {
|
||||
const isSwap = "assetIdIn" in transfer;
|
||||
const from = isSwap ? transfer.sender : transfer.from;
|
||||
const to = isSwap ? transfer.receiver : transfer.to;
|
||||
const elementFrom = createHistoryElement(extrinsic, from, `-from`);
|
||||
addTransferToHistoryElement(elementFrom, transfer);
|
||||
|
||||
// FIXME: Try to find more appropriate way to handle failed transferAll events
|
||||
if ((!isTransferAll && !isSwap) || from.toString() != to.toString()) {
|
||||
const elementTo = createHistoryElement(extrinsic, to, `-to`);
|
||||
addTransferToHistoryElement(elementTo, transfer);
|
||||
const elementFrom = createHistoryElement(extrinsic, transfer.from, `-from`);
|
||||
elementFrom.transfer = transfer;
|
||||
|
||||
if (!isTransferAll || transfer.from !== transfer.to) {
|
||||
const elementTo = createHistoryElement(extrinsic, transfer.to, `-to`);
|
||||
elementTo.transfer = transfer;
|
||||
await elementTo.save();
|
||||
}
|
||||
|
||||
@@ -160,35 +92,6 @@ async function saveExtrinsic(extrinsic: SubstrateExtrinsic): Promise<void> {
|
||||
await element.save();
|
||||
}
|
||||
|
||||
async function saveEvmExtrinsic(extrinsic: SubstrateExtrinsic): Promise<void> {
|
||||
const executedEvent = extrinsic.events.find(isEvmExecutedEvent);
|
||||
if (!executedEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
const addressFrom = ethereumEncode(executedEvent.event.data?.[0]?.toString());
|
||||
const hash = executedEvent.event.data?.[2]?.toString();
|
||||
const success = !!(executedEvent.event.data?.[3].toJSON() as any).succeed;
|
||||
|
||||
const element = createHistoryElement(
|
||||
extrinsic,
|
||||
addressFrom,
|
||||
"-extrinsic",
|
||||
hash
|
||||
);
|
||||
|
||||
element.extrinsic = {
|
||||
hash,
|
||||
module: extrinsic.extrinsic.method.section,
|
||||
call: extrinsic.extrinsic.method.method,
|
||||
success,
|
||||
fee: calculateFeeAsString(extrinsic, addressFrom),
|
||||
};
|
||||
|
||||
await element.save();
|
||||
}
|
||||
|
||||
/// Success Transfer emits Transfer event that is handled at Transfers.ts handleTransfer()
|
||||
function findFailedTransferCalls(
|
||||
extrinsic: SubstrateExtrinsic
|
||||
): Array<TransferData> | null {
|
||||
@@ -197,121 +100,30 @@ function findFailedTransferCalls(
|
||||
}
|
||||
|
||||
let sender = extrinsic.extrinsic.signer;
|
||||
const transferCallback: TransferCallback = (
|
||||
isTransferAll,
|
||||
address,
|
||||
amount,
|
||||
assetId?
|
||||
) => {
|
||||
const transfer: Transfer = {
|
||||
amount: amount.toString(),
|
||||
from: sender.toString(),
|
||||
to: address,
|
||||
fee: calculateFeeAsString(extrinsic),
|
||||
eventIdx: -1,
|
||||
success: false,
|
||||
};
|
||||
|
||||
if (assetId) {
|
||||
(transfer as AssetTransfer).assetId = assetId;
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
isTransferAll,
|
||||
transfer,
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const assetHubSwapCallback: AssetHubSwapCallback = (
|
||||
path,
|
||||
amountIn,
|
||||
amountOut,
|
||||
receiver
|
||||
) => {
|
||||
let assetIdFee = "native";
|
||||
let fee = calculateFeeAsString(extrinsic);
|
||||
let foundAssetTxFeePaid = extrinsic.block.events.find((e) =>
|
||||
isAssetTxFeePaidEvent(eventRecordToSubstrateEvent(e))
|
||||
);
|
||||
if (foundAssetTxFeePaid !== undefined) {
|
||||
const [who, actual_fee, tip, rawAssetIdFee] = getEventData(
|
||||
eventRecordToSubstrateEvent(foundAssetTxFeePaid)
|
||||
);
|
||||
if ("interior" in rawAssetIdFee) {
|
||||
assetIdFee = getAssetIdFromMultilocation(rawAssetIdFee);
|
||||
fee = actual_fee.toString();
|
||||
}
|
||||
}
|
||||
|
||||
const assetIdIn = getAssetIdFromMultilocation(path[0], true);
|
||||
const assetIdOut = getAssetIdFromMultilocation(
|
||||
path[path["length"] - 1],
|
||||
true
|
||||
);
|
||||
|
||||
if (assetIdIn === undefined || assetIdOut === undefined) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const swap: Swap = {
|
||||
assetIdIn: assetIdIn,
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: assetIdOut,
|
||||
amountOut: amountOut.toString(),
|
||||
sender: sender.toString(),
|
||||
receiver: receiver.toString(),
|
||||
assetIdFee: assetIdFee,
|
||||
fee: fee,
|
||||
eventIdx: -1,
|
||||
success: false,
|
||||
};
|
||||
|
||||
return [
|
||||
{
|
||||
isTransferAll: false,
|
||||
transfer: swap,
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const hydraDxSwapCallback: HydraDxSwapCallback = (
|
||||
assetIn: Codec,
|
||||
assetOut: Codec,
|
||||
amountIn: Codec,
|
||||
amountOut: Codec
|
||||
) => {
|
||||
let fee = findHydraDxFeeTyped(extrinsic.events);
|
||||
|
||||
const assetIdIn = convertHydraDxTokenIdToString(assetIn);
|
||||
const assetIdOut = convertHydraDxTokenIdToString(assetOut);
|
||||
|
||||
const swap: Swap = {
|
||||
assetIdIn: assetIdIn,
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: assetIdOut,
|
||||
amountOut: amountOut.toString(),
|
||||
sender: sender.toString(),
|
||||
receiver: sender.toString(),
|
||||
assetIdFee: fee.tokenId,
|
||||
fee: fee.amount,
|
||||
eventIdx: -1,
|
||||
success: false,
|
||||
};
|
||||
|
||||
const createTransfer = (
|
||||
isTransferAll: boolean,
|
||||
address: string,
|
||||
amount: bigint
|
||||
): TransferData => {
|
||||
return {
|
||||
isTransferAll: false,
|
||||
transfer: swap,
|
||||
isTransferAll,
|
||||
transfer: {
|
||||
amount: amount.toString(),
|
||||
from: sender.toString(),
|
||||
to: address,
|
||||
fee: calculateFeeAsString(extrinsic),
|
||||
eventIdx: -1,
|
||||
success: false,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
let transferCalls = determineTransferCallsArgs(
|
||||
extrinsic.extrinsic.method,
|
||||
transferCallback,
|
||||
assetHubSwapCallback,
|
||||
hydraDxSwapCallback
|
||||
createTransfer
|
||||
);
|
||||
|
||||
if (transferCalls.length == 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -320,186 +132,23 @@ function findFailedTransferCalls(
|
||||
}
|
||||
|
||||
function determineTransferCallsArgs(
|
||||
causeCall: CallBase<AnyTuple>,
|
||||
transferCallback: TransferCallback,
|
||||
assetHubSwapCallback: AssetHubSwapCallback,
|
||||
hydraDxSwapCallback: HydraDxSwapCallback
|
||||
causeCall: any,
|
||||
createTransfer: (isTransferAll: boolean, address: string, amount: bigint) => TransferData
|
||||
): Array<TransferData> {
|
||||
if (isNativeTransfer(causeCall)) {
|
||||
return transferCallback(false, ...extractArgsFromTransfer(causeCall));
|
||||
} else if (isAssetTransfer(causeCall)) {
|
||||
return transferCallback(false, ...extractArgsFromAssetTransfer(causeCall));
|
||||
} else if (isOrmlTransfer(causeCall)) {
|
||||
return transferCallback(false, ...extractArgsFromOrmlTransfer(causeCall));
|
||||
} else if (isEquilibriumTransfer(causeCall)) {
|
||||
return transferCallback(
|
||||
false,
|
||||
...extractArgsFromEquilibriumTransfer(causeCall)
|
||||
);
|
||||
const [destinationAddress, amount] = causeCall.args;
|
||||
return [createTransfer(false, destinationAddress.toString(), (amount as any).toBigInt())];
|
||||
} else if (isNativeTransferAll(causeCall)) {
|
||||
return transferCallback(true, ...extractArgsFromTransferAll(causeCall));
|
||||
} else if (isOrmlTransferAll(causeCall)) {
|
||||
return transferCallback(true, ...extractArgsFromOrmlTransferAll(causeCall));
|
||||
} else if (isSwapExactTokensForTokens(causeCall)) {
|
||||
return assetHubSwapCallback(
|
||||
...extractArgsFromSwapExactTokensForTokens(causeCall)
|
||||
);
|
||||
} else if (isSwapTokensForExactTokens(causeCall)) {
|
||||
return assetHubSwapCallback(
|
||||
...extractArgsFromSwapTokensForExactTokens(causeCall)
|
||||
);
|
||||
} else if (isHydraOmnipoolBuy(causeCall)) {
|
||||
return [hydraDxSwapCallback(...extractArgsFromHydraOmnipoolBuy(causeCall))];
|
||||
} else if (isHydraOmnipoolSell(causeCall)) {
|
||||
return [
|
||||
hydraDxSwapCallback(...extractArgsFromHydraOmnipoolSell(causeCall)),
|
||||
];
|
||||
} else if (isHydraRouterBuy(causeCall)) {
|
||||
return [hydraDxSwapCallback(...extractArgsFromHydraRouterBuy(causeCall))];
|
||||
} else if (isHydraRouterSell(causeCall)) {
|
||||
return [hydraDxSwapCallback(...extractArgsFromHydraRouterSell(causeCall))];
|
||||
const [destinationAddress] = causeCall.args;
|
||||
return [createTransfer(true, destinationAddress.toString(), BigInt(0))];
|
||||
} else if (isBatch(causeCall)) {
|
||||
return callsFromBatch(causeCall)
|
||||
.map((call) => {
|
||||
return determineTransferCallsArgs(
|
||||
call,
|
||||
transferCallback,
|
||||
assetHubSwapCallback,
|
||||
hydraDxSwapCallback
|
||||
).map((value, index, array) => {
|
||||
return value;
|
||||
});
|
||||
})
|
||||
.map((call: any) => determineTransferCallsArgs(call, createTransfer))
|
||||
.flat();
|
||||
} else if (isProxy(causeCall)) {
|
||||
let proxyCall = callFromProxy(causeCall);
|
||||
return determineTransferCallsArgs(
|
||||
proxyCall,
|
||||
transferCallback,
|
||||
assetHubSwapCallback,
|
||||
hydraDxSwapCallback
|
||||
);
|
||||
return determineTransferCallsArgs(proxyCall, createTransfer);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function extractArgsFromTransfer(call: CallBase<AnyTuple>): [string, bigint] {
|
||||
const [destinationAddress, amount] = call.args;
|
||||
|
||||
return [destinationAddress.toString(), (amount as u64).toBigInt()];
|
||||
}
|
||||
|
||||
function extractArgsFromAssetTransfer(
|
||||
call: CallBase<AnyTuple>
|
||||
): [string, bigint, string] {
|
||||
const [assetId, destinationAddress, amount] = call.args;
|
||||
|
||||
return [
|
||||
destinationAddress.toString(),
|
||||
(amount as u64).toBigInt(),
|
||||
assetId.toString(),
|
||||
];
|
||||
}
|
||||
|
||||
function extractArgsFromOrmlTransfer(
|
||||
call: CallBase<AnyTuple>
|
||||
): [string, bigint, string] {
|
||||
const [destinationAddress, currencyId, amount] = call.args;
|
||||
|
||||
return [
|
||||
destinationAddress.toString(),
|
||||
(amount as u64).toBigInt(),
|
||||
currencyId.toHex().toString(),
|
||||
];
|
||||
}
|
||||
|
||||
function extractArgsFromEquilibriumTransfer(
|
||||
call: CallBase<AnyTuple>
|
||||
): [string, bigint, string] {
|
||||
const [assetId, destinationAddress, amount] = call.args;
|
||||
|
||||
return [
|
||||
destinationAddress.toString(),
|
||||
(amount as u64).toBigInt(),
|
||||
assetId.toString(),
|
||||
];
|
||||
}
|
||||
|
||||
function extractArgsFromTransferAll(
|
||||
call: CallBase<AnyTuple>
|
||||
): [string, bigint] {
|
||||
const [destinationAddress] = call.args;
|
||||
|
||||
return [destinationAddress.toString(), BigInt(0)];
|
||||
}
|
||||
|
||||
function extractArgsFromOrmlTransferAll(
|
||||
call: CallBase<AnyTuple>
|
||||
): [string, bigint, string] {
|
||||
const [destinationAddress, currencyId] = call.args;
|
||||
|
||||
return [
|
||||
destinationAddress.toString(),
|
||||
BigInt(0),
|
||||
convertOrmlCurrencyIdToString(currencyId),
|
||||
];
|
||||
}
|
||||
|
||||
function extractArgsFromSwapExactTokensForTokens(
|
||||
call: CallBase<AnyTuple>
|
||||
): [any, Codec, Codec, Codec] {
|
||||
const [path, amountIn, amountOut, receiver, _] = call.args;
|
||||
|
||||
return [path, amountIn, amountOut, receiver];
|
||||
}
|
||||
|
||||
function extractArgsFromSwapTokensForExactTokens(
|
||||
call: CallBase<AnyTuple>
|
||||
): [any, Codec, Codec, Codec] {
|
||||
const [path, amountOut, amountIn, receiver, _] = call.args;
|
||||
|
||||
return [path, amountIn, amountOut, receiver];
|
||||
}
|
||||
|
||||
function extractArgsFromHydraRouterSell(
|
||||
call: CallBase<AnyTuple>
|
||||
): [Codec, Codec, Codec, Codec] {
|
||||
const [assetIn, assetOut, amountIn, minAmountOut, _] = call.args;
|
||||
|
||||
return [assetIn, assetOut, amountIn, minAmountOut];
|
||||
}
|
||||
|
||||
function extractArgsFromHydraRouterBuy(
|
||||
call: CallBase<AnyTuple>
|
||||
): [Codec, Codec, Codec, Codec] {
|
||||
const [assetIn, assetOut, amountOut, maxAmountIn, _] = call.args;
|
||||
|
||||
return [assetIn, assetOut, maxAmountIn, amountOut];
|
||||
}
|
||||
|
||||
function extractArgsFromHydraOmnipoolSell(
|
||||
call: CallBase<AnyTuple>
|
||||
): [Codec, Codec, Codec, Codec] {
|
||||
const [assetIn, assetOut, amount, minBuyAmount, _] = call.args;
|
||||
|
||||
return [
|
||||
assetIn,
|
||||
assetOut,
|
||||
amount, // amountIn
|
||||
minBuyAmount, // amountOut
|
||||
];
|
||||
}
|
||||
|
||||
function extractArgsFromHydraOmnipoolBuy(
|
||||
call: CallBase<AnyTuple>
|
||||
): [Codec, Codec, Codec, Codec] {
|
||||
const [assetOut, assetIn, amount, maxSellAmount, _] = call.args;
|
||||
|
||||
return [
|
||||
assetIn,
|
||||
assetOut,
|
||||
maxSellAmount, // amountIn
|
||||
amount, // amountOut
|
||||
];
|
||||
}
|
||||
|
||||
+9
-18
@@ -2,13 +2,8 @@ import { SubstrateEvent } from "@subql/types";
|
||||
import { eventId } from "./common";
|
||||
import { EraValidatorInfo } from "../types/models/EraValidatorInfo";
|
||||
import { IndividualExposure } from "../types";
|
||||
import {
|
||||
SpStakingPagedExposureMetadata,
|
||||
SpStakingExposurePage,
|
||||
} from "@polkadot/types/lookup";
|
||||
import { Option } from "@polkadot/types";
|
||||
import { INumber } from "@polkadot/types-codec/types/interfaces";
|
||||
import { Exposure } from "@polkadot/types/interfaces";
|
||||
import { Option } from "@pezkuwi/types";
|
||||
import { Exposure } from "@pezkuwi/types/interfaces/staking";
|
||||
|
||||
export async function handleStakersElected(
|
||||
event: SubstrateEvent,
|
||||
@@ -17,7 +12,7 @@ export async function handleStakersElected(
|
||||
}
|
||||
|
||||
export async function handleNewEra(event: SubstrateEvent): Promise<void> {
|
||||
const currentEra = ((await api.query.staking.currentEra()) as Option<INumber>)
|
||||
const currentEra = ((await api.query.staking.currentEra()) as Option<any>)
|
||||
.unwrap()
|
||||
.toNumber();
|
||||
|
||||
@@ -65,19 +60,17 @@ async function processEraStakersPaged(
|
||||
const pages = await api.query.staking.erasStakersPaged.entries(currentEra);
|
||||
|
||||
interface AccumulatorType {
|
||||
[key: string]: any;
|
||||
[key: string]: { [page: number]: IndividualExposure[] };
|
||||
}
|
||||
|
||||
const othersCounted = pages.reduce(
|
||||
(accumulator: AccumulatorType, [key, exp]) => {
|
||||
const exposure = (
|
||||
exp as unknown as Option<SpStakingExposurePage>
|
||||
).unwrap();
|
||||
const exposure = (exp as Option<any>).unwrap();
|
||||
const [, validatorId, pageId] = key.args;
|
||||
const pageNumber = (pageId as INumber).toNumber();
|
||||
const pageNumber = (pageId as any).toNumber();
|
||||
const validatorIdString = validatorId.toString();
|
||||
|
||||
const others = exposure.others.map(({ who, value }) => {
|
||||
const others: IndividualExposure[] = exposure.others.map(({ who, value }: any) => {
|
||||
return {
|
||||
who: who.toString(),
|
||||
value: value.toString(),
|
||||
@@ -93,13 +86,11 @@ async function processEraStakersPaged(
|
||||
);
|
||||
|
||||
for (const [key, exp] of overview) {
|
||||
const exposure = (
|
||||
exp as unknown as Option<SpStakingPagedExposureMetadata>
|
||||
).unwrap();
|
||||
const exposure = (exp as Option<any>).unwrap();
|
||||
const [, validatorId] = key.args;
|
||||
let validatorIdString = validatorId.toString();
|
||||
|
||||
let others = [];
|
||||
let others: IndividualExposure[] = [];
|
||||
for (let i = 0; i < exposure.pageCount.toNumber(); ++i) {
|
||||
others.push(...othersCounted[validatorIdString][i]);
|
||||
}
|
||||
|
||||
+26
-40
@@ -12,25 +12,15 @@ import {
|
||||
eventIdWithAddress,
|
||||
blockNumber,
|
||||
} from "./common";
|
||||
import { Codec } from "@polkadot/types/types";
|
||||
import { u32 } from "@polkadot/types-codec";
|
||||
import { INumber } from "@polkadot/types-codec/types/interfaces";
|
||||
import {
|
||||
PalletNominationPoolsBondedPoolInner,
|
||||
PalletNominationPoolsPoolMember,
|
||||
PalletNominationPoolsSubPools,
|
||||
} from "@polkadot/types/lookup";
|
||||
import {
|
||||
handleGenericForTxHistory,
|
||||
updateAccumulatedGenericReward,
|
||||
} from "./Rewards";
|
||||
import { getPoolMembers } from "./Cache";
|
||||
import { Option } from "@polkadot/types";
|
||||
import { Option } from "@pezkuwi/types";
|
||||
|
||||
export async function handlePoolReward(
|
||||
rewardEvent: SubstrateEvent<
|
||||
[accountId: Codec, poolId: INumber, reward: INumber]
|
||||
>,
|
||||
rewardEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
await handlePoolRewardForTxHistory(rewardEvent);
|
||||
let accumulatedReward = await updateAccumulatedPoolReward(rewardEvent, true);
|
||||
@@ -42,17 +32,15 @@ export async function handlePoolReward(
|
||||
await updateAccountPoolRewards(
|
||||
rewardEvent,
|
||||
accountId.toString(),
|
||||
amount.toBigInt(),
|
||||
poolId.toNumber(),
|
||||
(amount as any).toBigInt(),
|
||||
(poolId as any).toNumber(),
|
||||
RewardType.reward,
|
||||
accumulatedReward.amount,
|
||||
);
|
||||
}
|
||||
|
||||
async function handlePoolRewardForTxHistory(
|
||||
rewardEvent: SubstrateEvent<
|
||||
[accountId: Codec, poolId: INumber, reward: INumber]
|
||||
>,
|
||||
rewardEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
const {
|
||||
event: {
|
||||
@@ -67,7 +55,7 @@ async function handlePoolRewardForTxHistory(
|
||||
eventIdx: rewardEvent.idx,
|
||||
amount: amount.toString(),
|
||||
isReward: true,
|
||||
poolId: poolId.toNumber(),
|
||||
poolId: (poolId as any).toNumber(),
|
||||
};
|
||||
return element;
|
||||
},
|
||||
@@ -75,7 +63,7 @@ async function handlePoolRewardForTxHistory(
|
||||
}
|
||||
|
||||
async function updateAccumulatedPoolReward(
|
||||
event: SubstrateEvent<[accountId: Codec, poolId: INumber, reward: INumber]>,
|
||||
event: SubstrateEvent,
|
||||
isReward: boolean,
|
||||
): Promise<AccumulatedReward> {
|
||||
let {
|
||||
@@ -86,7 +74,7 @@ async function updateAccumulatedPoolReward(
|
||||
return await updateAccumulatedGenericReward(
|
||||
AccumulatedPoolReward,
|
||||
accountId.toString(),
|
||||
amount.toBigInt(),
|
||||
(amount as any).toBigInt(),
|
||||
isReward,
|
||||
);
|
||||
}
|
||||
@@ -114,62 +102,60 @@ async function updateAccountPoolRewards(
|
||||
}
|
||||
|
||||
export async function handlePoolBondedSlash(
|
||||
bondedSlashEvent: SubstrateEvent<[poolId: INumber, slash: INumber]>,
|
||||
bondedSlashEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
const {
|
||||
event: {
|
||||
data: [poolIdEncoded, slash],
|
||||
},
|
||||
} = bondedSlashEvent;
|
||||
const poolId = poolIdEncoded.toNumber();
|
||||
const poolId = (poolIdEncoded as any).toNumber();
|
||||
|
||||
const poolOption = (await api.query.nominationPools.bondedPools(
|
||||
poolId,
|
||||
)) as Option<PalletNominationPoolsBondedPoolInner>;
|
||||
)) as Option<any>;
|
||||
const pool = poolOption.unwrap();
|
||||
|
||||
await handleRelaychainPooledStakingSlash(
|
||||
bondedSlashEvent,
|
||||
poolId,
|
||||
pool.points.toBigInt(),
|
||||
slash.toBigInt(),
|
||||
(member: PalletNominationPoolsPoolMember): bigint => {
|
||||
(slash as any).toBigInt(),
|
||||
(member: any): bigint => {
|
||||
return member.points.toBigInt();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export async function handlePoolUnbondingSlash(
|
||||
unbondingSlashEvent: SubstrateEvent<
|
||||
[poolId: INumber, era: INumber, slash: INumber]
|
||||
>,
|
||||
unbondingSlashEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
const {
|
||||
event: {
|
||||
data: [poolId, era, slash],
|
||||
},
|
||||
} = unbondingSlashEvent;
|
||||
const poolIdNumber = poolId.toNumber();
|
||||
const eraIdNumber = era.toNumber();
|
||||
const poolIdNumber = (poolId as any).toNumber();
|
||||
const eraIdNumber = (era as any).toNumber();
|
||||
|
||||
const unbondingPools = (
|
||||
(await api.query.nominationPools.subPoolsStorage(
|
||||
poolIdNumber,
|
||||
)) as Option<PalletNominationPoolsSubPools>
|
||||
)) as Option<any>
|
||||
).unwrap();
|
||||
|
||||
const pool =
|
||||
unbondingPools.withEra.get(eraIdNumber as unknown as u32) ??
|
||||
(unbondingPools.withEra as any).get(eraIdNumber) ??
|
||||
unbondingPools.noEra;
|
||||
|
||||
await handleRelaychainPooledStakingSlash(
|
||||
unbondingSlashEvent,
|
||||
poolIdNumber,
|
||||
pool.points.toBigInt(),
|
||||
slash.toBigInt(),
|
||||
(member: PalletNominationPoolsPoolMember): bigint => {
|
||||
(slash as any).toBigInt(),
|
||||
(member: any): bigint => {
|
||||
return (
|
||||
member.unbondingEras.get(eraIdNumber as unknown as u32)?.toBigInt() ??
|
||||
((member.unbondingEras as any).get(eraIdNumber))?.toBigInt() ??
|
||||
BigInt(0)
|
||||
);
|
||||
},
|
||||
@@ -181,7 +167,7 @@ async function handleRelaychainPooledStakingSlash(
|
||||
poolId: number,
|
||||
poolPoints: bigint,
|
||||
slash: bigint,
|
||||
memberPointsCounter: (member: PalletNominationPoolsPoolMember) => bigint,
|
||||
memberPointsCounter: (member: any) => bigint,
|
||||
): Promise<void> {
|
||||
if (poolPoints == BigInt(0)) {
|
||||
return;
|
||||
@@ -229,16 +215,16 @@ async function handlePoolSlashForTxHistory(
|
||||
): Promise<void> {
|
||||
const extrinsic = slashEvent.extrinsic;
|
||||
const block = slashEvent.block;
|
||||
const blockNumber = block.block.header.number.toString();
|
||||
const blockNum = block.block.header.number.toString();
|
||||
const blockTimestamp = timestamp(block);
|
||||
const eventId = eventIdFromBlockAndIdxAndAddress(
|
||||
blockNumber,
|
||||
const evtId = eventIdFromBlockAndIdxAndAddress(
|
||||
blockNum,
|
||||
slashEvent.idx.toString(),
|
||||
accountId,
|
||||
);
|
||||
|
||||
const element = HistoryElement.create({
|
||||
id: eventId,
|
||||
id: evtId,
|
||||
timestamp: blockTimestamp,
|
||||
blockNumber: block.block.header.number.toNumber(),
|
||||
extrinsicHash:
|
||||
|
||||
+28
-123
@@ -21,64 +21,50 @@ import {
|
||||
callFromProxy,
|
||||
blockNumber,
|
||||
} from "./common";
|
||||
import { CallBase } from "@polkadot/types/types/calls";
|
||||
import { AnyTuple } from "@polkadot/types/types/codec";
|
||||
import { EraIndex } from "@polkadot/types/interfaces/staking";
|
||||
import { Balance, EventRecord } from "@polkadot/types/interfaces";
|
||||
import {
|
||||
cachedRewardDestination,
|
||||
cachedController,
|
||||
cachedStakingRewardEraIndex,
|
||||
} from "./Cache";
|
||||
import { Codec } from "@polkadot/types/types";
|
||||
import { INumber } from "@polkadot/types-codec/types/interfaces";
|
||||
|
||||
function isPayoutStakers(call: CallBase<AnyTuple>): boolean {
|
||||
function isPayoutStakers(call: any): boolean {
|
||||
return call.method == "payoutStakers";
|
||||
}
|
||||
|
||||
function isPayoutStakersByPage(call: CallBase<AnyTuple>): boolean {
|
||||
function isPayoutStakersByPage(call: any): boolean {
|
||||
return call.method == "payoutStakersByPage";
|
||||
}
|
||||
|
||||
function isPayoutValidator(call: CallBase<AnyTuple>): boolean {
|
||||
function isPayoutValidator(call: any): boolean {
|
||||
return call.method == "payoutValidator";
|
||||
}
|
||||
|
||||
function extractArgsFromPayoutStakers(
|
||||
call: CallBase<AnyTuple>,
|
||||
): [string, number] {
|
||||
function extractArgsFromPayoutStakers(call: any): [string, number] {
|
||||
const [validatorAddressRaw, eraRaw] = call.args;
|
||||
|
||||
return [validatorAddressRaw.toString(), (eraRaw as EraIndex).toNumber()];
|
||||
return [validatorAddressRaw.toString(), (eraRaw as any).toNumber()];
|
||||
}
|
||||
|
||||
function extractArgsFromPayoutStakersByPage(
|
||||
call: CallBase<AnyTuple>,
|
||||
): [string, number] {
|
||||
function extractArgsFromPayoutStakersByPage(call: any): [string, number] {
|
||||
const [validatorAddressRaw, eraRaw, _] = call.args;
|
||||
|
||||
return [validatorAddressRaw.toString(), (eraRaw as EraIndex).toNumber()];
|
||||
return [validatorAddressRaw.toString(), (eraRaw as any).toNumber()];
|
||||
}
|
||||
|
||||
function extractArgsFromPayoutValidator(
|
||||
call: CallBase<AnyTuple>,
|
||||
call: any,
|
||||
sender: string,
|
||||
): [string, number] {
|
||||
const [eraRaw] = call.args;
|
||||
|
||||
return [sender, (eraRaw as EraIndex).toNumber()];
|
||||
return [sender, (eraRaw as any).toNumber()];
|
||||
}
|
||||
|
||||
export async function handleRewarded(
|
||||
rewardEvent: SubstrateEvent<[accountId: Codec, reward: INumber]>,
|
||||
): Promise<void> {
|
||||
export async function handleRewarded(rewardEvent: SubstrateEvent): Promise<void> {
|
||||
await handleReward(rewardEvent);
|
||||
}
|
||||
|
||||
export async function handleReward(
|
||||
rewardEvent: SubstrateEvent<[accountId: Codec, reward: INumber]>,
|
||||
): Promise<void> {
|
||||
export async function handleReward(rewardEvent: SubstrateEvent): Promise<void> {
|
||||
await handleRewardForTxHistory(rewardEvent);
|
||||
let accumulatedReward = await updateAccumulatedReward(rewardEvent, true);
|
||||
await updateAccountRewards(
|
||||
@@ -86,22 +72,6 @@ export async function handleReward(
|
||||
RewardType.reward,
|
||||
accumulatedReward.amount,
|
||||
);
|
||||
// let rewardEventId = eventId(rewardEvent)
|
||||
// try {
|
||||
// let errorOccursOnEvent = await ErrorEvent.get(rewardEventId)
|
||||
// if (errorOccursOnEvent !== undefined) {
|
||||
// logger.info(`Skip rewardEvent: ${rewardEventId}`)
|
||||
// return;
|
||||
// }
|
||||
|
||||
// await handleRewardForTxHistory(rewardEvent)
|
||||
// await updateAccumulatedReward(rewardEvent, true)
|
||||
// } catch (error) {
|
||||
// logger.error(`Got error on reward event: ${rewardEventId}: ${error.toString()}`)
|
||||
// let saveError = new ErrorEvent(rewardEventId)
|
||||
// saveError.description = error.toString()
|
||||
// await saveError.save()
|
||||
// }
|
||||
}
|
||||
|
||||
async function handleRewardForTxHistory(
|
||||
@@ -209,7 +179,7 @@ async function handleRewardForTxHistory(
|
||||
}
|
||||
|
||||
function determinePayoutCallsArgs(
|
||||
causeCall: CallBase<AnyTuple>,
|
||||
causeCall: any,
|
||||
sender: string,
|
||||
): [string, number][] {
|
||||
if (isPayoutStakers(causeCall)) {
|
||||
@@ -220,7 +190,7 @@ function determinePayoutCallsArgs(
|
||||
return [extractArgsFromPayoutValidator(causeCall, sender)];
|
||||
} else if (isBatch(causeCall)) {
|
||||
return callsFromBatch(causeCall)
|
||||
.map((call) => {
|
||||
.map((call: any) => {
|
||||
return determinePayoutCallsArgs(call, sender).map(
|
||||
(value, index, array) => {
|
||||
return value;
|
||||
@@ -236,15 +206,11 @@ function determinePayoutCallsArgs(
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleSlashed(
|
||||
slashEvent: SubstrateEvent<[accountId: Codec, slash: INumber]>,
|
||||
): Promise<void> {
|
||||
export async function handleSlashed(slashEvent: SubstrateEvent): Promise<void> {
|
||||
await handleSlash(slashEvent);
|
||||
}
|
||||
|
||||
export async function handleSlash(
|
||||
slashEvent: SubstrateEvent<[accountId: Codec, slash: INumber]>,
|
||||
): Promise<void> {
|
||||
export async function handleSlash(slashEvent: SubstrateEvent): Promise<void> {
|
||||
await handleSlashForTxHistory(slashEvent);
|
||||
let accumulatedReward = await updateAccumulatedReward(slashEvent, false);
|
||||
await updateAccountRewards(
|
||||
@@ -252,29 +218,13 @@ export async function handleSlash(
|
||||
RewardType.slash,
|
||||
accumulatedReward.amount,
|
||||
);
|
||||
// let slashEventId = eventId(slashEvent)
|
||||
// try {
|
||||
// let errorOccursOnEvent = await ErrorEvent.get(slashEventId)
|
||||
// if (errorOccursOnEvent !== undefined) {
|
||||
// logger.info(`Skip slashEvent: ${slashEventId}`)
|
||||
// return;
|
||||
// }
|
||||
|
||||
// await handleSlashForTxHistory(slashEvent)
|
||||
// await updateAccumulatedReward(slashEvent, false)
|
||||
// } catch (error) {
|
||||
// logger.error(`Got error on slash event: ${slashEventId}: ${error.toString()}`)
|
||||
// let saveError = new ErrorEvent(slashEventId)
|
||||
// saveError.description = error.toString()
|
||||
// await saveError.save()
|
||||
// }
|
||||
}
|
||||
|
||||
async function getValidators(era: number): Promise<Set<string>> {
|
||||
const eraStakersInSlashEra = await (api.query.staking.erasStakersClipped
|
||||
? api.query.staking.erasStakersClipped.keys(era)
|
||||
: api.query.staking.erasStakersOverview.keys(era));
|
||||
const validatorsInSlashEra = eraStakersInSlashEra.map((key) => {
|
||||
const validatorsInSlashEra = eraStakersInSlashEra.map((key: any) => {
|
||||
let [, validatorId] = key.args;
|
||||
|
||||
return validatorId.toString();
|
||||
@@ -347,7 +297,7 @@ async function buildRewardEvents<A>(
|
||||
amount: string,
|
||||
) => Reward,
|
||||
) {
|
||||
let blockNumber = block.block.header.number.toString();
|
||||
let blockNum = block.block.header.number.toString();
|
||||
let blockTimestamp = timestamp(block);
|
||||
|
||||
let innerAccumulator = initialInnerAccumulator;
|
||||
@@ -371,13 +321,13 @@ async function buildRewardEvents<A>(
|
||||
account.toString(),
|
||||
);
|
||||
|
||||
const eventId = eventIdFromBlockAndIdx(blockNumber, eventIndex.toString());
|
||||
const evtId = eventIdFromBlockAndIdx(blockNum, eventIndex.toString());
|
||||
|
||||
const accountAddress = account.toString();
|
||||
const destinationAddress = accountsMapping[accountAddress];
|
||||
|
||||
const element = new HistoryElement(
|
||||
eventId,
|
||||
evtId,
|
||||
block.block.header.number.toNumber(),
|
||||
blockTimestamp,
|
||||
destinationAddress !== undefined ? destinationAddress : accountAddress,
|
||||
@@ -407,7 +357,7 @@ async function updateAccumulatedReward(
|
||||
return await updateAccumulatedGenericReward(
|
||||
AccumulatedReward,
|
||||
accountId.toString(),
|
||||
(amount as unknown as Balance).toBigInt(),
|
||||
(amount as any).toBigInt(),
|
||||
isReward,
|
||||
);
|
||||
}
|
||||
@@ -425,7 +375,7 @@ async function updateAccountRewards(
|
||||
accountAddress,
|
||||
blockNumber(event),
|
||||
timestamp(event.block),
|
||||
(amount as unknown as Balance).toBigInt(),
|
||||
(amount as any).toBigInt(),
|
||||
accumulatedAmount,
|
||||
rewardType,
|
||||
);
|
||||
@@ -463,7 +413,7 @@ async function handleParachainRewardForTxHistory(
|
||||
}
|
||||
|
||||
export async function handleParachainRewarded(
|
||||
rewardEvent: SubstrateEvent<[accountId: Codec, reward: INumber]>,
|
||||
rewardEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
await handleParachainRewardForTxHistory(rewardEvent);
|
||||
let accumulatedReward = await updateAccumulatedReward(rewardEvent, true);
|
||||
@@ -474,46 +424,6 @@ export async function handleParachainRewarded(
|
||||
);
|
||||
}
|
||||
|
||||
// ============= Mythos ================
|
||||
|
||||
export async function handleMythosRewarded(
|
||||
rewardEvent: SubstrateEvent<[accountId: Codec, reward: INumber]>,
|
||||
): Promise<void> {
|
||||
await handleMythosRewardForTxHistory(rewardEvent);
|
||||
let accumulatedReward = await updateAccumulatedReward(rewardEvent, true);
|
||||
await updateAccountRewards(
|
||||
rewardEvent,
|
||||
RewardType.reward,
|
||||
accumulatedReward.amount,
|
||||
);
|
||||
}
|
||||
|
||||
async function handleMythosRewardForTxHistory(
|
||||
rewardEvent: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
let [account, amount] = decodeDataFromReward(rewardEvent);
|
||||
|
||||
await handleGenericForTxHistory(
|
||||
rewardEvent,
|
||||
account.toString(),
|
||||
async (element: HistoryElement) => {
|
||||
element.reward = {
|
||||
eventIdx: rewardEvent.idx,
|
||||
amount: amount.toString(),
|
||||
isReward: true,
|
||||
stash: account.toString(),
|
||||
// Mythos staking rewards are paid manually by the user so each reward
|
||||
// aggregates multiple payouts, and it is hard to split it into
|
||||
// individual per-session per-validator pieces
|
||||
validator: null,
|
||||
era: null,
|
||||
};
|
||||
|
||||
return element;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ============= GENERICS ================
|
||||
|
||||
interface AccumulatedInterface {
|
||||
@@ -558,12 +468,12 @@ export async function handleGenericForTxHistory(
|
||||
): Promise<void> {
|
||||
const extrinsic = event.extrinsic;
|
||||
const block = event.block;
|
||||
const blockNumber = block.block.header.number.toString();
|
||||
const blockNum = block.block.header.number.toString();
|
||||
const blockTimestamp = timestamp(block);
|
||||
const eventId = eventIdFromBlockAndIdx(blockNumber, event.idx.toString());
|
||||
const evtId = eventIdFromBlockAndIdx(blockNum, event.idx.toString());
|
||||
|
||||
const element = new HistoryElement(
|
||||
eventId,
|
||||
evtId,
|
||||
block.block.header.number.toNumber(),
|
||||
blockTimestamp,
|
||||
address,
|
||||
@@ -593,20 +503,15 @@ interface AccountRewardsInterface {
|
||||
save(): Promise<void>;
|
||||
}
|
||||
|
||||
export function eventRecordToSubstrateEvent(
|
||||
eventRecord: EventRecord,
|
||||
): SubstrateEvent {
|
||||
export function eventRecordToSubstrateEvent(eventRecord: any): SubstrateEvent {
|
||||
return eventRecord as unknown as SubstrateEvent;
|
||||
}
|
||||
|
||||
function decodeDataFromReward(event: SubstrateEvent): [Codec, Codec] {
|
||||
// In early version staking.Reward data only have 2 parameters [accountId, amount]
|
||||
// Now rewarded changed to https://polkadot.js.org/docs/substrate/events/#rewardedaccountid32-palletstakingrewarddestination-u128
|
||||
// And we can direct access property from data
|
||||
function decodeDataFromReward(event: SubstrateEvent): [any, any] {
|
||||
const {
|
||||
event: { data: innerData },
|
||||
} = event;
|
||||
let account: Codec, amount: Codec;
|
||||
let account: any, amount: any;
|
||||
if (innerData.length == 2) {
|
||||
[account, amount] = innerData;
|
||||
} else {
|
||||
|
||||
+31
-261
@@ -1,6 +1,4 @@
|
||||
import { Codec } from "@polkadot/types/types";
|
||||
import { HistoryElement } from "../types";
|
||||
import { HistoryElementProps } from "../types/models/HistoryElement";
|
||||
import { HistoryElement, Transfer } from "../types";
|
||||
import { SubstrateEvent } from "@subql/types";
|
||||
import {
|
||||
blockNumber,
|
||||
@@ -8,286 +6,58 @@ import {
|
||||
calculateFeeAsString,
|
||||
timestamp,
|
||||
getEventData,
|
||||
isEvmTransaction,
|
||||
isEvmExecutedEvent,
|
||||
isAssetTxFeePaidEvent,
|
||||
isSwapExecutedEvent,
|
||||
eventRecordToSubstrateEvent,
|
||||
getAssetIdFromMultilocation,
|
||||
BigIntFromCodec,
|
||||
convertOrmlCurrencyIdToString,
|
||||
} from "./common";
|
||||
|
||||
type TransferPayload = {
|
||||
event: SubstrateEvent;
|
||||
address: Codec;
|
||||
from: Codec;
|
||||
to: Codec;
|
||||
amount: Codec;
|
||||
suffix: string;
|
||||
assetId?: string;
|
||||
};
|
||||
|
||||
export async function handleSwap(event: SubstrateEvent): Promise<void> {
|
||||
const [from, to, path, amountIn, amountOut] = getEventData(event);
|
||||
|
||||
let element = await HistoryElement.get(`${eventId(event)}-from`);
|
||||
|
||||
if (element !== undefined) {
|
||||
// already processed swap previously
|
||||
return;
|
||||
}
|
||||
|
||||
let assetIdFee: string;
|
||||
let fee: string;
|
||||
let foundAssetTxFeePaid = event.block.events.find((e) =>
|
||||
isAssetTxFeePaidEvent(eventRecordToSubstrateEvent(e)),
|
||||
);
|
||||
let swaps = event.block.events.filter((e) =>
|
||||
isSwapExecutedEvent(eventRecordToSubstrateEvent(e)),
|
||||
);
|
||||
if (foundAssetTxFeePaid === undefined) {
|
||||
assetIdFee = "native";
|
||||
fee = calculateFeeAsString(event.extrinsic, from.toString());
|
||||
} else {
|
||||
const [who, actualFee, tip, rawAssetIdFee] = getEventData(
|
||||
eventRecordToSubstrateEvent(foundAssetTxFeePaid),
|
||||
);
|
||||
assetIdFee = getAssetIdFromMultilocation(rawAssetIdFee);
|
||||
fee = actualFee.toString();
|
||||
|
||||
let {
|
||||
event: {
|
||||
data: [feeFrom, feeTo, feePath, feeAmountIn, feeAmountOut],
|
||||
},
|
||||
} = swaps[0];
|
||||
|
||||
swaps = swaps.slice(1);
|
||||
if (BigIntFromCodec(actualFee) != BigIntFromCodec(feeAmountIn)) {
|
||||
let {
|
||||
event: {
|
||||
data: [
|
||||
refundFrom,
|
||||
refundTo,
|
||||
refundPath,
|
||||
refundAmountIn,
|
||||
refundAmountOut,
|
||||
],
|
||||
},
|
||||
} = swaps[swaps.length - 1];
|
||||
|
||||
if (
|
||||
BigIntFromCodec(feeAmountIn) ==
|
||||
BigIntFromCodec(actualFee) + BigIntFromCodec(refundAmountOut) &&
|
||||
getAssetIdFromMultilocation((feePath as any)[0]) ==
|
||||
getAssetIdFromMultilocation(
|
||||
(refundPath as any)[(refundPath as any)["length"] - 1],
|
||||
)
|
||||
) {
|
||||
swaps = swaps.slice(swaps.length - 1);
|
||||
// TODO: if fee splitted, than we will process the same block two times
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const swap of swaps) {
|
||||
await processSwap(eventRecordToSubstrateEvent(swap), assetIdFee, fee);
|
||||
}
|
||||
}
|
||||
|
||||
async function processSwap(
|
||||
event: SubstrateEvent,
|
||||
assetIdFee: string,
|
||||
fee: string,
|
||||
): Promise<void> {
|
||||
const [from, to, path, amountIn, amountOut] = getEventData(event);
|
||||
|
||||
const swap = {
|
||||
assetIdIn: getAssetIdFromMultilocation((path as any)[0]),
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: getAssetIdFromMultilocation(
|
||||
(path as any)[(path as any)["length"] - 1],
|
||||
),
|
||||
amountOut: amountOut.toString(),
|
||||
sender: from.toString(),
|
||||
receiver: to.toString(),
|
||||
assetIdFee: assetIdFee,
|
||||
fee: fee,
|
||||
eventIdx: event.idx,
|
||||
success: true,
|
||||
};
|
||||
|
||||
await createAssetTransmission(event, from.toString(), "-from", {
|
||||
swap: swap,
|
||||
});
|
||||
if (from.toString() != to.toString()) {
|
||||
await createAssetTransmission(event, to.toString(), "-to", { swap: swap });
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleTransfer(event: SubstrateEvent): Promise<void> {
|
||||
const [from, to, amount] = getEventData(event);
|
||||
|
||||
await createTransfer({
|
||||
await createTransfer(
|
||||
event,
|
||||
address: from,
|
||||
from,
|
||||
to,
|
||||
suffix: "-from",
|
||||
amount,
|
||||
});
|
||||
await createTransfer({ event, address: to, from, to, suffix: "-to", amount });
|
||||
from.toString(),
|
||||
"-from",
|
||||
from.toString(),
|
||||
to.toString(),
|
||||
amount.toString(),
|
||||
);
|
||||
await createTransfer(
|
||||
event,
|
||||
to.toString(),
|
||||
"-to",
|
||||
from.toString(),
|
||||
to.toString(),
|
||||
amount.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
export async function handleAssetTransfer(
|
||||
async function createTransfer(
|
||||
event: SubstrateEvent,
|
||||
address: string,
|
||||
suffix: string,
|
||||
from: string,
|
||||
to: string,
|
||||
amount: string,
|
||||
): Promise<void> {
|
||||
const [assetId, from, to, amount] = getEventData(event);
|
||||
|
||||
await createTransfer({
|
||||
event,
|
||||
address: from,
|
||||
from,
|
||||
to,
|
||||
suffix: "-from",
|
||||
amount,
|
||||
assetId: assetId.toString(),
|
||||
});
|
||||
await createTransfer({
|
||||
event,
|
||||
address: to,
|
||||
from,
|
||||
to,
|
||||
suffix: "-to",
|
||||
amount,
|
||||
assetId: assetId.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
export async function handleOrmlTransfer(event: SubstrateEvent): Promise<void> {
|
||||
const [currencyId, from, to, amount] = getEventData(event);
|
||||
|
||||
await createTransfer({
|
||||
event,
|
||||
address: from,
|
||||
from,
|
||||
to,
|
||||
suffix: "-from",
|
||||
amount,
|
||||
assetId: convertOrmlCurrencyIdToString(currencyId),
|
||||
});
|
||||
await createTransfer({
|
||||
event,
|
||||
address: to,
|
||||
from,
|
||||
to,
|
||||
suffix: "-to",
|
||||
amount,
|
||||
assetId: convertOrmlCurrencyIdToString(currencyId),
|
||||
});
|
||||
}
|
||||
|
||||
export async function handleEquilibriumTransfer(
|
||||
event: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
const [from, to, assetId, amount] = getEventData(event);
|
||||
|
||||
await createTransfer({
|
||||
event,
|
||||
address: from,
|
||||
from,
|
||||
to,
|
||||
suffix: "-from",
|
||||
amount,
|
||||
assetId: assetId.toString(),
|
||||
});
|
||||
await createTransfer({
|
||||
event,
|
||||
address: to,
|
||||
from,
|
||||
to,
|
||||
suffix: "-to",
|
||||
amount,
|
||||
assetId: assetId.toString(),
|
||||
});
|
||||
}
|
||||
|
||||
export async function handleTokenTransfer(
|
||||
event: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
await handleOrmlTransfer(event);
|
||||
}
|
||||
|
||||
export async function handleCurrencyTransfer(
|
||||
event: SubstrateEvent,
|
||||
): Promise<void> {
|
||||
await handleOrmlTransfer(event);
|
||||
}
|
||||
|
||||
async function createTransfer({
|
||||
event,
|
||||
address,
|
||||
suffix,
|
||||
from,
|
||||
to,
|
||||
amount,
|
||||
assetId = null,
|
||||
}: TransferPayload) {
|
||||
const transfer = {
|
||||
amount: amount.toString(),
|
||||
from: from.toString(),
|
||||
to: to.toString(),
|
||||
fee: calculateFeeAsString(event.extrinsic, from.toString()),
|
||||
const transfer: Transfer = {
|
||||
amount: amount,
|
||||
from: from,
|
||||
to: to,
|
||||
fee: calculateFeeAsString(event.extrinsic, from),
|
||||
eventIdx: event.idx,
|
||||
success: true,
|
||||
};
|
||||
|
||||
let data;
|
||||
if (assetId) {
|
||||
data = {
|
||||
assetTransfer: {
|
||||
...transfer,
|
||||
assetId: assetId,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
data = {
|
||||
transfer: transfer,
|
||||
};
|
||||
}
|
||||
|
||||
await createAssetTransmission(event, address, suffix, data);
|
||||
}
|
||||
|
||||
export async function createAssetTransmission(
|
||||
event: SubstrateEvent,
|
||||
address: any,
|
||||
suffix: string,
|
||||
data: Partial<HistoryElementProps>,
|
||||
) {
|
||||
const element = new HistoryElement(
|
||||
`${eventId(event)}${suffix}`,
|
||||
blockNumber(event),
|
||||
timestamp(event.block),
|
||||
address.toString(),
|
||||
address,
|
||||
);
|
||||
if (event.extrinsic !== undefined) {
|
||||
if (isEvmTransaction(event.extrinsic.extrinsic.method)) {
|
||||
const executedEvent = event.extrinsic.events.find(isEvmExecutedEvent);
|
||||
element.extrinsicHash =
|
||||
executedEvent?.event.data?.[2]?.toString() ||
|
||||
event.extrinsic.extrinsic.hash.toString();
|
||||
} else {
|
||||
element.extrinsicHash = event.extrinsic.extrinsic.hash.toString();
|
||||
}
|
||||
|
||||
if (event.extrinsic !== undefined) {
|
||||
element.extrinsicHash = event.extrinsic.extrinsic.hash.toString();
|
||||
element.extrinsicIdx = event.extrinsic.idx;
|
||||
}
|
||||
|
||||
for (var key in data) {
|
||||
(element[key as keyof HistoryElementProps] as any) =
|
||||
data[key as keyof HistoryElementProps];
|
||||
}
|
||||
|
||||
element.transfer = transfer;
|
||||
await element.save();
|
||||
}
|
||||
|
||||
+32
-190
@@ -1,127 +1,35 @@
|
||||
import { SubstrateBlock, SubstrateEvent, TypedEventRecord } from "@subql/types";
|
||||
import { SubstrateBlock, SubstrateEvent } from "@subql/types";
|
||||
import { SubstrateExtrinsic } from "@subql/types";
|
||||
import { Balance, EventRecord } from "@polkadot/types/interfaces";
|
||||
import { CallBase } from "@polkadot/types/types/calls";
|
||||
import { AnyTuple, Codec } from "@polkadot/types/types/codec";
|
||||
import { Vec, GenericEventData } from "@polkadot/types";
|
||||
import { INumber } from "@polkadot/types-codec/types/interfaces";
|
||||
import { u8aToHex } from "@polkadot/util";
|
||||
|
||||
const batchCalls = ["batch", "batchAll", "forceBatch"];
|
||||
const transferCalls = ["transfer", "transferKeepAlive"];
|
||||
const ormlSections = ["currencies", "tokens"];
|
||||
|
||||
export function distinct<T>(array: Array<T>): Array<T> {
|
||||
return [...new Set(array)];
|
||||
}
|
||||
|
||||
export function isBatch(call: CallBase<AnyTuple>): boolean {
|
||||
export function isBatch(call: any): boolean {
|
||||
return call.section == "utility" && batchCalls.includes(call.method);
|
||||
}
|
||||
|
||||
export function isProxy(call: CallBase<AnyTuple>): boolean {
|
||||
export function isProxy(call: any): boolean {
|
||||
return call.section == "proxy" && call.method == "proxy";
|
||||
}
|
||||
|
||||
export function isNativeTransfer(call: CallBase<AnyTuple>): boolean {
|
||||
return (
|
||||
(call.section == "balances" && transferCalls.includes(call.method)) ||
|
||||
(call.section == "currencies" && call.method == "transferNativeCurrency")
|
||||
);
|
||||
export function isNativeTransfer(call: any): boolean {
|
||||
return call.section == "balances" && transferCalls.includes(call.method);
|
||||
}
|
||||
|
||||
export function isAssetTransfer(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section == "assets" && transferCalls.includes(call.method);
|
||||
}
|
||||
|
||||
export function isEquilibriumTransfer(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section == "eqBalances" && transferCalls.includes(call.method);
|
||||
}
|
||||
|
||||
export function isEvmTransaction(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section === "ethereum" && call.method === "transact";
|
||||
}
|
||||
|
||||
export function isEvmExecutedEvent(event: TypedEventRecord<Codec[]>): boolean {
|
||||
return (
|
||||
event.event.section === "ethereum" && event.event.method === "Executed"
|
||||
);
|
||||
}
|
||||
|
||||
export function isAssetTxFeePaidEvent(event: SubstrateEvent): boolean {
|
||||
return (
|
||||
event.event.section === "assetTxPayment" &&
|
||||
event.event.method === "AssetTxFeePaid"
|
||||
);
|
||||
}
|
||||
|
||||
export function isCurrencyDepositedEvent(event: SubstrateEvent): boolean {
|
||||
return (
|
||||
event.event.section === "currencies" && event.event.method === "Deposited"
|
||||
);
|
||||
}
|
||||
|
||||
export function isSwapExecutedEvent(event: SubstrateEvent): boolean {
|
||||
return (
|
||||
event.event.section === "assetConversion" &&
|
||||
event.event.method === "SwapExecuted"
|
||||
);
|
||||
}
|
||||
|
||||
export function isSwapExactTokensForTokens(call: CallBase<AnyTuple>): boolean {
|
||||
return (
|
||||
call.section === "assetConversion" &&
|
||||
call.method === "swapExactTokensForTokens"
|
||||
);
|
||||
}
|
||||
|
||||
export function isSwapTokensForExactTokens(call: CallBase<AnyTuple>): boolean {
|
||||
return (
|
||||
call.section === "assetConversion" &&
|
||||
call.method === "swapTokensForExactTokens"
|
||||
);
|
||||
}
|
||||
|
||||
export function isHydraOmnipoolBuy(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section === "omnipool" && call.method == "buy";
|
||||
}
|
||||
|
||||
export function isHydraOmnipoolSell(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section === "omnipool" && call.method == "sell";
|
||||
}
|
||||
|
||||
export function isHydraRouterBuy(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section === "router" && call.method == "buy";
|
||||
}
|
||||
|
||||
export function isHydraRouterSell(call: CallBase<AnyTuple>): boolean {
|
||||
return call.section === "router" && call.method == "sell";
|
||||
}
|
||||
|
||||
export function isOrmlTransfer(call: CallBase<AnyTuple>): boolean {
|
||||
return (
|
||||
ormlSections.includes(call.section) && transferCalls.includes(call.method)
|
||||
);
|
||||
}
|
||||
|
||||
export function isNativeTransferAll(call: CallBase<AnyTuple>): boolean {
|
||||
export function isNativeTransferAll(call: any): boolean {
|
||||
return call.section == "balances" && call.method === "transferAll";
|
||||
}
|
||||
|
||||
export function isOrmlTransferAll(call: CallBase<AnyTuple>): boolean {
|
||||
return ormlSections.includes(call.section) && call.method === "transferAll";
|
||||
export function callsFromBatch(batchCall: any): any[] {
|
||||
return batchCall.args[0] as any[];
|
||||
}
|
||||
|
||||
export function callsFromBatch(
|
||||
batchCall: CallBase<AnyTuple>,
|
||||
): CallBase<AnyTuple>[] {
|
||||
return batchCall.args[0] as Vec<CallBase<AnyTuple>>;
|
||||
}
|
||||
|
||||
export function callFromProxy(
|
||||
proxyCall: CallBase<AnyTuple>,
|
||||
): CallBase<AnyTuple> {
|
||||
return proxyCall.args[2] as CallBase<AnyTuple>;
|
||||
export function callFromProxy(proxyCall: any): any {
|
||||
return proxyCall.args[2];
|
||||
}
|
||||
|
||||
export function eventIdWithAddress(
|
||||
@@ -186,12 +94,6 @@ export function calculateFeeAsString(
|
||||
const withdrawFee = exportFeeFromBalancesWithdrawEvent(extrinsic, from);
|
||||
|
||||
if (withdrawFee !== BigInt(0)) {
|
||||
if (isEvmTransaction(extrinsic.extrinsic.method)) {
|
||||
const feeRefund = exportFeeRefund(extrinsic, from);
|
||||
return feeRefund
|
||||
? (withdrawFee - feeRefund).toString()
|
||||
: withdrawFee.toString();
|
||||
}
|
||||
return withdrawFee.toString();
|
||||
}
|
||||
|
||||
@@ -205,51 +107,29 @@ export function calculateFeeAsString(
|
||||
}
|
||||
}
|
||||
|
||||
export function getEventData(event: SubstrateEvent): GenericEventData {
|
||||
return event.event.data as GenericEventData;
|
||||
export function getEventData(event: SubstrateEvent): any {
|
||||
return event.event.data;
|
||||
}
|
||||
|
||||
export function eventRecordToSubstrateEvent(
|
||||
eventRecord: EventRecord,
|
||||
): SubstrateEvent {
|
||||
export function eventRecordToSubstrateEvent(eventRecord: any): SubstrateEvent {
|
||||
return eventRecord as unknown as SubstrateEvent;
|
||||
}
|
||||
|
||||
export function BigIntFromCodec(eventRecord: Codec): bigint {
|
||||
return (eventRecord as unknown as INumber).toBigInt();
|
||||
export function BigIntFromCodec(codec: any): bigint {
|
||||
return codec.toBigInt();
|
||||
}
|
||||
|
||||
export function convertOrmlCurrencyIdToString(currencyId: Codec): string {
|
||||
// make sure first we have scale encoded bytes
|
||||
const bytes = currencyId.toU8a();
|
||||
|
||||
return u8aToHex(bytes).toString();
|
||||
}
|
||||
|
||||
function exportFeeRefund(
|
||||
extrinsic: SubstrateExtrinsic,
|
||||
from: string = "",
|
||||
): bigint {
|
||||
const extrinsicSigner = from || extrinsic.extrinsic.signer.toString();
|
||||
|
||||
const eventRecord = extrinsic.events.find(
|
||||
(event) =>
|
||||
event.event.method == "Deposit" &&
|
||||
event.event.section == "balances" &&
|
||||
event.event.data[0].toString() === extrinsicSigner,
|
||||
);
|
||||
|
||||
if (eventRecord != undefined) {
|
||||
const {
|
||||
event: {
|
||||
data: [, fee],
|
||||
},
|
||||
} = eventRecord;
|
||||
|
||||
return (fee as unknown as Balance).toBigInt();
|
||||
export function getRewardData(event: SubstrateEvent): [any, any] {
|
||||
const {
|
||||
event: { data: innerData },
|
||||
} = event;
|
||||
let account: any, amount: any;
|
||||
if (innerData.length == 2) {
|
||||
[account, amount] = innerData;
|
||||
} else {
|
||||
[account, , amount] = innerData;
|
||||
}
|
||||
|
||||
return BigInt(0);
|
||||
return [account, amount];
|
||||
}
|
||||
|
||||
function exportFeeFromBalancesWithdrawEvent(
|
||||
@@ -271,7 +151,7 @@ function exportFeeFromBalancesWithdrawEvent(
|
||||
const extrinsicSigner = from || extrinsic.extrinsic.signer.toString();
|
||||
const withdrawAccountId = accountid.toString();
|
||||
return extrinsicSigner === withdrawAccountId
|
||||
? (fee as unknown as Balance).toBigInt()
|
||||
? (fee as any).toBigInt()
|
||||
: BigInt(0);
|
||||
}
|
||||
|
||||
@@ -295,7 +175,7 @@ function exportFeeFromTransactionFeePaidEvent(
|
||||
},
|
||||
} = eventRecord;
|
||||
|
||||
const fullFee = (fee as Balance).toBigInt() + (tip as Balance).toBigInt();
|
||||
const fullFee = (fee as any).toBigInt() + (tip as any).toBigInt();
|
||||
|
||||
const extrinsicSigner = from || extrinsic.extrinsic.signer.toString();
|
||||
const withdrawAccountId = accountid.toString();
|
||||
@@ -319,7 +199,7 @@ function exportFeeFromBalancesDepositEvent(
|
||||
},
|
||||
} = eventRecord;
|
||||
|
||||
return (fee as unknown as Balance).toBigInt();
|
||||
return (fee as any).toBigInt();
|
||||
}
|
||||
|
||||
return BigInt(0);
|
||||
@@ -339,53 +219,15 @@ function exportFeeFromTreasureDepositEvent(
|
||||
},
|
||||
} = eventRecord;
|
||||
|
||||
return (fee as unknown as Balance).toBigInt();
|
||||
return (fee as any).toBigInt();
|
||||
} else {
|
||||
return BigInt(0);
|
||||
}
|
||||
}
|
||||
|
||||
export function getAssetIdFromMultilocation(
|
||||
multilocation: any,
|
||||
safe = false,
|
||||
): string | undefined {
|
||||
try {
|
||||
let junctions = multilocation.interior;
|
||||
|
||||
if (junctions.isHere) {
|
||||
return "native";
|
||||
} else if (multilocation.parents != "0") {
|
||||
return multilocation.toHex();
|
||||
} else {
|
||||
return junctions.asX2[1].asGeneralIndex.toString();
|
||||
}
|
||||
} catch (e) {
|
||||
if (safe) {
|
||||
return undefined;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getRewardData(event: SubstrateEvent): [Codec, Codec] {
|
||||
const {
|
||||
event: { data: innerData },
|
||||
} = event;
|
||||
let account: Codec, amount: Codec;
|
||||
if (innerData.length == 2) {
|
||||
[account, amount] = innerData;
|
||||
} else {
|
||||
[account, , amount] = innerData;
|
||||
}
|
||||
return [account, amount];
|
||||
}
|
||||
|
||||
export function extractTransactionPaidFee(
|
||||
events: EventRecord[],
|
||||
): string | undefined {
|
||||
export function extractTransactionPaidFee(events: any[]): string | undefined {
|
||||
const eventRecord = events.find(
|
||||
(event) =>
|
||||
(event: any) =>
|
||||
event.event.method == "TransactionFeePaid" &&
|
||||
event.event.section == "transactionPayment",
|
||||
);
|
||||
@@ -398,7 +240,7 @@ export function extractTransactionPaidFee(
|
||||
},
|
||||
} = eventRecord;
|
||||
|
||||
const fullFee = (fee as Balance).toBigInt() + (tip as Balance).toBigInt();
|
||||
const fullFee = (fee as any).toBigInt() + (tip as any).toBigInt();
|
||||
|
||||
return fullFee.toString();
|
||||
}
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
import { SubstrateEvent } from "@subql/types";
|
||||
import {
|
||||
BigIntFromCodec,
|
||||
calculateFeeAsString,
|
||||
eventId,
|
||||
eventRecordToSubstrateEvent,
|
||||
getAssetIdFromMultilocation,
|
||||
getEventData,
|
||||
isAssetTxFeePaidEvent,
|
||||
isSwapExecutedEvent,
|
||||
} from "../common";
|
||||
import { HistoryElement } from "../../types";
|
||||
import { createAssetTransmission } from "../Transfers";
|
||||
|
||||
export async function handleAssetConversionSwap(
|
||||
event: SubstrateEvent
|
||||
): Promise<void> {
|
||||
const [from, to, path, amountIn, amountOut] = getEventData(event);
|
||||
|
||||
let element = await HistoryElement.get(`${eventId(event)}-from`);
|
||||
|
||||
if (element !== undefined) {
|
||||
// already processed swap previously
|
||||
return;
|
||||
}
|
||||
|
||||
let assetIdFee: string;
|
||||
let fee: string;
|
||||
let foundAssetTxFeePaid = event.block.events.find((e) =>
|
||||
isAssetTxFeePaidEvent(eventRecordToSubstrateEvent(e))
|
||||
);
|
||||
let swaps = event.block.events.filter((e) =>
|
||||
isSwapExecutedEvent(eventRecordToSubstrateEvent(e))
|
||||
);
|
||||
if (foundAssetTxFeePaid === undefined) {
|
||||
assetIdFee = "native";
|
||||
fee = calculateFeeAsString(event.extrinsic, from.toString());
|
||||
} else {
|
||||
const [who, actualFee, tip, rawAssetIdFee] = getEventData(
|
||||
eventRecordToSubstrateEvent(foundAssetTxFeePaid)
|
||||
);
|
||||
assetIdFee = getAssetIdFromMultilocation(rawAssetIdFee);
|
||||
fee = actualFee.toString();
|
||||
|
||||
let {
|
||||
event: {
|
||||
data: [feeFrom, feeTo, feePath, feeAmountIn, feeAmountOut],
|
||||
},
|
||||
} = swaps[0];
|
||||
|
||||
swaps = swaps.slice(1);
|
||||
if (BigIntFromCodec(actualFee) != BigIntFromCodec(feeAmountIn)) {
|
||||
let {
|
||||
event: {
|
||||
data: [
|
||||
refundFrom,
|
||||
refundTo,
|
||||
refundPath,
|
||||
refundAmountIn,
|
||||
refundAmountOut,
|
||||
],
|
||||
},
|
||||
} = swaps[swaps.length - 1];
|
||||
|
||||
const feePathArray = feePath as unknown as any[];
|
||||
const refundPathArray = refundPath as unknown as any[];
|
||||
|
||||
if (
|
||||
BigIntFromCodec(feeAmountIn) ==
|
||||
BigIntFromCodec(actualFee) + BigIntFromCodec(refundAmountOut) &&
|
||||
getAssetIdFromMultilocation(feePathArray[0]) ==
|
||||
getAssetIdFromMultilocation(
|
||||
refundPathArray[refundPathArray["length"] - 1]
|
||||
)
|
||||
) {
|
||||
swaps = swaps.slice(swaps.length - 1);
|
||||
// TODO: if fee splitted, than we will process the same block two times
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const e of swaps) {
|
||||
await processAssetConversionSwap(
|
||||
eventRecordToSubstrateEvent(e),
|
||||
assetIdFee,
|
||||
fee
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function processAssetConversionSwap(
|
||||
event: SubstrateEvent,
|
||||
assetIdFee: string,
|
||||
fee: string
|
||||
): Promise<void> {
|
||||
const [from, to, path, amountIn, amountOut] = getEventData(event);
|
||||
|
||||
const pathArray = path as unknown as any[];
|
||||
|
||||
const swap = {
|
||||
assetIdIn: getAssetIdFromMultilocation(pathArray[0]),
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: getAssetIdFromMultilocation(pathArray[pathArray["length"] - 1]),
|
||||
amountOut: amountOut.toString(),
|
||||
sender: from.toString(),
|
||||
receiver: to.toString(),
|
||||
assetIdFee: assetIdFee,
|
||||
fee: fee,
|
||||
eventIdx: event.idx,
|
||||
success: true,
|
||||
};
|
||||
|
||||
await createAssetTransmission(event, from.toString(), "-from", {
|
||||
swap: swap,
|
||||
});
|
||||
if (from.toString() != to.toString()) {
|
||||
await createAssetTransmission(event, to.toString(), "-to", { swap: swap });
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
import { SubstrateEvent, TypedEventRecord } from "@subql/types";
|
||||
import {
|
||||
eventId,
|
||||
eventRecordToSubstrateEvent,
|
||||
extractTransactionPaidFee,
|
||||
isCurrencyDepositedEvent,
|
||||
convertOrmlCurrencyIdToString,
|
||||
} from "../common";
|
||||
import { HistoryElement } from "../../types";
|
||||
import { createAssetTransmission } from "../Transfers";
|
||||
import { AccountId32 } from "@polkadot/types/interfaces/runtime";
|
||||
import { u128, u32 } from "@polkadot/types-codec";
|
||||
import { EventRecord } from "@polkadot/types/interfaces";
|
||||
import { Codec } from "@polkadot/types/types";
|
||||
import { INumber } from "@polkadot/types-codec/types/interfaces";
|
||||
|
||||
type OmnipoolSwapArgs = [
|
||||
who: AccountId32,
|
||||
assetIn: u32,
|
||||
assetOut: u32,
|
||||
amountIn: u128,
|
||||
amountOut: u128,
|
||||
assetFeeAmount: u128,
|
||||
protocolFeeAmount: u128,
|
||||
];
|
||||
|
||||
type RouterSwapArgs = [
|
||||
assetIn: u32,
|
||||
assetOut: u32,
|
||||
amountIn: u128,
|
||||
amountOut: u128,
|
||||
];
|
||||
|
||||
export async function handleOmnipoolSwap(
|
||||
event: SubstrateEvent<OmnipoolSwapArgs>,
|
||||
): Promise<void> {
|
||||
let element = await HistoryElement.get(`${eventId(event)}-from`);
|
||||
if (element !== undefined) {
|
||||
// already processed swap previously
|
||||
return;
|
||||
}
|
||||
if (event.extrinsic == undefined) {
|
||||
// TODO we dont yet process swap events that were initiated by the system and not by the user
|
||||
// Example: https://hydradx.subscan.io/block/4361343?tab=event&event=4361343-27
|
||||
return;
|
||||
}
|
||||
|
||||
if (isPartOfRouterSwap(event.extrinsic.events)) {
|
||||
// TODO: we currently don't support swaps in batch
|
||||
return;
|
||||
}
|
||||
|
||||
const fee = findHydraDxFeeTyped(event.extrinsic.events);
|
||||
const [who, assetIn, assetOut, amountIn, amountOut] = event.event.data;
|
||||
|
||||
const swap = {
|
||||
assetIdIn: convertHydraDxTokenIdToString(assetIn),
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: convertHydraDxTokenIdToString(assetOut),
|
||||
amountOut: amountOut.toString(),
|
||||
sender: who.toString(),
|
||||
receiver: who.toString(),
|
||||
assetIdFee: fee.tokenId,
|
||||
fee: fee.amount,
|
||||
eventIdx: event.idx,
|
||||
success: true,
|
||||
};
|
||||
|
||||
const blockNumber = event.block.block.header.number;
|
||||
logger.info(
|
||||
`Constructed omnipool swap ${JSON.stringify(
|
||||
swap,
|
||||
)} for block ${blockNumber.toString()}`,
|
||||
);
|
||||
|
||||
await createAssetTransmission(event, who.toString(), "-from", { swap: swap });
|
||||
}
|
||||
|
||||
export async function handleHydraRouterSwap(
|
||||
event: SubstrateEvent<RouterSwapArgs>,
|
||||
): Promise<void> {
|
||||
let element = await HistoryElement.get(`${eventId(event)}-from`);
|
||||
if (element !== undefined) {
|
||||
return;
|
||||
}
|
||||
if (event.extrinsic == undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const who = event.extrinsic.extrinsic.signer.toString();
|
||||
const fee = findHydraDxFeeTyped(event.extrinsic.events);
|
||||
const [assetIn, assetOut, amountIn, amountOut] = event.event.data;
|
||||
|
||||
const swap = {
|
||||
assetIdIn: convertHydraDxTokenIdToString(assetIn),
|
||||
amountIn: amountIn.toString(),
|
||||
assetIdOut: convertHydraDxTokenIdToString(assetOut),
|
||||
amountOut: amountOut.toString(),
|
||||
sender: who.toString(),
|
||||
receiver: who.toString(),
|
||||
assetIdFee: fee.tokenId,
|
||||
fee: fee.amount,
|
||||
eventIdx: event.idx,
|
||||
success: true,
|
||||
};
|
||||
|
||||
const blockNumber = event.block.block.header.number;
|
||||
logger.info(
|
||||
`Constructed router swap ${JSON.stringify(
|
||||
swap,
|
||||
)} for block ${blockNumber.toString()}`,
|
||||
);
|
||||
|
||||
await createAssetTransmission(event, who.toString(), "-from", { swap: swap });
|
||||
}
|
||||
|
||||
export type Fee = {
|
||||
tokenId: string;
|
||||
amount: string;
|
||||
};
|
||||
|
||||
export function findHydraDxFeeTyped(events: TypedEventRecord<Codec[]>[]): Fee {
|
||||
return findHydraDxFee(events as EventRecord[]);
|
||||
}
|
||||
|
||||
export function findHydraDxFee(events: EventRecord[]): Fee {
|
||||
const lastCurrenciesDepositEvent = findLastEvent(events, (event) =>
|
||||
isCurrencyDepositedEvent(eventRecordToSubstrateEvent(event)),
|
||||
);
|
||||
|
||||
if (lastCurrenciesDepositEvent == undefined) return findNativeFee(events);
|
||||
|
||||
const {
|
||||
event: {
|
||||
data: [currencyId, _, amount],
|
||||
},
|
||||
} = lastCurrenciesDepositEvent;
|
||||
|
||||
return {
|
||||
tokenId: convertHydraDxTokenIdToString(currencyId),
|
||||
amount: (amount as INumber).toString(),
|
||||
};
|
||||
}
|
||||
|
||||
function isPartOfRouterSwap(events: TypedEventRecord<Codec[]>[]): boolean {
|
||||
const eventRecords = events as EventRecord[];
|
||||
for (const eventRecord of eventRecords) {
|
||||
if (
|
||||
eventRecord.event.section == "router" &&
|
||||
(eventRecord.event.method == "Executed" ||
|
||||
eventRecord.event.method == "RouteExecuted")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function findNativeFee(events: EventRecord[]): Fee {
|
||||
let foundAssetTxFeePaid = extractTransactionPaidFee(events);
|
||||
if (foundAssetTxFeePaid == undefined) foundAssetTxFeePaid = "0";
|
||||
|
||||
return {
|
||||
tokenId: "native",
|
||||
amount: foundAssetTxFeePaid,
|
||||
};
|
||||
}
|
||||
|
||||
export function convertHydraDxTokenIdToString(hydraDxTokenId: Codec): string {
|
||||
const asString = hydraDxTokenId.toString();
|
||||
|
||||
if (asString == "0") {
|
||||
return "native";
|
||||
} else {
|
||||
return convertOrmlCurrencyIdToString(hydraDxTokenId);
|
||||
}
|
||||
}
|
||||
|
||||
function findLastEvent(
|
||||
events: EventRecord[],
|
||||
expression: (event: EventRecord) => boolean,
|
||||
): EventRecord | undefined {
|
||||
const currenciesDepositedEvents = events.filter(expression);
|
||||
|
||||
if (currenciesDepositedEvents.length == 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return currenciesDepositedEvents[currenciesDepositedEvents.length - 1];
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
//Exports all handler functions
|
||||
export * from "./AssetConversion";
|
||||
export * from "./HydraDx";
|
||||
import "@polkadot/api-augment";
|
||||
+17
-1
@@ -7,12 +7,28 @@
|
||||
"importHelpers": true,
|
||||
"resolveJsonModule": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "dist",
|
||||
"rootDir": ".",
|
||||
"target": "es2020",
|
||||
"skipLibCheck": true,
|
||||
"strictNullChecks": false,
|
||||
"strict": true
|
||||
"strict": true,
|
||||
"paths": {
|
||||
"@polkadot/types": ["./node_modules/@pezkuwi/types/cjs/index.d.ts"],
|
||||
"@polkadot/types/*": ["./node_modules/@pezkuwi/types/cjs/*"],
|
||||
"@polkadot/types-codec": ["./node_modules/@pezkuwi/types-codec/cjs/index.d.ts"],
|
||||
"@polkadot/types-codec/*": ["./node_modules/@pezkuwi/types-codec/cjs/*"],
|
||||
"@polkadot/api": ["./node_modules/@pezkuwi/api/cjs/index.d.ts"],
|
||||
"@polkadot/api/*": ["./node_modules/@pezkuwi/api/cjs/*"],
|
||||
"@pezkuwi/types": ["./node_modules/@pezkuwi/types/cjs/index.d.ts"],
|
||||
"@pezkuwi/types/*": ["./node_modules/@pezkuwi/types/cjs/*"],
|
||||
"@pezkuwi/types-codec": ["./node_modules/@pezkuwi/types-codec/cjs/index.d.ts"],
|
||||
"@pezkuwi/types-codec/*": ["./node_modules/@pezkuwi/types-codec/cjs/*"],
|
||||
"@pezkuwi/api": ["./node_modules/@pezkuwi/api/cjs/index.d.ts"],
|
||||
"@pezkuwi/api/*": ["./node_modules/@pezkuwi/api/cjs/*"]
|
||||
},
|
||||
"baseUrl": "."
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
|
||||
Reference in New Issue
Block a user