From ab8c3e425f55d1bb80ca74b7eb56ec4d0f22d3ce Mon Sep 17 00:00:00 2001 From: Thong Dang Date: Fri, 3 Jan 2025 09:48:56 +0700 Subject: [PATCH] chore: refactor createSignature function --- test/LemonadeStakePaymentV1.ts | 24 +++++------------------- test/helper.ts | 28 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/test/LemonadeStakePaymentV1.ts b/test/LemonadeStakePaymentV1.ts index e4941cb..b45aaf9 100644 --- a/test/LemonadeStakePaymentV1.ts +++ b/test/LemonadeStakePaymentV1.ts @@ -6,7 +6,7 @@ import { ethers, upgrades } from 'hardhat'; import { toId } from "./utils"; -import { deployAccessRegistry, deployConfigRegistry, getBalances, mintERC20 } from "./helper"; +import { createSignature, deployAccessRegistry, deployConfigRegistry, getBalances, mintERC20 } from "./helper"; const deployStake = async (signer: SignerWithAddress) => { const { accessRegistry } = await deployAccessRegistry(signer); @@ -22,20 +22,6 @@ const deployStake = async (signer: SignerWithAddress) => { return { configRegistry, stakePayment }; } -const createSignature = (signer: SignerWithAddress, type: string, paymentIds: string[]) => { - const data = [toId(type), ...paymentIds.map(toId)]; - - let encoded = "0x"; - - for (let i = 0; i < data.length; i++) { - encoded = ethers.solidityPacked(["bytes", "bytes32"], [encoded, data[i]]); - } - - return signer.signMessage( - ethers.getBytes(encoded) - ); -} - const salt = toId("SALT"); const register = async (ppm: bigint) => { @@ -152,7 +138,7 @@ async function testWith(currencyResolver: () => Promise) { const expectedRefund = BigInt(amount * ppm / 1000000); //-- generate refund signature - const signature = await createSignature(signer, "STAKE_REFUND", [paymentId]); + const signature = await createSignature(signer, ["STAKE_REFUND", paymentId]); const { balanceBefore, balanceAfter, fee } = await getBalances( signer2.address, @@ -180,7 +166,7 @@ async function testWith(currencyResolver: () => Promise) { const { paymentId } = await stake(vault, configRegistry, stakePayment, "3", currency); //-- generate refund signature - const signature = await createSignature(signer, "STAKE_REFUND", [paymentId]); + const signature = await createSignature(signer, ["STAKE_REFUND", paymentId]); await stakePayment.connect(signer2).refund(paymentId, signature); await assert.rejects(stakePayment.connect(signer2).refund(paymentId, signature)); @@ -197,7 +183,7 @@ async function testWith(currencyResolver: () => Promise) { const expectedSlashAmount = BigInt(stake1.amount + stake2.amount); //-- generate slash signature - const signature = await createSignature(signer, "STAKE_SLASH", [stake1.paymentId, stake2.paymentId]); + const signature = await createSignature(signer, ["STAKE_SLASH", stake1.paymentId, stake2.paymentId]); //-- signer 2 is expecting the slash amount const { balanceBefore, balanceAfter } = await getBalances( @@ -228,7 +214,7 @@ async function testWith(currencyResolver: () => Promise) { const { paymentId } = await stake(vault, configRegistry, stakePayment, "5", currency); - const signature = await createSignature(signer, "STAKE_SLASH", [paymentId]); + const signature = await createSignature(signer, ["STAKE_SLASH", paymentId]); await stakePayment.connect(signer).slash( vault, diff --git a/test/helper.ts b/test/helper.ts index d9f59b0..3910c76 100644 --- a/test/helper.ts +++ b/test/helper.ts @@ -30,20 +30,6 @@ export async function deployConfigRegistry(signer: SignerWithAddress, ...args: u return { configRegistry }; } -export async function createSignature(signer: SignerWithAddress, type: string, paymentIds: string[]) { - const data = [toId(type), ...paymentIds.map(toId)]; - - let encoded = "0x"; - - for (let i = 0; i < data.length; i++) { - encoded = ethers.solidityPacked(["bytes", "bytes32"], [encoded, data[i]]); - } - - return signer.signMessage( - ethers.getBytes(encoded) - ); -} - export async function getBalances(wallet: string, currency: string, op: () => Promise) { const isNative = currency === ethers.ZeroAddress; @@ -59,3 +45,17 @@ export async function getBalances(wallet: string, currency: string, op: () => Pr return { balanceBefore, balanceAfter, fee: isNative ? receipt.gasPrice * receipt.gasUsed : 0n }; } + +export function createSignature(signer: SignerWithAddress, args: string[]) { + const data = args.map(toId); + + let encoded = "0x"; + + for (let i = 0; i < data.length; i++) { + encoded = ethers.solidityPacked(["bytes", "bytes32"], [encoded, data[i]]); + } + + return signer.signMessage( + ethers.getBytes(encoded) + ); +}