diff --git a/.gitignore b/.gitignore index 0ae6e738..5914948f 100644 --- a/.gitignore +++ b/.gitignore @@ -175,6 +175,7 @@ dist .svelte-kit ### VisualStudioCode ### +.vscode .vscode/* !.vscode/settings.json !.vscode/tasks.json @@ -268,3 +269,4 @@ next-env.d.ts # E2E Chrome Extensions extensions + diff --git a/tests/helper/index.ts b/tests/helper/index.ts index 6ec4ba49..980d5474 100644 --- a/tests/helper/index.ts +++ b/tests/helper/index.ts @@ -10,6 +10,7 @@ import ECPairFactory from "ecpair"; import { GlobalParamsVersion } from "@/app/types/globalParams"; import { createStakingTx } from "@/utils/delegations/signStakingTx"; import { getCurrentGlobalParamsVersion } from "@/utils/globalParams"; +import { getPublicKeyNoCoord } from "@/utils/wallet"; import { UTXO } from "@/utils/wallet/wallet_provider"; // Initialize the ECC library @@ -62,7 +63,7 @@ export class DataGenerator { return { keyPair, publicKey: pk, - noCoordPublicKey: pk.slice(2), + noCoordPublicKey: getPublicKeyNoCoord(pk).toString("hex"), }; }; @@ -113,9 +114,9 @@ export class DataGenerator { const unbondingTime = this.generateRandomUnbondingTime(stakingTerm); const tag = this.generateRandomTag().toString("hex"); - let minStakingTimeBlocks = Math.floor(Math.random() * 100); + let minStakingTimeBlocks = this.getRandomIntegerBetween(1, 100); let maxStakingTimeBlocks = - minStakingTimeBlocks + Math.floor(Math.random() * 1000); + minStakingTimeBlocks + this.getRandomIntegerBetween(1, 1000); if (isFixedTimelock) { maxStakingTimeBlocks = minStakingTimeBlocks; } diff --git a/tests/utils/delegations/createStakingTx.test.ts b/tests/utils/delegations/createStakingTx.test.ts index 486883f0..a85649bf 100644 --- a/tests/utils/delegations/createStakingTx.test.ts +++ b/tests/utils/delegations/createStakingTx.test.ts @@ -11,7 +11,6 @@ describe.each(testingNetworks)( const feeRate = DEFAULT_TEST_FEE_RATE; const { address: stakerTaprootAddress, scriptPubKey } = dataGen.getAddressAndScriptPubKey(randomStakerKeys.publicKey).taproot; - const randomParam = dataGen.generateRandomGlobalParams(isFixed); const randomStakingAmount = dataGen.getRandomIntegerBetween( randomParam.minStakingAmountSat, @@ -22,8 +21,8 @@ describe.each(testingNetworks)( randomParam.maxStakingTimeBlocks, ); const randomInputUTXOs = dataGen.generateRandomUTXOs( - randomStakingAmount + Math.floor(Math.random() * 100000000), - Math.floor(Math.random() * 10) + 1, + randomStakingAmount + dataGen.getRandomIntegerBetween(0, 100000000), + dataGen.getRandomIntegerBetween(1, 10), scriptPubKey, ); const testTermDescription = isFixed ? "fixed term" : "variable term"; diff --git a/tests/utils/delegations/fee.test.ts b/tests/utils/delegations/fee.test.ts index b1a46b82..746ee41c 100644 --- a/tests/utils/delegations/fee.test.ts +++ b/tests/utils/delegations/fee.test.ts @@ -2,63 +2,58 @@ import { txFeeSafetyCheck } from "@/utils/delegations/fee"; import { testingNetworks } from "../../helper"; -describe.each(testingNetworks)( - "txFeeSafetyCheck", - ({ networkName, dataGenerator }) => { - const feeRate = dataGenerator.generateRandomFeeRates(); - const globalParams = dataGenerator.generateGlobalPramsVersions( - dataGenerator.getRandomIntegerBetween(1, 10), - ); - const randomParam = - globalParams[ - dataGenerator.getRandomIntegerBetween(0, globalParams.length - 1) - ]; - const { signedPsbt } = dataGenerator.createRandomStakingPsbt( - globalParams, - randomParam.activationHeight + 1, - ); - const tx = signedPsbt.extractTransaction(); - - describe(`on ${networkName} - `, () => { - test("should not throw an error if the estimated fee is within the acceptable range", () => { - let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - - estimatedFee = tx.virtualSize() * feeRate * 2 - 1; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - }); - - test("should throw an error if the estimated fee is too high", () => { - const estimatedFee = tx.virtualSize() * feeRate * 2 + 1; - - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).toThrow("Estimated fee is too high"); - }); - - test("should throw an error if the estimated fee is too low", () => { - const estimatedFee = (tx.virtualSize() * feeRate) / 2 - 1; - - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).toThrow("Estimated fee is too low"); - }); - - test("should not throw an error if the estimated fee is exactly within the boundary", () => { - let estimatedFee = (tx.virtualSize() * feeRate) / 2; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - - estimatedFee = tx.virtualSize() * feeRate * 2; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - }); - }); - }, -); +describe.each(testingNetworks)("txFeeSafetyCheck", ({ dataGenerator }) => { + const feeRate = dataGenerator.generateRandomFeeRates(); + const globalParams = dataGenerator.generateGlobalPramsVersions( + dataGenerator.getRandomIntegerBetween(1, 10), + ); + const randomParam = + globalParams[ + dataGenerator.getRandomIntegerBetween(0, globalParams.length - 1) + ]; + const { signedPsbt } = dataGenerator.createRandomStakingPsbt( + globalParams, + randomParam.activationHeight + 1, + ); + const tx = signedPsbt.extractTransaction(); + + test("should not throw an error if the estimated fee is within the acceptable range", () => { + let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + + estimatedFee = tx.virtualSize() * feeRate * 2 - 1; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + }); + + test("should throw an error if the estimated fee is too high", () => { + const estimatedFee = tx.virtualSize() * feeRate * 2 + 1; + + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).toThrow("Estimated fee is too high"); + }); + + test("should throw an error if the estimated fee is too low", () => { + const estimatedFee = (tx.virtualSize() * feeRate) / 2 - 1; + + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).toThrow("Estimated fee is too low"); + }); + + test("should not throw an error if the estimated fee is exactly within the boundary", () => { + let estimatedFee = (tx.virtualSize() * feeRate) / 2; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + + estimatedFee = tx.virtualSize() * feeRate * 2; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + }); +}); diff --git a/tests/utils/delegations/signUnbondingTx.test.ts b/tests/utils/delegations/signUnbondingTx.test.ts index 315a53f1..77b300c9 100644 --- a/tests/utils/delegations/signUnbondingTx.test.ts +++ b/tests/utils/delegations/signUnbondingTx.test.ts @@ -1,9 +1,3 @@ -import { Psbt, Transaction } from "bitcoinjs-lib"; - -import { signUnbondingTx } from "@/utils/delegations/signUnbondingTx"; - -import { testingNetworks } from "../../helper"; - jest.mock("@/app/api/getUnbondingEligibility", () => ({ getUnbondingEligibility: jest.fn(), })); @@ -14,6 +8,15 @@ jest.mock("@/app/api/postUnbonding", () => ({ postUnbonding: jest.fn(), })); +import { Psbt, Transaction } from "bitcoinjs-lib"; + +import { getGlobalParams } from "@/app/api/getGlobalParams"; +import { getUnbondingEligibility } from "@/app/api/getUnbondingEligibility"; +import { postUnbonding } from "@/app/api/postUnbonding"; +import { signUnbondingTx } from "@/utils/delegations/signUnbondingTx"; + +import { testingNetworks } from "../../helper"; + describe("signUnbondingTx", () => { const { network, dataGenerator } = testingNetworks[0]; const randomTxId = dataGenerator.generateRandomTxId(); @@ -41,7 +44,8 @@ describe("signUnbondingTx", () => { dataGenerator.generateRandomKeyPair().noCoordPublicKey, stakingTx: { startHeight: randomStakingTxHeight, - timelock: signedPsbtTx.locktime, + timelock: 150, + //timelock: signedPsbtTx.locktime, txHex: signedPsbtTx.toHex(), outputIndex: 0, }, @@ -89,10 +93,7 @@ describe("signUnbondingTx", () => { }); it("should throw an error if the stakingTx is not eligible for unbonding", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return false; }); @@ -108,15 +109,10 @@ describe("signUnbondingTx", () => { }); it("should throw an error if global param is not loaded", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return []; }); @@ -132,15 +128,10 @@ describe("signUnbondingTx", () => { }); it("should throw an error if the current version is not found", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); @@ -166,18 +157,12 @@ describe("signUnbondingTx", () => { }); it("should throw error if fail to signPsbtTx", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); - mockedSignPsbtTx.mockRejectedValueOnce(new Error("oops!")); expect( @@ -192,20 +177,13 @@ describe("signUnbondingTx", () => { }); it("should return the signed unbonding transaction", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); - - const { postUnbonding } = require("@/app/api/postUnbonding"); - postUnbonding.mockImplementationOnce(async () => { + (postUnbonding as any).mockImplementationOnce(async () => { return; }); diff --git a/tests/utils/globalParams.test.ts b/tests/utils/globalParams.test.ts index dceae9dd..2fd07f4b 100644 --- a/tests/utils/globalParams.test.ts +++ b/tests/utils/globalParams.test.ts @@ -11,7 +11,7 @@ describe("globalParams", () => { beforeEach(() => { globalParams = dataGenerator.generateGlobalPramsVersions( - dataGenerator.getRandomIntegerBetween(1, 100), + dataGenerator.getRandomIntegerBetween(2, 100), ); lastActivationHeight = globalParams[globalParams.length - 1].activationHeight;