From e5841736b84a30a799251a861617edbef50ed11e Mon Sep 17 00:00:00 2001 From: chrisli30 Date: Tue, 5 Dec 2023 23:26:57 -0800 Subject: [PATCH 1/2] Export utils constants and errors from adapters --- packages/adapter/src/chains/astar.ts | 53 ++++++---------------- packages/adapter/src/chains/mangata.ts | 2 +- packages/adapter/src/chains/moonbeam.ts | 2 +- packages/adapter/src/chains/oak.ts | 2 +- packages/adapter/src/index.ts | 3 ++ packages/adapter/src/{util.ts => utils.ts} | 0 6 files changed, 19 insertions(+), 43 deletions(-) rename packages/adapter/src/{util.ts => utils.ts} (100%) diff --git a/packages/adapter/src/chains/astar.ts b/packages/adapter/src/chains/astar.ts index 9baa694..e61da8f 100644 --- a/packages/adapter/src/chains/astar.ts +++ b/packages/adapter/src/chains/astar.ts @@ -8,11 +8,7 @@ import { u8aToHex } from "@polkadot/util"; import type { KeyringPair } from "@polkadot/keyring/types"; import { Weight, XcmInstructionNetworkType } from "@oak-network/config"; import { ChainAdapter, TaskScheduler } from "./chainAdapter"; -import { - convertAbsoluteLocationToRelative, - getDerivativeAccountV3, - sendExtrinsic, -} from "../util"; +import { convertAbsoluteLocationToRelative, getDerivativeAccountV3, sendExtrinsic } from "../utils"; import { SendExtrinsicResult } from "../types"; import { WEIGHT_REF_TIME_PER_SECOND } from "../constants"; @@ -32,8 +28,7 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { */ public async fetchAndUpdateConfigs(): Promise { await super.fetchAndUpdateConfigs(); - this.chainConfig.xcm.instructionNetworkType = - XcmInstructionNetworkType.Concrete; + this.chainConfig.xcm.instructionNetworkType = XcmInstructionNetworkType.Concrete; } /** @@ -43,12 +38,8 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { * @returns Extrinsic weight */ // eslint-disable-next-line class-methods-use-this - async getExtrinsicWeight( - extrinsic: SubmittableExtrinsic<"promise">, - account: AddressOrPair, - ): Promise { - const { refTime, proofSize } = (await extrinsic.paymentInfo(account)) - .weight as unknown as WeightV2; + async getExtrinsicWeight(extrinsic: SubmittableExtrinsic<"promise">, account: AddressOrPair): Promise { + const { refTime, proofSize } = (await extrinsic.paymentInfo(account)).weight as unknown as WeightV2; return new Weight(new BN(refTime.unwrap()), new BN(proofSize.unwrap())); } @@ -58,15 +49,10 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { * @param instructionCount The number of XCM instructions * @returns XCM overall weight */ - async calculateXcmOverallWeight( - transactCallWeight: Weight, - instructionCount: number, - ): Promise { + async calculateXcmOverallWeight(transactCallWeight: Weight, instructionCount: number): Promise { const { xcm } = this.chainConfig; if (_.isUndefined(xcm)) throw new Error("chainConfig.xcm not set"); - const overallWeight = transactCallWeight.add( - xcm.instructionWeight.muln(instructionCount), - ); + const overallWeight = transactCallWeight.add(xcm.instructionWeight.muln(instructionCount)); return overallWeight; } @@ -78,21 +64,16 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { */ async weightToFee(weight: Weight, assetLocation: any): Promise { const [defaultAsset] = this.chainConfig.assets; - if (_.isUndefined(defaultAsset)) - throw new Error("chainConfig.defaultAsset not set"); + if (_.isUndefined(defaultAsset)) throw new Error("chainConfig.defaultAsset not set"); const api = this.getApi(); if (_.isEqual(defaultAsset.location, assetLocation)) { - const fee = (await api.call.transactionPaymentApi.queryWeightToFee( - weight, - )) as u64; + const fee = (await api.call.transactionPaymentApi.queryWeightToFee(weight)) as u64; return fee; } - const AssetLocationUnitsPerSecond = - await api.query.xcAssetConfig.assetLocationUnitsPerSecond(assetLocation); + const AssetLocationUnitsPerSecond = await api.query.xcAssetConfig.assetLocationUnitsPerSecond(assetLocation); const metadataItem = AssetLocationUnitsPerSecond as unknown as Option; - if (metadataItem.isNone) - throw new Error("MetadatAssetLocationUnitsPerSecond is null"); + if (metadataItem.isNone) throw new Error("MetadatAssetLocationUnitsPerSecond is null"); const unitsPerSecond = metadataItem.unwrap(); return weight.refTime.mul(unitsPerSecond).div(WEIGHT_REF_TIME_PER_SECOND); } @@ -190,10 +171,7 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { }, ); - console.log( - `Send extrinsic from ${key} to schedule task. extrinsic:`, - extrinsic.method.toHex(), - ); + console.log(`Send extrinsic from ${key} to schedule task. extrinsic:`, extrinsic.method.toHex()); const result = await sendExtrinsic(api, extrinsic, keyringPair); return result; @@ -229,9 +207,7 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { if (_.isUndefined(key)) throw new Error("chainConfig.key not set"); const api = this.getApi(); - const transferAssetLocation = this.isNativeAsset(assetLocation) - ? convertAbsoluteLocationToRelative(assetLocation) - : assetLocation; + const transferAssetLocation = this.isNativeAsset(assetLocation) ? convertAbsoluteLocationToRelative(assetLocation) : assetLocation; const extrinsic = api.tx.xtokens.transferMultiasset( { @@ -243,10 +219,7 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler { { V3: { interior: { - X2: [ - destination.interior.X1, - { AccountId32: { id: recipient, network: null } }, - ], + X2: [destination.interior.X1, { AccountId32: { id: recipient, network: null } }], }, parents: 1, }, diff --git a/packages/adapter/src/chains/mangata.ts b/packages/adapter/src/chains/mangata.ts index 61af5ed..2b8a39a 100644 --- a/packages/adapter/src/chains/mangata.ts +++ b/packages/adapter/src/chains/mangata.ts @@ -7,7 +7,7 @@ import type { HexString } from "@polkadot/util/types"; import type { KeyringPair } from "@polkadot/keyring/types"; import { Weight } from "@oak-network/config"; import { ChainAdapter } from "./chainAdapter"; -import { getDerivativeAccountV2, sendExtrinsic } from "../util"; +import { getDerivativeAccountV2, sendExtrinsic } from "../utils"; import { WEIGHT_REF_TIME_PER_NANOS, WEIGHT_REF_TIME_PER_SECOND, WEIGHT_PROOF_SIZE_PER_MB } from "../constants"; import { SendExtrinsicResult } from "../types"; diff --git a/packages/adapter/src/chains/moonbeam.ts b/packages/adapter/src/chains/moonbeam.ts index 5da6d2c..21b22c0 100644 --- a/packages/adapter/src/chains/moonbeam.ts +++ b/packages/adapter/src/chains/moonbeam.ts @@ -7,7 +7,7 @@ import type { HexString } from "@polkadot/util/types"; import type { KeyringPair } from "@polkadot/keyring/types"; import { Weight } from "@oak-network/config"; import { ChainAdapter, TaskScheduler } from "./chainAdapter"; -import { convertAbsoluteLocationToRelative, getDerivativeAccountV3, sendExtrinsic } from "../util"; +import { convertAbsoluteLocationToRelative, getDerivativeAccountV3, sendExtrinsic } from "../utils"; import { AccountType, SendExtrinsicResult } from "../types"; import { WEIGHT_REF_TIME_PER_SECOND } from "../constants"; diff --git a/packages/adapter/src/chains/oak.ts b/packages/adapter/src/chains/oak.ts index ec12f2c..4bdc1ef 100644 --- a/packages/adapter/src/chains/oak.ts +++ b/packages/adapter/src/chains/oak.ts @@ -9,7 +9,7 @@ import type { KeyringPair } from "@polkadot/keyring/types"; import { Weight, XcmInstructionNetworkType, Chain, XToken } from "@oak-network/config"; import { ISubmittableResult } from "@polkadot/types/types"; import { ChainAdapter } from "./chainAdapter"; -import { getDerivativeAccountV2, isValidAddress, sendExtrinsic, getDecimalBN } from "../util"; +import { getDerivativeAccountV2, isValidAddress, sendExtrinsic, getDecimalBN } from "../utils"; import { AccountType, SendExtrinsicResult } from "../types"; import { WEIGHT_REF_TIME_PER_SECOND } from "../constants"; import { InvalidAddress } from "../errors"; diff --git a/packages/adapter/src/index.ts b/packages/adapter/src/index.ts index 313ed9e..fdcb569 100644 --- a/packages/adapter/src/index.ts +++ b/packages/adapter/src/index.ts @@ -1,2 +1,5 @@ export * from "./chains"; export * from "./types"; +export * from "./utils"; +export * from "./constants"; +export * from "./errors"; diff --git a/packages/adapter/src/util.ts b/packages/adapter/src/utils.ts similarity index 100% rename from packages/adapter/src/util.ts rename to packages/adapter/src/utils.ts From 5be143e5a3b8406f1f235089f68373a9d546441c Mon Sep 17 00:00:00 2001 From: chrisli30 Date: Tue, 5 Dec 2023 23:27:20 -0800 Subject: [PATCH 2/2] docs(changeset): Export utils constants and errors from adapters --- .changeset/fast-bulldogs-knock.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fast-bulldogs-knock.md diff --git a/.changeset/fast-bulldogs-knock.md b/.changeset/fast-bulldogs-knock.md new file mode 100644 index 0000000..1ed3c0a --- /dev/null +++ b/.changeset/fast-bulldogs-knock.md @@ -0,0 +1,5 @@ +--- +"@oak-network/adapter": patch +--- + +Export utils constants and errors from adapters