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

[Bridge] fix issues #1409

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/store/bridge/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
}
}

function bridgeDataToHistoryItem(

Check failure on line 94 in src/store/bridge/actions.ts

View check run for this annotation

Soramitsu-Sonar-PR-decoration / polkaswap-exchange-web Sonarqube Results

src/store/bridge/actions.ts#L94

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
context: ActionContext<any, any>,
{ date = Date.now(), payload = {}, ...params } = {}
): IBridgeTransaction {
Expand All @@ -106,7 +106,9 @@
: BridgeNetworkType.Sub;

const [from, to] = isSubBridge
? [getters.sender, getters.recipient]
? state.isSoraToEvm

Check warning on line 109 in src/store/bridge/actions.ts

View check run for this annotation

Soramitsu-Sonar-PR-decoration / polkaswap-exchange-web Sonarqube Results

src/store/bridge/actions.ts#L109

Extract this nested ternary operation into an independent statement.
? [getters.sender, getters.recipient]
: [getters.recipient, getters.sender]
: [rootState.wallet.account.address, getters.externalAccount];

const data = {
Expand Down
16 changes: 8 additions & 8 deletions src/utils/bridge/sub/classes/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@
asset: RegisteredAccountAsset;
events: any[];
}): Promise<Nullable<SubHistory>> {
const { soraParachainApi } = this;

if (!soraParachainApi) throw new Error('SORA Parachain Api is not exists');

// update SORA network fee
const soraFeeEvent = events.find((e) => this.soraApi.events.transactionPayment.TransactionFeePaid.is(e.event));
history.soraNetworkFee = soraFeeEvent.event.data[1].toString();
Expand All @@ -261,6 +257,10 @@
return await this.processOutgoingToLiberland(history);
}

const { soraParachainApi } = this;

if (!soraParachainApi) throw new Error('SORA Parachain Api is not exists');

// SORA Parachain extrinsic events for next search
const parachainExtrinsicEvents = networkEventsReversed.slice(messageDispatchedIndex);
// sended from SORA Parachain to Relaychain message hash (1)
Expand Down Expand Up @@ -354,7 +354,7 @@
return history;
}

private async processIncomingTx({

Check failure on line 357 in src/utils/bridge/sub/classes/history.ts

View check run for this annotation

Soramitsu-Sonar-PR-decoration / polkaswap-exchange-web Sonarqube Results

src/utils/bridge/sub/classes/history.ts#L357

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
history,
txEvents,
blockEvents,
Expand All @@ -363,10 +363,7 @@
txEvents: any[];
blockEvents: any[];
}): Promise<Nullable<SubHistory>> {
const { soraApi, soraParachainApi } = this;

if (!soraParachainApi) throw new Error('SORA Parachain Api is not exists');

const { soraApi } = this;
// Token is minted to account event
const [_, eventIndex] = getDepositedBalance(blockEvents, history.from as string, this.soraApi);
history.payload.eventIndex = eventIndex;
Expand Down Expand Up @@ -425,6 +422,9 @@
return history;
}

const { soraParachainApi } = this;
if (!soraParachainApi) throw new Error('SORA Parachain Api is not exists');

// If transfer received from Parachain, extrinsic events should have xcmpQueue.Success event
const messageEvent = networkExtrinsicEvents.find((e) => soraParachainApi.events.xcmpQueue.Success.is(e.event));
const externalNetwork = history.externalNetwork as SubNetwork;
Expand Down
10 changes: 7 additions & 3 deletions src/utils/bridge/sub/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ export const getBridgeProxyHash = (events: Array<any>, api: ApiPromise): string
return bridgeProxyEvent.event.data[0].toString();
};

const isEvent = (e, section: string, method: string) => {
return e.event.section === section && e.event.method === method;
};

export const getDepositedBalance = (events: Array<any>, to: string, api: ApiPromise): [string, number] => {
const recipient = subBridgeApi.formatAddress(to);

const index = events.findIndex((e) => {
let eventRecipient = '';

if (api.events.balances?.Deposit.is(e.event) || api.events.tokens?.Deposited.is(e.event)) {
if (isEvent(e, 'balances', 'Deposit') || isEvent(e, 'balances', 'Minted') || isEvent(e, 'tokens', 'Deposited')) {
eventRecipient = e.event.data.who.toString();
} else if (api.events.assets?.Transfer.is(e.event)) {
} else if (isEvent(e, 'assets', 'Transfer')) {
eventRecipient = e.event.data[1].toString();
}

Expand All @@ -65,7 +69,7 @@ export const getDepositedBalance = (events: Array<any>, to: string, api: ApiProm
return subBridgeApi.formatAddress(eventRecipient) === recipient;
});

if (index === -1) throw new Error(`Unable to find "balances.Deposit" or "tokens.Deposited" event`);
if (index === -1) throw new Error(`Unable to find balance deposit like event`);

const event = events[index];
const balance = event.event.data.amount?.toString() ?? event.event.data[3].toString();
Expand Down
15 changes: 2 additions & 13 deletions src/views/BridgeTransaction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,10 @@ export default class BridgeTransaction extends Mixins(
return this.txSoraHash || this.txInternalBlockId || this.txSoraId;
}

private getTxAccountAddress(isInternal = true) {
const accounts = [this.txInternalAccount, this.txExternalAccount];
const [a, b] = isInternal ? accounts : [...accounts].reverse();

if (this.isEvmTxType) return a;

return this.isOutgoing ? a : b;
}

get accountLinks(): LinkData[] {
const name = this.t('accountAddressText');
const internalAddress = this.getTxAccountAddress(true);
const externalAddress = this.getTxAccountAddress(false);
const internal = this.getLinkData(internalAddress, this.internalAccountLinks, name);
const external = this.getLinkData(externalAddress, this.externalAccountLinks, name, this.externalNetworkId);
const internal = this.getLinkData(this.txInternalAccount, this.internalAccountLinks, name);
const external = this.getLinkData(this.txExternalAccount, this.externalAccountLinks, name, this.externalNetworkId);

return this.sortLinksByTxDirection([internal, external]);
}
Expand Down