|
| 1 | +import { ParachainBase } from "./parachainBase" |
| 2 | +import { DOT_LOCATION, erc20Location } from "../xcmBuilder" |
| 3 | +import { PNAMap } from "../assets_v2" |
| 4 | +import { AssetMap } from "@snowbridge/base-types" |
| 5 | +import { ApiPromise } from "@polkadot/api" |
| 6 | +import { SubmittableExtrinsic } from "@polkadot/api/types" |
| 7 | +import { ISubmittableResult } from "@polkadot/types/types" |
| 8 | + |
| 9 | +export const NEUROWEB_TEST_CHAIN_ID = 11155111 // Sepolia |
| 10 | +export const NEUROWEB_TEST_TOKEN_ID = "0xef32abea56beff54f61da319a7311098d6fbcea9" |
| 11 | +export const NEUROWEB_CHAIN_ID = 1 // Ethereum Mainnet |
| 12 | +export const NEUROWEB_TOKEN_ID = "0xaa7a9ca87d3694b5755f213b5d04094b8d0f0a6f" |
| 13 | + |
| 14 | +export class NeurowebParachain extends ParachainBase { |
| 15 | + getXC20DOT() { |
| 16 | + return undefined |
| 17 | + } |
| 18 | + |
| 19 | + async getLocationBalance(location: any, account: string, _pnaAssetId?: any): Promise<bigint> { |
| 20 | + const accountData = ( |
| 21 | + await this.provider.query.foreignAssets.account(location, account) |
| 22 | + ).toPrimitive() as any |
| 23 | + return BigInt(accountData?.balance ?? 0n) |
| 24 | + } |
| 25 | + |
| 26 | + async getDotBalance(account: string): Promise<bigint> { |
| 27 | + const accountData = ( |
| 28 | + await this.provider.query.foreignAssets.account(DOT_LOCATION, account) |
| 29 | + ).toPrimitive() as any |
| 30 | + return BigInt(accountData?.balance ?? 0n) |
| 31 | + } |
| 32 | + |
| 33 | + async getAssets(ethChainId: number, _pnas: PNAMap): Promise<AssetMap> { |
| 34 | + const assets: AssetMap = {} |
| 35 | + if (this.specName !== "origintrail-parachain") { |
| 36 | + throw Error( |
| 37 | + `Cannot get balance for spec ${this.specName}. Location = ${JSON.stringify( |
| 38 | + location |
| 39 | + )}` |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + if (ethChainId === NEUROWEB_TEST_CHAIN_ID) { |
| 44 | + assets[NEUROWEB_TEST_TOKEN_ID.toLowerCase()] = { |
| 45 | + token: NEUROWEB_TEST_TOKEN_ID.toLowerCase(), |
| 46 | + name: "Trac", |
| 47 | + minimumBalance: 1_000_000_000_000_000n, |
| 48 | + symbol: "TRAC", |
| 49 | + decimals: 18, |
| 50 | + isSufficient: true, |
| 51 | + } |
| 52 | + } else if (ethChainId === NEUROWEB_CHAIN_ID) { |
| 53 | + assets[NEUROWEB_TOKEN_ID.toLowerCase()] = { |
| 54 | + token: NEUROWEB_TOKEN_ID.toLowerCase(), |
| 55 | + name: "Trac", |
| 56 | + minimumBalance: 1_000_000_000_000_000n, |
| 57 | + symbol: "TRAC", |
| 58 | + decimals: 18, |
| 59 | + isSufficient: true, |
| 60 | + } |
| 61 | + } else { |
| 62 | + throw Error(`Cannot get assets for chain ID ${ethChainId}.`) |
| 63 | + } |
| 64 | + return assets |
| 65 | + } |
| 66 | + |
| 67 | + async calculateXcmFee(destinationXcm: any, asset: any): Promise<bigint> { |
| 68 | + if (JSON.stringify(asset) == JSON.stringify(DOT_LOCATION)) { |
| 69 | + console.warn( |
| 70 | + `${this.specName} does not support calculating fee for asset '${JSON.stringify( |
| 71 | + asset |
| 72 | + )}'. Using default.` |
| 73 | + ) |
| 74 | + return 10_000_000_000n // TODO |
| 75 | + } |
| 76 | + return await this.calculateXcmFee(destinationXcm, asset) |
| 77 | + } |
| 78 | + |
| 79 | + async wrapExecutionFeeInNative(parachain: ApiPromise) { |
| 80 | + // Mock transaction to get extrinsic fee |
| 81 | + let tx = parachain.tx.wrapper.tracWrap(100000000) |
| 82 | + const paymentInfo = await tx.paymentInfo( |
| 83 | + "0x0000000000000000000000000000000000000000000000000000000000000000" |
| 84 | + ) |
| 85 | + const executionFee = paymentInfo["partialFee"].toBigInt() |
| 86 | + console.log("wrap execution fee:", executionFee) |
| 87 | + return executionFee |
| 88 | + } |
| 89 | + |
| 90 | + async unwrapExecutionFeeInNative(parachain: ApiPromise) { |
| 91 | + // Mock transaction to get extrinsic fee |
| 92 | + let tx = parachain.tx.wrapper.tracUnwrap(100000000) |
| 93 | + const paymentInfo = await tx.paymentInfo( |
| 94 | + "0x0000000000000000000000000000000000000000000000000000000000000000" |
| 95 | + ) |
| 96 | + const executionFee = paymentInfo["partialFee"].toBigInt() |
| 97 | + console.log("unwrap execution fee:", executionFee) |
| 98 | + return executionFee |
| 99 | + } |
| 100 | + |
| 101 | + snowTRACBalance(account: string, ethChainId: number) { |
| 102 | + if (ethChainId === NEUROWEB_TEST_CHAIN_ID) { |
| 103 | + return this.getLocationBalance( |
| 104 | + erc20Location(ethChainId, NEUROWEB_TEST_TOKEN_ID), |
| 105 | + account |
| 106 | + ) |
| 107 | + } else if (ethChainId === NEUROWEB_CHAIN_ID) { |
| 108 | + return this.getLocationBalance(erc20Location(ethChainId, NEUROWEB_TOKEN_ID), account) |
| 109 | + } else { |
| 110 | + throw Error(`Cannot get snowTRAC balance for chain ID ${ethChainId}.`) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + async tracBalance(account: string) { |
| 115 | + const accountData = ( |
| 116 | + await this.provider.query.assets.account(1, account) |
| 117 | + ).toPrimitive() as any |
| 118 | + return BigInt(accountData?.balance ?? 0n) |
| 119 | + } |
| 120 | + |
| 121 | + createWrapTx( |
| 122 | + parachain: ApiPromise, |
| 123 | + amount: bigint |
| 124 | + ): SubmittableExtrinsic<"promise", ISubmittableResult> { |
| 125 | + return parachain.tx.wrapper.tracWrap(amount) |
| 126 | + } |
| 127 | + |
| 128 | + createUnwrapTx( |
| 129 | + parachain: ApiPromise, |
| 130 | + amount: bigint |
| 131 | + ): SubmittableExtrinsic<"promise", ISubmittableResult> { |
| 132 | + return parachain.tx.wrapper.tracUnwrap(amount) |
| 133 | + } |
| 134 | +} |
0 commit comments