Skip to content

Commit

Permalink
chore: refactor createSignature function
Browse files Browse the repository at this point in the history
  • Loading branch information
thongxuan committed Jan 3, 2025
1 parent 5103534 commit ab8c3e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
24 changes: 5 additions & 19 deletions test/LemonadeStakePaymentV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -152,7 +138,7 @@ async function testWith(currencyResolver: () => Promise<string>) {
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,
Expand Down Expand Up @@ -180,7 +166,7 @@ async function testWith(currencyResolver: () => Promise<string>) {
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));
Expand All @@ -197,7 +183,7 @@ async function testWith(currencyResolver: () => Promise<string>) {
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(
Expand Down Expand Up @@ -228,7 +214,7 @@ async function testWith(currencyResolver: () => Promise<string>) {

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,
Expand Down
28 changes: 14 additions & 14 deletions test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionReceipt>) {
const isNative = currency === ethers.ZeroAddress;

Expand All @@ -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)
);
}

0 comments on commit ab8c3e4

Please sign in to comment.