Skip to content

Commit

Permalink
Update transfer pallets for parachains 📦
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Nov 24, 2023
1 parent 2580385 commit 2ec2723
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/nodes/ParachainNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class ParachainNode {

private readonly _version: Version

protected _assetCheckEnabled = true

constructor(node: TNode, name: string, type: TRelayChainType, version: Version) {
this._name = name
this._type = type
Expand All @@ -64,6 +66,10 @@ abstract class ParachainNode {
return this._version
}

get assetCheckEnabled(): boolean {
return this._assetCheckEnabled
}

transfer(
api: ApiPromise,
currencySymbol: string | undefined,
Expand Down
16 changes: 14 additions & 2 deletions src/nodes/supported/ComposableFinance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
// Contains detailed structure of XCM call construction for ComposableFinance Parachain

import { Version } from '../../types'
import {
type IXTokensTransfer,
Version,
type XTokensTransferInput,
type Extrinsic,
type TSerializedApiCall
} from '../../types'
import ParachainNode from '../ParachainNode'
import XTokensTransferImpl from '../XTokensTransferImpl'

class ComposableFinance extends ParachainNode {
class ComposableFinance extends ParachainNode implements IXTokensTransfer {
constructor() {
super('ComposableFinance', 'composable', 'polkadot', Version.V3)
}

transferXTokens(input: XTokensTransferInput): Extrinsic | TSerializedApiCall {
const { currencyID } = input
return XTokensTransferImpl.transferXTokens(input, currencyID)
}
}

export default ComposableFinance
4 changes: 2 additions & 2 deletions src/nodes/supported/Manta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import XTokensTransferImpl from '../XTokensTransferImpl'

class Manta extends ParachainNode implements IXTokensTransfer {
constructor() {
super('Manta', 'manta', 'polkadot', Version.V3)
super('Manta', 'manta', 'polkadot', Version.V1)
}

transferXTokens(input: XTokensTransferInput): Extrinsic | TSerializedApiCall {
return XTokensTransferImpl.transferXTokens(input, { ForeignAssetId: input.currencyID })
return XTokensTransferImpl.transferXTokens(input, { MantaCurrency: input.currencyID })
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/nodes/supported/Quartz.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
// Contains detailed structure of XCM call construction for Quartz Parachain

import {
type IPolkadotXCMTransfer,
type PolkadotXCMTransferInput,
Version,
type Extrinsic,
type TSerializedApiCall
type TSerializedApiCall,
type IXTokensTransfer,
type XTokensTransferInput
} from '../../types'
import { ScenarioNotSupportedError } from '../../errors/ScenarioNotSupportedError'
import ParachainNode from '../ParachainNode'
import PolkadotXCMTransferImpl from '../PolkadotXCMTransferImpl'
import XTokensTransferImpl from '../XTokensTransferImpl'

class Quartz extends ParachainNode implements IPolkadotXCMTransfer {
class Quartz extends ParachainNode implements IXTokensTransfer {
constructor() {
super('Quartz', 'quartz', 'kusama', Version.V3)
}

transferPolkadotXCM(input: PolkadotXCMTransferInput): Extrinsic | TSerializedApiCall {
// TESTED https://quartz.subscan.io/xcm_message/kusama-f5b6580f8d7f97a8d33209d2b5b34d97454587e9
if (input.scenario === 'ParaToPara') {
return PolkadotXCMTransferImpl.transferPolkadotXCM(input, 'reserveTransferAssets')
}
throw new ScenarioNotSupportedError(this.node, input.scenario)
_assetCheckEnabled = false

transferXTokens(input: XTokensTransferInput): Extrinsic | TSerializedApiCall {
const { currencyID } = input
return XTokensTransferImpl.transferXTokens(input, { ForeignAssetId: currencyID })
}
}

Expand Down
41 changes: 26 additions & 15 deletions src/pallets/xcmPallet/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Tests designed to try different XCM Pallet XCM messages and errors

import { type ApiPromise } from '@polkadot/api'
import { vi, describe, expect, it, beforeEach } from 'vitest'
import { describe, expect, it, beforeEach, vi } from 'vitest'
import { NODE_NAMES } from '../../maps/consts'
import { createApiInstance } from '../../utils'
import { getAllAssetsSymbols, getRelayChainSymbol } from '../assets'
import { InvalidCurrencyError } from '../../errors/InvalidCurrencyError'
import { IncompatibleNodesError } from '../../errors'
import { type TNode } from '../../types'
import { send } from './transfer'

vi.mock('../../utils', () => ({
constructXTokens: vi.fn(),
constructPolkadotXCM: vi.fn(),
createApiInstance: vi.fn()
}))
import ParachainNode from '../../nodes/ParachainNode'
import { createApiInstance, getNode } from '../../utils'

vi.spyOn(ParachainNode.prototype, 'transfer').mockImplementation(
(
api: ApiPromise,
currencySymbol: string | undefined,
currencyId: string | undefined,
amount: string,
to: string,
destination?: TNode,
serializedApiCallEnabled = false
) => {
return null as any
}
)

const WS_URL = 'wss://para.f3joule.space'
const ADDRESS = '23sxrMSmaUMqe2ufSJg8U3Y8kxHfKT67YbubwXWFazpYi7w6'
Expand Down Expand Up @@ -91,13 +100,15 @@ describe('send', () => {

it('should not throw an InvalidCurrencyError when passing all defined symbols from all nodes', () => {
NODE_NAMES.forEach(node => {
const symbols = getAllAssetsSymbols(node)
symbols.forEach(symbol => {
const t = (): void => {
send(api, node, symbol, AMOUNT, ADDRESS)
}
expect(t).not.toThrowError(InvalidCurrencyError)
})
if (getNode(node).assetCheckEnabled) {
const symbols = getAllAssetsSymbols(node)
symbols.forEach(symbol => {
const t = (): void => {
send(api, node, symbol, AMOUNT, ADDRESS)
}
expect(t).not.toThrowError(InvalidCurrencyError)
})
}
})
})

Expand Down
14 changes: 9 additions & 5 deletions src/pallets/xcmPallet/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@ const sendCommon = (
}
}

if (asset === null) {
const originNode = getNode(origin)

if (asset === null && originNode.assetCheckEnabled) {
throw new InvalidCurrencyError(
`Origin node ${origin} does not support currency or currencyId ${currencySymbolOrId}.`
)
}

if (
destination !== undefined &&
asset.symbol !== undefined &&
asset?.symbol !== undefined &&
getNode(destination).assetCheckEnabled &&
!hasSupportForAsset(destination, asset.symbol)
) {
throw new InvalidCurrencyError(
`Destination node ${destination} does not support currency or currencyId ${currencySymbolOrId}.`
)
}
const { symbol: currencySymbol, assetId: currencyId } = asset

return getNode(origin).transfer(
const currencyId = originNode.assetCheckEnabled ? asset?.assetId : currencySymbolOrId.toString()

return originNode.transfer(
api,
currencySymbol,
asset?.symbol,
currencyId,
amount,
to,
Expand Down

0 comments on commit 2ec2723

Please sign in to comment.