Skip to content

Commit

Permalink
feat: getTestProvider util
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jun 14, 2022
1 parent 8e86c0e commit 8d6f0b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
28 changes: 15 additions & 13 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { isBN } from 'bn.js';

import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, defaultProvider, ec, number, stark } from '../src';
import { Account, Contract, Provider, ec, number, stark } from '../src';
import { toBN } from '../src/utils/number';
import {
compiledErc20,
compiledOpenZeppelinAccount,
compiledTestDapp,
getTestAccount,
getTestProvider,
} from './fixtures';

describe('deploy and test Wallet', () => {
const account: Account = getTestAccount();
const provider = getTestProvider();
let erc20: Contract;
let erc20Address: string;
let dapp: Contract;

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

const erc20Response = await defaultProvider.deployContract({
const erc20Response = await provider.deployContract({
contract: compiledErc20,
});
erc20Address = erc20Response.address;
erc20 = new Contract(compiledErc20.abi, erc20Address);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');

await defaultProvider.waitForTransaction(erc20Response.transaction_hash);
await provider.waitForTransaction(erc20Response.transaction_hash);

const mintResponse = await account.execute({
contractAddress: erc20Address,
Expand All @@ -36,15 +38,15 @@ describe('deploy and test Wallet', () => {

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

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

const dappResponse = await defaultProvider.deployContract({
const dappResponse = await provider.deployContract({
contract: compiledTestDapp,
});
dapp = new Contract(compiledTestDapp.abi, dappResponse.address);
expect(dappResponse.code).toBe('TRANSACTION_RECEIVED');

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

test('estimate fee', async () => {
Expand All @@ -71,7 +73,7 @@ describe('deploy and test Wallet', () => {
});

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

test('read balance of wallet after transfer', async () => {
Expand All @@ -97,7 +99,7 @@ describe('deploy and test Wallet', () => {
);

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

test('execute multiple transactions', async () => {
Expand All @@ -115,7 +117,7 @@ describe('deploy and test Wallet', () => {
]);

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

const response = await dapp.get_number(account.address);
expect(toBN(response.number as string).toString()).toStrictEqual('57');
Expand All @@ -134,14 +136,14 @@ describe('deploy and test Wallet', () => {
const starkKeyPair = ec.genKeyPair();
const starkKeyPub = ec.getStarkKey(starkKeyPair);

const accountResponse = await defaultProvider.deployContract({
const accountResponse = await provider.deployContract({
contract: compiledOpenZeppelinAccount,
constructorCalldata: [starkKeyPub],
});

await defaultProvider.waitForTransaction(accountResponse.transaction_hash);
await provider.waitForTransaction(accountResponse.transaction_hash);

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

test('read nonce', async () => {
Expand All @@ -167,7 +169,7 @@ describe('deploy and test Wallet', () => {

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

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

test('change from provider to account', async () => {
Expand Down
30 changes: 16 additions & 14 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isBN } from 'bn.js';

import { Account, Contract, ContractFactory, Provider, defaultProvider, stark } from '../src';
import { Account, Contract, ContractFactory, Provider, stark } from '../src';
import { getSelectorFromName } from '../src/utils/hash';
import { BigNumberish, toBN } from '../src/utils/number';
import { compileCalldata } from '../src/utils/stark';
Expand All @@ -9,37 +9,39 @@ import {
compiledMulticall,
compiledTypeTransformation,
getTestAccount,
getTestProvider,
} from './fixtures';

describe('class Contract {}', () => {
const wallet = stark.randomAddress();
const provider = getTestProvider();

describe('Basic Interaction', () => {
let erc20: Contract;
let contract: Contract;

beforeAll(async () => {
const { code, transaction_hash, address } = await defaultProvider.deployContract({
const { code, transaction_hash, address } = await provider.deployContract({
contract: compiledErc20,
});
erc20 = new Contract(compiledErc20.abi, address, defaultProvider);
erc20 = new Contract(compiledErc20.abi, address, provider);
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);
await provider.waitForTransaction(transaction_hash);
// Deploy Multicall

const {
code: m_code,
transaction_hash: m_transaction_hash,
address: multicallAddress,
} = await defaultProvider.deployContract({
} = await provider.deployContract({
contract: compiledMulticall,
});

contract = new Contract(compiledMulticall.abi, multicallAddress);

expect(m_code).toBe('TRANSACTION_RECEIVED');

await defaultProvider.waitForTransaction(m_transaction_hash);
await provider.waitForTransaction(m_transaction_hash);
});

test('populate transaction for initial balance of that account', async () => {
Expand Down Expand Up @@ -89,12 +91,12 @@ describe('class Contract {}', () => {
let contract: Contract;

beforeAll(async () => {
const { code, transaction_hash, address } = await defaultProvider.deployContract({
const { code, transaction_hash, address } = await provider.deployContract({
contract: compiledTypeTransformation,
});
contract = new Contract(compiledTypeTransformation.abi, address, defaultProvider);
contract = new Contract(compiledTypeTransformation.abi, address, provider);
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);
await provider.waitForTransaction(transaction_hash);
});

describe('Request Type Transformation', () => {
Expand Down Expand Up @@ -199,13 +201,13 @@ describe('class Contract {}', () => {
let erc20Address: string;

beforeAll(async () => {
const erc20Response = await defaultProvider.deployContract({
const erc20Response = await provider.deployContract({
contract: compiledErc20,
});
erc20Address = erc20Response.address;
erc20 = new Contract(compiledErc20.abi, erc20Address, defaultProvider);
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(erc20Response.transaction_hash);
await provider.waitForTransaction(erc20Response.transaction_hash);
});

test('read balance of wallet', async () => {
Expand All @@ -232,11 +234,11 @@ describe('class Contract {}', () => {
describe('class ContractFactory {}', () => {
let erc20Address: string;
beforeAll(async () => {
const { code, transaction_hash, address } = await defaultProvider.deployContract({
const { code, transaction_hash, address } = await provider.deployContract({
contract: compiledErc20,
});
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTransaction(transaction_hash);
await provider.waitForTransaction(transaction_hash);
erc20Address = address;
});
test('deployment of new contract', async () => {
Expand Down
5 changes: 5 additions & 0 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export const getTestAccount = () => {

return new Account(defaultProvider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
};

export const getTestProvider = () => {
// Will support both local and remote providers in the future
return defaultProvider;
};

0 comments on commit 8d6f0b9

Please sign in to comment.