Skip to content

Commit

Permalink
Fix handling of nameless parameters, add more testing (#26)
Browse files Browse the repository at this point in the history
* Nit

* Add sinon as an optional peer dependency too use the stubs

* Fix friendlyToArray for empty name params

* Add testing for utils and use `describe` to format output

* Add changeset
  • Loading branch information
ryangoree authored Feb 14, 2024
1 parent f4ea043 commit 9db4f0f
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 181 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-news-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/evm-client": patch
---

Fix argument handling for parameters that have empty strings as names
6 changes: 6 additions & 0 deletions packages/evm-client-viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@
"watch": "tsup --watch"
},
"peerDependencies": {
"sinon": "^17.0.1",
"viem": "^2"
},
"peerDependenciesMeta": {
"sinon": {
"optional": true
}
},
"dependencies": {
"@delvtech/evm-client": "0.0.6"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/evm-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,19 @@
"scripts": {
"build": "tsup",
"lint": "eslint .",
"test:watch": "vitest",
"test:watch": "vitest --reporter=verbose",
"test": "vitest run",
"typecheck": "tsc --noEmit",
"watch": "tsup --watch"
},
"peerDependencies": {
"sinon": "^17.0.1"
},
"peerDependenciesMeta": {
"sinon": {
"optional": true
}
},
"dependencies": {
"lru-cache": "^10.0.1"
},
Expand Down
20 changes: 10 additions & 10 deletions packages/evm-client/src/base/testing/IERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const IERC20 = {
constant: false,
inputs: [
{
name: '_spender',
name: 'spender',
type: 'address',
},
{
name: '_value',
name: 'value',
type: 'uint256',
},
],
Expand Down Expand Up @@ -55,15 +55,15 @@ export const IERC20 = {
constant: false,
inputs: [
{
name: '_from',
name: 'from',
type: 'address',
},
{
name: '_to',
name: 'to',
type: 'address',
},
{
name: '_value',
name: 'value',
type: 'uint256',
},
],
Expand Down Expand Up @@ -96,7 +96,7 @@ export const IERC20 = {
constant: true,
inputs: [
{
name: '_owner',
name: 'owner',
type: 'address',
},
],
Expand Down Expand Up @@ -129,11 +129,11 @@ export const IERC20 = {
constant: false,
inputs: [
{
name: '_to',
name: 'to',
type: 'address',
},
{
name: '_value',
name: 'value',
type: 'uint256',
},
],
Expand All @@ -152,11 +152,11 @@ export const IERC20 = {
constant: true,
inputs: [
{
name: '_owner',
name: 'owner',
type: 'address',
},
{
name: '_spender',
name: 'spender',
type: 'address',
},
],
Expand Down
152 changes: 77 additions & 75 deletions packages/evm-client/src/contract/factories/CachedReadContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,102 @@ import { IERC20 } from 'src/base/testing/IERC20';
import { ALICE, BOB } from 'src/base/testing/accounts';
import { ReadContractStub } from 'src/contract/stubs/ReadContractStub';
import { Event } from 'src/contract/types/Event';
import { expect, test } from 'vitest';
import { describe, expect, it } from 'vitest';
import { createCachedReadContract } from './createCachedReadContract';

const ERC20ABI = IERC20.abi;

test('It caches the read function', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });
describe('createCachedReadContract', () => {
it('caches the read function', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });

const stubbedValue = '0x123abc';
contract.stubRead({
functionName: 'name',
value: stubbedValue,
});
const stubbedValue = '0x123abc';
contract.stubRead({
functionName: 'name',
value: stubbedValue,
});

const value = await cachedContract.read('name');
expect(value).toBe(stubbedValue);
const value = await cachedContract.read('name');
expect(value).toBe(stubbedValue);

const value2 = await cachedContract.read('name');
expect(value2).toBe(stubbedValue);
const value2 = await cachedContract.read('name');
expect(value2).toBe(stubbedValue);

const stub = contract.getReadStub('name');
expect(stub?.callCount).toBe(1);
});
const stub = contract.getReadStub('name');
expect(stub?.callCount).toBe(1);
});

test('It caches the getEvents function', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });

const stubbedEvents: Event<typeof ERC20ABI, 'Transfer'>[] = [
{
eventName: 'Transfer',
args: {
from: ALICE,
to: BOB,
value: 100n,
it('caches the getEvents function', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });

const stubbedEvents: Event<typeof ERC20ABI, 'Transfer'>[] = [
{
eventName: 'Transfer',
args: {
from: ALICE,
to: BOB,
value: 100n,
},
blockNumber: 1n,
data: '0x123abc',
transactionHash: '0x123abc',
},
blockNumber: 1n,
data: '0x123abc',
transactionHash: '0x123abc',
},
];
contract.stubEvents('Transfer', stubbedEvents);
];
contract.stubEvents('Transfer', stubbedEvents);

const events = await cachedContract.getEvents('Transfer');
expect(events).toBe(stubbedEvents);

const events2 = await cachedContract.getEvents('Transfer');
expect(events2).toBe(stubbedEvents);

const stub = contract.getEventsStub('Transfer');
expect(stub?.callCount).toBe(1);
});
const events = await cachedContract.getEvents('Transfer');
expect(events).toBe(stubbedEvents);

test('The deleteRead function deletes the cached read value', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });
const events2 = await cachedContract.getEvents('Transfer');
expect(events2).toBe(stubbedEvents);

const stubbedValue = 100n;
contract.stubRead({ functionName: 'balanceOf', value: stubbedValue });
const stub = contract.getEventsStub('Transfer');
expect(stub?.callCount).toBe(1);
});

const value = await cachedContract.read('balanceOf', '0x123abc');
expect(value).toBe(stubbedValue);
it('deletes cached reads', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });

cachedContract.deleteRead('balanceOf', '0x123abc');
const stubbedValue = 100n;
contract.stubRead({ functionName: 'balanceOf', value: stubbedValue });

const value2 = await cachedContract.read('balanceOf', '0x123abc');
expect(value2).toBe(stubbedValue);
const value = await cachedContract.read('balanceOf', '0x123abc');
expect(value).toBe(stubbedValue);

const stub = contract.getReadStub('balanceOf');
expect(stub?.callCount).toBe(2);
});
cachedContract.deleteRead('balanceOf', '0x123abc');

test('It clears the cache', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });
const value2 = await cachedContract.read('balanceOf', '0x123abc');
expect(value2).toBe(stubbedValue);

contract.stubRead({ functionName: 'balanceOf', value: 100n });
const stubbedValue = '0x123abc';
contract.stubRead({
functionName: 'name',
value: stubbedValue,
const stub = contract.getReadStub('balanceOf');
expect(stub?.callCount).toBe(2);
});
2;
await cachedContract.read('balanceOf', '0x123abc');
await cachedContract.read('name');

cachedContract.clearCache();

await cachedContract.read('balanceOf', '0x123abc');
await cachedContract.read('name');

const stubA = contract.getReadStub('balanceOf');
const stubB = contract.getReadStub('name');
expect(stubA?.callCount).toBe(2);
expect(stubB?.callCount).toBe(2);
it('clears the cache', async () => {
const contract = new ReadContractStub(ERC20ABI);
const cachedContract = createCachedReadContract({ contract });

contract.stubRead({ functionName: 'balanceOf', value: 100n });
const stubbedValue = '0x123abc';
contract.stubRead({
functionName: 'name',
value: stubbedValue,
});
2;
await cachedContract.read('balanceOf', '0x123abc');
await cachedContract.read('name');

cachedContract.clearCache();

await cachedContract.read('balanceOf', '0x123abc');
await cachedContract.read('name');

const stubA = contract.getReadStub('balanceOf');
const stubB = contract.getReadStub('name');
expect(stubA?.callCount).toBe(2);
expect(stubB?.callCount).toBe(2);
});
});
Loading

0 comments on commit 9db4f0f

Please sign in to comment.