Skip to content

Commit

Permalink
Merge pull request #34 from argentlabs/poc-library-calls
Browse files Browse the repository at this point in the history
there are a few comments open but can be addressed later, merging now to move faster
  • Loading branch information
sgc-code authored Jun 25, 2024
2 parents 3282203 + 90996fa commit 2af66b8
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 466 deletions.
32 changes: 16 additions & 16 deletions gas-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ Summary:
│ Transfer ETH (FeeToken: WEI) │ '828.000.000.192' │ 0.0033 │ 828000000000 │ 23 │ 21 │ 2 │ 1 │ 'steps' │ 3 │ 192 │ 'BLOB' │
│ Transfer STRK (FeeToken: WEI) │ '828.000.000.320' │ 0.0033 │ 828000000000 │ 23 │ 21 │ 2 │ 1 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Gifting WEI (FeeToken: WEI) │ '1.548.000.000.288' │ 0.0061 │ 1548000000000 │ 43 │ 37 │ 5 │ 2 │ 'steps' │ 3 │ 288 │ 'BLOB' │
│ Claiming WEI (FeeToken: WEI) │ '1.368.000.000.192' │ 0.005413680000000003835 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ 'BLOB' │
│ Claiming WEI (FeeToken: WEI) │ '1.188.000.000.192' │ 0.004711880000000003330 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ 'BLOB' │
│ Claiming external WEI (FeeToken: WEI) │ '1.512.000.000.256' │ 0.006 │ 1512000000000 │ 42 │ 39 │ 2 │ 2 │ 'steps' │ 4 │ 256 │ 'BLOB' │
│ Gifting WEI (FeeToken: FRI) │ '2.196.000.000.480' │ 0.0087 │ 2196000000000 │ 61 │ 52 │ 7 │ 3 │ 'steps' │ 5 │ 480 │ 'BLOB' │
│ Claiming WEI (FeeToken: FRI) │ '1.368.000.000.320' │ 0 │ 13680000000003835 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Claiming WEI (FeeToken: FRI) │ '1.188.000.000.320' │ 0 │ 11880000000003330 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Claiming external WEI (FeeToken: FRI) │ '1.512.000.000.256' │ 0.006 │ 1512000000000 │ 42 │ 39 │ 2 │ 2 │ 'steps' │ 4 │ 256 │ 'BLOB' │
│ Gifting FRI (FeeToken: WEI) │ '2.196.000.000.480' │ 0.0087 │ 2196000000000 │ 61 │ 52 │ 7 │ 3 │ 'steps' │ 5 │ 480 │ 'BLOB' │
│ Claiming FRI (FeeToken: WEI) │ '1.368.000.000.320' │ 0.005413680000000003835 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Claiming FRI (FeeToken: WEI) │ '1.188.000.000.320' │ 0.004711880000000003330 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Claiming external FRI (FeeToken: WEI) │ '1.512.000.000.320' │ 0.006 │ 1512000000000 │ 42 │ 39 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Gifting FRI (FeeToken: FRI) │ '1.548.000.000.416' │ 0.0061 │ 1548000000000 │ 43 │ 37 │ 5 │ 2 │ 'steps' │ 4 │ 416 │ 'BLOB' │
│ Claiming FRI (FeeToken: FRI) │ '1.368.000.000.192' │ 0 │ 13680000000003835 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ 'BLOB' │
│ Claiming FRI (FeeToken: FRI) │ '1.188.000.000.192' │ 0 │ 11880000000003330 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ 'BLOB' │
│ Claiming external FRI (FeeToken: FRI) │ '1.512.000.000.320' │ 0.006 │ 1512000000000 │ 42 │ 39 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
└───────────────────────────────────────┴─────────────────────┴─────────┴────────────────┴────────────────┴─────────────────┴───────────┴──────────────┴──────────────────────────────┴───────────────┴────────┴─────────┘
Resources:
Expand All @@ -23,16 +23,16 @@ Resources:
├───────────────────────────────────────┼─────────┼───────┼───────┼────────┼──────────┼──────────┼─────────────┼───────┤
│ Transfer ETH (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 25 │ 0 │ 181 │ 8184 │
│ Transfer STRK (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 25 │ 0 │ 181 │ 8184 │
│ Gifting WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 33414506
│ Claiming WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 75 │ 0 │ 47513662
│ Claiming external WEI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 64 │ 4 │ 45115316
│ Gifting WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 63 │ 0 │ 46020478
│ Claiming WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 75 │ 0 │ 50913860
│ Claiming external WEI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 64 │ 4 │ 45115316
│ Gifting FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 63 │ 0 │ 46020477
│ Claiming FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 75 │ 0 │ 47513662
│ Claiming external FRI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 64 │ 4 │ 45115316
│ Gifting FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 33414507
│ Claiming FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 75 │ 0 │ 50913860
│ Claiming external FRI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 64 │ 4 │ 45115316
│ Gifting WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 48 │ 0 │ 33914627
│ Claiming WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 37311715
│ Claiming external WEI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 42715434
│ Gifting WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 46520599
│ Claiming WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 40711913
│ Claiming external WEI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 42715434
│ Gifting FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 46520598
│ Claiming FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 37311715
│ Claiming external FRI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 42715434
│ Gifting FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 48 │ 0 │ 33914628
│ Claiming FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 40711913
│ Claiming external FRI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 42715434
└───────────────────────────────────────┴─────────┴───────┴───────┴────────┴──────────┴──────────┴─────────────┴───────┘
69 changes: 56 additions & 13 deletions lib/claim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { Account, RPC, TransactionReceipt, UniversalDetails, ec, encode, num, shortString, uint256 } from "starknet";
import {
Account,
Call,
CallData,
Calldata,
RPC,
TransactionReceipt,
UniversalDetails,
ec,
encode,
hash,
num,
shortString,
uint256,
} from "starknet";

import {
LegacyStarknetKeyPair,
StarknetSignature,
Expand Down Expand Up @@ -96,46 +111,52 @@ export async function claimExternal(args: {
receiver: string;
claimPrivateKey: string;
dustReceiver?: string;
overrides?: { claimAccountAddress?: string; factoryAddress?: string; account?: Account };
overrides?: { account?: Account };
details?: UniversalDetails;
}): Promise<TransactionReceipt> {
const account = args.overrides?.account || deployer;
const signature = await signExternalClaim({
claim: args.claim,
receiver: args.receiver,
claimPrivateKey: args.claimPrivateKey,
forceClaimAddress: args.overrides?.claimAccountAddress,
dustReceiver: args.dustReceiver,
});

const claimExternalCallData = CallData.compile([
buildCallDataClaim(args.claim),
args.receiver,
args.dustReceiver || "0x0",
signature,
]);
const response = await account.execute(
[
{
contractAddress: args.overrides?.factoryAddress || args.claim.factory,
calldata: [buildCallDataClaim(args.claim), args.receiver, args.dustReceiver || "0x0", signature],
entrypoint: "claim_external",
},
],
executeActionOnAccount("claim_external", calculateClaimAddress(args.claim), claimExternalCallData),
undefined,
{ ...args.details },
);

return manager.waitForTransaction(response.transaction_hash);
}

function executeActionOnAccount(functionName: string, accountAddress: string, args: Calldata): Call {
return {
contractAddress: accountAddress,
calldata: { selector: hash.getSelectorFromName(functionName), calldata: args },
entrypoint: "execute_action",
};
}

export async function claimInternal(args: {
claim: Claim;
receiver: string;
claimPrivateKey: string;
overrides?: { claimAccountAddress?: string; factoryAddress?: string };
overrides?: { claimAccountAddress?: string; callToAddress?: string };
details?: UniversalDetails;
}): Promise<TransactionReceipt> {
const claimAddress = args.overrides?.claimAccountAddress || calculateClaimAddress(args.claim);
const claimAccount = getClaimAccount(args.claim, args.claimPrivateKey, claimAddress);
const response = await claimAccount.execute(
[
{
contractAddress: args.overrides?.factoryAddress || args.claim.factory,
contractAddress: args.overrides?.callToAddress ?? claimAddress,
calldata: [buildCallDataClaim(args.claim), args.receiver],
entrypoint: "claim_internal",
},
Expand All @@ -146,6 +167,28 @@ export async function claimInternal(args: {
return manager.waitForTransaction(response.transaction_hash);
}

export async function cancelGift(args: { claim: Claim; senderAccount?: Account }): Promise<TransactionReceipt> {
const cancelCallData = CallData.compile([buildCallDataClaim(args.claim)]);
const account = args.senderAccount || deployer;
const response = await account.execute(
executeActionOnAccount("cancel", calculateClaimAddress(args.claim), cancelCallData),
);
return manager.waitForTransaction(response.transaction_hash);
}

export async function getDust(args: {
claim: Claim;
receiver: string;
factoryOwner?: Account;
}): Promise<TransactionReceipt> {
const getDustCallData = CallData.compile([buildCallDataClaim(args.claim), args.receiver]);
const account = args.factoryOwner || deployer;
const response = await account.execute(
executeActionOnAccount("get_dust", calculateClaimAddress(args.claim), getDustCallData),
);
return manager.waitForTransaction(response.transaction_hash);
}

function useTxv3(tokenAddress: string): boolean {
if (tokenAddress === ethAddress) {
return false;
Expand Down
15 changes: 5 additions & 10 deletions lib/expectations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ export async function expectRevertWithErrorMessage(
if (e.toString().includes(encodedErrorMessage)) {
return;
}

const match = e.toString().match(/\[([^\]]+)]/);
if (match && match.length > 1) {
console.log(e);
assert.fail(`"${errorMessage}" not detected, instead got: "${shortString.decodeShortString(match[1])}"`);
} else {
// With e.toString() the error message is not fully displayed
console.log(e);
assert.fail(`Couldn't find the error (see just above)`);
}
// With e.toString() the error message is not fully displayed
console.log(e);
assert.fail(
`Couldn't find the error '${errorMessage}' (${shortString.encodeShortString(errorMessage)}) see message above`,
);
}
assert.fail("No error detected");
}
Expand Down
9 changes: 6 additions & 3 deletions lib/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ export async function deployMockERC20(): Promise<Contract> {
export async function setupGiftProtocol(): Promise<{
factory: Contract;
claimAccountClassHash: string;
accountImplementationClassHash: string;
}> {
const claimAccountClassHash = await manager.declareLocalContract("ClaimAccount");
const accountImplementationClassHash = await manager.declareLocalContract("ClaimAccountImpl");
const cachedFactory = protocolCache["GiftFactory"];
if (cachedFactory) {
return { factory: cachedFactory, claimAccountClassHash };
return { factory: cachedFactory, claimAccountClassHash, accountImplementationClassHash };
}
const factory = await manager.deployContract("GiftFactory", {
unique: true,
constructorCalldata: [claimAccountClassHash, deployer.address],
constructorCalldata: [claimAccountClassHash, accountImplementationClassHash, deployer.address],
});

protocolCache["GiftFactory"] = factory;
return { factory, claimAccountClassHash };
return { factory, claimAccountClassHash, accountImplementationClassHash };
}
Loading

0 comments on commit 2af66b8

Please sign in to comment.