diff --git a/packages/lib-sourcify/src/lib/CheckedContract.ts b/packages/lib-sourcify/src/lib/CheckedContract.ts index 3177e0b5a..b1bb99297 100644 --- a/packages/lib-sourcify/src/lib/CheckedContract.ts +++ b/packages/lib-sourcify/src/lib/CheckedContract.ts @@ -338,7 +338,7 @@ export async function performFetch( hash?: string, fileName?: string ): Promise { - console.log(`Fetching the file ${fileName} from ${url}...`); + console.log(`Fetching the file ${fileName} from ${url}`); const res = await fetchWithTimeout(url, { timeout: FETCH_TIMEOUT }).catch( (err) => { if (err.type === 'aborted') diff --git a/test/chains/deployContracts.js b/test/chains/deployContracts.js index ef3078365..0bcc0bdc1 100644 --- a/test/chains/deployContracts.js +++ b/test/chains/deployContracts.js @@ -1,5 +1,4 @@ const { deployFromPrivateKey } = require("../helpers/helpers"); -const Web3 = require("web3"); const StorageArtifact = require("./sources/shared/1_Storage.json"); const { supportedChainsArray } = require("../../dist/sourcify-chains"); const { program } = require("commander"); diff --git a/test/helpers/helpers.js b/test/helpers/helpers.js index c40f69f56..0685ee29f 100644 --- a/test/helpers/helpers.js +++ b/test/helpers/helpers.js @@ -16,7 +16,7 @@ const unsupportedChain = "3"; // Ropsten async function deployFromAbiAndBytecode(signer, abi, bytecode, args) { const contractFactory = new ContractFactory(abi, bytecode, signer); - console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}}`); + console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}`); const deployment = await contractFactory.deploy(...(args || [])); await deployment.waitForDeployment(); @@ -28,7 +28,6 @@ async function deployFromAbiAndBytecode(signer, abi, bytecode, args) { /** * Creator tx hash is needed for tests. This function returns the tx hash in addition to the contract address. * - * @returns The contract address and the tx hash */ async function deployFromAbiAndBytecodeForCreatorTxHash( signer, @@ -37,16 +36,14 @@ async function deployFromAbiAndBytecodeForCreatorTxHash( args ) { const contractFactory = new ContractFactory(abi, bytecode, signer); - console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}}`); + console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}`); const deployment = await contractFactory.deploy(...(args || [])); await deployment.waitForDeployment(); const contractAddress = await deployment.getAddress(); const creationTx = deployment.deploymentTransaction(); if (!creationTx) { - throw new Error( - `No deployment transaction found for ${contractAddress} in contract folder ${contractFolderPath}` - ); + throw new Error(`No deployment transaction found for ${contractAddress}`); } console.log( `Deployed contract at ${contractAddress} with tx ${creationTx.hash}` @@ -60,7 +57,7 @@ async function deployFromAbiAndBytecodeForCreatorTxHash( async function deployFromPrivateKey(provider, abi, bytecode, privateKey, args) { const signer = new Wallet(privateKey, provider); const contractFactory = new ContractFactory(abi, bytecode, signer); - console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}}`); + console.log(`Deploying contract ${args?.length ? `with args ${args}` : ""}`); const deployment = await contractFactory.deploy(...(args || [])); await deployment.waitForDeployment(); @@ -88,7 +85,7 @@ async function callContractMethod( args ) { const contract = new BaseContract(contractAddress, abi, provider); - const callResponse = await contract.interface[methodName].staticCall(...args); + const callResponse = await contract[methodName].staticCall(...args); return callResponse; } @@ -99,12 +96,11 @@ async function callContractMethodWithTx( abi, contractAddress, methodName, - from, args ) { const contract = new BaseContract(contractAddress, abi, signer); - const txReceipt = await contract.interface[methodName].send(...args); - + const txResponse = await contract[methodName].send(...args); + const txReceipt = await txResponse.wait(); return txReceipt; } diff --git a/test/server.js b/test/server.js index bd33f1083..985dfcb26 100644 --- a/test/server.js +++ b/test/server.js @@ -17,7 +17,6 @@ const { assertLookupAll, } = require("./helpers/assertions"); //const IPFS = require("ipfs-core"); -const { HttpGateway } = require("ipfs-http-gateway"); const ganache = require("ganache"); const chai = require("chai"); const chaiHttp = require("chai-http"); @@ -26,7 +25,6 @@ const util = require("util"); const fs = require("fs"); const rimraf = require("rimraf"); const path = require("path"); -const Web3 = require("web3"); const MAX_FILE_SIZE = require("../dist/config").default.server.maxFileSize; const MAX_SESSION_SIZE = require("../dist/server/controllers/verification/verification.common").MAX_SESSION_SIZE; @@ -38,15 +36,9 @@ const { deployFromAbiAndBytecodeForCreatorTxHash, } = require("./helpers/helpers"); const { deployFromAbiAndBytecode } = require("./helpers/helpers"); +const { JsonRpcProvider } = require("ethers"); chai.use(chaiHttp); -const binaryParser = function (res, cb) { - res.setEncoding("binary"); - res.data = ""; - res.on("data", (chunk) => (res.data += chunk)); - res.on("end", () => cb(null, Buffer.from(res.data, "binary"))); -}; - const EXTENDED_TIME = 20000; // 20 seconds const EXTENDED_TIME_60 = 60000; // 60 seconds @@ -61,7 +53,7 @@ describe("Server", function () { networkId: parseInt(defaultContractChain), }, }); - let localWeb3Provider; + let localSigner; let accounts; let defaultContractAddress; let currentResponse = null; // to log server response when test fails @@ -87,16 +79,16 @@ describe("Server", function () { console.log("Started ganache local server on port " + GANACHE_PORT); - localWeb3Provider = new Web3(`http://localhost:${GANACHE_PORT}`); - accounts = await localWeb3Provider.eth.getAccounts(); - console.log("Initialized web3 provider"); + localSigner = await new JsonRpcProvider( + `http://localhost:${GANACHE_PORT}` + ).getSigner(); + console.log("Initialized Provider"); // Deploy the test contract defaultContractAddress = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, - artifact.bytecode, - accounts[0] + artifact.bytecode ); const promisified = util.promisify(server.app.listen); @@ -163,12 +155,12 @@ describe("Server", function () { it("should input files from existing contract via auxdata ipfs", async () => { const artifacts = require("./testcontracts/Create2/Wallet.json"); + const account = await localSigner.getAddress(); const addressDeployed = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifacts.abi, artifacts.bytecode, - accounts[0], - [accounts[0], accounts[0]] + [account, account] ); const res = await agent @@ -687,10 +679,9 @@ describe("Server", function () { const metadataBuffer = fs.readFileSync(metadataPath); const metadata = JSON.parse(metadataBuffer.toString()); const address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, metadata.output.abi, - bytecode, - accounts[0] + bytecode ); const res = await chai @@ -713,13 +704,13 @@ describe("Server", function () { it("should verify a contract with immutables and save immutable-references.json", async () => { const artifact = require("./testcontracts/WithImmutables/artifact.json"); - const [address] = await deployFromAbiAndBytecodeForCreatorTxHash( - localWeb3Provider, - artifact.abi, - artifact.bytecode, - accounts[0], - [999] - ); + const { contractAddress } = + await deployFromAbiAndBytecodeForCreatorTxHash( + localSigner, + artifact.abi, + artifact.bytecode, + [999] + ); const metadata = require("./testcontracts/WithImmutables/metadata.json"); const sourcePath = path.join( @@ -736,21 +727,27 @@ describe("Server", function () { .request(server.app) .post("/") .send({ - address: address, + address: contractAddress, chain: defaultContractChain, files: { "metadata.json": JSON.stringify(metadata), "WithImmutables.sol": sourceBuffer.toString(), }, }); - assertVerification(null, res, null, address, defaultContractChain); + assertVerification( + null, + res, + null, + contractAddress, + defaultContractChain + ); const isExist = fs.existsSync( path.join( server.repository, "contracts", "full_match", defaultContractChain, - address, + contractAddress, "immutable-references.json" ) ); @@ -759,10 +756,9 @@ describe("Server", function () { it("should return validation error for adding standard input JSON without a compiler version", async () => { const address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, // Storage.sol - artifact.bytecode, - accounts[0] + artifact.bytecode ); const solcJsonPath = path.join( "test", @@ -785,10 +781,9 @@ describe("Server", function () { it("should return validation error for adding standard input JSON without a contract name", async () => { const address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, // Storage.sol - artifact.bytecode, - accounts[0] + artifact.bytecode ); const solcJsonPath = path.join( "test", @@ -811,10 +806,9 @@ describe("Server", function () { it("should verify a contract with Solidity standard input JSON", async () => { const address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, // Storage.sol - artifact.bytecode, - accounts[0] + artifact.bytecode ); const solcJsonPath = path.join( "test", @@ -847,10 +841,9 @@ describe("Server", function () { ); before(async function () { address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, MyToken.abi, MyToken.evm.bytecode.object, - accounts[0], ["Sourcify Hardhat Test", "TEST"] ); console.log(`Contract deployed at ${address}`); @@ -897,13 +890,13 @@ describe("Server", function () { it("should store a contract in /contracts/full_match|partial_match/0xADDRESS despite the files paths in the metadata", async () => { const artifact = require("./testcontracts/Storage/Storage.json"); - const [address] = await deployFromAbiAndBytecodeForCreatorTxHash( - localWeb3Provider, - artifact.abi, - artifact.bytecode, - accounts[0], - [] - ); + const { contractAddress } = + await deployFromAbiAndBytecodeForCreatorTxHash( + localSigner, + artifact.abi, + artifact.bytecode, + [] + ); const metadata = require("./testcontracts/Storage/metadata.upMultipleDirs.json"); const sourcePath = path.join( @@ -919,7 +912,7 @@ describe("Server", function () { .request(server.app) .post("/") .send({ - address: address, + address: contractAddress, chain: defaultContractChain, files: { "metadata.json": JSON.stringify(metadata), @@ -930,7 +923,7 @@ describe("Server", function () { null, res, null, - address, + contractAddress, defaultContractChain, "partial" ); @@ -940,7 +933,7 @@ describe("Server", function () { "contracts", "partial_match", defaultContractChain, - address, + contractAddress, "sources", "..contracts", "Storage.sol" @@ -958,10 +951,9 @@ describe("Server", function () { before(async () => { contractAddress = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, bytecodeMismatchArtifact.abi, - bytecodeMismatchArtifact.bytecode, - accounts[0] + bytecodeMismatchArtifact.bytecode ); }); @@ -1459,13 +1451,13 @@ describe("Server", function () { it("should verify a contract with immutables and save immutable-references.json", async () => { const artifact = require("./testcontracts/WithImmutables/artifact.json"); - const [address] = await deployFromAbiAndBytecodeForCreatorTxHash( - localWeb3Provider, - artifact.abi, - artifact.bytecode, - accounts[0], - [999] - ); + const { contractAddress } = + await deployFromAbiAndBytecodeForCreatorTxHash( + localSigner, + artifact.abi, + artifact.bytecode, + [999] + ); const metadata = require("./testcontracts/WithImmutables/metadata.json"); const metadataBuffer = Buffer.from(JSON.stringify(metadata)); @@ -1487,7 +1479,7 @@ describe("Server", function () { let contracts = assertSingleContractStatus(res1, "error"); - contracts[0].address = address; + contracts[0].address = contractAddress; contracts[0].chainId = defaultContractChain; const res2 = await agent .post("/session/verify-validated") @@ -1500,7 +1492,7 @@ describe("Server", function () { "contracts", "full_match", defaultContractChain, - address, + contractAddress, "immutable-references.json" ) ); @@ -1512,25 +1504,23 @@ describe("Server", function () { const artifact = require("./testcontracts/FactoryImmutable/Factory.json"); const factoryAddress = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, - artifact.bytecode, - accounts[0] + artifact.bytecode ); // Deploy child by calling deploy(uint) const childMetadata = require("./testcontracts/FactoryImmutable/Child_metadata.json"); const childMetadataBuffer = Buffer.from(JSON.stringify(childMetadata)); const txReceipt = await callContractMethodWithTx( - localWeb3Provider, + localSigner, artifact.abi, factoryAddress, "deploy", - accounts[0], [deployValue] ); - const childAddress = txReceipt.events.Deployment.returnValues[0]; + const childAddress = txReceipt.logs[0].args[0]; const sourcePath = path.join( "test", "testcontracts", @@ -1560,25 +1550,23 @@ describe("Server", function () { it("should verify a contract created by a factory contract and has immutables without constructor arguments but with msg.sender assigned immutable", async () => { const artifact = require("./testcontracts/FactoryImmutableWithoutConstrArg/Factory3.json"); const factoryAddress = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, - artifact.bytecode, - accounts[0] + artifact.bytecode ); // Deploy child by calling deploy(uint) const childMetadata = require("./testcontracts/FactoryImmutableWithoutConstrArg/Child3_metadata.json"); const childMetadataBuffer = Buffer.from(JSON.stringify(childMetadata)); const txReceipt = await callContractMethodWithTx( - localWeb3Provider, + localSigner, artifact.abi, factoryAddress, "createChild", - accounts[0], [] ); - const childAddress = txReceipt.events.ChildCreated.returnValues[0]; + const childAddress = txReceipt.logs[0].args[0]; const sourcePath = path.join( "test", "testcontracts", @@ -1625,10 +1613,9 @@ describe("Server", function () { it("should verify a contract with Solidity standard input JSON", async () => { const agent = chai.request.agent(server.app); const address = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, artifact.abi, // Storage.sol - artifact.bytecode, - accounts[0] + artifact.bytecode ); const solcJsonPath = path.join( "test", @@ -1663,10 +1650,9 @@ describe("Server", function () { before(async () => { contractAddress = await deployFromAbiAndBytecode( - localWeb3Provider, + localSigner, bytecodeMismatchArtifact.abi, - bytecodeMismatchArtifact.bytecode, - accounts[0] + bytecodeMismatchArtifact.bytecode ); });