Skip to content

Commit

Permalink
improve sora links & refactoring (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov authored Apr 1, 2024
1 parent 4bd0f2d commit 205d12a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 91 deletions.
55 changes: 21 additions & 34 deletions src/components/mixins/NetworkFormatterMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SUB_NETWORKS } from '@/consts/sub';
import { state, getter } from '@/store/decorators';
import type { AvailableNetwork } from '@/store/web3/types';
import type { NetworkData } from '@/types/bridge';
import { getSubscanTxLink, getPolkadotTxLink } from '@/utils';
import { getSubstrateExplorerLinks } from '@/utils';
import { isOutgoingTransaction } from '@/utils/bridge/common/utils';
import { isUnsignedToPart } from '@/utils/bridge/eth/utils';

Expand All @@ -27,41 +27,28 @@ const getSubNetworkLinks = (
blockId?: number | string,
eventIndex?: number
): WALLET_CONSTS.ExplorerLink[] => {
const links: Array<WALLET_CONSTS.ExplorerLink> = [];
const explorerUrl = networkData.blockExplorerUrls[0];

switch (type) {
case EvmLinkType.Account: {
if (explorerUrl && value) {
links.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: `${explorerUrl}/account/${value}`,
});
}
break;
}
case EvmLinkType.Transaction: {
if (explorerUrl) {
links.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: getSubscanTxLink(explorerUrl, value, blockId, eventIndex),
});
}

const networkUrl = networkData.nodes?.[0].address;
const polkadotBaseLink = `https://polkadot.js.org/apps/?rpc=${networkUrl}#/explorer/query`;
const polkadotLink = {
type: WALLET_CONSTS.ExplorerType.Polkadot,
value: getPolkadotTxLink(polkadotBaseLink, value, blockId, eventIndex),
};

links.push(polkadotLink);

break;
}
const baseLinks: WALLET_CONSTS.ExplorerLink[] = [];

const subscanLink = networkData.blockExplorerUrls[0];
const polkadotUrl = networkData.nodes?.[0].address;
const polkadotLink = polkadotUrl
? `https://polkadot.js.org/apps/?rpc=${networkData.nodes?.[0].address}#/explorer/query`
: '';

if (subscanLink) {
baseLinks.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: subscanLink,
});
}
if (polkadotLink) {
baseLinks.push({
type: WALLET_CONSTS.ExplorerType.Polkadot,
value: polkadotLink,
});
}

return links.filter((link) => !!link.value);
return getSubstrateExplorerLinks(baseLinks, type === EvmLinkType.Account, value, blockId, eventIndex);
};

const getEvmNetworkLinks = (
Expand Down
25 changes: 15 additions & 10 deletions src/utils/bridge/sub/classes/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { subBridgeApi } from '@/utils/bridge/sub/api';
import { SubNetworksConnector, subBridgeConnector } from '@/utils/bridge/sub/classes/adapter';
import {
getDepositedBalance,
getTokensDepositedBalance,
getParachainBridgeAppMintedBalance,
getMessageAcceptedNonces,
getMessageDispatchedNonces,
isMessageDispatchedNonces,
Expand Down Expand Up @@ -191,11 +191,11 @@ class SubBridgeHistory extends SubNetworksConnector {
events: soraTxEvents,
});
} else {
const [_, eventIndex] = getTokensDepositedBalance(soraBlockEvents, history.from as string, this.soraApi);
// tokens.Deposited event index
history.payload.eventIndex = eventIndex;

return await this.processIncomingTxExternalData({ history, events: soraTxEvents });
return await this.processIncomingTxExternalData({
history,
txEvents: soraTxEvents,
blockEvents: soraBlockEvents,
});
}
} catch (error) {
console.error(`[${id}]`, error);
Expand Down Expand Up @@ -380,13 +380,15 @@ class SubBridgeHistory extends SubNetworksConnector {

private async processIncomingTxExternalData({
history,
events,
txEvents,
blockEvents,
}: {
history: SubHistory;
events: any[];
txEvents: any[];
blockEvents: any[];
}): Promise<Nullable<SubHistory>> {
// find SORA hash event index
const requestStatusUpdateEventIndex = events.findIndex((e) => {
const requestStatusUpdateEventIndex = txEvents.findIndex((e) => {
if (!this.soraApi.events.bridgeProxy.RequestStatusUpdate.is(e.event)) return false;

const hash = e.event.data[0].toString();
Expand All @@ -395,7 +397,7 @@ class SubBridgeHistory extends SubNetworksConnector {
});
// Received on SORA nonces
const [soraBatchNonce, soraMessageNonce] = getMessageDispatchedNonces(
events.slice(requestStatusUpdateEventIndex),
txEvents.slice(requestStatusUpdateEventIndex),
this.soraApi
);
// api for Standalone network or SORA parachain
Expand Down Expand Up @@ -433,6 +435,9 @@ class SubBridgeHistory extends SubNetworksConnector {
const incomingMessageFromRelaychain = networkExtrinsicEvents.find((e) =>
parachainApi.events.parachainSystem.DownwardMessagesProcessed.is(e.event)
);
// Token is minted to account event
const [_, eventIndex] = getParachainBridgeAppMintedBalance(blockEvents, history.from as string, this.soraApi);
history.payload.eventIndex = eventIndex;

if (incomingMessageFromRelaychain) {
history.parachainBlockId = history.externalBlockId;
Expand Down
8 changes: 6 additions & 2 deletions src/utils/bridge/sub/classes/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SubTransferType } from '@/utils/bridge/sub/types';
import {
getBridgeProxyHash,
getDepositedBalance,
getTokensDepositedBalance,
getParachainBridgeAppMintedBalance,
getMessageAcceptedNonces,
isMessageDispatchedNonces,
isAssetAddedToChannel,
Expand Down Expand Up @@ -338,7 +338,11 @@ export class SubBridgeIncomingReducer extends SubBridgeReducer {
const foundedEvents = events.slice(substrateDispatchEventIndex);

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

resolve();
} catch (error) {
Expand Down
31 changes: 17 additions & 14 deletions src/utils/bridge/sub/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ export const getBridgeProxyHash = (events: Array<any>, api: ApiPromise): string

// Native token for network
export const getDepositedBalance = (events: Array<any>, to: string, api: ApiPromise): [string, number] => {
const index = events.findIndex(
(e) =>
api.events.balances.Deposit.is(e.event) &&
subBridgeApi.formatAddress(e.event.data.who.toString()) === subBridgeApi.formatAddress(to)
);
const index = events.findIndex((e) => {
if (!api.events.balances.Deposit.is(e.event)) return false;
return subBridgeApi.formatAddress(e.event.data.who.toString()) === subBridgeApi.formatAddress(to);
});

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

Expand All @@ -64,18 +63,22 @@ 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`);
// for SORA from Relaychain
export const getParachainBridgeAppMintedBalance = (
events: Array<any>,
to: string,
api: ApiPromise
): [string, number] => {
const index = events.findIndex((e) => {
if (!api.events.parachainBridgeApp.Minted.is(e.event)) return false;
return subBridgeApi.formatAddress(e.event.data[3].toString()) === subBridgeApi.formatAddress(to);
});

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

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

return [balance, index];
};
Expand Down
65 changes: 34 additions & 31 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,64 +384,67 @@ export const sortPools = <T extends Asset>(a: PoolAssets<T>, b: PoolAssets<T>) =

export const calcElScrollGutter: () => number = scrollbarWidth;

export const getSubscanTxLink = (
baseUrl: string,
txId?: string,
blockId?: number | string,
eventIndex?: number
): string => {
const getSubscanTxLink = (baseUrl: string, txId?: string, blockId?: number | string, eventIndex?: number): string => {
if (!(txId || blockId)) return '';

let link = txId ? `${baseUrl}/extrinsic/${txId}` : `${baseUrl}/block/${blockId}`;

if (Number.isFinite(eventIndex) && Number.isFinite(blockId)) {
return `${baseUrl}/block/${blockId}?tab=event&event=${blockId}-${eventIndex}`;
} else if (txId) {
return `${baseUrl}/extrinsic/${txId}`;
} else if (blockId) {
return `${baseUrl}/block/${blockId}`;
link += `?event=${blockId}-${eventIndex}`;

if (!txId) {
link += '&tab=event';
}
}
return '';

return link;
};

export const getPolkadotTxLink = (
baseUrl: string,
txId?: string,
blockId?: number | string,
eventIndex?: number
): string => {
const getPolkadotTxLink = (baseUrl: string, txId?: string, blockId?: number | string, eventIndex?: number): string => {
if (blockId) {
return `${baseUrl}/${blockId}`;
}
return '';
};

export const soraExplorerLinks = (
soraNetwork: Nullable<WALLET_CONSTS.SoraNetwork>,
txValue?: string,
export const getSubstrateExplorerLinks = (
baseLinks: WALLET_CONSTS.ExplorerLink[],
isAccount = false,
id?: string, // tx hash or account address
blockId?: number | string,
eventIndex?: number,
isAccount = false
): Array<WALLET_CONSTS.ExplorerLink> => {
if (!soraNetwork) return [];

const baseLinks = getExplorerLinks(soraNetwork);

eventIndex?: number
) => {
if (!baseLinks.length) return [];

if (isAccount) {
return baseLinks
.filter(({ type }) => type !== WALLET_CONSTS.ExplorerType.Polkadot)
.map(({ type, value }) => ({ type, value: `${value}/account/${txValue}` }));
.map(({ type, value }) => ({ type, value: `${value}/account/${id}` }));
}

return baseLinks
.map(({ type, value }) => {
const link = { type } as WALLET_CONSTS.ExplorerLink;

if (type === WALLET_CONSTS.ExplorerType.Subscan) {
link.value = getSubscanTxLink(value, txValue, blockId, eventIndex);
link.value = getSubscanTxLink(value, id, blockId, eventIndex);
} else if (type === WALLET_CONSTS.ExplorerType.Polkadot) {
link.value = getPolkadotTxLink(value, txValue, blockId, eventIndex);
link.value = getPolkadotTxLink(value, id, blockId, eventIndex);
}

return link;
})
.filter((value) => !!value.value);
};

export const soraExplorerLinks = (
soraNetwork: Nullable<WALLET_CONSTS.SoraNetwork>,
txValue?: string,
blockId?: number | string,
eventIndex?: number,
isAccount = false
): Array<WALLET_CONSTS.ExplorerLink> => {
if (!soraNetwork) return [];

return getSubstrateExplorerLinks(getExplorerLinks(soraNetwork), isAccount, txValue, blockId, eventIndex);
};

0 comments on commit 205d12a

Please sign in to comment.