Skip to content

Commit

Permalink
fix: call contract latest block
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 18, 2022
1 parent a9ea023 commit 1476461
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 65 deletions.
64 changes: 2 additions & 62 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { isBN } from 'bn.js';

import { Account, Contract, RpcProvider } from '../src';
import { toBN } from '../src/utils/number';
import { compiledErc20, describeIfRpc, getTestAccount, getTestProvider } from './fixtures';
import { RpcProvider } from '../src';
import { describeIfRpc, getTestProvider } from './fixtures';

describeIfRpc('RPCProvider', () => {
let provider: RpcProvider;
Expand All @@ -17,61 +14,4 @@ describeIfRpc('RPCProvider', () => {
expect(chainId).toBe('0x534e5f474f45524c49');
});
});

describe('Account', () => {
let account: Account;
let erc20Address: string;
let erc20: Contract;

beforeAll(async () => {
account = getTestAccount(provider);
expect(account).toBeInstanceOf(Account);

const erc20Response = await provider.deployContract({
contract: compiledErc20,
});

erc20Address = erc20Response.contract_address;
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);

await provider.waitForTransaction(erc20Response.transaction_hash);
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);

const mintResponse = await account.execute({
contractAddress: erc20Address,
entrypoint: 'mint',
calldata: [account.address, '1000'],
});

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

test('estimate fee', async () => {
const { overall_fee, gas_consumed, gas_price } = await account.estimateFee({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10'],
});

expect(isBN(overall_fee)).toBe(true);
expect(isBN(gas_consumed)).toBe(true);
expect(isBN(gas_price)).toBe(true);
});

test('execute by wallet owner', async () => {
const { res: before } = await erc20.balance_of(account.address);

const { transaction_hash } = await account.execute({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10'],
});

await account.waitForTransaction(transaction_hash);

const { res: after } = await erc20.balance_of(account.address);

expect(toBN(before).sub(toBN(after)).toString(10)).toBe('10');
});
});
});
2 changes: 1 addition & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Provider implements ProviderInterface {

public async callContract(
request: Call,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = 'latest'
): Promise<CallContractResponse> {
return this.provider.callContract(request, blockIdentifier);
}
Expand Down
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class RpcProvider implements ProviderInterface {

public async callContract(
call: Call,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = 'latest'
): Promise<CallContractResponse> {
const result = await this.fetchEndpoint('starknet_call', [
{
Expand Down
2 changes: 1 addition & 1 deletion src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class SequencerProvider implements ProviderInterface {

public async callContract(
{ contractAddress, entrypoint: entryPointSelector, calldata = [] }: Call,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = 'latest'
): Promise<CallContractResponse> {
return this.fetchEndpoint(
'call_contract',
Expand Down

0 comments on commit 1476461

Please sign in to comment.