From 4fc7d611f9cef0b855bcf56b12e1d6e3b8608eed Mon Sep 17 00:00:00 2001 From: Leonard Paturel Date: Fri, 7 Jun 2024 14:23:40 +0100 Subject: [PATCH] fix (nearly) all broken tests --- tests-integration/account.test.ts | 137 +++++++++++++++++++----------- 1 file changed, 86 insertions(+), 51 deletions(-) diff --git a/tests-integration/account.test.ts b/tests-integration/account.test.ts index 8bde5aa..58af4d2 100644 --- a/tests-integration/account.test.ts +++ b/tests-integration/account.test.ts @@ -1,7 +1,8 @@ import { expect } from "chai"; -import { num } from "starknet"; +import { Account, RPC, num } from "starknet"; import { GIFT_MAX_FEE, + buildCallDataClaim, calculateClaimAddress, claimInternal, defaultDepositTestSetup, @@ -56,69 +57,103 @@ describe("Gifting", function () { }); } - // it(`Test only protocol can call claim contract`, async function () { - // const { factory } = await setupGiftProtocol(); - // const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); - // const receiver = randomReceiver(); + it(`Test only protocol can call claim contract`, async function () { + const { factory } = await setupGiftProtocol(); + const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); - // await expectRevertWithErrorMessage("gift-acc/only-protocol", () => - // claimInternal(factory, tokenContract, claimClassHash, claimPrivateKey, receiver), - // ); - // }); + const claimAddress = calculateClaimAddress(claim); + + const claimAccount = new Account( + manager, + num.toHex(claimAddress), + claimPrivateKey, + undefined, + RPC.ETransactionVersion.V2, + ); + const claimContract = await manager.loadContract(claimAddress); + claimContract.connect(claimAccount); + await expectRevertWithErrorMessage("gift-acc/only-protocol", () => claimContract.__validate__([])); + }); - // it(`Test claim contract cant call another contract`, async function () { - // const { factory } = await setupGiftProtocol(); + // 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 claimFakeFactory = { ...claim, factory: "0x123" }; - // await expectRevertWithErrorMessage("gift-acc/invalid-call-to", () => - // claimInternal(claimFakeFactory, claimPrivateKey, receiver), + // 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(`Test claim contract can only call 'claim_internal'`, async function () { - // const { factory } = await setupGiftProtocol(); - // const { tokenContract, claimAddress, claimClassHash, claimPrivateKey } = await defaultDepositTestSetup(factory); - // const receiver = randomReceiver(); + it(`Test claim contract can only call 'claim_internal'`, async function () { + const { factory } = await setupGiftProtocol(); + const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); + const receiver = randomReceiver(); - // const claimAccount = new Account(manager, num.toHex(claimAddress), claimPrivateKey, undefined); + const claimAddress = calculateClaimAddress(claim); - // let claim = buildClaim( - // factory, - // claimAccountClassHash, - // GIFT_AMOUNT, - // GIFT_MAX_FEE, - // tokenContract, - // claimSigner.publicKey, - // ); + const claimAccount = new Account( + manager, + num.toHex(claimAddress), + claimPrivateKey, + undefined, + RPC.ETransactionVersion.V2, + ); - // factory.connect(claimAccount); - // await expectRevertWithErrorMessage("gift-acc/invalid-call-selector", () => - // factory.get_dust(claim, receiver, { maxFee: 400000000000000n }), - // ); - // }); + factory.connect(claimAccount); + await expectRevertWithErrorMessage("gift-acc/invalid-call-selector", () => + factory.get_dust(claim, receiver, { maxFee: 400000000000000n }), + ); + }); - // it(`Test claim contract cant preform a multicall`, async function () { - // const { factory } = await setupGiftProtocol(); - // const { tokenContract, claimSigner } = await defaultDepositTestSetup(factory); - // const receiver = randomReceiver(); + it(`Test claim contract cant preform a multicall`, async function () { + const { factory } = await setupGiftProtocol(); + const { claim, claimPrivateKey } = await defaultDepositTestSetup(factory); + const receiver = randomReceiver(); - // await expectRevertWithErrorMessage("gift-acc/invalid-call-len", () => - // claimAccount.execute([ - // { - // contractAddress: factory.address, - // calldata: [claim, receiver], - // entrypoint: "claim_internal", - // }, - // { - // contractAddress: factory.address, - // calldata: [claim, receiver], - // entrypoint: "claim_internal", - // }, - // ]), - // ); - // }); + const claimAddress = calculateClaimAddress(claim); + + const claimAccount = new Account( + manager, + num.toHex(claimAddress), + claimPrivateKey, + undefined, + RPC.ETransactionVersion.V2, + ); + + await expectRevertWithErrorMessage("gift-acc/invalid-call-len", () => + claimAccount.execute([ + { + contractAddress: factory.address, + calldata: [buildCallDataClaim(claim), receiver], + entrypoint: "claim_internal", + }, + { + contractAddress: factory.address, + calldata: [buildCallDataClaim(claim), receiver], + entrypoint: "claim_internal", + }, + ]), + ); + }); it(`Test cannot call 'claim_internal' twice`, async function () { const { factory } = await setupGiftProtocol();