Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard-Pat committed Jun 5, 2024
1 parent e04942d commit d313bd9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 181 deletions.
39 changes: 21 additions & 18 deletions tests-integration/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { num } from "starknet";
import { deployer, expectRevertWithErrorMessage, manager } from "../lib";
import { GIFT_AMOUNT, GIFT_MAX_FEE, GIFT_RECEIVER, setupClaim } from "./setupClaim";
import { GIFT_AMOUNT, GIFT_MAX_FEE, setupClaim } from "./setupClaim";

describe("Gifting", function () {
let claimAccountClassHash: string;
Expand All @@ -12,10 +12,19 @@ describe("Gifting", function () {

for (const useTxV3 of [false, true]) {
it(`Testing simple claim flow using txV3: ${useTxV3}`, async function () {
const { factory, claimAccount, claim, tokenContract } = await setupClaim(useTxV3);
// Deploy factory
const { factory, claimAccount, claim, tokenContract, receiver } = await setupClaim(useTxV3);
await factory.claim_internal(claim, receiver);

// Final check
const finalBalance = await tokenContract.balance_of(claimAccount.address);
expect(finalBalance < GIFT_MAX_FEE).to.be.true;
await tokenContract.balance_of(receiver).should.eventually.equal(GIFT_AMOUNT);
});

it(`Test max fee too high`, async function () {
const { factory, claimAccount, claim, receiver } = await setupClaim(useTxV3);
if (useTxV3) {
const estimate = await factory.estimateFee.claim_internal(claim, GIFT_RECEIVER);
const estimate = await factory.estimateFee.claim_internal(claim, receiver);
const newResourceBounds = {
...estimate.resourceBounds,
l2_gas: {
Expand All @@ -29,7 +38,7 @@ describe("Gifting", function () {
[
{
contractAddress: factory.address,
calldata: [claim, GIFT_RECEIVER],
calldata: [claim, receiver],
entrypoint: "claim_internal",
},
],
Expand All @@ -39,20 +48,14 @@ describe("Gifting", function () {
);
} else {
await expectRevertWithErrorMessage("gift-acc/insufficient-v1-fee", () =>
factory.claim_internal(claim, GIFT_RECEIVER, { maxFee: GIFT_MAX_FEE + 1n }),
factory.claim_internal(claim, receiver, { maxFee: GIFT_MAX_FEE + 1n }),
);
}
await factory.claim_internal(claim, GIFT_RECEIVER);

// Final check
const finalBalance = await tokenContract.balance_of(claimAccount.address);
expect(finalBalance < GIFT_MAX_FEE).to.be.true;
await tokenContract.balance_of(GIFT_RECEIVER).should.eventually.equal(GIFT_AMOUNT);
});
}

it(`Test basic validation asserts`, async function () {
const { factory, claimAccount, claim } = await setupClaim();
const { factory, claimAccount, claim, receiver } = await setupClaim();

const claimContract = await manager.loadContract(num.toHex(claimAccount.address));

Expand All @@ -67,7 +70,7 @@ describe("Gifting", function () {
});
fakeFactory.connect(claimAccount);
await expectRevertWithErrorMessage("gift-acc/invalid-call-to", () =>
fakeFactory.claim_internal(claim, GIFT_RECEIVER, { maxFee: 400000000000000n }),
fakeFactory.claim_internal(claim, receiver, { maxFee: 400000000000000n }),
);

// wrong selector
Expand All @@ -79,25 +82,25 @@ describe("Gifting", function () {
claimAccount.execute([
{
contractAddress: factory.address,
calldata: [claim, GIFT_RECEIVER],
calldata: [claim, receiver],
entrypoint: "claim_internal",
},
{
contractAddress: factory.address,
calldata: [claim, GIFT_RECEIVER],
calldata: [claim, receiver],
entrypoint: "claim_internal",
},
]),
);

// double claim
await factory.claim_internal(claim, GIFT_RECEIVER);
await factory.claim_internal(claim, receiver);
await expectRevertWithErrorMessage("gift-acc/invalid-claim-nonce", () =>
claimAccount.execute(
[
{
contractAddress: factory.address,
calldata: [claim, GIFT_RECEIVER],
calldata: [claim, receiver],
entrypoint: "claim_internal",
},
],
Expand Down
58 changes: 14 additions & 44 deletions tests-integration/claim_external.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
import { uint256 } from "starknet";
import { LegacyStarknetKeyPair, deployer, getClaimExternalData, manager } from "../lib";
import { deployer, getClaimExternalData, manager } from "../lib";
import { GIFT_SIGNER, setupClaim } from "./setupClaim";

describe("claim_external", function () {
const useTxV3 = true;
it(`Testing claim_external flow using txV3: ${useTxV3}`, async function () {
await manager.restartDevnetAndClearClassCache();
// Deploy factory
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
const signer = new LegacyStarknetKeyPair();
const claimPubkey = signer.publicKey;
const amount = 1000000000000000n;
const maxFee = 50000000000000n;
const receiver = "0x42";

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(useTxV3);
tokenContract.connect(deployer);
factory.connect(deployer);
await tokenContract.approve(factory.address, amount + maxFee);
await factory.deposit(amount, maxFee, tokenContract.address, claimPubkey);

const claimAddress = await factory.get_claim_address(
deployer.address,
amount,
maxFee,
tokenContract.address,
claimPubkey,
);
before(async () => {
await manager.declareLocalContract("ClaimAccount");
});

const claim = {
factory: factory.address,
class_hash: claimAccountClassHash,
sender: deployer.address,
amount: uint256.bnToUint256(amount),
max_fee: maxFee,
token: tokenContract.address,
claim_pubkey: claimPubkey,
};
for (const useTxV3 of [false, true]) {
it(`Testing claim_external flow using txV3: ${useTxV3}`, async function () {
const { factory, claimAccount, claim, receiver } = await setupClaim(useTxV3);

const claimExternalData = await getClaimExternalData({ receiver });
const signature = await signer.signMessage(claimExternalData, claimAddress);
const claimExternalData = await getClaimExternalData({ receiver });
const signature = await GIFT_SIGNER.signMessage(claimExternalData, claimAccount.address);

await factory.claim_external(claim, receiver, signature);
});
factory.connect(deployer);
await factory.claim_external(claim, receiver, signature);
});
}
});
140 changes: 27 additions & 113 deletions tests-integration/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,26 @@
import { expect } from "chai";
import { Account, RPC, num, uint256 } from "starknet";
import { LegacyStarknetKeyPair, deployer, expectRevertWithErrorMessage, genericAccount, manager } from "../lib";
import { GIFT_AMOUNT, GIFT_MAX_FEE, setupClaim } from "./setupClaim";

describe("Factory", function () {
let claimAccountClassHash: string;
before(async () => {
claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
});

for (const useTxV3 of [false, true]) {
it(`get_dust: ${useTxV3}`, async function () {
await manager.restartDevnetAndClearClassCache();
// Deploy factory
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
const signer = new LegacyStarknetKeyPair();
const claimPubkey = signer.publicKey;
const amount = 1000000000000000n;
const maxFee = 50000000000000n;
const receiver = "0x42";
const { factory, tokenContract, claimAccount, claim, receiver } = await setupClaim(useTxV3);

const receiverDust = "0x43";

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(useTxV3);
tokenContract.connect(deployer);
factory.connect(deployer);
await tokenContract.approve(factory.address, amount + maxFee);
await factory.deposit(amount, maxFee, tokenContract.address, claimPubkey);

// Ensure there is a contract for the claim
const claimAddress = await factory.get_claim_address(
deployer.address,
amount,
maxFee,
tokenContract.address,
claimPubkey,
);

const claim = {
factory: factory.address,
class_hash: claimAccountClassHash,
sender: deployer.address,
amount: uint256.bnToUint256(amount),
max_fee: maxFee,
token: tokenContract.address,
claim_pubkey: claimPubkey,
};

// Check balance of the claim contract is correct
await tokenContract.balance_of(claimAddress).should.eventually.equal(amount + maxFee);
// Check balance receiver address == 0
await tokenContract.balance_of(receiver).should.eventually.equal(0n);

const claimContract = await manager.loadContract(num.toHex(claimAddress));
const txVersion = useTxV3 ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2;
const claimAccount = new Account(manager, claimContract.address, signer, undefined, txVersion);
factory.connect(claimAccount);
await factory.claim_internal(claim, receiver);

// Final check
const dustBalance = await tokenContract.balance_of(claimAddress);
expect(dustBalance < maxFee).to.be.true;
await tokenContract.balance_of(receiver).should.eventually.equal(amount);
const dustBalance = await tokenContract.balance_of(claimAccount.address);
expect(dustBalance < GIFT_MAX_FEE).to.be.true;
await tokenContract.balance_of(receiver).should.eventually.equal(GIFT_AMOUNT);

// Test dust
await tokenContract.balance_of(receiverDust).should.eventually.equal(0n);
Expand All @@ -72,103 +33,56 @@ describe("Factory", function () {
}

it(`Test Cancel Claim`, async function () {
await manager.restartDevnetAndClearClassCache();
// Deploy factory
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
const signer = new LegacyStarknetKeyPair();
const claimPubkey = signer.publicKey;
const amount = 1000000000000000n;
const maxFee = 50000000000000n;
const receiver = "0x42";

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(false);
tokenContract.connect(deployer);
factory.connect(deployer);
await tokenContract.approve(factory.address, amount + maxFee);
await factory.deposit(amount, maxFee, tokenContract.address, claimPubkey);

// Ensure there is a contract for the claim
const claimAddress = await factory.get_claim_address(
deployer.address,
amount,
maxFee,
tokenContract.address,
claimPubkey,
);

const claim = {
factory: factory.address,
class_hash: claimAccountClassHash,
sender: deployer.address,
amount: uint256.bnToUint256(amount),
max_fee: maxFee,
token: tokenContract.address,
claim_pubkey: claimPubkey,
};

// Check balance of the claim contract is correct
await tokenContract.balance_of(claimAddress).should.eventually.equal(amount + maxFee);
// Check balance receiver address == 0
await tokenContract.balance_of(receiver).should.eventually.equal(0n);

const claimContract = await manager.loadContract(num.toHex(claimAddress));
const claimAccount = new Account(manager, claimContract.address, signer, undefined, RPC.ETransactionVersion.V2);
const { factory, tokenContract, claimAccount, claim, receiver } = await setupClaim();

const balanceSenderBefore = await tokenContract.balance_of(deployer.address);
const { transaction_hash } = await factory.cancel(claim);
const txFee = BigInt((await manager.getTransactionReceipt(transaction_hash)).actual_fee.amount);
// Check balance of the sender is correct
await tokenContract
.balance_of(deployer.address)
.should.eventually.equal(balanceSenderBefore + amount + maxFee - txFee);
.should.eventually.equal(balanceSenderBefore + GIFT_AMOUNT + GIFT_MAX_FEE - txFee);
// Check balance claim address address == 0
await tokenContract.balance_of(claimAddress).should.eventually.equal(0n);
await tokenContract.balance_of(claimAccount.address).should.eventually.equal(0n);

factory.connect(claimAccount);
await expectRevertWithErrorMessage("gift-acc/gift-canceled", () => factory.claim_internal(claim, receiver));
});

it(`Test pausable`, async function () {
await manager.restartDevnetAndClearClassCache();
// Deploy factory
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
const signer = new LegacyStarknetKeyPair();
const claimPubkey = signer.publicKey;
const amount = 1000000000000000n;
const maxFee = 50000000000000n;
const receiver = "0x42";
const GIFT_AMOUNT = 1000000000000000n;
const GIFT_MAX_FEE = 50000000000000n;
const receiver = "0x45";

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(false);
tokenContract.connect(deployer);
factory.connect(deployer);
await tokenContract.approve(factory.address, amount + maxFee);
await tokenContract.approve(factory.address, GIFT_AMOUNT + GIFT_MAX_FEE);

factory.connect(genericAccount);
await expectRevertWithErrorMessage("Caller is not the owner", () => factory.pause());
factory.connect(deployer);
await factory.pause();
await expectRevertWithErrorMessage("Pausable: paused", () =>
factory.deposit(amount, maxFee, tokenContract.address, claimPubkey),
factory.deposit(GIFT_AMOUNT, GIFT_MAX_FEE, tokenContract.address, claimPubkey),
);

await factory.unpause();
await factory.deposit(amount, maxFee, tokenContract.address, claimPubkey);
await factory.deposit(GIFT_AMOUNT, GIFT_MAX_FEE, tokenContract.address, claimPubkey);

// Ensure there is a contract for the claim
const claimAddress = await factory.get_claim_address(
deployer.address,
amount,
maxFee,
GIFT_AMOUNT,
GIFT_MAX_FEE,
tokenContract.address,
claimPubkey,
);
Expand All @@ -177,8 +91,8 @@ describe("Factory", function () {
factory: factory.address,
class_hash: claimAccountClassHash,
sender: deployer.address,
amount: uint256.bnToUint256(amount),
max_fee: maxFee,
GIFT_AMOUNT: uint256.bnToUint256(GIFT_AMOUNT),
max_fee: GIFT_MAX_FEE,
token: tokenContract.address,
claim_pubkey: claimPubkey,
};
Expand All @@ -187,7 +101,7 @@ describe("Factory", function () {
const claimAccount = new Account(manager, claimContract.address, signer, undefined, RPC.ETransactionVersion.V2);

// Check balance of the claim contract is correct
await tokenContract.balance_of(claimAddress).should.eventually.equal(amount + maxFee);
await tokenContract.balance_of(claimAddress).should.eventually.equal(GIFT_AMOUNT + GIFT_MAX_FEE);
// Check balance receiver address == 0
await tokenContract.balance_of(receiver).should.eventually.equal(0n);

Expand All @@ -196,7 +110,7 @@ describe("Factory", function () {

// Final check
const dustBalance = await tokenContract.balance_of(claimAddress);
expect(dustBalance < maxFee).to.be.true;
await tokenContract.balance_of(receiver).should.eventually.equal(amount);
expect(dustBalance < GIFT_MAX_FEE).to.be.true;
await tokenContract.balance_of(receiver).should.eventually.equal(GIFT_AMOUNT);
});
});
Loading

0 comments on commit d313bd9

Please sign in to comment.