Skip to content

Commit

Permalink
Merge pull request #463 from 0xs34n/v5/refactor-1
Browse files Browse the repository at this point in the history
V5/refactor 1
  • Loading branch information
tabaktoni authored Feb 10, 2023
2 parents 558b20e + 9339212 commit e17ff17
Show file tree
Hide file tree
Showing 21 changed files with 160 additions and 113 deletions.
10 changes: 7 additions & 3 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, ec, number, stark } from '../src';
import { Account, Contract, Provider, TransactionStatus, ec, number, stark } from '../src';
import { parseUDCEvent } from '../src/utils/events';
import { calculateContractAddressFromHash, feeTransactionVersion } from '../src/utils/hash';
import { cleanHex, hexToDecimalString, toBigInt, toHex } from '../src/utils/number';
Expand Down Expand Up @@ -123,7 +123,9 @@ describe('deploy and test Wallet', () => {
calldata: [erc20.address, '10', '0'],
});

await provider.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
await provider.waitForTransaction(transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});
});

test('read balance of wallet after transfer', async () => {
Expand Down Expand Up @@ -162,7 +164,9 @@ describe('deploy and test Wallet', () => {
},
]);

await provider.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
await provider.waitForTransaction(transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});

const response = await dapp.get_number(account.address);
expect(toBigInt(response.number as string).toString()).toStrictEqual('57');
Expand Down
9 changes: 6 additions & 3 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';

import { Account, ProviderInterface, RpcProvider, SequencerProvider, json } from '../src';
import { CompiledContract } from '../src/types';
import { CompiledContract, waitForTransactionOptions } from '../src/types';
import { toHex } from '../src/utils/number';

const readContract = (name: string): CompiledContract =>
Expand Down Expand Up @@ -51,8 +51,11 @@ export const getTestProvider = (): ProviderInterface => {
if (IS_LOCALHOST_DEVNET) {
// accelerate the tests when running locally
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
provider.waitForTransaction = (txHash: string, retryInterval?: number) => {
return originalWaitForTransaction(txHash, retryInterval || 1000);
provider.waitForTransaction = (
txHash: string,
{ retryInterval }: waitForTransactionOptions = {}
) => {
return originalWaitForTransaction(txHash, { retryInterval: retryInterval || 1000 });
};
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('hashMessage()', () => {
transactionVersion,
calldata,
maxFee,
StarknetChainId.TESTNET,
StarknetChainId.SN_GOERLI,
nonce
);

Expand Down
6 changes: 3 additions & 3 deletions __tests__/utils/starknetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ describe('Should tets StarknetId utils', () => {
});

test('Should test getStarknetIdContract', () => {
expect(getStarknetIdContract(StarknetChainId.TESTNET)).toBe(
expect(getStarknetIdContract(StarknetChainId.SN_GOERLI)).toBe(
'0x05cf267a0af6101667013fc6bd3f6c11116a14cda9b8c4b1198520d59f900b17'
);

expect(() => {
getStarknetIdContract(StarknetChainId.TESTNET2);
getStarknetIdContract(StarknetChainId.SN_GOERLI2);
}).toThrow();

expect(getStarknetIdContract(StarknetChainId.MAINNET)).toBe(
expect(getStarknetIdContract(StarknetChainId.SN_MAIN)).toBe(
'0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678'
);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils/transactionHash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('calculateTransactionHashCommon()', () => {
'0x64',
[],
'0x0',
StarknetChainId.TESTNET
StarknetChainId.SN_GOERLI
);
expect(result).toBe('0x7d260744de9d8c55e7675a34512d1951a7b262c79e685d26599edd2948de959');
});
Expand Down
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
MultiDeployContractResponse,
Signature,
TransactionBulk,
TransactionStatus,
TransactionType,
UniversalDeployerContractPayload,
} from '../types';
import { EstimateFeeBulk, TransactionSimulation } from '../types/account';
Expand Down Expand Up @@ -111,13 +113,6 @@ export class Account extends Provider implements AccountInterface {
}
}

public async estimateFee(
calls: AllowArray<Call>,
estimateFeeDetails?: EstimateFeeDetails | undefined
): Promise<EstimateFee> {
return this.estimateInvokeFee(calls, estimateFeeDetails);
}

public async estimateInvokeFee(
calls: AllowArray<Call>,
{ nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
Expand Down Expand Up @@ -315,7 +310,10 @@ export class Account extends Provider implements AccountInterface {
const nonce = toBigInt(transactionsDetail.nonce ?? (await this.getNonce()));
const maxFee =
transactionsDetail.maxFee ??
(await this.getSuggestedMaxFee({ type: 'INVOKE', payload: calls }, transactionsDetail));
(await this.getSuggestedMaxFee(
{ type: TransactionType.INVOKE, payload: calls },
transactionsDetail
));
const version = toBigInt(transactionVersion);
const chainId = await this.getChainId();

Expand Down Expand Up @@ -352,7 +350,7 @@ export class Account extends Provider implements AccountInterface {
const maxFee =
transactionsDetail.maxFee ??
(await this.getSuggestedMaxFee(
{ type: 'DECLARE', payload: { classHash, contract } }, // Provide the classHash to avoid re-computing it
{ type: TransactionType.DECLARE, payload: { classHash, contract } }, // Provide the classHash to avoid re-computing it
transactionsDetail
));

Expand Down Expand Up @@ -431,9 +429,9 @@ export class Account extends Provider implements AccountInterface {
details?: InvocationsDetails | undefined
): Promise<DeployContractUDCResponse> {
const deployTx = await this.deploy(payload, details);
const txReceipt = await this.waitForTransaction(deployTx.transaction_hash, undefined, [
'ACCEPTED_ON_L2',
]);
const txReceipt = await this.waitForTransaction(deployTx.transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});
return parseUDCEvent(txReceipt);
}

Expand All @@ -443,7 +441,9 @@ export class Account extends Provider implements AccountInterface {
): Promise<DeclareDeployUDCResponse> {
const { contract, constructorCalldata, salt, unique } = payload;
const { transaction_hash, class_hash } = await this.declare({ contract }, details);
const declare = await this.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
const declare = await this.waitForTransaction(transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});
const deploy = await this.deployContract(
{ classHash: class_hash, salt, unique, constructorCalldata },
details
Expand Down Expand Up @@ -472,7 +472,7 @@ export class Account extends Provider implements AccountInterface {
transactionsDetail.maxFee ??
(await this.getSuggestedMaxFee(
{
type: 'DEPLOY_ACCOUNT',
type: TransactionType.DEPLOY_ACCOUNT,
payload: { classHash, constructorCalldata, addressSalt, contractAddress },
},
transactionsDetail
Expand Down Expand Up @@ -535,19 +535,19 @@ export class Account extends Provider implements AccountInterface {
let feeEstimate: EstimateFee;

switch (type) {
case 'INVOKE':
case TransactionType.INVOKE:
feeEstimate = await this.estimateInvokeFee(payload, details);
break;

case 'DECLARE':
case TransactionType.DECLARE:
feeEstimate = await this.estimateDeclareFee(payload, details);
break;

case 'DEPLOY_ACCOUNT':
case TransactionType.DEPLOY_ACCOUNT:
feeEstimate = await this.estimateAccountDeployFee(payload, details);
break;

case 'DEPLOY':
case TransactionType.DEPLOY:
feeEstimate = await this.estimateDeployFee(payload, details);
break;

Expand Down
16 changes: 0 additions & 16 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ export abstract class AccountInterface extends ProviderInterface {

public abstract signer: SignerInterface;

/**
* @deprecated Use estimateInvokeFee or estimateDeclareFee instead
* Estimate Fee for executing an INVOKE transaction on starknet
*
* @param calls the invocation object containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
*
* @returns response from estimate_fee
*/
public abstract estimateFee(
calls: AllowArray<Call>,
estimateFeeDetails?: EstimateFeeDetails
): Promise<EstimateFeeResponse>;

/**
* Estimate Fee for executing an INVOKE transaction on starknet
*
Expand Down
19 changes: 16 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ export const MASK_250 = 2n ** 250n - 1n; // 2 ** 250 - 1
export const MASK_251 = 2n ** 251n;
export const API_VERSION = ZERO;

export enum BaseUrl {
SN_MAIN = 'https://alpha-mainnet.starknet.io',
SN_GOERLI = 'https://alpha4.starknet.io',
SN_GOERLI2 = 'https://alpha4-2.starknet.io',
}

export enum NetworkName {
SN_MAIN = 'SN_MAIN',
SN_GOERLI = 'SN_GOERLI',
SN_GOERLI2 = 'SN_GOERLI2',
}

export enum StarknetChainId {
MAINNET = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
TESTNET = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
TESTNET2 = '0x534e5f474f45524c4932',
SN_MAIN = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
SN_GOERLI = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
SN_GOERLI2 = '0x534e5f474f45524c4932', // encodeShortString('SN_GOERLI2'),
}

export enum TransactionHashPrefix {
DECLARE = '0x6465636c617265', // encodeShortString('declare'),
DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'),
Expand Down
7 changes: 3 additions & 4 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Status,
TransactionSimulationResponse,
waitForTransactionOptions,
} from '../types';
import { BigNumberish } from '../utils/number';
import { ProviderInterface } from './interface';
Expand Down Expand Up @@ -185,10 +185,9 @@ export class Provider implements ProviderInterface {

public async waitForTransaction(
txHash: BigNumberish,
retryInterval?: number,
successStates?: Array<Status>
options: waitForTransactionOptions
): Promise<GetTransactionReceiptResponse> {
return this.provider.waitForTransaction(txHash, retryInterval, successStates);
return this.provider.waitForTransaction(txHash, options);
}

public async getSimulateTransaction(
Expand Down
5 changes: 2 additions & 3 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type {
InvocationBulk,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Status,
TransactionSimulationResponse,
waitForTransactionOptions,
} from '../types';
import type { BigNumberish } from '../utils/number';
import { BlockIdentifier } from './utils';
Expand Down Expand Up @@ -293,8 +293,7 @@ export abstract class ProviderInterface {
*/
public abstract waitForTransaction(
txHash: BigNumberish,
retryInterval?: number,
successStates?: Array<Status>
options?: waitForTransactionOptions
): Promise<GetTransactionReceiptResponse>;

/**
Expand Down
Loading

0 comments on commit e17ff17

Please sign in to comment.