Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new transaction test cases #35

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
"@hapi/sntp": "^4.0.0",
"@shardus/archiver-discovery": "1.1.0",
"@shardus/crypto-utils": "4.1.3",
"axios": "1.6.1",
"axios": "^1.6.1",
"better-sqlite3": "7.6.2",
"body-parser": "1.19.0",
"connect": "3.7.0",
"cookie-parser": "1.4.6",
"cors": "2.8.5",
"eth-rpc-errors": "4.0.3",
"ethereumjs-tx": "^2.1.2",
"ethereumjs-util": "7.1.3",
"execa": "^5.1.1",
"express": "4.17.2",
Expand Down
123 changes: 123 additions & 0 deletions src/__tests__/integration/ethCall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import request from 'supertest';
import { extendedServer } from '../../server';

describe('JSON-RPC Methods - eth_call', () => {
it('should execute eth_call and return the result', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
id: 1,
method: 'eth_call',
params: [{
from: '0x1923A1Eb8e4dA49604aFfd34De1B478580cf8698',
to: '0xC5223533feB845fD28717A7813a72af4df5f2751',
gas: '0x5208',
gasPrice: '0x09184e72a000',
value: '0x0',
data: '0x70a08231000000000000000000000000d46e8dd67c5d32be8058bb8eb970870f07244567'
}, 'latest']
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(1);
expect(response.body.result).toBeDefined();
expect(response.body.result).toMatch(/^0x[0-9a-fA-F]*$/);
});

it('should return an error if jsonrpc property is missing', async () => {
const response = await request(extendedServer)
.post('/')
.send({
id: 1,
method: 'eth_call',
params: [{
from: '0x1923A1Eb8e4dA49604aFfd34De1B478580cf8698',
to: '0xC5223533feB845fD28717A7813a72af4df5f2751',
gas: '0x5208',
gasPrice: '0x09184e72a000',
value: '0x0',
data: '0x70a08231000000000000000000000000d46e8dd67c5d32be8058bb8eb970870f07244567'
}, 'latest']
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(null);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32600);
expect(response.body.error.message).toBe('Invalid request');
});

it('should return no response if id property is missing', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
method: 'eth_call',
params: [{
from: '0x1923A1Eb8e4dA49604aFfd34De1B478580cf8698',
to: '0xC5223533feB845fD28717A7813a72af4df5f2751',
gas: '0x5208',
gasPrice: '0x09184e72a000',
value: '0x0',
data: '0x70a08231000000000000000000000000d46e8dd67c5d32be8058bb8eb970870f07244567'
}, 'latest']
});

expect(response.status).toBe(204);
});

it('should return an error if to property is missing', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
id: 1,
method: 'eth_call',
params: [{
from: '0x1923A1Eb8e4dA49604aFfd34De1B478580cf8698',
gas: '0x5208',
gasPrice: '0x09184e72a000',
value: '0x0',
data: '0x70a08231000000000000000000000000d46e8dd67c5d32be8058bb8eb970870f07244567'
}, 'latest']
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(1);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32602);
expect(response.body.error.message).toBe(`Invalid params: 'to' or 'data' not provided`);
});

it('should return an error if data property is not provided', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
id: 1,
method: 'eth_call',
params: [{
from: '0x1923A1Eb8e4dA49604aFfd34De1B478580cf8698',
to: '0xC5223533feB845fD28717A7813a72af4df5f2751',
gas: '0x5208',
gasPrice: '0x09184e72a000',
value: '0x0'
}, 'latest']
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(1);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32602);
expect(response.body.error.message).toBe(`Invalid params: 'to' or 'data' not provided`);
});
});
52 changes: 52 additions & 0 deletions src/__tests__/integration/ethSendRawTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import request from 'supertest';
import { extendedServer } from '../../server';

describe('JSON-RPC Methods - eth_sendRawTransaction', () => {
it('should send a valid raw transaction and return the transaction hash', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
id: 1,
method: 'eth_sendRawTransaction',
params: ['0xf86c808609184e72a00082520894c5223533feb845fd28717a7813a72af4df5f2751872386f26fc100008026a0126b583b8b05b1b2a3b548fd08553769d365b833ca980d4d7ccf6ba84458353ea031470ab76cd14c1725c58eb947a69a285186714894d632655718a35fa0435231'] // This is a valid raw transaction, replace with your own
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(1);
expect(response.body.result).toMatch(/^0x[0-9a-fA-F]+$/);
console.log('Transaction hash:', response.body.result);
});

it('should return an error if jsonrpc property is missing', async () => {
const response = await request(extendedServer)
.post('/')
.send({
id: 1,
method: 'eth_sendRawTransaction',
params: ['0xf86c808609184e72a00082520894c5223533feb845fd28717a7813a72af4df5f2751872386f26fc100008026a0126b583b8b05b1b2a3b548fd08553769d365b833ca980d4d7ccf6ba84458353ea031470ab76cd14c1725c58eb947a69a285186714894d632655718a35fa0435231'] // This is a valid raw transaction, replace with your own
});

expect(response.status).toBe(200);
expect(response.body).toBeDefined();
expect(response.body.jsonrpc).toBe('2.0');
expect(response.body.id).toBe(null);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32600);
expect(response.body.error.message).toBe('Invalid request');
});

it('should return no response if id property is missing', async () => {
const response = await request(extendedServer)
.post('/')
.send({
jsonrpc: '2.0',
method: 'eth_sendRawTransaction',
params: ['0xf86c808609184e72a00082520894c5223533feb845fd28717a7813a72af4df5f2751872386f26fc100008026a0126b583b8b05b1b2a3b548fd08553769d365b833ca980d4d7ccf6ba84458353ea031470ab76cd14c1725c58eb947a69a285186714894d632655718a35fa0435231'] // This is a valid raw transaction, replace with your own
});
expect(response.status).toBe(204);
});
});

Loading