Skip to content

Commit

Permalink
refactor(sdk): Refactor transferRelayToPara function 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Nov 25, 2024
1 parent 822710b commit 608ff63
Show file tree
Hide file tree
Showing 140 changed files with 655 additions and 921 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/src/api/IPolkadotApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
HexString,
THexString,
TAsset,
TMultiLocation,
TNodeWithRelayChains,
Expand All @@ -12,7 +12,7 @@ export interface IPolkadotApi<TApi, TRes> {
getApi(): TApi
init(node: TNodeWithRelayChains): Promise<void>
createApiInstance: (wsUrl: string) => Promise<TApi>
createAccountId(address: string): HexString
createAccountId(address: string): THexString
callTxMethod(serializedCall: TSerializedApiCall): TRes
calculateTransactionFee(tx: TRes, address: string): Promise<bigint>
getBalanceNative(address: string): Promise<bigint>
Expand Down
22 changes: 11 additions & 11 deletions packages/sdk/src/nodes/ParachainNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { NoXCMSupportImplementedError } from '../errors/NoXCMSupportImplementedE
import type { TRelayToParaOptions } from '../types'
import {
Version,
type PolkadotXcmSection,
type PolkadotXCMTransferInput,
type TPolkadotXcmSection,
type TPolkadotXCMTransferOptions,
type TSendInternalOptions,
type TXTokensCurrencySelection,
type XTokensTransferInput,
type XTransferTransferInput
type TXTokensTransferOptions,
type TXTransferTransferOptions
} from '../types'
import type { ApiPromise } from '@polkadot/api'
import type { Extrinsic } from '../pjs/types'
Expand Down Expand Up @@ -41,20 +41,20 @@ vi.mock('./config', () => ({

class TestParachainNode extends ParachainNode<ApiPromise, Extrinsic> {
transferXTokens(
_input: XTokensTransferInput<ApiPromise, Extrinsic>,
_input: TXTokensTransferOptions<ApiPromise, Extrinsic>,
_currencySelection: TXTokensCurrencySelection,
_fees: string | number = 'Unlimited'
) {
return 'transferXTokens called'
}

transferXTransfer(_input: XTransferTransferInput<ApiPromise, Extrinsic>) {
transferXTransfer(_input: TXTransferTransferOptions<ApiPromise, Extrinsic>) {
return 'transferXTransfer called'
}

transferPolkadotXCM(
_options: PolkadotXCMTransferInput<ApiPromise, Extrinsic>,
_section: PolkadotXcmSection,
_options: TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>,
_section: TPolkadotXcmSection,
_fees: 'Unlimited' | { Limited: string } | undefined = undefined
) {
return 'transferPolkadotXCM called'
Expand All @@ -66,15 +66,15 @@ class TestParachainNode extends ParachainNode<ApiPromise, Extrinsic> {
}

class NoXTokensNode extends ParachainNode<ApiPromise, Extrinsic> {
transferXTransfer(_input: XTransferTransferInput<ApiPromise, Extrinsic>) {
transferXTransfer(_input: TXTransferTransferOptions<ApiPromise, Extrinsic>) {
return 'transferXTransfer called'
}
}

class OnlyPolkadotXCMNode extends ParachainNode<ApiPromise, Extrinsic> {
transferPolkadotXCM(
_options: PolkadotXCMTransferInput<ApiPromise, Extrinsic>,
_section: PolkadotXcmSection,
_options: TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>,
_section: TPolkadotXcmSection,
_fees: 'Unlimited' | { Limited: string } | undefined = undefined
) {
return 'transferPolkadotXCM called'
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/nodes/ParachainNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import type {
TMultiLocationHeader,
TSerializedApiCall,
TAsset,
XTokensTransferInput
TXTokensTransferOptions,
TRelayToParaOverrides
} from '../types'
import { Version, Parents } from '../types'
import { generateAddressPayload, getFees } from '../utils'
Expand Down Expand Up @@ -119,7 +120,7 @@ abstract class ParachainNode<TApi, TRes> {
const isAssetHubDest = destination === 'AssetHubPolkadot' || destination === 'AssetHubKusama'
const shouldUseMultiasset = isAssetHubDest && !isBifrostOrigin

const input: XTokensTransferInput<TApi, TRes> = {
const input: TXTokensTransferOptions<TApi, TRes> = {
api,
asset,
amount,
Expand Down Expand Up @@ -191,12 +192,17 @@ abstract class ParachainNode<TApi, TRes> {
}
}

getRelayToParaOverrides(): TRelayToParaOverrides {
return { section: 'reserve_transfer_assets', includeFee: false }
}

transferRelayToPara(options: TRelayToParaOptions<TApi, TRes>): TSerializedApiCall {
const { version = Version.V3 } = options
const { section, includeFee } = this.getRelayToParaOverrides()
return {
module: 'XcmPallet',
section: 'reserve_transfer_assets',
parameters: constructRelayToParaParameters(options, version)
section,
parameters: constructRelayToParaParameters(options, version, { includeFee })
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect, vi } from 'vitest'
import PolkadotXCMTransferImpl from './PolkadotXCMTransferImpl'
import type {
PolkadotXcmSection,
PolkadotXCMTransferInput,
TPolkadotXcmSection,
TPolkadotXCMTransferOptions,
TCurrencySelectionHeaderArr,
TMultiLocationHeader
} from '../../types'
Expand Down Expand Up @@ -50,7 +50,7 @@ const mockCurrencySelection: TCurrencySelectionHeaderArr = {
}

const mockFeeAsset = 1
const mockSection: PolkadotXcmSection = 'limited_reserve_transfer_assets'
const mockSection: TPolkadotXcmSection = 'limited_reserve_transfer_assets'

describe('PolkadotXCMTransferImpl.transferPolkadotXCM', () => {
it('should call api.tx[module][section] with correct parameters when fees is undefined', () => {
Expand All @@ -63,7 +63,7 @@ describe('PolkadotXCMTransferImpl.transferPolkadotXCM', () => {
addressSelection: mockAddressSelection,
currencySelection: mockCurrencySelection,
feeAsset: mockFeeAsset
} as unknown as PolkadotXCMTransferInput<ApiPromise, Extrinsic>,
} as unknown as TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>,
mockSection,
undefined
)
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('PolkadotXCMTransferImpl.transferPolkadotXCM', () => {
addressSelection: mockAddressSelection,
currencySelection: mockCurrencySelection,
feeAsset: mockFeeAsset
} as unknown as PolkadotXCMTransferInput<ApiPromise, Extrinsic>,
} as unknown as TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>,
mockSection,
'Unlimited'
)
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('PolkadotXCMTransferImpl.transferPolkadotXCM', () => {
addressSelection: mockAddressSelection,
currencySelection: mockCurrencySelection,
feeAsset: mockFeeAsset
} as unknown as PolkadotXCMTransferInput<ApiPromise, Extrinsic>,
} as unknown as TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>,
mockSection,
{ Limited: '1000' }
)
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/nodes/polkadotXcm/PolkadotXCMTransferImpl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Contains basic structure of polkadotXCM call

import { DEFAULT_FEE_ASSET } from '../../const'
import type { PolkadotXcmSection, TPallet, TSerializedApiCall } from '../../types'
import { type PolkadotXCMTransferInput } from '../../types'
import type { TPolkadotXcmSection, TPallet, TSerializedApiCall } from '../../types'
import { type TPolkadotXCMTransferOptions } from '../../types'

class PolkadotXCMTransferImpl {
static transferPolkadotXCM<TApi, TRes>(
Expand All @@ -12,8 +12,8 @@ class PolkadotXCMTransferImpl {
addressSelection,
currencySelection,
feeAsset = DEFAULT_FEE_ASSET
}: PolkadotXCMTransferInput<TApi, TRes>,
section: PolkadotXcmSection,
}: TPolkadotXCMTransferOptions<TApi, TRes>,
section: TPolkadotXcmSection,
fees: 'Unlimited' | { Limited: string } | undefined = undefined
): TRes {
const module: TPallet = 'PolkadotXcm'
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Acala.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { XTokensTransferInput } from '../../types'
import type { TXTokensTransferOptions } from '../../types'
import { Version } from '../../types'
import XTokensTransferImpl from '../xTokens'
import { getNode } from '../../utils/getNode'
Expand All @@ -23,7 +23,7 @@ describe('Acala', () => {
const mockInput = {
asset: { symbol: 'ACA' },
amount: '100'
} as XTokensTransferInput<ApiPromise, Extrinsic>
} as TXTokensTransferOptions<ApiPromise, Extrinsic>
const spyTransferXTokens = vi.spyOn(XTokensTransferImpl, 'transferXTokens')

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Acala.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type IXTokensTransfer,
type TForeignOrTokenAsset,
Version,
type XTokensTransferInput
type TXTokensTransferOptions
} from '../../types'
import { isForeignAsset } from '../../utils/assets'
import { getNodeProviders } from '../config'
Expand All @@ -16,7 +16,7 @@ class Acala<TApi, TRes> extends ParachainNode<TApi, TRes> implements IXTokensTra
super('Acala', 'acala', 'polkadot', Version.V3)
}

transferXTokens<TApi, TRes>(input: XTokensTransferInput<TApi, TRes>) {
transferXTokens<TApi, TRes>(input: TXTokensTransferOptions<TApi, TRes>) {
const { asset } = input
const currencySelection: TForeignOrTokenAsset = isForeignAsset(asset)
? { ForeignAsset: Number(asset.assetId) }
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Altair.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { XTokensTransferInput } from '../../types'
import type { TXTokensTransferOptions } from '../../types'
import { Version } from '../../types'
import XTokensTransferImpl from '../xTokens'
import { getNode } from '../../utils/getNode'
Expand All @@ -18,7 +18,7 @@ describe('Altair', () => {
const mockInput = {
asset: { symbol: 'AIR', assetId: '1' },
amount: '100'
} as XTokensTransferInput<ApiPromise, Extrinsic>
} as TXTokensTransferOptions<ApiPromise, Extrinsic>

beforeEach(() => {
altair = getNode<ApiPromise, Extrinsic, 'Altair'>('Altair')
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Altair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { InvalidCurrencyError } from '../../errors'
import type { TAsset, TForeignOrNativeAsset } from '../../types'
import { type IXTokensTransfer, Version, type XTokensTransferInput } from '../../types'
import { type IXTokensTransfer, Version, type TXTokensTransferOptions } from '../../types'
import { isForeignAsset } from '../../utils/assets'
import ParachainNode from '../ParachainNode'
import XTokensTransferImpl from '../xTokens'
Expand All @@ -22,7 +22,7 @@ class Altair<TApi, TRes> extends ParachainNode<TApi, TRes> implements IXTokensTr
return { ForeignAsset: Number(asset.assetId) }
}

transferXTokens<TApi, TRes>(input: XTokensTransferInput<TApi, TRes>) {
transferXTokens<TApi, TRes>(input: TXTokensTransferOptions<TApi, TRes>) {
const { asset } = input
const currencySelection = this.getCurrencySelection(asset)
return XTokensTransferImpl.transferXTokens(input, currencySelection)
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Amplitude.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { XTokensTransferInput } from '../../types'
import type { TXTokensTransferOptions } from '../../types'
import { Version } from '../../types'
import XTokensTransferImpl from '../xTokens'
import { getNode } from '../../utils/getNode'
Expand All @@ -18,7 +18,7 @@ describe('Amplitude', () => {
const mockInput = {
asset: { symbol: 'AMPE', assetId: '123' },
amount: '100'
} as XTokensTransferInput<ApiPromise, Extrinsic>
} as TXTokensTransferOptions<ApiPromise, Extrinsic>

beforeEach(() => {
amplitude = getNode<ApiPromise, Extrinsic, 'Amplitude'>('Amplitude')
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Amplitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InvalidCurrencyError } from '../../errors'
import {
type IXTokensTransfer,
Version,
type XTokensTransferInput,
type TXTokensTransferOptions,
type TXcmAsset
} from '../../types'
import { isForeignAsset } from '../../utils/assets'
Expand All @@ -16,7 +16,7 @@ class Amplitude<TApi, TRes> extends ParachainNode<TApi, TRes> implements IXToken
super('Amplitude', 'amplitude', 'kusama', Version.V3)
}

transferXTokens<TApi, TRes>(input: XTokensTransferInput<TApi, TRes>) {
transferXTokens<TApi, TRes>(input: TXTokensTransferOptions<TApi, TRes>) {
const { asset } = input

if (!isForeignAsset(asset)) {
Expand Down
29 changes: 22 additions & 7 deletions packages/sdk/src/nodes/supported/AssetHubKusama.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { ScenarioNotSupportedError } from '../../errors'
import type { PolkadotXCMTransferInput } from '../../types'
import type { TPolkadotXCMTransferOptions } from '../../types'
import { getNode } from '../../utils'
import type { ApiPromise } from '@polkadot/api'
import type { Extrinsic } from '../../pjs/types'
import type { Extrinsic, TPjsApi } from '../../pjs/types'
import type AssetHubKusama from './AssetHubKusama'

describe('transferPolkadotXCM', () => {
let node: AssetHubKusama<TPjsApi, Extrinsic>

beforeEach(() => {
vi.clearAllMocks()
node = getNode<TPjsApi, Extrinsic, 'AssetHubKusama'>('AssetHubKusama')
})

it('throws ScenarioNotSupportedError for native KSM transfers in para to para scenarios', () => {
const assetHub = getNode('AssetHubKusama')
const input = {
asset: {
symbol: 'KSM'
},
scenario: 'ParaToPara',
destination: 'Karura'
} as PolkadotXCMTransferInput<ApiPromise, Extrinsic>
} as TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>

expect(() => assetHub.transferPolkadotXCM(input)).toThrow(ScenarioNotSupportedError)
expect(() => node.transferPolkadotXCM(input)).toThrow(ScenarioNotSupportedError)
})

it('throws ScenarioNotSupportedError for native DOT transfers in para to para scenarios', () => {
Expand All @@ -27,8 +34,16 @@ describe('transferPolkadotXCM', () => {
},
scenario: 'ParaToPara',
destination: 'Karura'
} as PolkadotXCMTransferInput<ApiPromise, Extrinsic>
} as TPolkadotXCMTransferOptions<ApiPromise, Extrinsic>

expect(() => assetHub.transferPolkadotXCM(input)).toThrow(ScenarioNotSupportedError)
})

it('should call getRelayToParaOverrides with the correct parameters', () => {
const result = node.getRelayToParaOverrides()
expect(result).toEqual({
section: 'limited_teleport_assets',
includeFee: true
})
})
})
18 changes: 5 additions & 13 deletions packages/sdk/src/nodes/supported/AssetHubKusama.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Contains detailed structure of XCM call construction for AssetHubKusama Parachain

import { ScenarioNotSupportedError } from '../../errors'
import { constructRelayToParaParameters } from '../../pallets/xcmPallet/utils'
import type { TAsset } from '../../types'
import type { TAsset, TRelayToParaOverrides } from '../../types'
import {
type IPolkadotXCMTransfer,
type PolkadotXCMTransferInput,
type TPolkadotXCMTransferOptions,
Version,
type TSerializedApiCall,
type TScenario,
type TRelayToParaOptions,
type TMultiAsset,
type TMultiLocation
} from '../../types'
Expand All @@ -23,7 +20,7 @@ class AssetHubKusama<TApi, TRes> extends ParachainNode<TApi, TRes> implements IP
super('AssetHubKusama', 'KusamaAssetHub', 'kusama', Version.V3)
}

transferPolkadotXCM<TApi, TRes>(input: PolkadotXCMTransferInput<TApi, TRes>): Promise<TRes> {
transferPolkadotXCM<TApi, TRes>(input: TPolkadotXCMTransferOptions<TApi, TRes>): Promise<TRes> {
const { destination, asset, scenario } = input
// TESTED https://kusama.subscan.io/xcm_message/kusama-ddc2a48f0d8e0337832d7aae26f6c3053e1f4ffd
// TESTED https://kusama.subscan.io/xcm_message/kusama-8e423130a4d8b61679af95dbea18a55124f99672
Expand Down Expand Up @@ -53,13 +50,8 @@ class AssetHubKusama<TApi, TRes> extends ParachainNode<TApi, TRes> implements IP
return Promise.resolve(PolkadotXCMTransferImpl.transferPolkadotXCM(input, section, 'Unlimited'))
}

transferRelayToPara(options: TRelayToParaOptions<TApi, TRes>): TSerializedApiCall {
const { version = Version.V3 } = options
return {
module: 'XcmPallet',
section: 'limited_teleport_assets',
parameters: constructRelayToParaParameters(options, version, true)
}
getRelayToParaOverrides(): TRelayToParaOverrides {
return { section: 'limited_teleport_assets', includeFee: true }
}

createCurrencySpec(
Expand Down
Loading

0 comments on commit 608ff63

Please sign in to comment.