Skip to content

Commit

Permalink
Export utils constants and errors from adapters (#120)
Browse files Browse the repository at this point in the history
* Export utils constants and errors from adapters

* docs(changeset): Export utils constants and errors from adapters
  • Loading branch information
chrisli30 authored Dec 6, 2023
1 parent a035f5c commit 74c4832
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-bulldogs-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oak-network/adapter": patch
---

Export utils constants and errors from adapters
53 changes: 13 additions & 40 deletions packages/adapter/src/chains/astar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -32,8 +28,7 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler {
*/
public async fetchAndUpdateConfigs(): Promise<void> {
await super.fetchAndUpdateConfigs();
this.chainConfig.xcm.instructionNetworkType =
XcmInstructionNetworkType.Concrete;
this.chainConfig.xcm.instructionNetworkType = XcmInstructionNetworkType.Concrete;
}

/**
Expand All @@ -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<Weight> {
const { refTime, proofSize } = (await extrinsic.paymentInfo(account))
.weight as unknown as WeightV2;
async getExtrinsicWeight(extrinsic: SubmittableExtrinsic<"promise">, account: AddressOrPair): Promise<Weight> {
const { refTime, proofSize } = (await extrinsic.paymentInfo(account)).weight as unknown as WeightV2;
return new Weight(new BN(refTime.unwrap()), new BN(proofSize.unwrap()));
}

Expand All @@ -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<Weight> {
async calculateXcmOverallWeight(transactCallWeight: Weight, instructionCount: number): Promise<Weight> {
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;
}

Expand All @@ -78,21 +64,16 @@ export class AstarAdapter extends ChainAdapter implements TaskScheduler {
*/
async weightToFee(weight: Weight, assetLocation: any): Promise<BN> {
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<u128>;
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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
{
Expand All @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/chains/mangata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/chains/moonbeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/chains/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 3 additions & 0 deletions packages/adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from "./chains";
export * from "./types";
export * from "./utils";
export * from "./constants";
export * from "./errors";
File renamed without changes.

0 comments on commit 74c4832

Please sign in to comment.