0

etherjs (5.0) is used to deployed a smart contract Hello.sol. Here is the js code (truffle 5.1.52/ganache CLI 6.12.1):

  import { ContractFactory, ethers } from "ethers";

  let privateKey = "0xf43608a210da70d40115ad23526e50f2a502c018aeffd56477fcbe44119b53cc"; //<<==private key from ganache-cli
  let wallet = new ethers.Wallet(privateKey);  //address of the wallet
  console.log("wallet : ", wallet);
  //deploy
  const factory = new ContractFactory(abi, bytecode, signer); //<<==abi, bytecode, signer defined before
  const contract = await factory.deploy(wallet.address);
  console.log("contract deployed : ", contract);
  console.log("contract address : ", contract.address);
  // The transaction that the signer sent to deploy
  contract.deployTransaction;
  // Wait until the transaction is mined
  contract.deployTransaction.wait();

The execution of code above generates the output on ganache-cli -b 3:

  Transaction: 0xbbd7dfd7272cee291a2d1e62ac51e32dd99c98dacb44d04f1ae4bd50487f35b7
  Contract created: 0x03734542501c30e1ea22d6ddcb876e84eaa99381
  Gas usage: 92522
  Block Number: 2594
  Block Time: Thu Nov 12 2020 14:37:36 GMT-0800 (Pacific Standard Time)

The contract is deployed at the address of 0x03734542501c30e1ea22d6ddcb876e84eaa99381.

truffle is started with truffle development. command networks generates the following output:

truffle(development)> networks

Network: develop (id: 5777)
  No contracts deployed.

Why truffle doesn't find the contract deployed above?

1

Truffle stores deployment information in a JSON file in build/ directory.

Contracts deployed by ethers.js or web3.js do not update the JSON file, and truffle is not aware of contracts deployed ouside of their migrations scripts.

It is possible to use those contracts from the truffle console. It is required to have the code compiled by truffle.

Then to use MyContract from the console at address 0x1234123412341234 you can execute

> myContract = await MyContract.at('0x1234123412341234');

To call function makeBet from it.

> await myContract.makeBet(1, 1234, { from: '0x0001', value: '4444' })
  • hello=await Hello.at(0x9a2b80a18afdf621f527c4238febba36ae682aac); throws Uncaught error:Invalid address passed to Hello.at(): 8.801547160932206e+47 at evalmachine.<anonymous>:1:15 at Function.at (C:\D\code\js\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:67:1) The contract address was copied from ganache output. – user938363 Nov 13 '20 at 4:24
  • @user938363 My bad, the address should be a string, for example '0x12341234'. – Ismael Nov 13 '20 at 5:42
  • Works like charm. Many thanks. – user938363 Nov 13 '20 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.