Skip to content

Commit 5808ed7

Browse files
Fix moonbeam asset registry (#1482)
* fix moonbeam asset registry * revert fmt changes * up versions * fix get token balance * fix metadata for moonbeam assets * moonbeam assets are sufficient * soft block MYTH to ETH --------- Co-authored-by: Alistair Singh <[email protected]>
1 parent d836eb3 commit 5808ed7

File tree

5 files changed

+51
-59
lines changed

5 files changed

+51
-59
lines changed

web/packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snowbridge/api",
3-
"version": "0.1.54",
3+
"version": "0.1.55",
44
"description": "Snowbridge API client",
55
"license": "Apache-2.0",
66
"repository": {

web/packages/api/src/assets_v2.ts

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
erc20Location,
1010
} from "./xcmBuilder"
1111
import { IGatewayV1__factory as IGateway__factory } from "@snowbridge/contract-types"
12-
import { convertToXcmV3X1, getMoonbeamLocationBalance, toMoonbeamXC20 } from "./parachains/moonbeam"
12+
import {
13+
convertToXcmV3X1, getMoonbeamEvmAssetMetadata,
14+
getMoonbeamLocationBalance,
15+
toMoonbeamXC20
16+
} from "./parachains/moonbeam"
1317
import { MUSE_TOKEN_ID, MYTHOS_TOKEN_ID, getMythosLocationBalance } from "./parachains/mythos"
1418

1519
export type ERC20Metadata = {
@@ -879,41 +883,16 @@ async function indexParachainAssets(provider: ApiPromise, ethChainId: number, sp
879883
}
880884
case "moonriver":
881885
case "moonbeam": {
882-
const entries = await provider.query.assetManager.assetIdType.entries()
883-
for (const [key, value] of entries) {
884-
const location = (value.toJSON() as any).xcm
885-
886-
const assetId = BigInt(key.args.at(0)?.toPrimitive() as any)
887-
const xc20 = toMoonbeamXC20(assetId)
888-
889-
if (location.parents === 1 && location.interior.here !== undefined) {
890-
xcDOT = xc20
891-
}
892-
const token = getTokenFromLocation(location, ethChainId)
893-
if (!token) {
894-
continue
895-
}
896-
897-
const asset: any = (await provider.query.assets.asset(assetId)).toPrimitive()
898-
const metadata: any = (await provider.query.assets.metadata(assetId)).toPrimitive()
899-
900-
assets[token] = {
901-
token,
902-
name: String(metadata.name),
903-
minimumBalance: BigInt(asset.minBalance),
904-
symbol: String(metadata.symbol),
905-
decimals: Number(metadata.decimals),
906-
isSufficient: Boolean(asset.isSufficient),
907-
xc20,
908-
}
909-
}
910886
const foreignEntries = await provider.query.evmForeignAssets.assetsById.entries()
911887
for (const [key, value] of foreignEntries) {
912888
const location = value.toJSON() as any
913889

914890
const assetId = BigInt(key.args.at(0)?.toPrimitive() as any)
915891
const xc20 = toMoonbeamXC20(assetId)
916892

893+
if (location.parents === 1 && location.interior.here !== undefined) {
894+
xcDOT = xc20
895+
}
917896
const token = getTokenFromLocation(location, ethChainId)
918897
if (!token) {
919898
continue
@@ -923,16 +902,17 @@ async function indexParachainAssets(provider: ApiPromise, ethChainId: number, sp
923902
continue
924903
}
925904

926-
const asset: any = (await provider.query.assets.asset(assetId)).toPrimitive()
927-
const metadata: any = (await provider.query.assets.metadata(assetId)).toPrimitive()
905+
const symbol = await getMoonbeamEvmAssetMetadata(provider, "symbol", xc20)
906+
const name = await getMoonbeamEvmAssetMetadata(provider, "name", xc20)
907+
const decimals = await getMoonbeamEvmAssetMetadata(provider, "decimals", xc20)
928908

929909
assets[token] = {
930910
token,
931-
name: String(metadata.name),
932-
minimumBalance: BigInt(asset?.minBalance ?? 1),
933-
symbol: String(metadata.symbol),
934-
decimals: Number(metadata.decimals),
935-
isSufficient: Boolean(asset?.isSufficient ?? false),
911+
name: String(name),
912+
minimumBalance: 1n,
913+
symbol: String(symbol),
914+
decimals: Number(decimals),
915+
isSufficient: true,
936916
xc20,
937917
}
938918
}
@@ -1121,7 +1101,7 @@ async function indexEthChain(
11211101
)
11221102
}
11231103
if (!evmParachainChain.xcDOT) {
1124-
throw Error(`Could not DOT XC20 address for evm chain ${networkChainId}.`)
1104+
throw Error(`Could not find DOT XC20 address for evm chain ${networkChainId}.`)
11251105
}
11261106
const xc20DOTAsset: ERC20Metadata = await assetErc20Metadata(
11271107
provider,
@@ -1380,7 +1360,7 @@ function defaultPathFilter(envName: string): (_: Path) => boolean {
13801360
if (
13811361
path.asset === MUSE_TOKEN_ID &&
13821362
(
1383-
(path.destination !== 3369 && path.type === "ethereum") ||
1363+
(path.destination !== 3369 && path.type === "ethereum") ||
13841364
(path.source !== 3369 && path.type === "substrate")
13851365
)
13861366
) {
@@ -1398,8 +1378,8 @@ function defaultPathFilter(envName: string): (_: Path) => boolean {
13981378
if (
13991379
path.asset === MYTHOS_TOKEN_ID &&
14001380
(
1401-
(path.destination !== 3369 && path.type === "ethereum") ||
1402-
(path.source !== 3369 && path.type === "substrate")
1381+
// TODO: Disable Mythos to Eth until mythos is ready to enable
1382+
(path.destination !== 3369)
14031383
)
14041384
) {
14051385
return false
@@ -1595,5 +1575,5 @@ export async function getAssetHubConversationPalletSwap(assetHub: ApiPromise, as
15951575
if (asset1Balance == null) {
15961576
throw Error(`No pool set up in asset conversion pallet for '${JSON.stringify(asset1)}' and '${JSON.stringify(asset2)}'.`)
15971577
}
1598-
return BigInt(asset1Balance)
1578+
return BigInt(asset1Balance)
15991579
}

web/packages/api/src/parachains/moonbeam.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,20 @@ export async function getMoonbeamLocationBalance(
3232
) {
3333
// For PNA, use assetId directly; for ENA, query assetId by Multilocation
3434
let paraAssetId = pnaAssetId
35-
if (!paraAssetId) {
36-
// Moonbeam only supports v3 xcm locations on asset Manager. Deep clone the location because
37-
// we might modify it.
38-
const assetManagerLocation = convertToXcmV3X1(location)
39-
paraAssetId = (
40-
await provider.query.assetManager.assetTypeId({ xcm: assetManagerLocation })
41-
).toPrimitive()
42-
}
43-
4435
// If we cannot find the asset in asset manager look in foreign assets.
4536
if (!paraAssetId) {
4637
// evmForeignAssets uses xcm v4 so we use the original location.
4738
paraAssetId = (
4839
(await provider.query.evmForeignAssets.assetsByLocation(location)).toPrimitive() as any
4940
)[0]
50-
const xc20 = toMoonbeamXC20(BigInt(paraAssetId))
51-
return await getMoonbeamEvmForeignAssetBalance(provider, xc20, account)
5241
}
5342

5443
if (!paraAssetId) {
5544
throw Error(`Asset not registered for spec ${specName}.`)
5645
}
5746

58-
const accountData = (
59-
await provider.query.assets.account(paraAssetId, account)
60-
).toPrimitive() as any
61-
return BigInt(accountData?.balance ?? 0n)
47+
const xc20 = toMoonbeamXC20(BigInt(paraAssetId))
48+
return await getMoonbeamEvmForeignAssetBalance(provider, xc20, account)
6249
}
6350

6451
async function getMoonbeamEvmForeignAssetBalance(api: ApiPromise, token: string, account: string) {
@@ -86,3 +73,28 @@ async function getMoonbeamEvmForeignAssetBalance(api: ApiPromise, token: string,
8673
const retVal = MOONBEAM_ERC20.decodeFunctionResult(method, resultJson?.ok?.value)
8774
return BigInt(retVal[0])
8875
}
76+
77+
export async function getMoonbeamEvmAssetMetadata(api: ApiPromise, method: string, token: string) {
78+
const data = MOONBEAM_ERC20.encodeFunctionData(method, [])
79+
const result = await api.call.ethereumRuntimeRPCApi.call(
80+
"0x0000000000000000000000000000000000000000",
81+
token,
82+
data,
83+
0n,
84+
500_000n,
85+
null,
86+
null,
87+
null,
88+
false,
89+
null
90+
)
91+
const resultJson = result.toPrimitive() as any
92+
if (!(resultJson?.ok?.exitReason?.succeed === "Returned")) {
93+
console.error(resultJson)
94+
throw Error(
95+
`Could not fetch metadata for ${token}: ${JSON.stringify(resultJson?.ok?.exitReason)}`
96+
)
97+
}
98+
const retVal = MOONBEAM_ERC20.decodeFunctionResult(method, resultJson?.ok?.value)
99+
return retVal[0]
100+
}

web/packages/contract-types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snowbridge/contract-types",
3-
"version": "0.1.54",
3+
"version": "0.1.55",
44
"description": "Snowbridge contract type bindings",
55
"license": "Apache-2.0",
66
"repository": {

web/packages/contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snowbridge/contracts",
3-
"version": "0.1.54",
3+
"version": "0.1.55",
44
"description": "Snowbridge contract source and abi.",
55
"license": "Apache-2.0",
66
"repository": {

0 commit comments

Comments
 (0)