Skip to content

Commit

Permalink
chore: resolve merge conflicts from main into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Jul 23, 2024
2 parents 6cdadfa + f3ae459 commit 72e22ff
Show file tree
Hide file tree
Showing 16 changed files with 5,288 additions and 1,992 deletions.
6,813 changes: 4,843 additions & 1,970 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"@tanstack/react-query-next-experimental": "^5.28.14",
"axios": "^1.6.8",
"bitcoinjs-lib": "^6.1.5",
"btc-staking-ts": "^0.2.8",
"btc-staking-ts": "^0.2.10",
"date-fns": "^3.6.0",
"decimal.js-light": "^2.5.1",
"framer-motion": "^11.1.9",
"next": "14.1.3",
"next-themes": "^0.3.0",
Expand Down
1 change: 0 additions & 1 deletion src/utils/delegations/signWithdrawalTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const signWithdrawalTx = async (
address,
btcWalletNetwork,
feeRate.defaultFeeRate,
0,
);
} else {
// Withdraw funds from a staking transaction in which the timelock naturally expired
Expand Down
3 changes: 2 additions & 1 deletion src/utils/globalParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const getCurrentGlobalParamsVersion = (
throw new Error("No global params versions found");
}
// Step 1: Sort the versions in descending order based on activationHeight
const sorted = versionedParams.sort(
// Note, we have cloned the array to avoid mutating the original array
const sorted = [...versionedParams].sort(
(a, b) => b.activationHeight - a.activationHeight,
);

Expand Down
11 changes: 2 additions & 9 deletions src/utils/maxDecimals.tsx → src/utils/maxDecimals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Decimal } from "decimal.js-light";
/**
* Limits the number of decimal places of a given number to a specified maximum.
*
Expand All @@ -14,14 +15,6 @@
* maxDecimals(3.141, 3); // returns 3.141
* maxDecimals(3.149, 3); // returns 3.149
*/

export const maxDecimals = (value: number, maxDecimals: number): number => {
// Convert the number to a string with a fixed number of decimal places
const fixedString = value.toFixed(maxDecimals);

// Convert the fixed string back to a number using the unary plus operator
const result = +fixedString;

// Return the result
return result;
return new Decimal(value).toDecimalPlaces(maxDecimals).toNumber();
};
15 changes: 15 additions & 0 deletions src/utils/trim.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Trims a given string to a specified number of symbols, adding ellipsis in the
* middle if necessary.
*
* @param {string} str - The string to be trimmed.
* @param {number} [symbols=8] - The total number of symbols to retain in the
* trimmed string, including the ellipsis. Defaults to 8 if not provided.
* @returns {string} - The trimmed string with ellipsis in the middle if the
* original string length exceeds the specified symbol count.
*/
export const trim = (str: string, symbols: number = 8) => {
if (str.length <= symbols) {
return str;
} else if (symbols === 0) {
return "...";
}
return `${str.slice(0, symbols / 2)}...${str.slice(-symbols / 2)}`;
};
31 changes: 25 additions & 6 deletions tests/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ export class DataGenerator {
this.network = network;
}

generateRandomString = (length: number): string => {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};

generateRandomTxId = () => {
const randomBuffer = Buffer.alloc(32);
for (let i = 0; i < 32; i++) {
Expand Down Expand Up @@ -121,7 +132,7 @@ export class DataGenerator {
const prev = previousPram ? previousPram : defaultParams;

return {
version: prev.version ? prev.version + 1 : 0,
version: prev.version + 1,
activationHeight:
prev.activationHeight + this.getRandomIntegerBetween(0, 10000),
stakingCapSat:
Expand All @@ -135,7 +146,7 @@ export class DataGenerator {
minStakingAmountSat,
maxStakingTimeBlocks,
minStakingTimeBlocks,
confirmationDepth: Math.floor(Math.random() * 20),
confirmationDepth: this.getRandomIntegerBetween(1, 20),
unbondingTime,
tag,
};
Expand All @@ -152,6 +163,7 @@ export class DataGenerator {
versions.push(
this.generateRandomGlobalParams(isFixedTimelock, lastVersion),
);
lastVersion = versions[i];
}
return versions;
};
Expand Down Expand Up @@ -242,7 +254,7 @@ export class DataGenerator {
};
};

createRandomStakingTx = (
createRandomStakingPsbt = (
globalParams: GlobalParamsVersion[],
txHeight: number,
stakerKeysPairs?: KeyPairs,
Expand Down Expand Up @@ -283,10 +295,17 @@ export class DataGenerator {
scriptPubKey,
),
);
return unsignedStakingPsbt

const unsignedPsbt = unsignedStakingPsbt;

const signedPsbt = unsignedStakingPsbt
.signAllInputs(stakerKeys.keyPair)
.finalizeAllInputs()
.extractTransaction();
.finalizeAllInputs();

return {
unsignedPsbt,
signedPsbt,
};
};

private getTaprootAddress = (publicKey: string) => {
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/delegations/fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ describe("txFeeSafetyCheck", () => {
globalParams[
dataGenerator.getRandomIntegerBetween(0, globalParams.length - 1)
];
const tx = dataGenerator.createRandomStakingTx(
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;
Expand Down
9 changes: 6 additions & 3 deletions tests/utils/delegations/signUnbondingTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ describe("signUnbondingTx", () => {
randomGlobalParamsVersions.length - 1,
)
].activationHeight + 1;
const randomStakingTx = dataGenerator.createRandomStakingTx(
const { signedPsbt } = dataGenerator.createRandomStakingPsbt(
randomGlobalParamsVersions,
randomStakingTxHeight,
stakerKeys,
);

const signedPsbtTx = signedPsbt.extractTransaction();

const mockedDelegationApi = [
{
stakingTxHashHex: randomTxId,
finalityProviderPkHex:
dataGenerator.generateRandomKeyPair().noCoordPublicKey,
stakingTx: {
startHeight: randomStakingTxHeight,
timelock: randomStakingTx.locktime,
txHex: randomStakingTx.toHex(),
timelock: signedPsbtTx.locktime,
txHex: signedPsbtTx.toHex(),
outputIndex: 0,
},
},
Expand Down
44 changes: 44 additions & 0 deletions tests/utils/formatTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { durationTillNow } from "@/utils/formatTime";

describe("durationTillNow", () => {
const currentTime = new Date("2024-01-01T12:00:00Z").getTime();

it('should return "Ongoing" if time is empty', () => {
expect(durationTillNow("", currentTime)).toBe("Ongoing");
});

it('should return "Ongoing" if time starts with "000"', () => {
expect(durationTillNow("0000-00-00T00:00:00Z", currentTime)).toBe(
"Ongoing",
);
});

it("should return the correct duration in days, hours, and minutes", () => {
const pastTime = new Date("2023-12-31T10:00:00Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("1 day 2 hours ago");
});

it("should return the correct duration in hours", () => {
const pastTime = new Date("2024-01-01T10:00:00Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("2 hours ago");
});

it("should return the correct duration in minutes", () => {
const pastTime = new Date("2024-01-01T11:50:00Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("10 minutes ago");
});

it("should return the correct duration in seconds if less than a minute", () => {
const pastTime = new Date("2024-01-01T11:59:30Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("30 seconds ago");
});

it('should return "Just now" if the duration is less than a second', () => {
let pastTime = new Date("2024-01-01T12:00:00Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("Just now");

// test the ms
pastTime = new Date("2024-01-01T11:59:59.999Z").toISOString();
expect(durationTillNow(pastTime, currentTime)).toBe("Just now");
});
});
44 changes: 44 additions & 0 deletions tests/utils/getFeeRateFromMempool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getFeeRateFromMempool } from "@/utils/getFeeRateFromMempool";
import { Fees } from "@/utils/wallet/wallet_provider";

describe("getFeeRateFromMempool", () => {
const mempoolFeeRates: Fees = {
hourFee: 10,
fastestFee: 100,
halfHourFee: 50,
economyFee: 5,
minimumFee: 1,
};

it("should return 0 for min, default, and max fee rates when mempoolFeeRates is undefined", () => {
const result = getFeeRateFromMempool();
expect(result).toEqual({
minFeeRate: 0,
defaultFeeRate: 0,
maxFeeRate: 0,
});
});

it("should return the correct min, default, and max fee rates from mempoolFeeRates", () => {
const result = getFeeRateFromMempool(mempoolFeeRates);
expect(result).toEqual({
minFeeRate: mempoolFeeRates.hourFee,
defaultFeeRate: mempoolFeeRates.fastestFee,
maxFeeRate: 256, // Ensures that the max fee rate is the next power of two
});
});

it("should return max fee rate of at least 128 sat/vB", () => {
const feeRate: Fees = {
...mempoolFeeRates,
fastestFee: 5,
};

const result = getFeeRateFromMempool(feeRate);
expect(result).toEqual({
minFeeRate: feeRate.hourFee,
defaultFeeRate: feeRate.fastestFee,
maxFeeRate: 128, // Ensures that the max fee rate is at least 128
});
});
});
33 changes: 33 additions & 0 deletions tests/utils/getStakingTerm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getStakingTerm } from "@/utils/getStakingTerm";

describe("getStakingTerm", () => {
it("should return the fixed term when minStakingTimeBlocks equals maxStakingTimeBlocks", () => {
const params: any = {
minStakingTimeBlocks: 100,
maxStakingTimeBlocks: 100,
};
const term = 50;
expect(getStakingTerm(params, term)).toBe(100);
});

it("should return the input term when minStakingTimeBlocks does not equal maxStakingTimeBlocks", () => {
const params: any = {
minStakingTimeBlocks: 100,
maxStakingTimeBlocks: 200,
};
const term = 150;
expect(getStakingTerm(params, term)).toBe(term);
});

it("should return the input term when params is undefined", () => {
const params = undefined;
const term = 150;
expect(getStakingTerm(params as any, term)).toBe(term);
});

it("should return the input term when params is null", () => {
const params = null;
const term = 150;
expect(getStakingTerm(params as any, term)).toBe(term);
});
});
Loading

0 comments on commit 72e22ff

Please sign in to comment.