Skip to content

Commit

Permalink
Merge branch 'feat/add-stark-scan-client' into chore/refactor-txn-mgr
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyyconsensys committed Dec 5, 2024
2 parents 38e5d63 + 63e3030 commit 6391e81
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
10 changes: 4 additions & 6 deletions packages/starknet-snap/src/chain/data-client/starkscan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
generateStarkScanTransactions,
} from '../../__tests__/helper';
import {
ContractFuncName,
TransactionDataVersion,
type Network,
type Transaction,
Expand Down Expand Up @@ -345,11 +346,8 @@ describe('StarkScanClient', () => {
const client = createMockClient();
const result = client.toTransaction(mockTx);

const {
contract_address: contract,
selector_name: contractFuncName,
calldata: contractCallData,
} = mockTx.account_calls[0];
const { contract_address: contract, calldata: contractCallData } =
mockTx.account_calls[0];

expect(result).toStrictEqual({
txnHash: mockTx.transaction_hash,
Expand All @@ -367,7 +365,7 @@ describe('StarkScanClient', () => {
[contract]: [
{
contract,
contractFuncName,
contractFuncName: ContractFuncName.Transfer,
contractCallData,
recipient: contractCallData[0],
amount: contractCallData[1],
Expand Down
23 changes: 20 additions & 3 deletions packages/starknet-snap/src/chain/data-client/starkscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ 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 { InvalidNetworkError } from '../../utils/exceptions';
import type { HttpHeaders } from '../api-client';
import { ApiClient, HttpMethod } from '../api-client';
Expand Down Expand Up @@ -181,7 +186,7 @@ export class StarkScanClient extends ApiClient implements IDataClient {
}

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

protected getContractAddress(tx: StarkScanTransaction): string {
Expand Down Expand Up @@ -254,10 +259,11 @@ export class StarkScanClient extends ApiClient implements IDataClient {
) => {
const {
contract_address: contract,
selector_name: contractFuncName,
selector,
calldata: contractCallData,
} = accountCallArg;

const contractFuncName = this.selectorHexToName(selector);
if (!Object.prototype.hasOwnProperty.call(data, contract)) {
data[contract] = [];
}
Expand All @@ -268,7 +274,7 @@ export class StarkScanClient extends ApiClient implements IDataClient {
contractCallData,
};

if (this.isFundTransferTransaction(contractFuncName)) {
if (this.isFundTransferTransaction(selector)) {
accountCall.recipient = accountCallArg.calldata[0];
accountCall.amount = accountCallArg.calldata[1];
}
Expand All @@ -280,4 +286,15 @@ export class StarkScanClient extends ApiClient implements IDataClient {
{},
);
}

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const NullableStringArrayStruct = nullable(array(string()));
export const StarkScanAccountCallStruct = object({
contract_address: string(),
calldata: array(string()),
selector_name: string(),
selector: string(),
});

export const StarkScanTransactionStruct = object({
Expand Down
5 changes: 5 additions & 0 deletions packages/starknet-snap/src/types/snapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export enum TransactionDataVersion {
V2 = 'V2',
}

export enum ContractFuncName {
Upgrade = 'upgrade',
Transfer = 'transfer',
}

export type V2Transaction = {
txnHash: string; // in hex
txnType: StarkNetTransactionType;
Expand Down

0 comments on commit 6391e81

Please sign in to comment.