Skip to content

Commit

Permalink
refactor: update mainsail decimals to 18 and add unit formatters (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricio0312rev authored Nov 18, 2024
1 parent 9c32c5a commit 47e0251
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 11 deletions.
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";

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

0 comments on commit 47e0251

Please sign in to comment.