From 30b1781bda4cf2a8bac7651cfe19bad8f8c4864c Mon Sep 17 00:00:00 2001 From: Leonard Paturel Date: Fri, 7 Jun 2024 16:46:21 +0100 Subject: [PATCH] fixed tests --- lib/claim.ts | 11 +++--- lib/deposit.ts | 66 +++++++++++++++++++++---------- tests-integration/account.test.ts | 63 +++++++++++++++-------------- tests-integration/factory.test.ts | 15 +++---- 4 files changed, 90 insertions(+), 65 deletions(-) diff --git a/lib/claim.ts b/lib/claim.ts index 7ffd236..625adc1 100644 --- a/lib/claim.ts +++ b/lib/claim.ts @@ -38,9 +38,10 @@ export async function getClaimExternalData(claimExternal: ClaimExternal) { export interface AccountConstructorArguments { sender: string; - amount: bigint; - max_fee: bigint; - token: string; + gift_token: string; + gift_amount: bigint; + fee_token: string; + fee_amount: bigint; claim_pubkey: bigint; } @@ -52,7 +53,7 @@ export interface Claim extends AccountConstructorArguments { export function buildCallDataClaim(claim: Claim) { return { ...claim, - amount: uint256.bnToUint256(claim.amount), + gift_amount: uint256.bnToUint256(claim.gift_amount), }; } @@ -84,7 +85,7 @@ export async function claimInternal( ): Promise { const claimAddress = calculateClaimAddress(claim); - const txVersion = useTxv3(claim.token) ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2; + const txVersion = useTxv3(claim.fee_token) ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2; const claimAccount = new Account(manager, num.toHex(claimAddress), claimSignerPrivateKey, undefined, txVersion); return (await claimAccount.execute( [ diff --git a/lib/deposit.ts b/lib/deposit.ts index af8d2e5..c2b66f9 100644 --- a/lib/deposit.ts +++ b/lib/deposit.ts @@ -1,30 +1,40 @@ -import { CallData, Contract, ec, encode, hash, uint256 } from "starknet"; +import { Account, CallData, Contract, ec, encode, hash, uint256 } from "starknet"; import { AccountConstructorArguments, Claim, LegacyStarknetKeyPair, deployer, manager } from "./"; export const GIFT_AMOUNT = 1000000000000000n; export const GIFT_MAX_FEE = 50000000000000n; export async function deposit( - factory: Contract, - tokenContract: Contract, + sender: Account, + giftAmount: bigint, + feeAmount: bigint, + factoryAddress: string, + feeTokenAddress: string, + giftTokenAddress: string, claimSignerPubKey: bigint, - sender = deployer, - amount = GIFT_AMOUNT, - feeAmount = GIFT_MAX_FEE, ) { - // Make a gift - tokenContract.connect(sender); - factory.connect(sender); - await sender.execute([ - tokenContract.populateTransaction.approve(factory.address, amount + feeAmount), - factory.populateTransaction.deposit(amount, feeAmount, tokenContract.address, claimSignerPubKey), - ]); + const factory = await manager.loadContract(factoryAddress); + const feeToken = await manager.loadContract(feeTokenAddress); + const giftToken = await manager.loadContract(giftTokenAddress); + if (feeTokenAddress === giftTokenAddress) { + await sender.execute([ + feeToken.populateTransaction.approve(factory.address, giftAmount + feeAmount), + factory.populateTransaction.deposit(giftTokenAddress, giftAmount, feeTokenAddress, feeAmount, claimSignerPubKey), + ]); + } else { + await sender.execute([ + feeToken.populateTransaction.approve(factory.address, feeAmount), + giftToken.populateTransaction.approve(factory.address, giftAmount), + factory.populateTransaction.deposit(giftTokenAddress, giftAmount, feeTokenAddress, feeAmount, claimSignerPubKey), + ]); + } } export async function defaultDepositTestSetup( factory: Contract, useTxV3 = false, giftPrivateKey?: string, + giftTokenAddress?: string, giftAmount = GIFT_AMOUNT, giftMaxFee = GIFT_MAX_FEE, ): Promise<{ @@ -38,7 +48,15 @@ export async function defaultDepositTestSetup( giftPrivateKey || `0x${encode.buf2hex(ec.starkCurve.utils.randomPrivateKey())}`, ); const claimPubKey = claimSigner.publicKey; - await deposit(factory, tokenContract, claimPubKey); + await deposit( + deployer, + giftAmount, + giftMaxFee, + factory.address, + tokenContract.address, + giftTokenAddress || tokenContract.address, + claimPubKey, + ); const claimClassHash = await factory.get_latest_claim_class_hash(); @@ -46,27 +64,33 @@ export async function defaultDepositTestSetup( factory: factory.address, class_hash: claimClassHash, sender: deployer.address, - amount: giftAmount, - max_fee: giftMaxFee, - token: tokenContract.address, + gift_token: giftTokenAddress || tokenContract.address, + gift_amount: giftAmount, + fee_token: tokenContract.address, + fee_amount: giftMaxFee, claim_pubkey: claimPubKey, }; + return { claim, claimPrivateKey: claimSigner.privateKey }; } export function calculateClaimAddress(claim: Claim): string { const constructorArgs: AccountConstructorArguments = { sender: claim.sender, - amount: claim.amount, - max_fee: claim.max_fee, - token: claim.token, + gift_token: claim.gift_token, + gift_amount: claim.gift_amount, + fee_token: claim.fee_token, + fee_amount: claim.fee_amount, claim_pubkey: claim.claim_pubkey, }; const claimAddress = hash.calculateContractAddressFromHash( 0, claim.class_hash, - CallData.compile({ ...constructorArgs, amount: uint256.bnToUint256(claim.amount) }), + CallData.compile({ + ...constructorArgs, + gift_amount: uint256.bnToUint256(claim.gift_amount), + }), claim.factory, ); return claimAddress; diff --git a/tests-integration/account.test.ts b/tests-integration/account.test.ts index 12dab9c..a98c1c3 100644 --- a/tests-integration/account.test.ts +++ b/tests-integration/account.test.ts @@ -6,6 +6,7 @@ import { calculateClaimAddress, claimInternal, defaultDepositTestSetup, + deployer, expectRevertWithErrorMessage, manager, randomReceiver, @@ -18,14 +19,14 @@ describe("Gifting", function () { const { factory } = await setupGiftProtocol(); const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory, useTxV3); const receiver = randomReceiver(); - await claimInternal(claim, receiver, claimPrivateKey); - const claimAddress = calculateClaimAddress(claim); - const token = await manager.loadContract(claim.token); + await claimInternal(claim, receiver, claimPrivateKey); + + const token = await manager.loadContract(claim.gift_token); const finalBalance = await token.balance_of(claimAddress); - expect(finalBalance < claim.max_fee).to.be.true; - await token.balance_of(receiver).should.eventually.equal(claim.amount); + expect(finalBalance < claim.fee_amount).to.be.true; + await token.balance_of(receiver).should.eventually.equal(claim.gift_amount); }); it(`Test max fee too high using txV3: ${useTxV3}`, async function () { @@ -74,33 +75,31 @@ describe("Gifting", function () { await expectRevertWithErrorMessage("gift-acc/only-protocol", () => claimContract.__validate__([])); }); - // it.only(`Test claim contract cant call another contract`, async function () { - // const { factory, claimAccountClassHash } = await setupGiftProtocol(); - // const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); - // const receiver = randomReceiver(); - - // const fakeFactory = await manager.deployContract("GiftFactory", { - // unique: true, - // constructorCalldata: [claimAccountClassHash, deployer.address], - // }); - - // const fakeClaim = { ...claim, factory: fakeFactory.address }; - // const claimAddress = calculateClaimAddress(claim); - - // const claimAccount = new Account( - // manager, - // num.toHex(claimAddress), - // claimPrivateKey, - // undefined, - // RPC.ETransactionVersion.V2, - // ); - // fakeFactory.connect(claimAccount); - // await fakeFactory.claim_internal(buildCallDataClaim(fakeClaim), receiver, { maxFee: 400000000000000n }); - - // // await expectRevertWithErrorMessage("gift-acc/invalid-call-to", () => - // // claimInternal(fakeClaim, receiver, claimPrivateKey), - // // ); - // }); + it.only(`Test claim contract cant call another contract`, async function () { + const { factory, claimAccountClassHash } = await setupGiftProtocol(); + const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); + const receiver = randomReceiver(); + + const fakeFactory = await manager.deployContract("GiftFactory", { + unique: true, + constructorCalldata: [claimAccountClassHash, deployer.address], + }); + + const claimAddress = calculateClaimAddress(claim); + + const claimAccount = new Account( + manager, + num.toHex(claimAddress), + claimPrivateKey, + undefined, + RPC.ETransactionVersion.V2, + ); + fakeFactory.connect(claimAccount); + + await expectRevertWithErrorMessage("gift-acc/invalid-call-to", () => + fakeFactory.claim_internal(buildCallDataClaim(claim), receiver, { maxFee: 400000000000000n }), + ); + }); it(`Test claim contract can only call 'claim_internal'`, async function () { const { factory } = await setupGiftProtocol(); diff --git a/tests-integration/factory.test.ts b/tests-integration/factory.test.ts index ba2506a..3a17e8d 100644 --- a/tests-integration/factory.test.ts +++ b/tests-integration/factory.test.ts @@ -23,9 +23,10 @@ describe("Factory", function () { const claimAddress = await factory.get_claim_address( claim.class_hash, deployer.address, - GIFT_AMOUNT, - GIFT_MAX_FEE, - claim.token, + claim.gift_token, + claim.gift_amount, + claim.fee_token, + claim.fee_amount, claim.claim_pubkey, ); @@ -41,7 +42,7 @@ describe("Factory", function () { await claimInternal(claim, receiver, claimPrivateKey); const claimAddress = calculateClaimAddress(claim); - const token = await manager.loadContract(claim.token); + const token = await manager.loadContract(claim.gift_token); // Final check @@ -63,7 +64,7 @@ describe("Factory", function () { const { factory } = await setupGiftProtocol(); const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); const receiver = randomReceiver(); - const token = await manager.loadContract(claim.token); + const token = await manager.loadContract(claim.gift_token); const claimAddress = calculateClaimAddress(claim); const balanceSenderBefore = await token.balance_of(deployer.address); @@ -73,7 +74,7 @@ describe("Factory", function () { // Check balance of the sender is correct await token .balance_of(deployer.address) - .should.eventually.equal(balanceSenderBefore + claim.amount + claim.max_fee - txFee); + .should.eventually.equal(balanceSenderBefore + claim.gift_amount + claim.fee_amount - txFee); // Check balance claim address address == 0 await token.balance_of(claimAddress).should.eventually.equal(0n); @@ -100,7 +101,7 @@ describe("Factory", function () { factory.connect(deployer); await factory.pause(); await expectRevertWithErrorMessage("Pausable: paused", () => - factory.deposit(GIFT_AMOUNT, GIFT_MAX_FEE, tokenContract.address, claimSigner.publicKey), + factory.deposit(tokenContract.address, GIFT_AMOUNT, tokenContract.address, GIFT_MAX_FEE, claimSigner.publicKey), ); await factory.unpause();