From 3d75e0cefcef6fb84678fe1c2b75399c730ac5e3 Mon Sep 17 00:00:00 2001 From: Wyatt Barnes Date: Mon, 24 Jul 2023 16:53:24 -1000 Subject: [PATCH] Fix yarn nx run remix-simulator:test --- libs/remix-simulator/src/methods/accounts.ts | 6 +- libs/remix-simulator/test/accounts.ts | 12 ++-- libs/remix-simulator/test/blocks.ts | 46 ++++++------- libs/remix-simulator/test/misc.ts | 70 +++++++------------- 4 files changed, 54 insertions(+), 80 deletions(-) diff --git a/libs/remix-simulator/src/methods/accounts.ts b/libs/remix-simulator/src/methods/accounts.ts index ffbfc89a3a..0628809919 100644 --- a/libs/remix-simulator/src/methods/accounts.ts +++ b/libs/remix-simulator/src/methods/accounts.ts @@ -1,19 +1,17 @@ import { privateToAddress, toChecksumAddress, isValidPrivate, Address } from '@ethereumjs/util' import BN from 'bn.js' -const Web3EthAccounts = require('web3-eth-accounts'); +import { privateKeyToAccount } from 'web3-eth-accounts' import * as crypto from 'crypto' export class Web3Accounts { accounts: Record accountsKeys: Record - web3Accounts: any vmContext constructor (vmContext) { this.vmContext = vmContext // TODO: make it random and/or use remix-libs - this.web3Accounts = new Web3EthAccounts() this.accounts = {} this.accountsKeys = {} } @@ -99,7 +97,7 @@ export class Web3Accounts { if (!privateKey) { return cb(new Error('unknown account')) } - const account = this.web3Accounts.privateKeyToAccount(privateKey as string) + const account = privateKeyToAccount(privateKey as string) const data = account.sign(message) diff --git a/libs/remix-simulator/test/accounts.ts b/libs/remix-simulator/test/accounts.ts index 336d438380..cb923e209a 100644 --- a/libs/remix-simulator/test/accounts.ts +++ b/libs/remix-simulator/test/accounts.ts @@ -1,5 +1,5 @@ /* global describe, before, it */ -import Web3 from 'web3' +import Web3, { FMT_BYTES, FMT_NUMBER } from 'web3' import { Provider } from '../src/index' const web3 = new Web3() import * as assert from 'assert' @@ -21,9 +21,9 @@ describe('Accounts', () => { describe('eth_getBalance', () => { it('should get a account balance', async () => { const accounts: string[] = await web3.eth.getAccounts() - const balance0: string = await web3.eth.getBalance(accounts[0]) - const balance1: string = await web3.eth.getBalance(accounts[1]) - const balance2: string = await web3.eth.getBalance(accounts[2]) + const balance0: string = await web3.eth.getBalance(accounts[0], undefined, { number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }) + const balance1: string = await web3.eth.getBalance(accounts[1], undefined, { number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }) + const balance2: string = await web3.eth.getBalance(accounts[2], undefined, { number: FMT_NUMBER.STR, bytes: FMT_BYTES.HEX }) assert.deepEqual(balance0, '100000000000000000000') assert.deepEqual(balance1, '100000000000000000000') @@ -34,9 +34,9 @@ describe('Accounts', () => { describe('eth_sign', () => { it('should sign payloads', async () => { const accounts: string[] = await web3.eth.getAccounts() - const signature: string = await web3.eth.sign('Hello world', accounts[0]) + const signature = await web3.eth.sign(web3.utils.utf8ToHex('Hello world'), accounts[0]) - assert.deepEqual(signature.length, 132) + assert.deepEqual(typeof signature === 'string' ? signature.length : signature.signature.length, 132) }) }) }) diff --git a/libs/remix-simulator/test/blocks.ts b/libs/remix-simulator/test/blocks.ts index 72b454b8dd..08c5fe2e10 100644 --- a/libs/remix-simulator/test/blocks.ts +++ b/libs/remix-simulator/test/blocks.ts @@ -18,23 +18,22 @@ describe('blocks', () => { const block = await web3.eth.getBlock(0) const expectedBlock = { - baseFeePerGas: 1, - difficulty: 0, + baseFeePerGas: '1', + difficulty: '0', extraData: '0x0', - gasLimit: 8000000, - gasUsed: 0, + gasLimit: '8000000', + gasUsed: '0', hash: block.hash.toString(), - logsBloom: '0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331', + logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331', miner: '0x0000000000000000000000000000000000000001', - nonce: '0x0000000000000000', - number: 0, + nonce: '0', + number: '0', parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', - size: 163591, + size: '163591', stateRoot: '0x0000000000000000000000000000000000000000000000000000000000000000', timestamp: block.timestamp, totalDifficulty: '0', - transactions: [], transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', uncles: [] } @@ -94,10 +93,9 @@ describe('blocks', () => { it('should get block given its hash', async () => { const correctBlock = await web3.eth.getBlock(0) const numberTransactions = await (new Promise((resolve, reject) => { - web3['_requestManager'].send({method: 'eth_getUncleCountByBlockHash', params: [correctBlock.hash]}, (err, numberTransactions) => { - if (err) return reject(err) - resolve(numberTransactions) - }) + web3['_requestManager'].send({method: 'eth_getUncleCountByBlockHash', params: [correctBlock.hash]}) + .then(numberTransactions => resolve(numberTransactions)) + .catch(err => reject(err)) })) assert.deepEqual(numberTransactions, correctBlock.uncles.length) }) @@ -107,17 +105,16 @@ describe('blocks', () => { it('should get block given its number', async () => { const correctBlock = await web3.eth.getBlock(0) const numberTransactions = await (new Promise((resolve, reject) => { - web3['_requestManager'].send({method: 'eth_getUncleCountByBlockHash', params: [0]}, (err, numberTransactions) => { - if (err) return reject(err) - resolve(numberTransactions) - }) + web3['_requestManager'].send({method: 'eth_getUncleCountByBlockHash', params: [0]}) + .then(numberTransactions => resolve(numberTransactions)) + .catch(err => reject(err)) })) assert.deepEqual(numberTransactions, correctBlock.uncles.length) }) }) describe('eth_getStorageAt', () => { it('should get storage at position at given address', async () => { - const abi: any = [ + const abi = [ { 'constant': false, 'inputs': [ @@ -197,16 +194,16 @@ describe('blocks', () => { 'stateMutability': 'view', 'type': 'function' } - ] + ] as const const code = '0x608060405234801561001057600080fd5b506040516020806102018339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506101a9806100586000396000f3fe60806040526004361061005c576000357c0100000000000000000000000000000000000000000000000000000000900480632a1afcd91461006157806360fe47b11461008c5780636d4ce63c146100c7578063ce01e1ec146100f2575b600080fd5b34801561006d57600080fd5b5061007661012d565b6040518082815260200191505060405180910390f35b34801561009857600080fd5b506100c5600480360360208110156100af57600080fd5b8101908080359060200190929190505050610133565b005b3480156100d357600080fd5b506100dc61013d565b6040518082815260200191505060405180910390f35b3480156100fe57600080fd5b5061012b6004803603602081101561011557600080fd5b8101908080359060200190929190505050610146565b005b60005481565b8060008190555050565b60008054905090565b80600081905550807f63a242a632efe33c0e210e04e4173612a17efa4f16aa4890bc7e46caece80de060405160405180910390a25056fea165627a7a7230582063160eb16dc361092a85ced1a773eed0b63738b83bea1e1c51cf066fa90e135d0029' const contract = new web3.eth.Contract(abi) const accounts = await web3.eth.getAccounts() - const contractInstance: any = await contract.deploy({ data: code, arguments: [100] }).send({ from: accounts[0], gas: 400000 }) + const contractInstance: any = await contract.deploy({ data: code, arguments: [100] }).send({ from: accounts[0], gas: '400000' }) contractInstance.currentProvider = web3.eth.currentProvider - contractInstance.givenProvider = web3.eth.currentProvider + // contractInstance.givenProvider = web3.eth.currentProvider await contractInstance.methods.set(100).send({ from: accounts[0].toLowerCase(), gas: 400000 }) let storage = await web3.eth.getStorageAt(contractInstance.options.address, 0) @@ -223,7 +220,7 @@ describe('blocks', () => { }) describe('eth_call', () => { it('should get a value', async () => { - const abi: any = [ + const abi = [ { 'constant': false, 'inputs': [ @@ -303,16 +300,15 @@ describe('blocks', () => { 'stateMutability': 'view', 'type': 'function' } - ] + ] as const const code = '0x608060405234801561001057600080fd5b506040516020806102018339810180604052602081101561003057600080fd5b810190808051906020019092919050505080600081905550506101a9806100586000396000f3fe60806040526004361061005c576000357c0100000000000000000000000000000000000000000000000000000000900480632a1afcd91461006157806360fe47b11461008c5780636d4ce63c146100c7578063ce01e1ec146100f2575b600080fd5b34801561006d57600080fd5b5061007661012d565b6040518082815260200191505060405180910390f35b34801561009857600080fd5b506100c5600480360360208110156100af57600080fd5b8101908080359060200190929190505050610133565b005b3480156100d357600080fd5b506100dc61013d565b6040518082815260200191505060405180910390f35b3480156100fe57600080fd5b5061012b6004803603602081101561011557600080fd5b8101908080359060200190929190505050610146565b005b60005481565b8060008190555050565b60008054905090565b80600081905550807f63a242a632efe33c0e210e04e4173612a17efa4f16aa4890bc7e46caece80de060405160405180910390a25056fea165627a7a7230582063160eb16dc361092a85ced1a773eed0b63738b83bea1e1c51cf066fa90e135d0029' const contract = new web3.eth.Contract(abi) const accounts = await web3.eth.getAccounts() - const contractInstance: any = await contract.deploy({ data: code, arguments: [100] }).send({ from: accounts[0], gas: 400000 }) + const contractInstance: any = await contract.deploy({ data: code, arguments: [100] }).send({ from: accounts[0], gas: '400000' }) contractInstance.currentProvider = web3.eth.currentProvider - contractInstance.givenProvider = web3.eth.currentProvider const value = await contractInstance.methods.get().call({ from: accounts[0] }) assert.deepEqual(value, 100) diff --git a/libs/remix-simulator/test/misc.ts b/libs/remix-simulator/test/misc.ts index a339910996..785cc3dd55 100644 --- a/libs/remix-simulator/test/misc.ts +++ b/libs/remix-simulator/test/misc.ts @@ -12,26 +12,21 @@ describe('Misc', () => { }) describe('web3_clientVersion', () => { - it('should get correct remix simulator version', async (done) => { - web3['_requestManager'].send({ method: 'web3_clientVersion', params: [] }, (err, version) => { - if (err) { - throw new Error(err) - } - const remixVersion = require('../package.json').version - assert.equal(version, 'Remix Simulator/' + remixVersion) - done() - }) + it('should get correct remix simulator version', async () => { + web3['_requestManager'].send({ method: 'web3_clientVersion', params: [] }) + .then(version => { + const remixVersion = require('../package.json').version + assert.equal(version, 'Remix Simulator/' + remixVersion) + }) + .catch(err => { throw new Error(err) }) }) }) describe('eth_protocolVersion', () => { it('should get protocol version', async () => { - web3['_requestManager'].send({ method: 'eth_protocolVersion', params: [] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, '0x3f') - }) + web3['_requestManager'].send({ method: 'eth_protocolVersion', params: [] }) + .then(result => assert.equal(result, '0x3f')) + .catch(err => { throw new Error(err) }) }) }) @@ -58,56 +53,41 @@ describe('Misc', () => { describe('web3_sha3', () => { it('should get result of a sha3', async () => { - web3['_requestManager'].send({ method: 'web3_sha3', params: ['0x68656c6c6f20776f726c64'] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad') - }) + web3['_requestManager'].send({ method: 'web3_sha3', params: ['0x68656c6c6f20776f726c64'] }) + .then(result => assert.equal(result, '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad')) + .catch(err => { throw new Error(err)} ) }) }) describe('eth_getCompilers', () => { it('should get list of compilers', async () => { - web3['_requestManager'].send({ method: 'eth_getCompilers', params: [] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, 0) - }) + web3['_requestManager'].send({ method: 'eth_getCompilers', params: [] }) + .then(result => assert.equal(result, 0)) + .catch(err => { throw new Error(err) }) }) }) describe('eth_compileSolidity', () => { it('get unsupported result when requesting solidity compiler', async () => { - web3['_requestManager'].send({ method: 'eth_compileSolidity', params: [] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, 'unsupported') - }) + web3['_requestManager'].send({ method: 'eth_compileSolidity', params: [] }) + .then(result => assert.equal(result, 'unsupported')) + .catch(err => { throw new Error(err) }) }) }) describe('eth_compileLLL', () => { it('get unsupported result when requesting LLL compiler', async () => { - web3['_requestManager'].send({ method: 'eth_compileLLL', params: [] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, 'unsupported') - }) + web3['_requestManager'].send({ method: 'eth_compileLLL', params: [] }) + .then(result => assert.equal(result, 'unsupported')) + .catch(err => { throw new Error(err) }) }) }) describe('eth_compileSerpent', () => { it('get unsupported result when requesting serpent compiler', async () => { - web3['_requestManager'].send({ method: 'eth_compileSerpent', params: [] }, (err, result) => { - if (err) { - throw new Error(err) - } - assert.equal(result, 'unsupported') - }) + web3['_requestManager'].send({ method: 'eth_compileSerpent', params: [] }) + .then(result => assert.equal(result, 'unsupported')) + .catch(err => { throw new Error(err)} ) }) }) })