Skip to content

Commit

Permalink
fix(tests): refactor udc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Nov 11, 2022
1 parent 79641b3 commit 01a6eef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
38 changes: 38 additions & 0 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { isBN } from 'bn.js';
import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, number, stark } from '../src';
import { toBN } from '../src/utils/number';
import { encodeShortString } from '../src/utils/shortString';
import { randomAddress } from '../src/utils/stark';
import {
IS_DEVNET,
compiledErc20,
compiledTestDapp,
erc20ClassHash,
getERC20DeployPayload,
getTestAccount,
getTestProvider,
Expand Down Expand Up @@ -155,4 +159,38 @@ describe('deploy and test Wallet', () => {
expect(declareTx.class_hash).toBeDefined();
});
});

describe('Declare and UDC Deploy Flow', () => {
test('ERC20 Declare', async () => {
const declareTx = await account.declare({
classHash: erc20ClassHash,
contract: compiledErc20,
});

await provider.waitForTransaction(declareTx.transaction_hash);

expect(declareTx).toHaveProperty('class_hash');
expect(declareTx.class_hash).toEqual(erc20ClassHash);
});

test('UDC Deploy', async () => {
const salt = randomAddress(); // use random salt

const deployment = await account.deploy({
classHash: erc20ClassHash,
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
account.address,
],
salt,
unique: true, // Using true here so as not to clash with normal erc20 deploy in account and provider test
isDevnet: IS_DEVNET,
});

await provider.waitForTransaction(deployment.transaction_hash);

expect(deployment).toHaveProperty('transaction_hash');
});
});
});
32 changes: 15 additions & 17 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BlockNumber, GetBlockResponse, stark } from '../src';
import { toBN } from '../src/utils/number';
import { getERC20DeployPayload, getTestProvider } from './fixtures';
import { erc20ClassHash, getERC20DeployPayload, getTestProvider } from './fixtures';

const { compileCalldata } = stark;

const testProvider = getTestProvider();

describe('defaultProvider', () => {
let exampleTransactionHash: string;
let exampleContractAddress: string;
let erc20ContractAddress: string;

let exampleBlock: GetBlockResponse;
let exampleBlockNumber: BlockNumber;
Expand All @@ -23,7 +23,7 @@ describe('defaultProvider', () => {
);
await testProvider.waitForTransaction(transaction_hash);
exampleTransactionHash = transaction_hash;
exampleContractAddress = contract_address;
erc20ContractAddress = contract_address;

exampleBlock = await testProvider.getBlock('latest');
exampleBlockHash = exampleBlock.block_hash;
Expand All @@ -32,7 +32,7 @@ describe('defaultProvider', () => {

describe('endpoints', () => {
test('deployContract()', () => {
expect(exampleContractAddress).toBeTruthy();
expect(erc20ContractAddress).toBeTruthy();
expect(exampleTransactionHash).toBeTruthy();
});

Expand Down Expand Up @@ -75,43 +75,41 @@ describe('defaultProvider', () => {
});

test('getNonce()', async () => {
const nonce = await testProvider.getNonce(exampleContractAddress);
const nonce = await testProvider.getNonce(erc20ContractAddress);
return expect(nonce).toEqual('0x0');
});

test('getClassAt(contractAddress, blockNumber="latest")', async () => {
const classResponse = await testProvider.getClassAt(exampleContractAddress);
const classResponse = await testProvider.getClassAt(erc20ContractAddress);

expect(classResponse).toHaveProperty('program');
expect(classResponse).toHaveProperty('entry_points_by_type');
});

// TODO see if feasible to split
describe('getClassHashAt & GetClass', () => {
describe(' GetClass', () => {
test('responses', async () => {
const classHash = await testProvider.getClassHashAt(exampleContractAddress);
expect(typeof classHash).toBe('string');

const classResponse = await testProvider.getClass(classHash);
const classResponse = await testProvider.getClassByHash(erc20ClassHash);
expect(classResponse).toHaveProperty('program');
expect(classResponse).toHaveProperty('entry_points_by_type');
expect(classResponse).toHaveProperty('abi');
});
});

describe('getStorageAt', () => {
test('with "key" type of number', () => {
return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
return expect(testProvider.getStorageAt(erc20ContractAddress, 0)).resolves.not.toThrow();
});

test('"key" type of string', () => {
return expect(
testProvider.getStorageAt(exampleContractAddress, '0x0')
testProvider.getStorageAt(erc20ContractAddress, '0x0')
).resolves.not.toThrow();
});

test('with "key" type of BN', () => {
return expect(
testProvider.getStorageAt(exampleContractAddress, toBN('0x0'))
testProvider.getStorageAt(erc20ContractAddress, toBN('0x0'))
).resolves.not.toThrow();
});
});
Expand All @@ -135,7 +133,7 @@ describe('defaultProvider', () => {
test('callContract()', () => {
return expect(
testProvider.callContract({
contractAddress: exampleContractAddress,
contractAddress: erc20ContractAddress,
entrypoint: 'balanceOf',
calldata: compileCalldata({
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
Expand All @@ -148,7 +146,7 @@ describe('defaultProvider', () => {
return expect(
testProvider
.callContract({
contractAddress: exampleContractAddress,
contractAddress: erc20ContractAddress,
entrypoint: 'balanceOf',
calldata: compileCalldata({
user: wallet,
Expand All @@ -163,7 +161,7 @@ describe('defaultProvider', () => {
test('callContract() - gateway error', async () => {
return expect(
testProvider.callContract({
contractAddress: exampleContractAddress,
contractAddress: erc20ContractAddress,
entrypoint: 'non_existent_entrypoint',
calldata: compileCalldata({
user: '0xdeadbeef',
Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export const describeIfSequencer = describeIf(IS_DEVNET);
export const describeIfRpc = describeIf(IS_RPC);
export const describeIfNotDevnet = describeIf(!IS_DEVNET);

export const erc20ClassHash = '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a';

export const getERC20DeployPayload = (recipient: string): DeployContractPayload => {
return {
contract: compiledErc20,
Expand Down
41 changes: 0 additions & 41 deletions __tests__/udc.test.ts

This file was deleted.

0 comments on commit 01a6eef

Please sign in to comment.