Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: handle multi calls on RPC starkNet_executeTxn #457

Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
77f4b7b
feat: add stark scan client
stanleyyconsensys Sep 2, 2024
6b7a5e2
chore: add starkscan config
stanleyyconsensys Sep 2, 2024
3c9a523
chore: lint
stanleyyconsensys Sep 2, 2024
576f302
chore: add interface
stanleyyconsensys Sep 2, 2024
19fdf56
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Sep 5, 2024
5d446cb
chore: support multiple txn
stanleyyconsensys Sep 5, 2024
3dbdf32
chore: update starkscan
stanleyyconsensys Sep 5, 2024
924ea45
chore: update stark scan client
stanleyyconsensys Sep 6, 2024
bcf34c7
chore: update contract func name
stanleyyconsensys Sep 6, 2024
d1ad70c
chore: fix test
stanleyyconsensys Sep 6, 2024
1a061d3
chore: update data client
stanleyyconsensys Sep 6, 2024
3bda85f
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Sep 6, 2024
114ea6d
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Oct 22, 2024
7539056
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Dec 2, 2024
ed699ff
chore: re-structure starkscan type
stanleyyconsensys Dec 2, 2024
93adb36
chore: add test coverage
stanleyyconsensys Dec 2, 2024
dadb493
chore: factory and config
stanleyyconsensys Dec 2, 2024
b21a5d2
chore: add backward compatibility for transactions type
stanleyyconsensys Dec 2, 2024
07f0232
chore: add comment
stanleyyconsensys Dec 2, 2024
7a26c70
chore: lint
stanleyyconsensys Dec 2, 2024
804a2bd
chore: resolve review comment
stanleyyconsensys Dec 4, 2024
8e9e163
chore: change dataVersion to enum
stanleyyconsensys Dec 4, 2024
b09361f
chore: lint
stanleyyconsensys Dec 4, 2024
7cbd66f
chore: update test helper and refactor ContractAddressFilter
stanleyyconsensys Dec 5, 2024
f657422
chore: lint
stanleyyconsensys Dec 5, 2024
4ee01bc
chore: add test for dataVersion filter
stanleyyconsensys Dec 5, 2024
3a99e76
chore: update txn state mgr test
stanleyyconsensys Dec 5, 2024
38e5d63
chore: update search condition
stanleyyconsensys Dec 5, 2024
63e3030
chore: update starkscan to handle missing selector_name
stanleyyconsensys Dec 5, 2024
6391e81
Merge branch 'feat/add-stark-scan-client' into chore/refactor-txn-mgr
stanleyyconsensys Dec 5, 2024
873a1de
Merge branch 'main' into chore/refactor-txn-mgr
khanti42 Dec 5, 2024
97f0e05
Merge branch 'main' into chore/revamp-list-transactions
stanleyyconsensys Dec 5, 2024
956b10e
chore: apply starkscan for list transaction
stanleyyconsensys Dec 6, 2024
29f7c5f
chore: update list transactions handle
stanleyyconsensys Dec 6, 2024
103f087
Merge branch 'feat/refactor-list-transactions-with-starkscan' into ch…
stanleyyconsensys Dec 10, 2024
8b8d4b6
chore: refactor execute txn
stanleyyconsensys Dec 10, 2024
0ee7431
chore: refactor execute txn
stanleyyconsensys Dec 10, 2024
cf64931
chore: update create account and upgrade account
stanleyyconsensys Dec 10, 2024
12be249
chore: lint
stanleyyconsensys Dec 10, 2024
c5e7741
Revert "chore: lint"
stanleyyconsensys Dec 10, 2024
bb69ff5
Revert "chore: update create account and upgrade account"
stanleyyconsensys Dec 10, 2024
817e874
Merge branch 'feat/refactor-list-transactions-with-starkscan' into ch…
stanleyyconsensys Dec 10, 2024
4699e07
chore: resolve comment
stanleyyconsensys Dec 10, 2024
62429dc
chore: resolve comment
stanleyyconsensys Dec 10, 2024
6224e5a
chore: remove space
stanleyyconsensys Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 46 additions & 86 deletions packages/starknet-snap/src/chain/data-client/starkscan.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { TransactionType, constants } from 'starknet';
import type { Struct } from 'superstruct';

import {
ContractFuncName,
TransactionDataVersion,
type Network,
type Transaction,
type TranscationAccountCall,
} from '../../types/snapState';
import {
TRANSFER_SELECTOR_HEX,
UPGRADE_SELECTOR_HEX,
} from '../../utils/constants';
import type { V2Transaction } from '../../types/snapState';
import { type Network, type Transaction } from '../../types/snapState';
import { InvalidNetworkError } from '../../utils/exceptions';
import {
newDeployTransaction,
newInvokeTransaction,
} from '../../utils/transaction';
import type { HttpHeaders } from '../api-client';
import { ApiClient, HttpMethod } from '../api-client';
import type { IDataClient } from '../data-client';
import type { StarkScanTransactionsResponse } from './starkscan.type';
import {
type StarkScanAccountCall,
type StarkScanTransaction,
type StarkScanOptions,
StarkScanTransactionsResponseStruct,
Expand Down Expand Up @@ -185,10 +179,6 @@ export class StarkScanClient extends ApiClient implements IDataClient {
return tx.transaction_type === TransactionType.DEPLOY_ACCOUNT;
}

protected isFundTransferTransaction(entrypoint: string): boolean {
return entrypoint === TRANSFER_SELECTOR_HEX;
}

protected getContractAddress(tx: StarkScanTransaction): string {
// backfill the contract address if it is null
return tx.contract_address ?? '';
Expand All @@ -207,94 +197,64 @@ export class StarkScanClient extends ApiClient implements IDataClient {
}

protected toTransaction(tx: StarkScanTransaction): Transaction {
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/naming-convention, camelcase */
const {
transaction_hash: txnHash,
transaction_type: txnType,
timestamp,
transaction_finality_status: finalityStatus,
transaction_execution_status: executionStatus,
max_fee: maxFee,
max_fee,
actual_fee: actualFee,
revert_error: failureReason,
revert_error,
// account_calls representing the calls to invoke from the account contract, it can be multiple
// If the transaction is a deploy transaction, the account_calls is a empty array
account_calls: calls,
version,
version: txnVersion,
} = tx;

// account_calls representing the calls to invoke from the account contract, it can be multiple
// If the transaction is a deploy transaction, the account_calls is a empty array
const accountCalls = this.toAccountCall(calls);
const { chainId } = this.network;
const senderAddress = this.getSenderAddress(tx);
const failureReason = revert_error ?? '';
const maxFee = max_fee ?? '0';

let transaction: V2Transaction;

// eslint-disable-next-line no-negated-condition
if (!this.isDeployTransaction(tx)) {
transaction = newInvokeTransaction({
txnHash,
senderAddress,
chainId,
maxFee,
calls: calls.map((call) => ({
contractAddress: call.contract_address,
entrypoint: call.selector,
calldata: call.calldata,
})),
txnVersion,
});
} else {
transaction = newDeployTransaction({
txnHash,
senderAddress,
chainId,
txnVersion,
});
}

return {
txnHash,
txnType,
chainId: this.network.chainId,
senderAddress: this.getSenderAddress(tx),
...transaction,
// Override the fields from the StarkScanTransaction
timestamp,
finalityStatus,
executionStatus,
maxFee,
actualFee,
maxFee,
contractAddress: this.getContractAddress(tx),
accountCalls,
failureReason: failureReason ?? '',
version,
dataVersion: TransactionDataVersion.V2,
failureReason,
txnType,
};

/* eslint-enable */
}

protected toAccountCall(
accountCalls: StarkScanAccountCall[],
): Record<string, TranscationAccountCall[]> | null {
if (!accountCalls || accountCalls.length === 0) {
return null;
}

return accountCalls.reduce(
(
data: Record<string, TranscationAccountCall[]>,
accountCallArg: StarkScanAccountCall,
) => {
const {
contract_address: contract,
selector,
calldata: contractCallData,
} = accountCallArg;

const contractFuncName = this.selectorHexToName(selector);
if (!Object.prototype.hasOwnProperty.call(data, contract)) {
data[contract] = [];
}

const accountCall: TranscationAccountCall = {
contract,
contractFuncName,
contractCallData,
};

if (this.isFundTransferTransaction(selector)) {
accountCall.recipient = accountCallArg.calldata[0];
accountCall.amount = accountCallArg.calldata[1];
}

data[contract].push(accountCall);

return data;
},
{},
);
}

protected selectorHexToName(selector: string): string {
switch (selector.toLowerCase()) {
case TRANSFER_SELECTOR_HEX.toLowerCase():
return ContractFuncName.Transfer;
case UPGRADE_SELECTOR_HEX.toLowerCase():
return ContractFuncName.Upgrade;
default:
return selector;
}
}
}
Loading
Loading