diff --git a/src/__tests__/integration/ethAccounts.test.ts b/src/__tests__/integration/ethAccounts.test.ts new file mode 100644 index 00000000..d7f27a6e --- /dev/null +++ b/src/__tests__/integration/ethAccounts.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_accounts', () => { + it('should return an array of eth accounts', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_accounts", + params: ["", "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toContain('0x407d73d8a49eeb85d32cf465507dd71d507100c1'); + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethBlockNumber.test.ts b/src/__tests__/integration/ethBlockNumber.test.ts new file mode 100644 index 00000000..2982bdc8 --- /dev/null +++ b/src/__tests__/integration/ethBlockNumber.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_blockNumber', () => { + it('should return the block number', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_blockNumber", + params: ["", "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toMatch(/^0x[0-9a-fA-F]+$/); + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/eth_chainId.test.ts b/src/__tests__/integration/ethChainId.test.ts similarity index 100% rename from src/__tests__/integration/eth_chainId.test.ts rename to src/__tests__/integration/ethChainId.test.ts diff --git a/src/__tests__/integration/ethCoinbase.test.ts b/src/__tests__/integration/ethCoinbase.test.ts new file mode 100644 index 00000000..a9134f08 --- /dev/null +++ b/src/__tests__/integration/ethCoinbase.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_coinbase', () => { + it('should return false once not an active node', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_coinbase", + params: ['', "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toBe(''); + }); + }); +}); diff --git a/src/__tests__/integration/ethGasPrice.test.ts b/src/__tests__/integration/ethGasPrice.test.ts index 9f816040..b588117d 100644 --- a/src/__tests__/integration/ethGasPrice.test.ts +++ b/src/__tests__/integration/ethGasPrice.test.ts @@ -1,32 +1,22 @@ - import request from 'supertest'; import { extendedServer } from '../../server'; describe('JSON-RPC Methods', () => { - - describe('eth_gasPrice', () => { it('should return gas price', async () => { - let result = null; - let error = null; - - const callback = (err: any, res: any) => { - error = err; - result = res; - }; - const response = await request(extendedServer) .post('/') .send({ jsonrpc: '2.0', id: 1, method: 'eth_gasPrice', - params: [[], callback], + params: [] }); expect(response.statusCode).toBe(200); expect(response.body).toBeDefined(); - expect(response.body.result).toBe(result); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toMatch(/^0x[0-9a-fA-F]+$/); }); }); -}); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethGetBalance.test.ts b/src/__tests__/integration/ethGetBalance.test.ts index f104cf6b..00f10413 100644 --- a/src/__tests__/integration/ethGetBalance.test.ts +++ b/src/__tests__/integration/ethGetBalance.test.ts @@ -4,7 +4,7 @@ import { extendedServer } from '../../server'; describe('JSON-RPC Methods', () => { describe('eth_getBalance', () => { it('should return the correct balance for a valid address', async () => { - const address = '0xf1a66ee4db3bfec6a4233bd10e587dacdae985a6'; + const address = '0x4a372F3F5cFa12Ce491106BDD82735764ea29D62'; const response = await request(extendedServer) .post('/') .send( @@ -21,20 +21,20 @@ describe('JSON-RPC Methods', () => { expect(response.status).toBe(200); expect(response.body.result).toBeDefined(); - expect(response.body.result).toBe('0x0'); + expect(response.body.result).toMatch(/^0x[0-9a-fA-F]+$/); }); - // it('should return zero if the address has no transactions', async () => { - // const response = await request(extendedServer) - // .post('/') - // .send({ - // jsonrpc: '2.0', - // method: 'eth_getTransactionCount', - // params: ['0x742d35Cc6634C0532925a3b844Bc454e4438f44e', 'latest'], - // id: 4, - // }); - // expect(response.status).toBe(200); - // expect(response.body.result).toBe('0x0'); - // }); + it('should return zero if the address has no transactions', async () => { + const response = await request(extendedServer) + .post('/') + .send({ + jsonrpc: '2.0', + method: 'eth_getTransactionCount', + params: ['0x0D0668F67Ed7Ce0ce7D7AD234020054E9d5995C2', 'latest'], + id: 4, + }); + expect(response.status).toBe(200); + expect(response.body.result).toBe('0x0'); + }); }); }); diff --git a/src/__tests__/integration/ethGetStorageAt.test.ts b/src/__tests__/integration/ethGetStorageAt.test.ts new file mode 100644 index 00000000..4fbdd304 --- /dev/null +++ b/src/__tests__/integration/ethGetStorageAt.test.ts @@ -0,0 +1,26 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_getStorageAt', () => { + it('should return the storage value at a given position', async () => { + const response = await request(extendedServer) + .post('/') + .send({ + jsonrpc: '2.0', + id: 1, + method: 'eth_getStorageAt', + params: [ + '0xb6da4e0870f18247dafc9495652c394c3c8c60b8', + '0x0', + 'latest' + ] + }); + + expect(response.statusCode).toBe(200); + expect(response.body).toBeDefined(); + expect(response.body.result).toMatch(/^0x[0-9a-fA-F]*$/); // Matches any hex string + }); + + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethHashRate.test.ts b/src/__tests__/integration/ethHashRate.test.ts new file mode 100644 index 00000000..16693c43 --- /dev/null +++ b/src/__tests__/integration/ethHashRate.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_hashrate', () => { + it('should return the hashrate', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_hashrate", + params: ['', "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toBe('0x38a'); + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethMining.test.ts b/src/__tests__/integration/ethMining.test.ts new file mode 100644 index 00000000..725ca2fb --- /dev/null +++ b/src/__tests__/integration/ethMining.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_mining', () => { + it('should return mining as true', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_mining", + params: ['', "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toBe(true); + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethProtcolVersion.test.ts b/src/__tests__/integration/ethProtcolVersion.test.ts new file mode 100644 index 00000000..0eb0b7fc --- /dev/null +++ b/src/__tests__/integration/ethProtcolVersion.test.ts @@ -0,0 +1,23 @@ +import request from 'supertest'; +import { extendedServer } from '../../server'; + +describe('JSON-RPC Methods', () => { + describe('eth_protocolVersion', () => { + it('should return the protocol version', async () => { + const response = await request(extendedServer) + .post('/') + .send( + { + method: "eth_protocolVersion", + params: ['', "latest"], + id: 1, + jsonrpc: "2.0" + } + ); + + expect(response.status).toBe(200); + expect(response.body.result).toBeDefined(); + expect(response.body.result).toBe('54'); + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/integration/ethSignTransaction.test.ts b/src/__tests__/integration/ethSignTransaction.test.ts index add03aa0..820d8714 100644 --- a/src/__tests__/integration/ethSignTransaction.test.ts +++ b/src/__tests__/integration/ethSignTransaction.test.ts @@ -1,4 +1,3 @@ -// src/__tests__/integration/api/eth_signTransaction.test.ts import request from 'supertest'; import { extendedServer } from '../../server'; @@ -13,9 +12,9 @@ describe('POST /api/method eth_signTransaction', () => { { from: '0xa4c0aadcce9c04fe8b833279b0198d4ae29d76a7', to: '0x510e84aa16ab92752451a2763352681624c75ebe', - gas: '0x76c0', // 30400 - gasPrice: '0x9184e72a000', // 10000000000000 - value: '0x9184e72a', // 2441406250 + gas: '0x76c0', + gasPrice: '0x9184e72a000', + value: '0x9184e72a', data: '0x0', }, ], @@ -25,6 +24,4 @@ describe('POST /api/method eth_signTransaction', () => { expect(response.body).toHaveProperty('result'); expect(response.body.result).toBe('0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b'); }); - - }); diff --git a/src/__tests__/integration/eth_syncing.test.ts b/src/__tests__/integration/ethSyncing.test.ts similarity index 100% rename from src/__tests__/integration/eth_syncing.test.ts rename to src/__tests__/integration/ethSyncing.test.ts diff --git a/src/__tests__/integration/eth_blockNumber.test.ts b/src/__tests__/integration/eth_blockNumber.test.ts deleted file mode 100644 index 10d3ad03..00000000 --- a/src/__tests__/integration/eth_blockNumber.test.ts +++ /dev/null @@ -1,105 +0,0 @@ -// import { server } from '../../api'; -// import { Client } from 'pg'; - -// describe('eth_blockNumber', () => { -// let client: Client; - -// beforeAll(async () => { -// // Initialize the JSON RPC Server -// await server.start(); - -// // Connect to the local PostgreSQL database -// client = new Client({ -// user: 'your_db_user', -// host: 'localhost', -// database: 'your_db_name', -// password: 'your_db_password', -// port: 5432, -// }); -// await client.connect(); - -// // Ensure the database is populated with some initial data -// await client.query(` -// INSERT INTO blocks (number, hash, parent_hash, timestamp) -// VALUES -// (1, '0x1234abcd', '0x00000000', 1622548800), -// (2, '0x5678efgh', '0x1234abcd', 1622548805), -// (3, '0x9abcijkl', '0x5678efgh', 1622548810) -// ON CONFLICT DO NOTHING -// `); -// }); - -// afterAll(async () => { -// await client.end(); -// await server.stop(); -// }); - -// test('should return the latest block number', async () => { -// const response = await server.handleRequest({ -// jsonrpc: '2.0', -// method: 'eth_blockNumber', -// params: [], -// id: 1, -// }); - -// expect(response.result).toBeDefined(); -// expect(typeof response.result).toBe('string'); -// expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/); - -// // Verify that the block number is the latest one in the database -// const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks'); -// const latestBlockNumber = latestBlock.rows[0].number; - -// // Convert the result to a hexadecimal string -// const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16); - -// // Assert that the returned block number matches the latest block number in the database -// expect(response.result).toBe(expectedBlockNumberHex); -// }); - -// test('should handle no blocks gracefully', async () => { -// // Clear the blocks table -// await client.query('DELETE FROM blocks'); - -// const response = await server.handleRequest({ -// jsonrpc: '2.0', -// method: 'eth_blockNumber', -// params: [], -// id: 2, -// }); - -// expect(response.result).toBeDefined(); -// expect(response.result).toBe('0x0'); -// }); - -// test('should handle non-sequential blocks', async () => { -// // Insert a non-sequential block number -// await client.query(` -// INSERT INTO blocks (number, hash, parent_hash, timestamp) -// VALUES -// (5, '0xlmnopqr', '0x9abcijkl', 1622548815) -// ON CONFLICT DO NOTHING -// `); - -// const response = await server.handleRequest({ -// jsonrpc: '2.0', -// method: 'eth_blockNumber', -// params: [], -// id: 3, -// }); - -// expect(response.result).toBeDefined(); -// expect(typeof response.result).toBe('string'); -// expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/); - -// // Verify that the block number is the latest one in the database -// const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks'); -// const latestBlockNumber = latestBlock.rows[0].number; - -// // Convert the result to a hexadecimal string -// const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16); - -// // Assert that the returned block number matches the latest block number in the database -// expect(response.result).toBe(expectedBlockNumberHex); -// }); -// });