Skip to content

Commit

Permalink
Improve bridge sora links (#1370)
Browse files Browse the repository at this point in the history
* improve external links

* add tokens.Deposited handler

* fix sub blocks left & account formatting

* improve sora links
  • Loading branch information
Nikita-Polyakov authored Mar 29, 2024
1 parent f6f826f commit 4bd0f2d
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 115 deletions.
2 changes: 1 addition & 1 deletion public/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
{
"chain": "Liberland Dev Testnet",
"name": "Soramitsu",
"address": "wss://rpc.liberland-1.lib1.dev.sora2.soramitsu.co.jp"
"address": "wss://ws.liberland-node-1.lib1.dev.sora2.soramitsu.co.jp"
}
]
},
Expand Down
35 changes: 21 additions & 14 deletions src/components/mixins/BridgeTransactionMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class BridgeTransactionMixin extends Mixins(NetworkFormatterMixin
);
}

get txSoraAccount(): string {
get txInternalAccount(): string {
return this.tx?.from ?? '';
}

Expand All @@ -37,21 +37,19 @@ export default class BridgeTransactionMixin extends Mixins(NetworkFormatterMixin
return this.tx?.txId ?? '';
}

get txSoraBlockId(): string {
return this.tx?.blockId ?? '';
}

get txSoraHash(): string {
return this.tx?.hash ?? '';
}

get txInternalHash(): string {
if (!this.isOutgoing) return this.txSoraHash;
get txInternalBlockNumber(): number | undefined {
return this.tx?.blockHeight;
}

return this.txSoraHash || this.txSoraBlockId || this.txSoraId;
get txInternalBlockId(): string {
return this.tx?.blockId ?? '';
}

get txExternalEventIndex(): number | undefined {
get txEventIndex(): number | undefined {
return this.tx?.payload?.eventIndex;
}

Expand All @@ -63,6 +61,10 @@ export default class BridgeTransactionMixin extends Mixins(NetworkFormatterMixin
return this.tx?.externalBlockHeight;
}

get txExternalBlockId(): string {
return this.tx?.externalBlockId ?? '';
}

get externalNetworkType(): Nullable<BridgeNetworkType> {
return this.tx?.externalNetworkType;
}
Expand All @@ -71,8 +73,13 @@ export default class BridgeTransactionMixin extends Mixins(NetworkFormatterMixin
return this.tx?.externalNetwork;
}

get soraExplorerLinks(): Array<WALLET_CONSTS.ExplorerLink> {
return soraExplorerLinks(this.soraNetwork, this.txSoraId, this.txSoraBlockId);
get internalExplorerLinks(): Array<WALLET_CONSTS.ExplorerLink> {
return soraExplorerLinks(
this.soraNetwork,
this.txSoraId,
this.txInternalBlockNumber ?? this.txInternalBlockId,
this.txEventIndex
);
}

get externalExplorerLinks(): Array<WALLET_CONSTS.ExplorerLink> {
Expand All @@ -82,13 +89,13 @@ export default class BridgeTransactionMixin extends Mixins(NetworkFormatterMixin
this.externalNetworkType,
this.externalNetworkId,
this.txExternalHash,
this.txExternalBlockNumber,
this.txExternalEventIndex
this.txExternalBlockNumber ?? this.txExternalBlockId,
this.txEventIndex
);
}

get internalAccountLinks(): Array<WALLET_CONSTS.ExplorerLink> {
return soraExplorerLinks(this.soraNetwork, this.txSoraAccount, this.txSoraBlockId, true);
return soraExplorerLinks(this.soraNetwork, this.txInternalAccount, undefined, undefined, true);
}

get externalAccountLinks(): Array<WALLET_CONSTS.ExplorerLink> {
Expand Down
47 changes: 17 additions & 30 deletions src/components/mixins/NetworkFormatterMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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 { isOutgoingTransaction } from '@/utils/bridge/common/utils';
import { isUnsignedToPart } from '@/utils/bridge/eth/utils';

Expand All @@ -23,7 +24,7 @@ const getSubNetworkLinks = (
networkData: NetworkData,
type: EvmLinkType,
value?: string, // tx hash or account address
blockNumber?: number,
blockId?: number | string,
eventIndex?: number
): WALLET_CONSTS.ExplorerLink[] => {
const links: Array<WALLET_CONSTS.ExplorerLink> = [];
Expand All @@ -41,40 +42,26 @@ const getSubNetworkLinks = (
}
case EvmLinkType.Transaction: {
if (explorerUrl) {
if (value) {
links.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: `${explorerUrl}/extrinsic/${value}`,
});
} else if (Number.isFinite(blockNumber)) {
let link = `${explorerUrl}/block/${blockNumber}`;

if (Number.isFinite(eventIndex)) {
link += `?tab=event&event=${blockNumber}-${eventIndex}`;
}

links.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: link,
});
}
links.push({
type: WALLET_CONSTS.ExplorerType.Subscan,
value: getSubscanTxLink(explorerUrl, value, blockId, eventIndex),
});
}

if (Number.isFinite(blockNumber)) {
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: `${polkadotBaseLink}/${blockNumber}`,
};
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);

links.push(polkadotLink);
}
break;
}
}

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

const getEvmNetworkLinks = (
Expand Down Expand Up @@ -186,7 +173,7 @@ export default class NetworkFormatterMixin extends Mixins(TranslationMixin) {
networkType: BridgeNetworkType,
networkId: BridgeNetworkId,
value?: string,
blockNumber?: number,
blockId?: number | string,
eventIndex?: number,
type = EvmLinkType.Transaction
): Array<WALLET_CONSTS.ExplorerLink> {
Expand All @@ -198,7 +185,7 @@ export default class NetworkFormatterMixin extends Mixins(TranslationMixin) {
}

return networkType === BridgeNetworkType.Sub
? getSubNetworkLinks(networkData, type, value, blockNumber, eventIndex)
? getSubNetworkLinks(networkData, type, value, blockId, eventIndex)
: getEvmNetworkLinks(networkData, type, value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/pages/Bridge/TransferNotification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class BridgeTransferNotification extends Mixins(BridgeTransaction
}
get txLink() {
const link = this.isOutgoing ? this.externalExplorerLinks[0] : this.soraExplorerLinks[0];
const link = this.isOutgoing ? this.externalExplorerLinks[0] : this.internalExplorerLinks[0];
const network = this.isOutgoing ? this.externalNetworkId : undefined;
return this.prepareLink(link, network);
Expand Down
10 changes: 4 additions & 6 deletions src/store/bridge/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ const getters = defineGetters<BridgeState>()({
},

externalAccountFormatted(...args): string {
const { getters, state, rootState } = bridgeGetterContext(args);
const { getters, rootState } = bridgeGetterContext(args);
const { subSS58 } = rootState.web3;

if (getters.isSubBridge && state.isSoraToEvm && getters.externalAccount) {
return formatSubAddress(getters.externalAccount, subSS58);
} else {
return getters.externalAccount;
}
if (!getters.isSubBridge) return getters.externalAccount;

return formatSubAddress(getters.externalAccount, subSS58);
},

sender(...args): string {
Expand Down
32 changes: 18 additions & 14 deletions src/utils/bridge/sub/classes/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, txEvents, blockEvents };
};

class SubBridgeHistory extends SubNetworksConnector {
Expand Down Expand Up @@ -176,22 +176,26 @@ class SubBridgeHistory extends SubNetworksConnector {
history.blockId = blockId;
history.externalBlockId = externalBlockId;

const [{ hash, events: soraEvents }, startTime] = await Promise.all([
const [{ tx: soraTx, txEvents: soraTxEvents, blockEvents: soraBlockEvents }, 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) {
return await this.processOutgoingTxExternalData({
history,
asset,
soraEvents,
events: soraTxEvents,
});
} else {
return await this.processIncomingTxExternalData({ history, soraEvents });
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 });
}
} catch (error) {
console.error(`[${id}]`, error);
Expand All @@ -212,17 +216,17 @@ class SubBridgeHistory extends SubNetworksConnector {
private async processOutgoingTxExternalData({
history,
asset,
soraEvents,
events,
}: {
history: SubHistory;
asset: Nullable<RegisteredAccountAsset>;
soraEvents: any[];
events: any[];
}): Promise<SubHistory> {
// update SORA network fee
const soraFeeEvent = soraEvents.find((e) => this.soraApi.events.transactionPayment.TransactionFeePaid.is(e.event));
const soraFeeEvent = events.find((e) => this.soraApi.events.transactionPayment.TransactionFeePaid.is(e.event));
history.soraNetworkFee = soraFeeEvent.event.data[1].toString();
// sended from SORA nonces
const [soraBatchNonce, soraMessageNonce] = getMessageAcceptedNonces(soraEvents, this.soraApi);
const [soraBatchNonce, soraMessageNonce] = getMessageAcceptedNonces(events, this.soraApi);
// api for Standalone network or SORA parachain
const networkApi = this.getIntermediateApi(history);
const networkBlockId = history.externalBlockId as string;
Expand Down Expand Up @@ -376,13 +380,13 @@ class SubBridgeHistory extends SubNetworksConnector {

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

const hash = e.event.data[0].toString();
Expand All @@ -391,7 +395,7 @@ class SubBridgeHistory extends SubNetworksConnector {
});
// Received on SORA nonces
const [soraBatchNonce, soraMessageNonce] = getMessageDispatchedNonces(
soraEvents.slice(requestStatusUpdateEventIndex),
events.slice(requestStatusUpdateEventIndex),
this.soraApi
);
// api for Standalone network or SORA parachain
Expand Down
Loading

0 comments on commit 4bd0f2d

Please sign in to comment.