diff --git a/packages/mainsail/source/crypto/constants.ts b/packages/mainsail/source/crypto/constants.ts index 512c07c5..c62ee796 100644 --- a/packages/mainsail/source/crypto/constants.ts +++ b/packages/mainsail/source/crypto/constants.ts @@ -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; diff --git a/packages/mainsail/source/crypto/networks/devnet/milestones.ts b/packages/mainsail/source/crypto/networks/devnet/milestones.ts index 6789158d..b4ec6213 100644 --- a/packages/mainsail/source/crypto/networks/devnet/milestones.ts +++ b/packages/mainsail/source/crypto/networks/devnet/milestones.ts @@ -5,7 +5,7 @@ export const milestones = [ base58: 30, }, block: { - maxPayload: 2097152, + maxPayload: 2_097_152, maxTransactions: 150, version: 1, }, @@ -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", }, ]; diff --git a/packages/mainsail/source/helpers/format-units.test.ts b/packages/mainsail/source/helpers/format-units.test.ts new file mode 100644 index 00000000..08af3950 --- /dev/null +++ b/packages/mainsail/source/helpers/format-units.test.ts @@ -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'.", + ); + }); +}); diff --git a/packages/mainsail/source/helpers/format-units.ts b/packages/mainsail/source/helpers/format-units.ts new file mode 100644 index 00000000..f1e31956 --- /dev/null +++ b/packages/mainsail/source/helpers/format-units.ts @@ -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'.`); + } +}; diff --git a/packages/mainsail/source/helpers/parse-units.test.ts b/packages/mainsail/source/helpers/parse-units.test.ts new file mode 100644 index 00000000..da43f2b0 --- /dev/null +++ b/packages/mainsail/source/helpers/parse-units.test.ts @@ -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'.", + ); + }); +}); diff --git a/packages/mainsail/source/helpers/parse-units.ts b/packages/mainsail/source/helpers/parse-units.ts new file mode 100644 index 00000000..b147559e --- /dev/null +++ b/packages/mainsail/source/helpers/parse-units.ts @@ -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'.`); + } +}; diff --git a/packages/mainsail/source/networks/mainsail.devnet.ts b/packages/mainsail/source/networks/mainsail.devnet.ts index 252df5a4..ffa7dadf 100644 --- a/packages/mainsail/source/networks/mainsail.devnet.ts +++ b/packages/mainsail/source/networks/mainsail.devnet.ts @@ -9,7 +9,7 @@ const network: Networks.NetworkManifest = { slip44: 1, }, currency: { - decimals: 8, + decimals: 18, symbol: "TѦ", ticker: "ARK", }, diff --git a/packages/mainsail/source/networks/mainsail.mainnet.ts b/packages/mainsail/source/networks/mainsail.mainnet.ts index 32f817c1..9d531799 100644 --- a/packages/mainsail/source/networks/mainsail.mainnet.ts +++ b/packages/mainsail/source/networks/mainsail.mainnet.ts @@ -1,7 +1,7 @@ -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: { @@ -9,7 +9,7 @@ const network: Networks.NetworkManifest = { slip44: 111, }, currency: { - decimals: 8, + decimals: 18, symbol: "Ѧ", ticker: "ARK", }, diff --git a/packages/test/source/assert.ts b/packages/test/source/assert.ts index 920158e6..461878b2 100644 --- a/packages/test/source/assert.ts +++ b/packages/test/source/assert.ts @@ -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";