Skip to content

Commit

Permalink
fix: Add destination address checks
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Nov 21, 2024
1 parent f1afc4e commit f072da7
Show file tree
Hide file tree
Showing 23 changed files with 466 additions and 30 deletions.
1 change: 1 addition & 0 deletions apps/xcm-api/src/assets/assets.controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('AssetsController', () => {
const mockResult = {
paraId: 2009,
relayChainAssetSymbol: 'KSM',
isEVM: false,
nativeAssets: [{ symbol, decimals }],
otherAssets: [{ assetId: '234123123', symbol: 'FKK', decimals }],
nativeAssetSymbol: symbol,
Expand Down
1 change: 1 addition & 0 deletions apps/xcm-api/src/assets/assets.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('AssetsService', () => {
const assetsObject: paraspellSdk.TNodeAssets = {
relayChainAssetSymbol: symbol,
nativeAssetSymbol: 'DOT',
isEVM: false,
nativeAssets: [{ symbol, decimals }],
otherAssets: [{ assetId, symbol: 'BSK', decimals }],
};
Expand Down
9 changes: 6 additions & 3 deletions packages/sdk/e2e/xcm-papi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
getOtherAssets,
TNodeDotKsmWithRelayChains,
ForeignAbstract,
determineRelayChain
determineRelayChain,
isNodeEvm
} from '../src/papi'
import { getPolkadotSigner, PolkadotSigner } from 'polkadot-api/signer'

Expand Down Expand Up @@ -341,13 +342,15 @@ describe.sequential('XCM - e2e', () => {
const currency = assetId ? { id: assetId } : { symbol: asset ?? 'DOT' }
if (currency === null) return
expect(nodeTo).toBeDefined()
const resolvedNode = nodeTo ?? MOCK_POLKADOT_NODE
const resolvedAddress = isNodeEvm(resolvedNode) ? MOCK_ETH_ADDRESS : MOCK_ADDRESS
try {
const tx = await Builder(api)
.from(node)
.to(nodeTo ?? MOCK_POLKADOT_NODE)
.to(resolvedNode)
.currency(currency)
.amount(MOCK_AMOUNT)
.address(MOCK_ADDRESS)
.address(resolvedAddress)
.build()

if (
Expand Down
9 changes: 6 additions & 3 deletions packages/sdk/e2e/xcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
getSupportedAssets,
TNodeDotKsmWithRelayChains,
ForeignAbstract,
determineRelayChain
determineRelayChain,
isNodeEvm
} from '../src'
import { type ApiPromise } from '@polkadot/api'
import { isForeignAsset } from '../src/utils/assets'
Expand Down Expand Up @@ -299,13 +300,15 @@ describe.sequential('XCM - e2e', () => {
const currency = assetId ? { id: assetId } : { symbol: asset ?? 'DOT' }
if (currency === null) return
expect(nodeTo).toBeDefined()
const resolvedNode = nodeTo ?? MOCK_POLKADOT_NODE
const resolvedAddress = isNodeEvm(resolvedNode) ? MOCK_ETH_ADDRESS : MOCK_ADDRESS
try {
const tx = await Builder(api)
.from(node)
.to(nodeTo ?? MOCK_POLKADOT_NODE)
.to(resolvedNode)
.currency(currency)
.amount(MOCK_AMOUNT)
.address(MOCK_ADDRESS)
.address(resolvedAddress)
.build()
expect(tx).toBeDefined()
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/scripts/assets/fetchAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { GLOBAL, nodeToQuery } from './nodeToQueryMap'
import { fetchEthereumAssets } from './fetchEthereumAssets'
import { addAliasesToDuplicateSymbols } from './addAliases'
import { capitalizeMultiLocation, fetchOtherAssetsRegistry } from './fetchOtherAssetsRegistry'
import { isNodeEvm } from './isNodeEvm'

const fetchNativeAssets = async (api: ApiPromise): Promise<TNativeAsset[]> => {
const propertiesRes = await api.rpc.system.properties()
Expand Down Expand Up @@ -360,7 +361,8 @@ const fetchNodeAssets = async (
return {
nativeAssets,
otherAssets,
nativeAssetSymbol
nativeAssetSymbol,
isEVM: isNodeEvm(api)
}
}

Expand Down Expand Up @@ -403,6 +405,7 @@ export const fetchAllNodesAssets = async (assetsMapJson: any) => {
output[nodeName] = {
relayChainAssetSymbol: getNode(nodeName).type === 'polkadot' ? 'DOT' : 'KSM',
nativeAssetSymbol: newData?.nativeAssetSymbol ?? '',
isEVM: newData?.isEVM ?? false,
nativeAssets: combinedNativeAssets,
otherAssets: combinedOtherAssets
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/scripts/assets/fetchEthereumAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const fetchEthereumAssets = async (): Promise<TNodeAssets> => {
})

return {
isEVM: true,
relayChainAssetSymbol: 'DOT',
nativeAssetSymbol: 'ETH',
nativeAssets: [
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/scripts/assets/isNodeEvm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { TPjsApi } from '../../src/pjs'

export const isNodeEvm = (api: TPjsApi) => {
const types = api.runtimeMetadata.asLatest.lookup.types
const type = types[0]?.type.path.toJSON() as string[]
return type.includes('AccountId20')
}
14 changes: 14 additions & 0 deletions packages/sdk/src/errors/InvalidAddressError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Error thrown when an invalid address is provided.
*/
export class InvalidAddressError extends Error {
/**
* Constructs a new InvalidAddressError.
*
* @param message - The error message.
*/
constructor(message: string) {
super(message)
this.name = 'InvalidAddressError'
}
}
1 change: 1 addition & 0 deletions packages/sdk/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { ScenarioNotSupportedError } from './ScenarioNotSupportedError'
export { IncompatibleNodesError } from './IncompatibleNodesError'
export { DuplicateAssetError } from './DuplicateAssetError'
export { DuplicateAssetIdError } from './DuplicateAssetIdError'
export { InvalidAddressError } from './InvalidAddressError'
Loading

0 comments on commit f072da7

Please sign in to comment.