Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard-Pat committed Jun 7, 2024
1 parent 8c228d1 commit 30b1781
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 65 deletions.
11 changes: 6 additions & 5 deletions lib/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

Expand Down Expand Up @@ -84,7 +85,7 @@ export async function claimInternal(
): Promise<TransactionReceipt> {
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(
[
Expand Down
66 changes: 45 additions & 21 deletions lib/deposit.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -38,35 +48,49 @@ 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();

const claim: Claim = {
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;
Expand Down
63 changes: 31 additions & 32 deletions tests-integration/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
calculateClaimAddress,
claimInternal,
defaultDepositTestSetup,
deployer,
expectRevertWithErrorMessage,
manager,
randomReceiver,
Expand All @@ -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 () {
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions tests-integration/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand All @@ -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

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

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

0 comments on commit 30b1781

Please sign in to comment.