Skip to content

Commit

Permalink
fix: use xTokens pallet on astar (#1004)
Browse files Browse the repository at this point in the history
* fix: use xtokens pallet on astar

* fix: dest balance

* fix: renamed statemint to AssetHub
  • Loading branch information
impelcrypto committed Nov 2, 2023
1 parent bab1445 commit 8bae1d5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 205 deletions.
22 changes: 22 additions & 0 deletions src/assets/img/chain/asset-hub.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 23 additions & 5 deletions src/hooks/xcm/useXcmBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
capitalize,
isValidAddressPolkadotAddress,
isValidEvmAddress,
wait,
} from '@astar-network/astar-sdk-core';
import { ApiPromise } from '@polkadot/api';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -35,6 +36,8 @@ import { Ref, computed, ref, watch, watchEffect } from 'vue';
import { useI18n } from 'vue-i18n';

import { evmToAddress } from '@polkadot/util-crypto';
import { LOCAL_STORAGE } from 'src/config/localStorage';
import { castChainName, castXcmEndpoint } from 'src/modules/xcm';
import { Path } from 'src/router';
import { container } from 'src/v2/common';
import { AstarToken } from 'src/v2/config/xcm/XcmRepositoryConfiguration';
Expand All @@ -47,8 +50,6 @@ import {
} from 'src/v2/services';
import { Symbols } from 'src/v2/symbols';
import { useRouter } from 'vue-router';
import { castChainName, castXcmEndpoint } from 'src/modules/xcm';
import { LOCAL_STORAGE } from 'src/config/localStorage';

const { Acala, Astar, Karura, Polkadot, Shiden } = xcmChainObj;

Expand Down Expand Up @@ -610,16 +611,33 @@ export function useXcmBridge(selectedToken: Ref<Asset>) {
await setOriginChainNativeBal();
});

// Memo: to avoid using previous endpoint to fetch destChainBalance and it causes a bug (ex: acala -> statemint)
const balMonitorDelay = 500;

watch(
[isLoadingApi, currentAccount, selectedToken, srcChain, originChainApiEndpoint],
async () => {
// Memo: to display the balance with the same timing as destination balance
await wait(balMonitorDelay);
await monitorFromChainBalance();
}
);

watchEffect(async () => {
await monitorDestChainBalance(inputtedAddress.value);
});
watch(
[
isLoadingApi,
currentAccount,
selectedToken,
destChain,
originChainApiEndpoint,
inputtedAddress,
],
async () => {
await wait(balMonitorDelay);
await monitorDestChainBalance(inputtedAddress.value);
},
{ immediate: true }
);

return {
amount,
Expand Down
20 changes: 10 additions & 10 deletions src/modules/xcm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ export let xcmChainObj: XcmChainObj = {
subscan: 'https://shiden.subscan.io',
isAstarNativeToken: false,
},
[Chain.STATEMINE]: {
name: Chain.STATEMINE,
[Chain.ASSET_HUB_KUSAMA]: {
name: Chain.ASSET_HUB_KUSAMA,
relayChain: Chain.KUSAMA,
img: require('/src/assets/img/chain/statemine.svg'),
parachainId: parachainIds.STATEMINE,
img: require('/src/assets/img/chain/asset-hub.svg'),
parachainId: parachainIds.ASSET_HUB_KUSAMA,
endpoints: [
'wss://kusama-asset-hub-rpc.polkadot.io',
'wss://statemine-rpc.dwellir.com',
'wss://rpc-asset-hub-kusama.luckyfriday.io',
'wss://statemine.api.onfinality.io/public-ws',
'wss://statemine.public.curie.radiumblock.co/ws',
],
subscan: 'https://statemine.subscan.io',
subscan: 'https://assethub-kusama.subscan.io',
isAstarNativeToken: false,
},
[Chain.KARURA]: {
Expand Down Expand Up @@ -220,18 +220,18 @@ export let xcmChainObj: XcmChainObj = {
subscan: 'https://moonbeam.subscan.io',
isAstarNativeToken: true,
},
[Chain.STATEMINT]: {
name: Chain.STATEMINT,
[Chain.ASSET_HUB]: {
name: Chain.ASSET_HUB,
relayChain: Chain.POLKADOT,
img: require('/src/assets/img/chain/statemine.svg'),
parachainId: parachainIds.STATEMINT,
img: require('/src/assets/img/chain/asset-hub.svg'),
parachainId: parachainIds.ASSET_HUB,
endpoints: [
'wss://statemint-rpc.dwellir.com',
'wss://polkadot-asset-hub-rpc.polkadot.io',
'wss://statemint.api.onfinality.io/public-ws',
'wss://statemint.public.curie.radiumblock.co/ws',
],
subscan: 'https://statemint.subscan.io',
subscan: 'https://assethub-polkadot.subscan.io',
isAstarNativeToken: false,
},
[Chain.KINTSUGI]: {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/xcm/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const xcmToken = {
originAssetId: '1984',
logo: require('/src/assets/img/token/usdt.png'),
isXcmCompatible: true,
originChain: Chain.STATEMINT,
originChain: Chain.ASSET_HUB,
minBridgeAmount: '1.5',
},
{
Expand Down Expand Up @@ -203,7 +203,7 @@ export const xcmToken = {
originAssetId: '1984',
logo: require('/src/assets/img/token/usdt.png'),
isXcmCompatible: true,
originChain: Chain.STATEMINE,
originChain: Chain.ASSET_HUB_KUSAMA,
minBridgeAmount: '0.1',
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/v2/config/xcm/XcmRepositoryConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const XcmRepositoryConfiguration: TypeMapping = {
[Chain.SHIDEN]: AstarXcmRepository,
[Chain.ACALA]: AcalaXcmRepository,
[Chain.KARURA]: AcalaXcmRepository,
[Chain.STATEMINE]: StatemintXcmRepository,
[Chain.STATEMINT]: StatemintXcmRepository,
[Chain.ASSET_HUB_KUSAMA]: StatemintXcmRepository,
[Chain.ASSET_HUB]: StatemintXcmRepository,
[Chain.MOONBEAM]: MoonbeamXcmRepository,
[Chain.MOONRIVER]: MoonbeamXcmRepository,
[Chain.POLKADOT]: PolkadotXcmRepository,
Expand Down
8 changes: 4 additions & 4 deletions src/v2/models/XcmModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export enum Chain {
ACALA = 'Acala',
MOONRIVER = 'Moonriver',
MOONBEAM = 'Moonbeam',
STATEMINE = 'Statemine',
STATEMINT = 'Statemint',
ASSET_HUB = 'Asset-hub',
ASSET_HUB_KUSAMA = 'Asset-hub-kusama',
KINTSUGI = 'Kintsugi',
INTERLAY = 'Interlay',
CRUST_SHADOW = 'Crust-shadow',
Expand All @@ -30,8 +30,8 @@ export enum parachainIds {
ACALA = 2000,
MOONRIVER = 2023,
MOONBEAM = 2004,
STATEMINE = 1000,
STATEMINT = 1000,
ASSET_HUB = 1000,
ASSET_HUB_KUSAMA = 1000,
KINTSUGI = 2092,
INTERLAY = 2032,
CRUST_SHADOW = 2012,
Expand Down
79 changes: 0 additions & 79 deletions src/v2/repositories/implementations/XcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,6 @@ export class XcmRepository implements IXcmRepository {
recipientAddress: string,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
const isAstar = from.name === 'Astar';
return isAstar
? await this.getPolkadotXcmToOriginChainCall(from, recipientAddress, amount, endpoint)
: await this.getXtokensToOriginChainCall(from, recipientAddress, amount, endpoint);
}

private async getXtokensToOriginChainCall(
from: XcmChain,
recipientAddress: string,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);

Expand Down Expand Up @@ -244,73 +232,6 @@ export class XcmRepository implements IXcmRepository {
);
}

// Memo: Remove this method after implementing Xtokens on Astar
private async getPolkadotXcmToOriginChainCall(
from: XcmChain,
recipientAddress: string,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);
const version = 'V3';
const destination = {
[version]: {
interior: 'Here',
parents: new BN(1),
},
};

const id = decodeAddress(recipientAccountId);
const AccountId32 = {
id,
};

const beneficiary = {
[version]: {
interior: {
X1: {
AccountId32,
},
},
parents: new BN(0),
},
};

const asset = {
Concrete: {
interior: 'Here',
parents: new BN(1),
},
};

const assets = {
[version]: [
{
fun: {
Fungible: new BN(amount),
},
id: asset,
},
],
};

const weightLimit = {
Unlimited: null,
};

return await this.buildTxCall(
from,
endpoint,
'polkadotXcm',
'limitedReserveWithdrawAssets',
destination,
beneficiary,
assets,
new BN(0),
weightLimit
);
}

public getTransferCall(
from: XcmChain,
to: XcmChain,
Expand Down
102 changes: 0 additions & 102 deletions src/v2/repositories/implementations/xcm/AstarXcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ export class AstarXcmRepository extends XcmRepository {
if (!to.parachainId) {
throw `Parachain id for ${to.name} is not defined`;
}
const isAstar = from.name === 'Astar';
return isAstar
? await this.getPolkadotXcmCall(from, to, recipientAddress, token, amount, endpoint)
: await this.getXtokensCall(from, to, recipientAddress, token, amount, endpoint);
}

private async getXtokensCall(
from: XcmChain,
to: XcmChain,
recipientAddress: string,
token: Asset,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);
const isWithdrawAssets = token.id !== this.astarNativeTokenId;

Expand Down Expand Up @@ -115,93 +102,4 @@ export class AstarXcmRepository extends XcmRepository {
weightLimit
);
}

// Memo: Remove this method after implementing Xtokens on Astar
private async getPolkadotXcmCall(
from: XcmChain,
to: XcmChain,
recipientAddress: string,
token: Asset,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);
const version = 'V3';
const isWithdrawAssets = token.id !== this.astarNativeTokenId;
const functionName = isWithdrawAssets
? 'limitedReserveWithdrawAssets'
: 'limitedReserveTransferAssets';

const destination = {
[version]: {
interior: {
X1: {
Parachain: new BN(to.parachainId),
},
},
parents: new BN(1),
},
};

const isAccountId20 = ethWalletChains.includes(to.name);

const X1_V3 = isAccountId20
? {
AccountKey20: {
key: recipientAccountId,
},
}
: {
AccountId32: {
id: decodeAddress(recipientAccountId),
},
};

const beneficiary = {
[version]: {
interior: {
X1: X1_V3,
},
parents: new BN(0),
},
};

const asset = isWithdrawAssets
? {
Concrete: await this.fetchAssetConfig(from, token, endpoint),
}
: {
Concrete: {
interior: 'Here',
parents: new BN(0),
},
};

const assets = {
[version]: [
{
fun: {
Fungible: new BN(amount),
},
id: asset,
},
],
};

const weightLimit = {
Unlimited: null,
};

return await this.buildTxCall(
from,
endpoint,
'polkadotXcm',
functionName,
destination,
beneficiary,
assets,
new BN(0),
weightLimit
);
}
}
Loading

0 comments on commit 8bae1d5

Please sign in to comment.