Skip to content

Commit

Permalink
fix(sdk): Fix Mythos transfer & assets
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Aug 12, 2024
1 parent 6866142 commit d46f739
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/builder/builders/Builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getRelayChainSymbol } from '../../pallets/assets'
import { Builder } from './Builder'
import { type TMultiAsset } from '../../types/TMultiAsset'

const WS_URL = 'wss://para.subsocial.network'
const WS_URL = 'wss://subsocial-rpc.dwellir.com'
const NODE: TNode = 'Acala'
const NODE_2: TNode = 'Acala'
const AMOUNT = 1000
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/nodes/supported/Mythos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Mythos extends ParachainNode implements IPolkadotXCMTransfer {
}

transferPolkadotXCM(input: PolkadotXCMTransferInput): Extrinsic | TSerializedApiCall {
const { scenario, currencySymbol } = input
const { scenario, currencySymbol, destination } = input
if (scenario !== 'ParaToPara') {
throw new ScenarioNotSupportedError(this.node, scenario)
}
Expand All @@ -35,7 +35,7 @@ class Mythos extends ParachainNode implements IPolkadotXCMTransfer {

return PolkadotXCMTransferImpl.transferPolkadotXCM(
input,
'limitedReserveTransferAssets',
destination === 'AssetHubPolkadot' ? 'limitedTeleportAssets' : 'limitedReserveTransferAssets',
'Unlimited'
)
}
Expand Down
77 changes: 72 additions & 5 deletions packages/sdk/src/pallets/assets/getBalanceForeign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { type ApiPromise } from '@polkadot/api'
import { TNodePolkadotKusama } from '../../types'
import { TMultiLocation, TNodePolkadotKusama } from '../../types'
import { getDefaultPallet } from '../pallets'
import { createApiInstanceForNode } from '../../utils'
import { type StorageKey, u32 } from '@polkadot/types'
import { type AnyTuple, type Codec } from '@polkadot/types/types'
import { getAssetBySymbolOrId } from './assetsUtils'
import { getOtherAssets } from './assets'

const getBalanceForeignXTokens = async (
address: string,
Expand All @@ -25,9 +26,8 @@ const getBalanceForeignXTokens = async (
{
args: [_, asset]
},
value1
_value1
]) => {
console.log(asset.toHuman(), value1.toHuman())
return (
asset.toString() === symbolOrId ||
asset.toString() === id ||
Expand All @@ -41,15 +41,80 @@ const getBalanceForeignXTokens = async (
return entry ? BigInt(entry[1].free.toString()) : null
}

const getAssetHubMultiLocation = (symbol?: string): TMultiLocation | null => {
if (symbol === 'MYTH') {
return {
parents: 1,
interior: {
X1: {
Parachain: '3369'
}
}
}
} else if (symbol === 'KSM') {
return {
parents: 2,
interior: {
X1: {
GlobalConsensus: 'Kusama'
}
}
}
}
const ethAssets = getOtherAssets('Ethereum')
const ethAsset = ethAssets.find(asset => asset.symbol === symbol)
if (ethAsset) {
return {
parents: 2,
interior: {
X2: [
{
GlobalConsensus: {
Ethereum: {
chainId: 1
}
}
},
{
AccountKey20: {
network: null,
key: ethAsset.assetId
}
}
]
}
}
}
return null
}

const getBalanceForeignPolkadotXcm = async (
address: string,
id: string | undefined,
api: ApiPromise
api: ApiPromise,
node?: TNodePolkadotKusama,
symbol?: string
): Promise<bigint | null> => {
try {
if (node === 'Mythos') {
const response: Codec = await api.query.balances.account(address)
const obj: any = response.toJSON()
return BigInt(obj.free)
}

if (node === 'AssetHubPolkadot') {
const multiLocation = getAssetHubMultiLocation(symbol ?? id)
if (multiLocation) {
const response: Codec = await api.query.foreignAssets.account(multiLocation, address)
const obj: any = response.toJSON()
return BigInt(obj === null ? 0 : obj.balance)
}
}

const parsedId = new u32(api.registry, id)
const response: Codec = await api.query.assets.account(parsedId, address)
const obj: any = response.toJSON()

return BigInt(obj.balance)
} catch (error) {
console.log('Error while fetching balance', error)
Expand Down Expand Up @@ -78,7 +143,9 @@ export const getBalanceForeign = async (
return await getBalanceForeignPolkadotXcm(
address,
asset?.assetId ?? symbolOrId,
apiWithFallback
apiWithFallback,
node,
asset?.symbol
)
}
throw new Error('Unsupported pallet')
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/pallets/xcmPallet/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vi.spyOn(ParachainNode.prototype, 'transfer').mockReturnValue(null as any)
vi.spyOn(Astar.prototype, 'transfer').mockReturnValue(null as any)
vi.spyOn(Shiden.prototype, 'transfer').mockReturnValue(null as any)

const WS_URL = 'wss://para.f3joule.space'
const WS_URL = 'wss://subsocial-rpc.dwellir.com'
const randomCurrencySymbol = 'DOT'

const MOCK_OPTIONS_BASE = {
Expand Down

0 comments on commit d46f739

Please sign in to comment.