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

Improve bridge sora links #1370

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tokens.Deposited handler
Nikita-Polyakov committed Mar 29, 2024
commit da471536748add1ed1d1375873bdcc3660c86b00
12 changes: 8 additions & 4 deletions src/utils/bridge/sub/classes/history.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import { subBridgeApi } from '@/utils/bridge/sub/api';
import { SubNetworksConnector, subBridgeConnector } from '@/utils/bridge/sub/classes/adapter';
import {
getDepositedBalance,
getTokensDepositedBalance,
getMessageAcceptedNonces,
getMessageDispatchedNonces,
isMessageDispatchedNonces,
@@ -54,9 +55,8 @@ const findTxInBlock = async (blockHash: string, soraHash: string) => {
const txEvents = getTxEvents(blockEvents, txIndex);
const extrinsics = await api.system.getExtrinsicsFromBlock(blockHash);
const tx = extrinsics[txIndex];
const txHash = tx.hash.toString();

return { hash: txHash, events: txEvents };
return { tx, events: txEvents };
};

class SubBridgeHistory extends SubNetworksConnector {
@@ -176,12 +176,12 @@ class SubBridgeHistory extends SubNetworksConnector {
history.blockId = blockId;
history.externalBlockId = externalBlockId;

const [{ hash, events: soraEvents }, startTime] = await Promise.all([
const [{ tx: soraTx, events: soraEvents }, startTime] = await Promise.all([
findTxInBlock(blockId, id),
api.system.getBlockTimestamp(blockId, this.soraApi),
]);

history.txId = hash;
history.txId = soraTx.hash.toString();
history.startTime = history.endTime = startTime;

if (isOutgoing) {
@@ -381,6 +381,10 @@ class SubBridgeHistory extends SubNetworksConnector {
history: SubHistory;
soraEvents: any[];
}): Promise<Nullable<SubHistory>> {
const [_, eventIndex] = getTokensDepositedBalance(soraEvents, history.from as string, this.soraApi);
// tokens.Deposited event index
history.payload.eventIndex = eventIndex;

// find SORA hash event index
const requestStatusUpdateEventIndex = soraEvents.findIndex((e) => {
if (!this.soraApi.events.bridgeProxy.RequestStatusUpdate.is(e.event)) return false;
18 changes: 12 additions & 6 deletions src/utils/bridge/sub/classes/reducers.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import { SubTransferType } from '@/utils/bridge/sub/types';
import {
getBridgeProxyHash,
getDepositedBalance,
getTokensDepositedBalance,
getMessageAcceptedNonces,
isMessageDispatchedNonces,
isAssetAddedToChannel,
@@ -290,8 +291,7 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
}
}

const received = FPNumber.fromCodecValue(recipientAmount, this.asset.externalDecimals);
const amount2 = received.toString();
const amount2 = FPNumber.fromCodecValue(recipientAmount, this.asset.externalDecimals).toString();

this.updateTransactionPayload(id, { messageNonce, batchNonce });
this.updateTransactionParams(id, { amount2 });
@@ -310,6 +310,8 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {

let subscription!: Subscription;
let soraHash!: string;
let amount!: string;
let eventIndex!: number;

try {
await new Promise<void>((resolve, reject) => {
@@ -324,7 +326,10 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {

if (substrateDispatchEventIndex === -1) return;

soraHash = getBridgeProxyHash(events.slice(substrateDispatchEventIndex), subBridgeApi.api);
const foundedEvents = events.slice(substrateDispatchEventIndex);

soraHash = getBridgeProxyHash(foundedEvents, subBridgeApi.api);
[amount, eventIndex] = getTokensDepositedBalance(foundedEvents, tx.from as string, subBridgeApi.api);

resolve();
} catch (error) {
@@ -336,9 +341,10 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
subscription.unsubscribe();
}

this.updateTransactionParams(id, {
hash: soraHash,
});
const amount2 = FPNumber.fromCodecValue(amount, this.asset.decimals).toString();

this.updateTransactionParams(id, { hash: soraHash, amount2 });
this.updateTransactionPayload(id, { eventIndex });
}

private async waitSoraBlockByHash(id: string): Promise<void> {
15 changes: 15 additions & 0 deletions src/utils/bridge/sub/utils.ts
Original file line number Diff line number Diff line change
@@ -64,6 +64,21 @@ export const getDepositedBalance = (events: Array<any>, to: string, api: ApiProm

return [balance, index];
};
// for SORA
export const getTokensDepositedBalance = (events: Array<any>, to: string, api: ApiPromise): [string, number] => {
const index = events.findIndex(
(e) =>
api.events.tokens.Deposited.is(e.event) &&
subBridgeApi.formatAddress(e.event.data.who.toString()) === subBridgeApi.formatAddress(to)
);

if (index === -1) throw new Error(`Unable to find "tokens.Deposited" event`);

const event = events[index];
const balance = event.event.data.amount.toString();

return [balance, index];
};

export const getReceivedAmount = (sendedAmount: string, receivedAmount: CodecString, decimals?: number) => {
const sended = new FPNumber(sendedAmount, decimals);