Skip to content

Commit

Permalink
split up setup and added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard-Pat committed Jun 6, 2024
1 parent 126d51e commit 633aa2c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
25 changes: 15 additions & 10 deletions tests-integration/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { expect } from "chai";
import { num } from "starknet";
import { deployer, expectRevertWithErrorMessage, manager } from "../lib";
import { GIFT_AMOUNT, GIFT_MAX_FEE, setupClaim } from "./setupClaim";
import { GIFT_AMOUNT, GIFT_MAX_FEE, setupClaim, setupGiftProtocol } from "./setupClaim";

describe("Gifting", function () {
let claimAccountClassHash: string;
before(async () => {
// declare claim account
claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
});

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

// Final check
Expand All @@ -22,7 +21,12 @@ describe("Gifting", function () {
});

it(`Test max fee too high`, async function () {
const { factory, claimAccount, claim, receiver } = await setupClaim(useTxV3);
const { factory, claimAccountClassHash } = await setupGiftProtocol();
const { claimAccount, claim, tokenContract, receiver } = await setupClaim(
factory,
claimAccountClassHash,
useTxV3,
);
if (useTxV3) {
const estimate = await factory.estimateFee.claim_internal(claim, receiver);
const newResourceBounds = {
Expand Down Expand Up @@ -55,7 +59,8 @@ describe("Gifting", function () {
}

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

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

Expand Down
5 changes: 3 additions & 2 deletions tests-integration/claim_external.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deployer, getClaimExternalData, manager } from "../lib";
import { setupClaim } from "./setupClaim";
import { setupClaim, setupGiftProtocol } from "./setupClaim";

describe("claim_external", function () {
before(async () => {
Expand All @@ -8,7 +8,8 @@ describe("claim_external", function () {

for (const useTxV3 of [false, true]) {
it(`Testing claim_external flow using txV3: ${useTxV3}`, async function () {
const { factory, claimAccount, claim, receiver, giftSigner } = await setupClaim(useTxV3);
const { factory, claimAccountClassHash } = await setupGiftProtocol();
const { claimAccount, claim, receiver, giftSigner } = await setupClaim(factory, claimAccountClassHash, useTxV3);

const claimExternalData = await getClaimExternalData({ receiver });
const signature = await giftSigner.signMessage(claimExternalData, claimAccount.address);
Expand Down
21 changes: 12 additions & 9 deletions tests-integration/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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";
import { GIFT_AMOUNT, GIFT_MAX_FEE, setupClaim, setupGiftProtocol } from "./setupClaim";

describe("Factory", function () {
let claimAccountClassHash: string;
Expand All @@ -11,8 +11,13 @@ describe("Factory", function () {

for (const useTxV3 of [false, true]) {
it(`get_dust: ${useTxV3}`, async function () {
const { factory, tokenContract, claimAccount, claim, receiver } = await setupClaim(useTxV3);
const receiverDust = `0x${Math.floor(Math.random() * 10)}`;
const { factory, claimAccountClassHash } = await setupGiftProtocol();
const { claimAccount, claim, tokenContract, receiver } = await setupClaim(
factory,
claimAccountClassHash,
useTxV3,
);
const receiverDust = `0x2${Math.floor(Math.random() * 1000)}`;

await factory.claim_internal(claim, receiver);

Expand All @@ -32,7 +37,8 @@ describe("Factory", function () {
}

it(`Test Cancel Claim`, async function () {
const { factory, tokenContract, claimAccount, claim, receiver } = await setupClaim();
const { factory, claimAccountClassHash } = await setupGiftProtocol();
const { claimAccount, claim, tokenContract, receiver } = await setupClaim(factory, claimAccountClassHash);

const balanceSenderBefore = await tokenContract.balance_of(deployer.address);
factory.connect(deployer);
Expand All @@ -51,15 +57,12 @@ describe("Factory", function () {

it(`Test pausable`, async function () {
// Deploy factory
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
const { factory, claimAccountClassHash } = await setupGiftProtocol();
const signer = new LegacyStarknetKeyPair();
const claimPubkey = signer.publicKey;
const GIFT_AMOUNT = 1000000000000000n;
const GIFT_MAX_FEE = 50000000000000n;
const receiver = "0x45";
const receiver = `0x5${Math.floor(Math.random() * 1000)}`;

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(false);
Expand Down
31 changes: 22 additions & 9 deletions tests-integration/setupClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,31 @@ interface Claim extends AccountConstructorArguments {
class_hash: string;
}

const cache: Record<string, Contract> = {};

export async function setupGiftProtocol(): Promise<{
factory: Contract;
claimAccountClassHash: string;
}> {
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const cachedFactory = cache["GiftFactory"];
if (cachedFactory) {
return { factory: cachedFactory, claimAccountClassHash };
}
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});
cache["GiftFactory"] = factory;
return { factory, claimAccountClassHash };
}

export async function setupClaim(
factory: Contract,
claimAccountClassHash: string,
useTxV3 = false,
useRandom = true,
): Promise<{
factory: Contract;
claimAccount: Account;
claim: Claim;
tokenContract: Contract;
Expand All @@ -34,13 +54,6 @@ export async function setupClaim(
const claimPubKey = giftSigner.publicKey;
const receiver = useRandom ? `0x${encode.buf2hex(ec.starkCurve.utils.randomPrivateKey())}` : "0x42";

// claim account class hash is read from cache
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
});

// Make a gift
const tokenContract = await manager.tokens.feeTokenContract(useTxV3);
tokenContract.connect(deployer);
Expand Down Expand Up @@ -87,5 +100,5 @@ export async function setupClaim(
const txVersion = useTxV3 ? RPC.ETransactionVersion.V3 : RPC.ETransactionVersion.V2;
const claimAccount = new Account(manager, num.toHex(claimAddress), giftSigner, undefined, txVersion);
factory.connect(claimAccount);
return { factory, claimAccount, claim, tokenContract, receiver, giftSigner };
return { claimAccount, claim, tokenContract, receiver, giftSigner };
}

0 comments on commit 633aa2c

Please sign in to comment.