Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WeightToFee function in MangataAdapter #111

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions packages/adapter/src/chains/mangata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import BN from "bn.js";
import type { SubmittableExtrinsic, AddressOrPair } from "@polkadot/api/types";
import type { u32, u64, u128, Option } from "@polkadot/types";
import type { u32, u128, Option } from "@polkadot/types";
import type { WeightV2 } from "@polkadot/types/interfaces";
import type { HexString } from "@polkadot/util/types";
import type { KeyringPair } from "@polkadot/keyring/types";
Expand All @@ -12,7 +12,11 @@ import {
getDerivativeAccountV2,
sendExtrinsic,
} from "../util";
import { WEIGHT_REF_TIME_PER_SECOND } from "../constants";
import {
WEIGHT_REF_TIME_PER_NANOS,
WEIGHT_REF_TIME_PER_SECOND,
WEIGHT_PROOF_SIZE_PER_MB,
} from "../constants";
import { SendExtrinsicResult } from "../types";

// MangataAdapter implements ChainAdapter
Expand Down Expand Up @@ -71,10 +75,19 @@ export class MangataAdapter extends ChainAdapter {

const api = this.getApi();
if (_.isEqual(defaultAsset.location, assetLocation)) {
const fee = (await api.call.transactionPaymentApi.queryWeightToFee(
weight,
)) as u64;
return fee;
const UNIT = new BN("1000000000000000000");
chrisli30 marked this conversation as resolved.
Show resolved Hide resolved
// ExtrinsicBaseWeight benchmark value: 114756 nano seconds
const extrinsicBaseWeight = WEIGHT_REF_TIME_PER_NANOS.mul(new BN(114756));
const feePerSecond = WEIGHT_REF_TIME_PER_SECOND.mul(
UNIT.div(extrinsicBaseWeight),
);
const refTimeFee = weight.refTime
.mul(feePerSecond)
.div(WEIGHT_REF_TIME_PER_SECOND);
const proofSizeFee = weight.proofSize
.mul(feePerSecond)
.div(WEIGHT_PROOF_SIZE_PER_MB);
return refTimeFee.add(proofSizeFee);
}
const storageValue =
await api.query.assetRegistry.locationToAssetId(assetLocation);
Expand Down
60 changes: 60 additions & 0 deletions packages/adapter/src/chains/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import { getDerivativeAccountV2, sendExtrinsic } from "../util";
import { SendExtrinsicResult } from "../types";
import { WEIGHT_REF_TIME_PER_SECOND } from "../constants";

export interface AutomationPriceTriggerParams {
chain: string;
exchange: string;
asset1: string;
asset2: string;
submittedAt: number;
triggerFunction: "lt" | "gt";
triggerParam: number[];
}

export enum OakAdapterTransactType {
PayThroughRemoteDerivativeAccount,
PayThroughSoverignAccount,
Expand Down Expand Up @@ -201,6 +211,56 @@ export class OakAdapter extends ChainAdapter {
return result;
}

/**
* Schedule XCMP task
* @param destination The location of the destination chain
* @param schedule Schedule setting
* @param scheduleFee Schedule fee
* @param executionFee Execution fee
* @param encodedCall Encoded call
* @param encodedCallWeight The encoded call weight weight of the XCM instructions
* @param overallWeight The overall weight of the XCM instructions
* @param keyringPair Operator's keyring pair
* @returns SendExtrinsicResult
*/
async scheduleXcmpPriceTask(
automationPriceTriggerParams: AutomationPriceTriggerParams,
destination: any,
scheduleFee: any,
executionFee: any,
encodedCall: HexString,
encodedCallWeight: Weight,
overallWeight: Weight,
keyringPair: KeyringPair,
): Promise<SendExtrinsicResult> {
const api = this.getApi();
const { key } = this.chainData;
if (_.isUndefined(key)) throw new Error("chainData.key not set");

const extrinsic = api.tx.automationPrice.scheduleXcmpTask(
automationPriceTriggerParams.chain,
automationPriceTriggerParams.exchange,
automationPriceTriggerParams.asset1,
automationPriceTriggerParams.asset2,
automationPriceTriggerParams.submittedAt,
automationPriceTriggerParams.triggerFunction,
automationPriceTriggerParams.triggerParam,
destination,
scheduleFee,
executionFee,
encodedCall,
encodedCallWeight,
overallWeight,
);

console.log(
`Send extrinsic from ${key} to schedule price task. extrinsic:`,
extrinsic.method.toHex(),
);
const result = await sendExtrinsic(api, extrinsic, keyringPair);
return result;
}

/**
* Calculate the derivative account ID of a certain account ID
* @param accountId
Expand Down
1 change: 1 addition & 0 deletions packages/adapter/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BN from "bn.js";

export const WEIGHT_REF_TIME_PER_NANOS = new BN(1000);
export const WEIGHT_REF_TIME_PER_SECOND = new BN(1000000000000);
export const WEIGHT_PROOF_SIZE_PER_MB = new BN(1024 * 1024);
42 changes: 37 additions & 5 deletions packages/config/src/tokens/dev.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,40 @@
import { createToken } from "./types/Token";

const tur = createToken({ key: "tur", symbol: "TUR", decimals: 10, network: "moonbase", parachainId: 2114 });
const sby = createToken({ key: "shibuya", symbol: "SBY", decimals: 18, network: "moonbase", parachainId: 2000 });
const moonbaseLocal = createToken({ key: "unit", symbol: "UNIT", decimals: 18, network: "moonbase", parachainId: 1000, palletInstance: 3 });
const mgr = createToken({ key: "mgr", symbol: "MGR", decimals: 18, network: "mangata", parachainId: 2110 });
const tur = createToken({
decimals: 10,
key: "tur",
network: "moonbase",
parachainId: 2114,
symbol: "TUR",
});
const sby = createToken({
decimals: 18,
key: "shibuya",
network: "moonbase",
parachainId: 2000,
symbol: "SBY",
});
const moonbaseLocal = createToken({
decimals: 18,
key: "unit",
network: "moonbase",
parachainId: 1000,
symbol: "UNIT",
x2Params: { palletInstance: 3 },
});
const mgr = createToken({
decimals: 18,
key: "mgr",
network: "mangata",
parachainId: 2110,
symbol: "MGR",
x2Params: {
GeneralKey: {
data: "0x0000000000000000000000000000000000000000000000000000000000000000",
length: 4,
},
},
});

export default { tur, sby, moonbaseLocal, mgr };
// eslint-disable-next-line import/no-default-export
export default { mgr, moonbaseLocal, sby, tur };
42 changes: 37 additions & 5 deletions packages/config/src/tokens/kusama.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { createToken } from "./types/Token";

const tur = createToken({ key: "tur", symbol: "TUR", decimals: 10, network: "kusama", parachainId: 2114 });
const tur = createToken({
decimals: 10,
key: "tur",
network: "kusama",
parachainId: 2114,
symbol: "TUR",
});

// TODO: movr is not defined in the original file; need to double check its value
const movr = createToken({ key: "movr", symbol: "MOVR", decimals: 10, network: "kusama", parachainId: 2023, palletInstance: 10 });
const sdn = createToken({ key: "shiden", symbol: "SDN", decimals: 18, network: "kusama", parachainId: 2007 });
const mgx = createToken({ key: "mangata", symbol: "MGX", decimals: 18, network: "kusama", parachainId: 2110 });
const movr = createToken({
decimals: 10,
key: "movr",
network: "kusama",
parachainId: 2023,
symbol: "MOVR",
x2Params: { palletInstance: 10 },
});
const sdn = createToken({
decimals: 18,
key: "shiden",
network: "kusama",
parachainId: 2007,
symbol: "SDN",
});
const mgx = createToken({
decimals: 18,
key: "mangata",
network: "kusama",
parachainId: 2110,
symbol: "MGX",
x2Params: {
GeneralKey: {
data: "0x0000000000000000000000000000000000000000000000000000000000000000",
length: 4,
},
},
});

export default { tur, movr, sdn, mgx };
// eslint-disable-next-line import/no-default-export
export default { mgx, movr, sdn, tur };
21 changes: 18 additions & 3 deletions packages/config/src/tokens/moonbase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { createToken } from "./types/Token";

const tur = createToken({ key: "tur", symbol: "TUR", decimals: 10, network: "moonbase", parachainId: 2114 });
const dev = createToken({ key: "dev", symbol: "DEV", decimals: 10, network: "moonbase", parachainId: 1000, palletInstance: 3 });
const tur = createToken({
decimals: 10,
key: "tur",
network: "moonbase",
parachainId: 2114,
symbol: "TUR",
});

export default { tur, dev };
const dev = createToken({
decimals: 10,
key: "dev",
network: "moonbase",
parachainId: 1000,
symbol: "DEV",
x2Params: { palletInstance: 3 },
});

// eslint-disable-next-line import/no-default-export
export default { dev, tur };
10 changes: 9 additions & 1 deletion packages/config/src/tokens/polkadot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createToken } from "./types/Token";

const glmr = createToken({ key: "glmr", symbol: "GLMR", decimals: 10, network: "kusama", parachainId: 2023, palletInstance: 10 });
const glmr = createToken({
decimals: 10,
key: "glmr",
network: "kusama",
parachainId: 2023,
symbol: "GLMR",
x2Params: { palletInstance: 10 },
});

// eslint-disable-next-line import/no-default-export
export default { glmr };
33 changes: 29 additions & 4 deletions packages/config/src/tokens/rococo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { createToken } from "./types/Token";

// You can now create assets without repeating the same structure over and over.
const tur = createToken({ key: "tur", symbol: "TUR", decimals: 10, network: "rococo", parachainId: 2114 });
const rstr = createToken({ key: "rocstar", symbol: "RSTR", decimals: 18, network: "rococo", parachainId: 2006 });
const mgr = createToken({ key: "mangata-rococo", symbol: "MGR", decimals: 18, network: "rococo", parachainId: 2110 });
const tur = createToken({
decimals: 10,
key: "tur",
network: "rococo",
parachainId: 2114,
symbol: "TUR",
});
const rstr = createToken({
decimals: 18,
key: "rocstar",
network: "rococo",
parachainId: 2006,
symbol: "RSTR",
});
const mgr = createToken({
decimals: 18,
key: "mangata-rococo",
network: "rococo",
parachainId: 2110,
symbol: "MGR",
x2Params: {
GeneralKey: {
data: "0x0000000000000000000000000000000000000000000000000000000000000000",
length: 4,
},
},
});

export default { tur, rstr, mgr };
// eslint-disable-next-line import/no-default-export
export default { mgr, rstr, tur };
Loading