Skip to content

V6 #893

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

Merged
merged 9 commits into from
Dec 14, 2023
4 changes: 3 additions & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ describe('deploy and test Wallet', () => {
});

expect(result).toMatchSchemaRef('EstimateFee');
expect(innerInvokeEstFeeSpy.mock.calls[0][1].version).toBe(constants.TRANSACTION_VERSION.F1);
expect([constants.TRANSACTION_VERSION.F1, constants.TRANSACTION_VERSION.F3]).toContain(
innerInvokeEstFeeSpy.mock.calls[0][1].version
);
innerInvokeEstFeeSpy.mockClear();
});

Expand Down
16 changes: 7 additions & 9 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
BigNumberish,
Contract,
ContractFactory,
GetTransactionReceiptResponse,
ParsedEvents,
RawArgs,
SuccessfulTransactionReceiptResponse,
json,
stark,
} from '../src';
Expand Down Expand Up @@ -720,7 +720,7 @@ describe('Complex interaction', () => {
test('invoke compiled data', async () => {
const result = await erc20Echo20Contract.iecho(CallData.compile(request));
const transaction = await provider.waitForTransaction(result.transaction_hash);
expect((transaction as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined();
expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined();
});

// skip on live for performance
Expand All @@ -730,19 +730,19 @@ describe('Complex interaction', () => {

const result = await erc20Echo20Contract.iecho(calldata);
const transaction = await provider.waitForTransaction(result.transaction_hash);
expect((transaction as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined();
expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined();

const result1 = await erc20Echo20Contract.iecho(...args);
const transaction1 = await provider.waitForTransaction(result1.transaction_hash);
expect((transaction1 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined();
expect((transaction1 as GetTransactionReceiptResponse).execution_status).toBeDefined();

const result2 = await erc20Echo20Contract.invoke('iecho', calldata);
const transaction2 = await provider.waitForTransaction(result2.transaction_hash);
expect((transaction2 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined();
expect((transaction2 as GetTransactionReceiptResponse).execution_status).toBeDefined();

const result3 = await erc20Echo20Contract.invoke('iecho', args);
const transaction3 = await provider.waitForTransaction(result3.transaction_hash);
expect((transaction3 as SuccessfulTransactionReceiptResponse).execution_status).toBeDefined();
expect((transaction3 as GetTransactionReceiptResponse).execution_status).toBeDefined();
});

describe('speedup live tests', () => {
Expand Down Expand Up @@ -795,9 +795,7 @@ describe('Complex interaction', () => {
{ formatResponse }
);
const transaction = await provider.waitForTransaction(result.transaction_hash);
expect(
(transaction as SuccessfulTransactionReceiptResponse).execution_status
).toBeDefined();
expect((transaction as GetTransactionReceiptResponse).execution_status).toBeDefined();
});
});

Expand Down
36 changes: 13 additions & 23 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
} from './config/fixtures';
import { initializeMatcher } from './config/schema';

const { isPendingStateUpdate } = provider;

const testProvider = new Provider(getTestProvider());

describe('defaultProvider', () => {
Expand Down Expand Up @@ -81,32 +83,20 @@ describe('defaultProvider', () => {

test(`getStateUpdate(blockHash=${exampleBlockHash}, blockNumber=undefined)`, async () => {
const stateUpdate = await testProvider.getStateUpdate(exampleBlockHash);
provider.defStateUpdate(
stateUpdate,
(state) => {
expect(state.block_hash).toBe(exampleBlockHash);
expect(state).toMatchSchemaRef('StateUpdateResponse');
},
(pending) => {
fail('exampleBlockHash is latest block, should not be pending');
expect(pending).toMatchSchemaRef('PendingStateUpdateResponse');
}
);
if (!isPendingStateUpdate(stateUpdate)) {
fail('exampleBlockHash is latest block, should not be pending');
}
expect(stateUpdate.block_hash).toBe(exampleBlockHash);
expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse');
});

test(`getStateUpdate(blockHash=undefined, blockNumber=${exampleBlockNumber})`, async () => {
const stateUpdate = await testProvider.getStateUpdate(exampleBlockNumber);
provider.defStateUpdate(
stateUpdate,
(state) => {
expect(state.block_hash).toBe(exampleBlockHash);
expect(state).toMatchSchemaRef('StateUpdateResponse');
},
(pending) => {
fail('exampleBlockHash is latest block, should not be pending');
expect(pending).toMatchSchemaRef('PendingStateUpdateResponse');
}
);
if (isPendingStateUpdate(stateUpdate)) {
fail('exampleBlockHash is latest block, should not be pending');
}
expect(stateUpdate.block_hash).toBe(exampleBlockHash);
expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse');
});
});

Expand Down Expand Up @@ -181,7 +171,7 @@ describe('defaultProvider', () => {
}),
})
.then((res) => {
expect(Array.isArray(res.result)).toBe(true);
expect(Array.isArray(res)).toBe(true);
})
).resolves.not.toThrow();
});
Expand Down
10 changes: 2 additions & 8 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { getStarkKey, utils } from '@scure/starknet';

import {
Account,
Block,
CallData,
Contract,
GetBlockResponse,
RPC,
TransactionExecutionStatus,
stark,
Expand Down Expand Up @@ -77,12 +77,6 @@ describeIfRpc('RPCProvider', () => {
expect(typeof spec).toBe('string');
});

test('getCode - not implemented', async () => {
expect(
rpcProvider.getCode('0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773')
).rejects.toThrow();
});

describe('Test Estimate message fee', () => {
const L1_ADDRESS = '0x8359E4B0152ed5A731162D3c7B0D8D56edB165A0';
let l1l2ContractAddress: string;
Expand Down Expand Up @@ -172,7 +166,7 @@ describeIfRpc('RPCProvider', () => {
});

describe('RPC methods', () => {
let latestBlock: GetBlockResponse;
let latestBlock: Block;

beforeAll(async () => {
latestBlock = await provider.getBlock('latest');
Expand Down
3 changes: 2 additions & 1 deletion __tests__/schemas/provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@
"type": "string"
},
"unit": {
"type": "string"
"type": "string",
"enum": ["WEI", "FRI"]
}
}
},
Expand Down
12 changes: 7 additions & 5 deletions __tests__/utils/stark.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CallData, EstimateFeeResponse, RawArgs, json, stark } from '../../src';
import { CallData, RawArgs, json, stark } from '../../src';
import { FeeEstimate } from '../../src/types/api';
import { toBigInt, toHex } from '../../src/utils/num';
import { compiledOpenZeppelinAccount } from '../config/fixtures';

Expand Down Expand Up @@ -71,10 +72,11 @@ describe('stark', () => {
});

test('estimateFeeToBounds', () => {
const estimateFeeResponse: EstimateFeeResponse = {
gas_consumed: 100n,
gas_price: 10n,
overall_fee: 1000n,
const estimateFeeResponse: FeeEstimate = {
gas_consumed: '100',
gas_price: '10',
overall_fee: '1000',
unit: 'FRI',
};
expect(stark.estimateFeeToBounds(estimateFeeResponse)).toStrictEqual({
l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' },
Expand Down
41 changes: 8 additions & 33 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
EstimateFee,
EstimateFeeAction,
EstimateFeeBulk,
EstimateFeeResponse,
Invocation,
Invocations,
InvocationsSignerDetails,
Expand All @@ -49,7 +48,6 @@ import { toBigInt, toCairoBool } from '../utils/num';
import { parseContract } from '../utils/provider';
import {
estimateFeeToBounds,
estimatedFeeToMaxFee,
formatSignature,
randomAddress,
reduceV2,
Expand Down Expand Up @@ -157,18 +155,12 @@ export class Account extends Provider implements AccountInterface {
};

const invocation = await this.buildInvocation(transactions, signerDetails);
const estimateFeeResponse = await super.getInvokeEstimateFee(
return super.getInvokeEstimateFee(
{ ...invocation },
{ ...v3Details(details), version, nonce },
blockIdentifier,
details.skipValidate
);

return {
...estimateFeeResponse,
suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee),
resourceBounds: estimateFeeToBounds(estimateFeeResponse),
};
}

public async estimateDeclareFee(
Expand All @@ -195,18 +187,12 @@ export class Account extends Provider implements AccountInterface {
cairoVersion: undefined, // unused parameter
});

const estimateFeeResponse = await super.getDeclareEstimateFee(
return super.getDeclareEstimateFee(
declareContractTransaction,
{ ...v3Details(details), version, nonce },
blockIdentifier,
details.skipValidate
);

return {
...estimateFeeResponse,
suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee),
resourceBounds: estimateFeeToBounds(estimateFeeResponse),
};
}

public async estimateAccountDeployFee(
Expand Down Expand Up @@ -239,18 +225,12 @@ export class Account extends Provider implements AccountInterface {
}
);

const estimateFeeResponse = await super.getDeployAccountEstimateFee(
return super.getDeployAccountEstimateFee(
{ ...payload },
{ ...v3Details(details), version, nonce },
blockIdentifier,
details.skipValidate
);

return {
...estimateFeeResponse,
suggestedMaxFee: estimatedFeeToMaxFee(estimateFeeResponse.overall_fee),
resourceBounds: estimateFeeToBounds(estimateFeeResponse),
};
}

public async estimateDeployFee(
Expand All @@ -276,18 +256,10 @@ export class Account extends Provider implements AccountInterface {
blockIdentifier,
});

const EstimateFeeResponseBulk = await super.getEstimateFeeBulk(accountInvocations, {
return super.getEstimateFeeBulk(accountInvocations, {
blockIdentifier,
skipValidate: details.skipValidate,
});

return [].concat(EstimateFeeResponseBulk as []).map((elem: EstimateFeeResponse) => {
return {
...elem,
suggestedMaxFee: estimatedFeeToMaxFee(elem.overall_fee),
resourceBounds: estimateFeeToBounds(elem),
};
});
}

public async simulateTransaction(
Expand Down Expand Up @@ -643,8 +615,11 @@ export class Account extends Provider implements AccountInterface {

default:
feeEstimate = {
suggestedMaxFee: ZERO,
gas_consumed: 0n,
gas_price: 0n,
overall_fee: ZERO,
unit: 'FRI',
suggestedMaxFee: ZERO,
resourceBounds: estimateFeeToBounds(ZERO),
};
break;
Expand Down
8 changes: 4 additions & 4 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,14 @@ export class Contract implements ContractInterface {
},
blockIdentifier
)
.then((x) => {
.then((it) => {
if (!parseResponse) {
return x.result;
return it;
}
if (formatResponse) {
return this.callData.format(method, x.result, formatResponse);
return this.callData.format(method, it, formatResponse);
}
return this.callData.parse(method, x.result);
return this.callData.parse(method, it);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/provider/extensions/starknetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class StarknetId {
address,
}),
});
const decimalDomain = hexDomain.result.map((element) => BigInt(element)).slice(1);
const decimalDomain = hexDomain.map((element) => BigInt(element)).slice(1);

const stringDomain = useDecoded(decimalDomain);

Expand Down Expand Up @@ -72,7 +72,7 @@ export class StarknetId {
}),
});

return addressData.result[0];
return addressData[0];
} catch {
throw Error('Could not get address from stark name');
}
Expand Down
14 changes: 5 additions & 9 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StarknetChainId } from '../constants';
import type {
AccountInvocations,
BigNumberish,
Block,
BlockIdentifier,
Call,
CallContractResponse,
Expand All @@ -16,13 +17,13 @@ import type {
EstimateFeeResponse,
EstimateFeeResponseBulk,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Nonce,
PendingBlock,
SimulateTransactionResponse,
StateUpdateResponse,
Storage,
Expand Down Expand Up @@ -60,16 +61,11 @@ export abstract class ProviderInterface {
* @param blockIdentifier block identifier
* @returns the block object
*/
public abstract getBlock(): Promise<PendingBlock>;
public abstract getBlock(blockIdentifier: 'pending'): Promise<PendingBlock>;
public abstract getBlock(blockIdentifier: 'latest'): Promise<Block>;
public abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;

/**
* @deprecated The method should not be used
*/
public abstract getCode(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<GetCodeResponse>;

/**
* Gets the contract class of the deployed contract.
*
Expand Down
Loading