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

feat: updated XCM logic to V3 for Shiden, Statemine and Kusama #758

Merged
merged 12 commits into from
May 7, 2023
62 changes: 62 additions & 0 deletions playwright-report/index.html

Large diffs are not rendered by default.

24 changes: 10 additions & 14 deletions src/components/assets/transfer/styles/select-evm-wallet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,11 @@
row-gap: 8px;
padding: 9px 15px;
border-radius: 6px;
width: 320px;
width: 344px;
background: transparent;
border: 1px solid $gray-4;
@media (min-width: $xs) {
width: 344px;
}
@media (min-width: $md) {
width: 400px;
}
@media (min-width: $xl) {
width: 420px;
@media (min-width: $sm) {
width: 412px;
}
}

Expand Down Expand Up @@ -100,33 +94,35 @@
}

.input--chain {
width: 230px;
width: 244px;
background-color: transparent;
&:focus {
outline: none;
}
@media (min-width: $md) {
@media (min-width: $sm) {
width: 320px;
}
}

.icon--selector {
height: 20px;
width: 20px;
color: $gray-4;
color: $navy-1;
}

.body--dark {
.box--chain-option {
background: $gray-6;
}
.box--input-select-wallet {
background: $gray-6;
border: 1px solid $gray-4;
border: 1px solid $gray-3;
}
.list {
&:hover {
background-color: $item--bg--hover-dark;
}
}
.icon--selector {
color: $gray-1;
}
}
3 changes: 1 addition & 2 deletions src/components/assets/transfer/styles/xcm-bridge.scss
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@
background: $body-bg-dark;
}
.box--input-address {
background: $gray-6;
border: 1px solid $gray-4;
border: 1px solid $gray-3;
}
.input--address {
&::placeholder {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/xcm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const xcmChainObj: XcmChainObj = {
img: require('/src/assets/img/token/csm.svg'),
parachainId: parachainIds.CRUST_SHADOW,
endpoint: 'wss://rpc2-shadow.crust.network',
subscan: 'https://crust.subscan.io',
subscan: 'https://shadow.subscan.io',
isAstarNativeToken: true,
},
[Chain.KHALA]: {
Expand All @@ -205,7 +205,7 @@ export const xcmChainObj: XcmChainObj = {
img: require('/src/assets/img/token/bnc.svg'),
parachainId: parachainIds.BIFROST_POLKADOT,
endpoint: 'wss://hk.p.bifrost-rpc.liebi.com/ws',
subscan: 'https://bifrost.subscan.io/',
subscan: 'https://bifrost.subscan.io',
isAstarNativeToken: true,
},
[Chain.BIFROST_KUSAMA]: {
Expand All @@ -214,7 +214,7 @@ export const xcmChainObj: XcmChainObj = {
img: require('/src/assets/img/token/bnc.svg'),
parachainId: parachainIds.BIFROST_KUSAMA,
endpoint: 'wss://bifrost-rpc.liebi.com/ws',
subscan: 'https://bifrost-kusama.subscan.io/',
subscan: 'https://bifrost-kusama.subscan.io',
isAstarNativeToken: true,
},
[Chain.EQUILIBRIUM]: {
Expand All @@ -223,7 +223,7 @@ export const xcmChainObj: XcmChainObj = {
img: require('/src/assets/img/EQ.png'),
parachainId: parachainIds.EQUILIBRIUM,
endpoint: 'wss://node.pol.equilibrium.io/',
subscan: 'https://equilibrium.subscan.io/',
subscan: 'https://equilibrium.subscan.io',
isAstarNativeToken: true,
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/v2/repositories/IXcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ export interface IXcmRepository {
): Promise<string>;

getNativeBalance(address: string, chain: XcmChain): Promise<BN>;

getXcmVersion(from: XcmChain): { version: string; isV3: boolean };
}
63 changes: 45 additions & 18 deletions src/v2/repositories/implementations/XcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { XcmTokenInformation } from 'src/modules/xcm';
import { decodeAddress, evmToAddress } from '@polkadot/util-crypto';
import { TokenId } from 'src/v2/config/types';
import { XcmChain } from 'src/v2/models/XcmModels';
import { Chain, XcmChain } from 'src/v2/models/XcmModels';

interface AssetConfig extends Struct {
v1: {
Expand Down Expand Up @@ -102,6 +102,13 @@ export class XcmRepository implements IXcmRepository {
return result;
}

public getXcmVersion(from: XcmChain): { version: string; isV3: boolean } {
const v3s = [Chain.KUSAMA, Chain.SHIDEN, Chain.STATEMINE, Chain.BIFROST_KUSAMA];
const version = v3s.find((it) => it === from.name) ? 'V3' : 'V1';
const isV3 = version === 'V3';
return { version, isV3 };
}

public async getTransferToParachainCall(
from: XcmChain,
to: XcmChain,
Expand All @@ -113,9 +120,12 @@ export class XcmRepository implements IXcmRepository {
throw `Parachain id for ${to.name} is not defined`;
}

// Todo: unify to 'V3' after Polkadot XCM version updates to V3
const { version, isV3 } = this.getXcmVersion(from);

// the target parachain connected to the current relaychain
const destination = {
V1: {
[version]: {
interior: {
X1: {
Parachain: new BN(to.parachainId),
Expand All @@ -127,23 +137,29 @@ export class XcmRepository implements IXcmRepository {

const recipientAddressId = this.getAddress(recipientAddress);

// the account ID within the destination parachain
const id = decodeAddress(recipientAddressId);
const AccountId32 = isV3
? {
id,
}
: {
network: 'Any',
id,
};

const beneficiary = {
V1: {
[version]: {
interior: {
X1: {
AccountId32: {
network: 'Any',
id: decodeAddress(recipientAddressId),
},
AccountId32,
},
},
parents: new BN(0),
},
};
// amount of fungible tokens to be transferred

const assets = {
V1: [
[version]: [
{
fun: {
Fungible: amount,
Expand Down Expand Up @@ -175,22 +191,31 @@ export class XcmRepository implements IXcmRepository {
amount: BN
): Promise<ExtrinsicPayload> {
const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);
// Todo: unify to 'V3' after Polkadot XCM version updates to V3
const { version, isV3 } = this.getXcmVersion(from);

const destination = {
V1: {
[version]: {
interior: 'Here',
parents: new BN(1),
},
};

const id = decodeAddress(recipientAccountId);
const AccountId32 = isV3
? {
id,
}
: {
network: 'Any',
id,
};

const beneficiary = {
V1: {
[version]: {
interior: {
X1: {
AccountId32: {
network: 'Any',
id: decodeAddress(recipientAccountId),
},
AccountId32,
},
},
parents: new BN(0),
Expand All @@ -205,7 +230,7 @@ export class XcmRepository implements IXcmRepository {
};

const assets = {
V1: [
[version]: [
{
fun: {
Fungible: new BN(amount),
Expand Down Expand Up @@ -282,8 +307,10 @@ export class XcmRepository implements IXcmRepository {
const config = await api.query.xcAssetConfig.assetIdToLocation<Option<AssetConfig>>(token.id);

// return config.unwrap().v1;
const { isV3 } = this.getXcmVersion(source);
const formattedAssetConfig = JSON.parse(config.toString());
return formattedAssetConfig.v1;

return isV3 ? formattedAssetConfig.v3 : formattedAssetConfig.v1;
}

protected isAstarNativeToken(token: Asset): boolean {
Expand Down
50 changes: 27 additions & 23 deletions src/v2/repositories/implementations/xcm/AstarXcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,67 +35,71 @@ export class AstarXcmRepository extends XcmRepository {
}

const recipientAccountId = getPubkeyFromSS58Addr(recipientAddress);
const { version, isV3 } = this.getXcmVersion(from);

const isWithdrawAssets = token.id !== this.astarNativeTokenId;
const functionName = isWithdrawAssets ? 'reserveWithdrawAssets' : 'reserveTransferAssets';
const isSendToParachain = to.parachainId > 0;
const destination = isSendToParachain

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

const isAccountId20 = ethWalletChains.includes(to.name);
const X1_V1 = isAccountId20
? {
V1: {
interior: {
X1: {
Parachain: new BN(to.parachainId),
},
},
parents: new BN(1),
AccountKey20: {
network: 'Any',
key: recipientAccountId,
},
}
: {
V1: {
interior: 'Here',
parents: new BN(1),
AccountId32: {
network: 'Any',
id: decodeAddress(recipientAccountId),
},
};

const isAccountId20 = ethWalletChains.includes(to.name);
const X1 = isAccountId20
const X1_V3 = isAccountId20
? {
AccountKey20: {
network: 'Any',
key: recipientAccountId,
},
}
: {
AccountId32: {
network: 'Any',
id: decodeAddress(recipientAccountId),
},
};

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

const isRegisteredAsset = isSendToParachain && isWithdrawAssets;

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

const assets = {
V1: [
[version]: [
{
fun: {
Fungible: new BN(amount),
Expand Down
27 changes: 19 additions & 8 deletions src/v2/repositories/implementations/xcm/BifrostXcmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,40 @@ export class BifrostXcmRepository extends XcmRepository {
} else {
throw `Token name for ${token.originAssetId} is not defined`;
}

const { version, isV3 } = this.getXcmVersion(from);

const AccountId32 = isV3
? {
id: decodeAddress(recipientAddress),
}
: {
id: decodeAddress(recipientAddress),
network: {
Any: null,
},
};

const destination = {
V1: {
[version]: {
parents: '1',
interior: {
X2: [
{
Parachain: to.parachainId,
},
{
AccountId32: {
network: {
Any: null,
},
id: decodeAddress(recipientAddress),
},
AccountId32,
},
],
},
},
};
const destWeight = {
limited: new BN(10).pow(new BN(9)).muln(5),
// limited: new BN(10).pow(new BN(9)).muln(5),
Unlimited: null,
};

return await this.buildTxCall(
from,
'xTokens',
Expand Down
Loading