Skip to content

Commit

Permalink
add PRUSD (#604)
Browse files Browse the repository at this point in the history
* Fix XORless events parsing

* add PRUSD

* add PRUSD

* format

* Update tsconfig.tsbuildinfo

---------

Co-authored-by: Vladimir Stepanenko <vovac12@gmail.com>
  • Loading branch information
RustemYuzlibaev and vovac12 authored Dec 16, 2024
1 parent cbc41d9 commit a33af1b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
14 changes: 8 additions & 6 deletions src/mappings/events/networkFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import { SubstrateEvent } from '@subql/types';
import { XOR } from '../../utils/consts';
import { getAmountUSD, formatU128ToBalance } from '../../utils/assets';
import { accountMetaStorage } from '../../utils/account';
import { getEventData } from '../../utils/events';
import { getFeeWithdrawnData } from '../../utils/events';
import { networkSnapshotsStorage } from '../../utils/network';
import { logStartProcessingEvent } from '../../utils/logs';

export async function handleNetworkFee(event: SubstrateEvent): Promise<void> {
logStartProcessingEvent(event);

const [account, fee] = getEventData(event);
const { accountId, fee, assetId } = getFeeWithdrawnData(event);

const accountId = account.toString();
const assetId = XOR;
const amount = formatU128ToBalance(fee.toString(), assetId);
if (assetId !== XOR) {
throw new Error('XORless transactions is not supported!');
}

const amount = formatU128ToBalance(fee, assetId);
const amountUSD = await getAmountUSD(event.block, assetId, amount);

await accountMetaStorage.updateFees(event.block, accountId, amount, amountUSD);
await networkSnapshotsStorage.updateFeesStats(event.block, BigInt(fee.toString()));
await networkSnapshotsStorage.updateFeesStats(event.block, BigInt(fee));
}
21 changes: 10 additions & 11 deletions src/mappings/events/referrerRewardHandler.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import { XOR } from '../../utils/consts';
import { SubstrateEvent } from '@subql/types';
import { ReferrerReward } from '../../types';
import { formatDateTimestamp } from '../../utils';
import { getEventData } from '../../utils/events';
import { getReferrerRewardedData } from '../../utils/events';
import { getEventHandlerLog, logStartProcessingEvent } from '../../utils/logs';

export async function referrerRewardHandler(event: SubstrateEvent): Promise<void> {
logStartProcessingEvent(event);

const [referral, referrer, amount] = getEventData(event);
const { referral, referrer, amount, assetId } = getReferrerRewardedData(event);

const key = `${referral.toString()}-${referrer.toString()}`;
if (assetId !== XOR) {
throw new Error('XORless referrer rewards is not supported!');
}

const key = `${referral}-${referrer}`;

let referrerReward = await ReferrerReward.get(key);

if (!referrerReward) {
referrerReward = new ReferrerReward(
key,
referral.toString(),
referrer.toString(),
formatDateTimestamp(event.block.timestamp),
BigInt(0)
);
referrerReward = new ReferrerReward(key, referral, referrer, formatDateTimestamp(event.block.timestamp), BigInt(0));
}

referrerReward.updated = formatDateTimestamp(event.block.timestamp);

referrerReward.amount = referrerReward.amount + BigInt(amount.toString());
referrerReward.amount = referrerReward.amount + BigInt(amount);

await referrerReward.save();

Expand Down
4 changes: 3 additions & 1 deletion src/utils/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const KUSD: string = '0x02000c0000000000000000000000000000000000000000000
export const KGOLD: string = '0x02000d0000000000000000000000000000000000000000000000000000000000';
export const KXOR: string = '0x02000e0000000000000000000000000000000000000000000000000000000000';
export const KARMA: string = '0x02000f0000000000000000000000000000000000000000000000000000000000';
export const PRUSD: string = '0x0200100000000000000000000000000000000000000000000000000000000000';

export const VXOR: string = '0x006a271832f44c93bd8692584d85415f0f3dccef9748fecd129442c8edcb4361';

Expand All @@ -36,9 +37,10 @@ export const predefinedAssets = {
KGOLD: KGOLD,
KXOR: KXOR,
KARMA: KARMA,
PRUSD: PRUSD,
};

export const BASE_ASSETS = [XOR, XSTUSD, KUSD, VXOR];
export const BASE_ASSETS = [XOR, XSTUSD, KUSD, VXOR, PRUSD];

export const DOUBLE_PRICE_POOL = [VAL, PSWAP, DAI, ETH, XST, TBCD, DOT];

Expand Down
49 changes: 48 additions & 1 deletion src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const isTokenDepositedEvent = (e: TypedEventRecord<Codec[]>) => {

export const isAccessGrantEvent = (e: TypedEventRecord<Codec[]>) => {
return isEvent(e, 'extendedAssets', 'SBTExpirationUpdated');
}
};

// substrate 3
export const isCurrencyDepositedEvent = (e: TypedEventRecord<Codec[]>) => {
Expand Down Expand Up @@ -86,3 +86,50 @@ export const getDepositedEventData = (e: TypedEventRecord<Codec[]>) => {
amount: amount.toString(),
};
};

export const getFeeWithdrawnData = (e: TypedEventRecord<Codec[]>) => {
const data = getEventData(e);
const accountId = data[0].toString();
if (data.length === 2) {
/// FeeWithdrawn(AccountIdOf<T>, BalanceOf<T>)
return {
accountId,
assetId: XOR,
fee: data[1].toString(),
};
} else if (data.length === 3) {
/// FeeWithdrawn(AccountIdOf<T>, AssetIdOf<T>, Balance)
return {
accountId,
assetId: getAssetId(data[1]),
fee: data[2].toString(),
};
} else {
throw new Error('FeeWithdrawn: wrong event data');
}
};

export const getReferrerRewardedData = (e: TypedEventRecord<Codec[]>) => {
const data = getEventData(e);
const referral = data[0].toString();
const referrer = data[1].toString();
if (data.length === 3) {
/// ReferrerRewarded(AccountIdOf<T>, AccountIdOf<T>, Balance)
return {
referral,
referrer,
assetId: XOR,
amount: data[2].toString(),
};
} else if (data.length === 4) {
/// ReferrerRewarded(AccountIdOf<T>, AccountIdOf<T>, AssetIdOf<T>, Balance)
return {
referral,
referrer,
assetId: getAssetId(data[2]),
amount: data[3].toString(),
};
} else {
throw new Error('ReferrerRewarded: wrong event data');
}
};

0 comments on commit a33af1b

Please sign in to comment.