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

refactor: update ark decimals to 18 and new helpers #114

Merged
merged 22 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
6 changes: 5 additions & 1 deletion packages/mainsail/source/crypto/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const SATOSHI = 1e8;
export const SATOSHI = 1e18;

export const WEI_MULTIPLIER = 1;
export const GWEI_MULTIPLIER = 1e9;
export const ARK_MULTIPLIER = 1e18;
10 changes: 5 additions & 5 deletions packages/mainsail/source/crypto/networks/devnet/milestones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const milestones = [
base58: 30,
},
block: {
maxPayload: 2097152,
maxPayload: 2_097_152,
maxTransactions: 150,
version: 1,
},
Expand All @@ -15,19 +15,19 @@ export const milestones = [
multiPaymentLimit: 256,
reward: "0",
satoshi: {
decimals: 8,
denomination: 100000000,
decimals: 18,
denomination: 1_000_000_000_000_000_000,
},
stageTimeout: 2000,
stageTimeoutIncrease: 2000,
vendorFieldLength: 255,
},
{
height: 1,
activeValidators: 53,
height: 1,
},
{
height: 75600,
height: 75_600,
reward: "200000000",
},
];
16 changes: 16 additions & 0 deletions packages/mainsail/source/helpers/format-units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { formatUnits } from "./format-units";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the tests aren't configured yet for mainsail. Should I remove these files until then? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue is that the mainsail package is a copy of ark and not everything may work. I'll add a card though to enable tests based on the areas that are working so those run at least


describe("formatUnits", async ({ assert, it, loader }) => {
it("should format the value to wei", () => {
assert.equal(formatUnits("1", "wei"), "1");
assert.equal(formatUnits("1000000000", "gwei"), "1");
assert.equal(formatUnits("100000000000000000000", "ark"), "1");
});

it("should throw an error for unsupported units", () => {
assert.throws(
() => formatUnits("1", "btc"),
"Unsupported unit: btc. Supported units are 'wei', 'gwei', and 'ark'.",
);
});
});
17 changes: 17 additions & 0 deletions packages/mainsail/source/helpers/format-units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable unicorn/require-number-to-fixed-digits-argument */
import { BigNumber } from "@mainsail/kernel/distribution/utils";

import { ARK_MULTIPLIER, GWEI_MULTIPLIER, WEI_MULTIPLIER } from "../crypto/constants";

export const formatUnits = (value: string, unit = "ark"): string => {
switch (unit.toLowerCase()) {
case "wei":
return BigNumber.make(value).dividedBy(WEI_MULTIPLIER).toFixed();
case "gwei":
return BigNumber.make(value).dividedBy(GWEI_MULTIPLIER).toFixed();
case "ark":
return BigNumber.make(value).dividedBy(ARK_MULTIPLIER).toFixed();
default:
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
}
};
16 changes: 16 additions & 0 deletions packages/mainsail/source/helpers/parse-units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { parseUnits } from "./parse-units";

describe("parseUnits", async ({ assert, it, loader }) => {
it("should parse the value to wei", () => {
assert.equal(parseUnits(1, "wei"), "1");
assert.equal(parseUnits(1, "gwei"), "1000000000");
assert.equal(parseUnits(1, "ark"), "100000000000000000000");
});

it("should throw an error for unsupported units", () => {
assert.throws(
() => parseUnits(1, "btc"),
"Unsupported unit: btc. Supported units are 'wei', 'gwei', and 'ark'.",
);
});
});
16 changes: 16 additions & 0 deletions packages/mainsail/source/helpers/parse-units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigNumber } from "@mainsail/kernel/distribution/utils";

import { ARK_MULTIPLIER, GWEI_MULTIPLIER, WEI_MULTIPLIER } from "../crypto/constants";

export const parseUnits = (value: number | string, unit = "ark"): string => {
switch (unit.toLowerCase()) {
case "wei":
return BigNumber.make(value).times(WEI_MULTIPLIER).toString();
case "gwei":
return BigNumber.make(value).times(GWEI_MULTIPLIER).toString();
case "ark":
return BigNumber.make(value).times(ARK_MULTIPLIER).toString();
default:
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
}
};
2 changes: 1 addition & 1 deletion packages/mainsail/source/networks/mainsail.devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const network: Networks.NetworkManifest = {
slip44: 1,
},
currency: {
decimals: 8,
decimals: 18,
symbol: "TѦ",
ticker: "ARK",
},
Expand Down
6 changes: 3 additions & 3 deletions packages/mainsail/source/networks/mainsail.mainnet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { explorer, featureFlags, importMethods, transactions } from "./shared.js";

import { Networks } from "@ardenthq/sdk";

import { explorer, featureFlags, importMethods, transactions } from "./shared.js";

const network: Networks.NetworkManifest = {
coin: "Mainsail",
constants: {
epoch: "2017-03-21T13:00:00.000Z",
slip44: 111,
},
currency: {
decimals: 8,
decimals: 18,
symbol: "Ѧ",
ticker: "ARK",
},
Expand Down
2 changes: 1 addition & 1 deletion packages/test/source/assert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { BigNumber } from "@ardenthq/sdk-helpers";
import { format } from "concordance";
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { join } from "path";
import * as uvu from "uvu/assert";
import { z, ZodRawShape } from "zod";
Expand Down
Loading