Skip to content

Commit

Permalink
Merge pull request #41 from argentlabs/profile/dust
Browse files Browse the repository at this point in the history
Adding dust profiler
  • Loading branch information
sgc-code authored Jul 1, 2024
2 parents 13c82b0 + b66de94 commit d25f799
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
40 changes: 28 additions & 12 deletions gas-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,49 @@ Summary:
│ 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.188.000.000.192' │ 0.0047 │ 1188000000000 │ 33 │ 30 │ 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' │
│ Claiming external WEI (FeeToken: WEI) │ '1.620.000.000.256' │ 0.0064 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 4 │ 256 │ 'BLOB' │
│ Get dust WEI (FeeToken: WEI) │ '1.620.000.000.192' │ 0.0064 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ '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.188.000.000.320' │ 0 │ 1188000000000 │ 33 │ 30 │ 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' │
│ Claiming external WEI (FeeToken: FRI) │ '1.620.000.000.320' │ 0 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Get dust WEI (FeeToken: FRI) │ '1.728.000.000.320' │ 0.0069 │ 1728000000000 │ 48 │ 45 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ '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.188.000.000.320' │ 0.0047 │ 1188000000000 │ 33 │ 30 │ 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' │
│ Claiming external FRI (FeeToken: WEI) │ '1.620.000.000.320' │ 0.0064 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Get dust FRI (FeeToken: WEI) │ '1.728.000.000.192' │ 0.0069 │ 1728000000000 │ 48 │ 45 │ 2 │ 2 │ 'steps' │ 3 │ 192 │ '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.188.000.000.192' │ 0 │ 1188000000000 │ 33 │ 30 │ 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' │
│ Claiming external FRI (FeeToken: FRI) │ '1.620.000.000.256' │ 0 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 4 │ 256 │ 'BLOB' │
│ Get dust FRI (FeeToken: FRI) │ '1.620.000.000.320' │ 0.0064 │ 1620000000000 │ 45 │ 42 │ 2 │ 2 │ 'steps' │ 4 │ 320 │ 'BLOB' │
│ Get dust 2 (FeeToken: WEI) │ '2.772.000.000.320' │ 0.011 │ 2772000000000 │ 77 │ 71 │ 3 │ 4 │ 'steps' │ 5 │ 320 │ 'BLOB' │
│ Get dust 3 (FeeToken: WEI) │ '3.960.000.000.384' │ 0.0158 │ 3960000000000 │ 110 │ 101 │ 4 │ 6 │ 'steps' │ 6 │ 384 │ 'BLOB' │
│ Get dust 4 (FeeToken: WEI) │ '5.112.000.000.448' │ 0.0204 │ 5112000000000 │ 142 │ 130 │ 5 │ 8 │ 'steps' │ 7 │ 448 │ 'BLOB' │
│ Get dust 5 (FeeToken: WEI) │ '6.264.000.000.512' │ 0.025 │ 6264000000000 │ 174 │ 160 │ 6 │ 9 │ 'steps' │ 8 │ 512 │ 'BLOB' │
└───────────────────────────────────────┴─────────────────────┴─────────┴────────────────┴────────────────┴─────────────────┴───────────┴──────────────┴──────────────────────────────┴───────────────┴────────┴─────────┘
Resources:
┌───────────────────────────────────────┬─────────┬───────┬───────┬────────┬──────────┬──────────┬─────────────┬───────┐
│ (index) │ bitwise │ ec_op │ ecdsa │ keccak │ pedersen │ poseidon │ range_check │ steps │
├───────────────────────────────────────┼─────────┼───────┼───────┼────────┼──────────┼──────────┼─────────────┼───────┤
│ 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 │ 48 │ 0 │ 339 │ 14627
│ Gifting WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 48 │ 0 │ 339 │ 14624
│ Claiming WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 373 │ 11715 │
│ Claiming external WEI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 427 │ 15434 │
│ Gifting WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 465 │ 20599 │
│ Claiming external WEI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 477 │ 16713 │
│ Get dust WEI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 49 │ 0 │ 480 │ 16585 │
│ Gifting WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 465 │ 20607 │
│ Claiming WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 407 │ 11913 │
│ Claiming external WEI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 427 │ 15434 │
│ Gifting FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 465 │ 20598 │
│ Claiming external WEI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 477 │ 16713 │
│ Get dust WEI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 50 │ 0 │ 514 │ 17757 │
│ Gifting FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 64 │ 0 │ 465 │ 20606 │
│ Claiming FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 373 │ 11715 │
│ Claiming external FRI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 427 │ 15434 │
│ Gifting FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 48 │ 0 │ 339 │ 14628 │
│ Claiming external FRI (FeeToken: WEI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 477 │ 16713 │
│ Get dust FRI (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 50 │ 0 │ 514 │ 17757 │
│ Gifting FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 48 │ 0 │ 339 │ 14625 │
│ Claiming FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 47 │ 0 │ 407 │ 11913 │
│ Claiming external FRI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 427 │ 15434 │
│ Claiming external FRI (FeeToken: FRI) │ 0 │ 6 │ 0 │ 0 │ 52 │ 4 │ 477 │ 16713 │
│ Get dust FRI (FeeToken: FRI) │ 0 │ 3 │ 0 │ 0 │ 49 │ 0 │ 480 │ 16585 │
│ Get dust 2 (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 83 │ 0 │ 856 │ 28376 │
│ Get dust 3 (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 117 │ 0 │ 1232 │ 40167 │
│ Get dust 4 (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 151 │ 0 │ 1608 │ 51958 │
│ Get dust 5 (FeeToken: WEI) │ 0 │ 3 │ 0 │ 0 │ 185 │ 0 │ 1984 │ 63749 │
└───────────────────────────────────────┴─────────┴───────┴───────┴────────┴──────────┴──────────┴─────────────┴───────┘
8 changes: 3 additions & 5 deletions lib/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
deployer,
ethAddress,
manager,
setDefaultTransactionVersionV3,
strkAddress,
} from ".";

Expand Down Expand Up @@ -110,11 +111,10 @@ export async function claimExternal(args: {
gift: Gift;
receiver: string;
giftPrivateKey: string;
useTxV3?: boolean;
dustReceiver?: string;
overrides?: { account?: Account };
details?: UniversalDetails;
}): Promise<TransactionReceipt> {
const account = args.overrides?.account || deployer;
const account = args.useTxV3 ? setDefaultTransactionVersionV3(deployer) : deployer;
const signature = await signExternalClaim({
gift: args.gift,
receiver: args.receiver,
Expand All @@ -130,8 +130,6 @@ export async function claimExternal(args: {
]);
const response = await account.execute(
executeActionOnAccount("claim_external", calculateEscrowAddress(args.gift), claimExternalCallData),
undefined,
{ ...args.details },
);
return manager.waitForTransaction(response.transaction_hash);
}
Expand Down
50 changes: 45 additions & 5 deletions scripts/profile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { CallData } from "starknet";
import {
buildGiftCallData,
calculateEscrowAddress,
claimDust,
claimExternal,
claimInternal,
defaultDepositTestSetup,
deployer,
executeActionOnAccount,
manager,
randomReceiver,
setDefaultTransactionVersionV3,
setupGiftProtocol,
} from "../lib";
import { newProfiler } from "../lib/gas";
Expand Down Expand Up @@ -36,12 +42,12 @@ await profiler.profile(
await strkContract.transfer(randomReceiver(), 1),
);

const receiver = "0x42";
const { factory } = await setupGiftProtocol();

for (const { giftTokenContract, unit } of tokens) {
for (const useTxV3 of [false, true]) {
const receiver = "0x42";
const { factory } = await setupGiftProtocol();

// Make a gift
// Profiling deposit
const { txReceipt, gift, giftPrivateKey } = await defaultDepositTestSetup({
factory,
useTxV3,
Expand All @@ -62,17 +68,51 @@ for (const { giftTokenContract, unit } of tokens) {

await profiler.profile(`Gifting ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`, txReceipt);

// Profiling claim internal
await profiler.profile(
`Claiming ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`,
await claimInternal({ gift, receiver, giftPrivateKey: giftPrivateKey }),
);

// Profiling claim external
await profiler.profile(
`Claiming external ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`,
await claimExternal({ gift: claimExternalGift, receiver, giftPrivateKey: giftPrivateKeyExternal }),
await claimExternal({ gift: claimExternalGift, receiver, useTxV3, giftPrivateKey: giftPrivateKeyExternal }),
);

// Profiling getting the dust
const account = useTxV3 ? setDefaultTransactionVersionV3(deployer) : deployer;
factory.connect(account);
await profiler.profile(
`Get dust ${unit} (FeeToken: ${manager.tokens.unitTokenContract(useTxV3)})`,
await claimDust({ gift, receiver: deployer.address }),
);
}
}

const limits = [2, 3, 4, 5];
for (const limit of limits) {
const claimDustCalls = [];
for (let i = 0; i < limit; i++) {
const { gift, giftPrivateKey } = await defaultDepositTestSetup({
factory,
overrides: {
giftTokenAddress: ethContract.address,
},
});

await claimInternal({ gift, receiver, giftPrivateKey: giftPrivateKey });
const claimDustCallData = CallData.compile([buildGiftCallData(gift), receiver]);
const call = executeActionOnAccount("claim_dust", calculateEscrowAddress(gift), claimDustCallData);

claimDustCalls.push(call);
}

await profiler.profile(
`Get dust ${limit} (FeeToken: ${manager.tokens.unitTokenContract(false)})`,
await deployer.execute(claimDustCalls),
);
}

profiler.printSummary();
profiler.updateOrCheckReport();
4 changes: 2 additions & 2 deletions tests-integration/claim_external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Claim External", function () {
const receiver = randomReceiver();
const escrowAddress = calculateEscrowAddress(gift);

await claimExternal({ gift, receiver, giftPrivateKey });
await claimExternal({ gift, receiver, useTxV3, giftPrivateKey });

const finalBalance = await manager.tokens.tokenBalance(escrowAddress, gift.gift_token);
expect(finalBalance).to.equal(gift.fee_amount);
Expand All @@ -36,7 +36,7 @@ describe("Claim External", function () {
const escrowAddress = calculateEscrowAddress(gift);

const balanceBefore = await manager.tokens.tokenBalance(escrowAddress, gift.gift_token);
await claimExternal({ gift, receiver, giftPrivateKey, dustReceiver });
await claimExternal({ gift, receiver, giftPrivateKey, useTxV3, dustReceiver });

await manager.tokens.tokenBalance(receiver, gift.gift_token).should.eventually.equal(gift.gift_amount);
await manager.tokens
Expand Down

0 comments on commit d25f799

Please sign in to comment.