Skip to content

Commit

Permalink
fix: account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 12, 2022
1 parent 998a2e3 commit cc3f362
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 48 deletions.
29 changes: 9 additions & 20 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ describe('deploy and test Wallet', () => {
contract: compiledErc20,
});

erc20Address = erc20Response.address!;
erc20Address = erc20Response.contract_address!;
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');

await provider.waitForTransaction(erc20Response.transaction_hash);

Expand All @@ -37,8 +36,6 @@ describe('deploy and test Wallet', () => {
calldata: [account.address, '1000'],
});

expect(mintResponse.code).toBe('TRANSACTION_RECEIVED');

await provider.waitForTransaction(mintResponse.transaction_hash);

const x = await erc20.balance_of(account.address);
Expand All @@ -48,20 +45,18 @@ describe('deploy and test Wallet', () => {
const dappResponse = await provider.deployContract({
contract: compiledTestDapp,
});
dapp = new Contract(compiledTestDapp.abi, dappResponse.address!, provider);
expect(dappResponse.code).toBe('TRANSACTION_RECEIVED');
dapp = new Contract(compiledTestDapp.abi, dappResponse.contract_address!, provider);

await provider.waitForTransaction(dappResponse.transaction_hash);
});

test('estimate fee', async () => {
const { amount, unit } = await account.estimateFee({
const { overall_fee } = await account.estimateFee({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10'],
});
expect(isBN(amount)).toBe(true);
expect(typeof unit).toBe('string');
expect(isBN(overall_fee)).toBe(true);
});

test('read balance of wallet', async () => {
Expand All @@ -71,13 +66,12 @@ describe('deploy and test Wallet', () => {
});

test('execute by wallet owner', async () => {
const { code, transaction_hash } = await account.execute({
const { transaction_hash } = await account.execute({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10'],
});

expect(code).toBe('TRANSACTION_RECEIVED');
await provider.waitForTransaction(transaction_hash);
});

Expand All @@ -93,7 +87,7 @@ describe('deploy and test Wallet', () => {
entrypoint: 'get_nonce',
});
const nonce = toBN(result[0]).toNumber();
const { code, transaction_hash } = await account.execute(
const { transaction_hash } = await account.execute(
{
contractAddress: erc20Address,
entrypoint: 'transfer',
Expand All @@ -103,12 +97,11 @@ describe('deploy and test Wallet', () => {
{ nonce }
);

expect(code).toBe('TRANSACTION_RECEIVED');
await provider.waitForTransaction(transaction_hash);
});

test('execute multiple transactions', async () => {
const { code, transaction_hash } = await account.execute([
const { transaction_hash } = await account.execute([
{
contractAddress: dapp.address,
entrypoint: 'set_number',
Expand All @@ -121,7 +114,6 @@ describe('deploy and test Wallet', () => {
},
]);

expect(code).toBe('TRANSACTION_RECEIVED');
await provider.waitForTransaction(transaction_hash);

const response = await dapp.get_number(account.address);
Expand Down Expand Up @@ -154,7 +146,7 @@ describe('deploy and test Wallet', () => {

await provider.waitForTransaction(accountResponse.transaction_hash);

newAccount = new Account(provider, accountResponse.address!, starkKeyPair);
newAccount = new Account(provider, accountResponse.contract_address!, starkKeyPair);
});

test('read nonce', async () => {
Expand All @@ -178,8 +170,6 @@ describe('deploy and test Wallet', () => {
calldata: [wallet, '1000'],
});

expect(mintResponse.code).toBe('TRANSACTION_RECEIVED');

await provider.waitForTransaction(mintResponse.transaction_hash);
});

Expand All @@ -191,8 +181,7 @@ describe('deploy and test Wallet', () => {

test('estimate gas fee for `mint`', async () => {
const res = await erc20.estimateFee.mint(wallet, '10');
expect(res).toHaveProperty('amount');
expect(res).toHaveProperty('unit');
expect(res).toHaveProperty('overall_fee');
});
});
});
8 changes: 4 additions & 4 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ZERO } from '../constants';
import { ProviderInterface, ProviderOptions } from '../provider';
import { ProviderOptions } from '../provider';
import { Provider } from '../provider/default';
import { Signer, SignerInterface } from '../signer';
import {
Expand All @@ -23,7 +23,7 @@ export class Account extends Provider implements AccountInterface {
public signer: SignerInterface;

constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
providerOrOptions: ProviderOptions | Provider,
public address: string,
keyPairOrSigner: KeyPair | SignerInterface
) {
Expand Down Expand Up @@ -61,9 +61,9 @@ export class Account extends Provider implements AccountInterface {

const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
const fetchedEstimate = await super.getEstimateFee(
{ contractAddress: this.address, entrypoint: '__execute__', calldata },
{ contractAddress: this.address, entrypoint: '__execute__', calldata, signature },
blockIdentifier,
signature
{ version }
);

const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.overall_fee);
Expand Down
11 changes: 5 additions & 6 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import { BigNumberish } from '../utils/number';
import { GatewayProvider, GatewayProviderOptions } from './gateway';
Expand All @@ -29,8 +28,8 @@ export interface ProviderOptions {
export class Provider implements ProviderInterface {
private provider!: ProviderInterface;

constructor(providerOrOptions?: ProviderOptions | ProviderInterface) {
if (providerOrOptions instanceof ProviderInterface) {
constructor(providerOrOptions?: ProviderOptions | Provider) {
if (providerOrOptions instanceof Provider) {
this.provider = providerOrOptions;
} else if (providerOrOptions && providerOrOptions.rpc) {
this.provider = new RPCProvider(providerOrOptions.rpc);
Expand All @@ -55,11 +54,11 @@ export class Provider implements ProviderInterface {
}

public async getEstimateFee(
request: Call,
invocation: Invocation,
blockIdentifier: BlockIdentifier,
signature?: Signature
invocationDetails?: InvocationsDetails
): Promise<EstimateFeeResponse> {
return this.provider.getEstimateFee(request, blockIdentifier, signature);
return this.provider.getEstimateFee(invocation, blockIdentifier, invocationDetails);
}

public async getStorageAt(
Expand Down
18 changes: 9 additions & 9 deletions src/provider/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
Expand Down Expand Up @@ -268,8 +267,8 @@ export class GatewayProvider implements ProviderInterface {
entry_point_selector: getSelectorFromName(functionInvocation.entrypoint),
calldata: bigNumberishArrayToDecimalStringArray(functionInvocation.calldata ?? []),
signature: bigNumberishArrayToDecimalStringArray(functionInvocation.signature ?? []),
max_fee: details.maxFee,
version: details.version,
max_fee: toHex(toBN(details.maxFee || 0)),
version: toHex(toBN(details.version || 0)),
}).then(this.responseParser.parseInvokeFunctionResponse);
}

Expand Down Expand Up @@ -303,18 +302,19 @@ export class GatewayProvider implements ProviderInterface {
}

public async getEstimateFee(
call: Call,
invocation: Invocation,
blockIdentifier: BlockIdentifier = 'pending',
signature?: Signature
invocationDetails: InvocationsDetails = {}
): Promise<EstimateFeeResponse> {
return this.fetchEndpoint(
'estimate_fee',
{ blockIdentifier },
{
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: bigNumberishArrayToDecimalStringArray(call.calldata ?? []),
signature: bigNumberishArrayToDecimalStringArray(signature || []),
contract_address: invocation.contractAddress,
entry_point_selector: getSelectorFromName(invocation.entrypoint),
calldata: invocation.calldata ?? [],
signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
version: toHex(toBN(invocationDetails?.version || 0)),
}
).then(this.responseParser.parseFeeEstimateResponse);
}
Expand Down
5 changes: 2 additions & 3 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import type { BigNumberish } from '../utils/number';
import { BlockIdentifier } from './utils';
Expand Down Expand Up @@ -128,9 +127,9 @@ export abstract class ProviderInterface {
): Promise<InvokeFunctionResponse>;

public abstract getEstimateFee(
request: Call,
request: Invocation,
blockIdentifier: BlockIdentifier,
signature?: Signature
invocationDetails?: InvocationsDetails
): Promise<EstimateFeeResponse>;

public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
Expand Down
11 changes: 5 additions & 6 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
InvocationsDetails,
InvokeFunctionResponse,
RPC,
Signature,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
Expand Down Expand Up @@ -130,15 +129,15 @@ export class RPCProvider implements ProviderInterface {
}

public async getEstimateFee(
call: Call,
invocation: Invocation,
blockIdentifier: BlockIdentifier = 'pending',
_signature: Signature = []
_invocationDetails: InvocationsDetails = {}
): Promise<EstimateFeeResponse> {
return this.fetchEndpoint('starknet_estimateFee', [
{
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: parseCalldata(call.calldata),
contract_address: invocation.contractAddress,
entry_point_selector: getSelectorFromName(invocation.entrypoint),
calldata: parseCalldata(invocation.calldata),
},
blockIdentifier,
]).then(this.responseParser.parseFeeEstimateResponse);
Expand Down

0 comments on commit cc3f362

Please sign in to comment.