Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Why can't I get a contract address #105

Open
ni1boo opened this issue Nov 18, 2023 · 6 comments
Open

Why can't I get a contract address #105

ni1boo opened this issue Nov 18, 2023 · 6 comments

Comments

@ni1boo
Copy link

ni1boo commented Nov 18, 2023

...
const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();

async function main() {
// http://127.0.0.1:7545
// console.log(process.env.PRIVATE_KEY);
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
// const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf8");
// let wallet = await ethers.Wallet.fromEncryptedJsonSync(
// encryptedJson,
// process.env.PRIVATE_KEY_PASSWORD
// );
// wallet = await wallet.connect(provider);
const abi = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.abi",
"utf8"
);
const binary = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.bin",
"utf8"
);
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("部署中,请稍等...");
const contract = await contractFactory.deploy();
// await contract.deployTransaction.wait(1);
await contract.deploymentTransaction().wait(1);
console.log(Contract Address:${contract.address});
//Get number
const currentFavoriteNumber = await contract.retrieve();
console.log(Current Favorite NUmber: ${currentFavoriteNumber.toString()});
const transactionResponse = await contract.store("7");
const transactionReceipt = await transactionResponse.wait(1);
const updatedFavoriteNumber = await contract.retrieve();
console.log(Updated favorite number is: ${updatedFavoriteNumber});
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
...

Here's my result:
部署中,请稍等...
Contract Address:undefined
Current Favorite NUmber: 0

@ni1boo
Copy link
Author

ni1boo commented Nov 18, 2023

Can you help me?

@ni1boo
Copy link
Author

ni1boo commented Nov 18, 2023

if i this,
...
await contract.deployTransaction.wait(1);
...

will be
...
部署中,请稍等...
TypeError: Cannot read properties of undefined (reading 'wait')
at main (/home/nibo/hh-fcc/ethers-simple-storage-fcc/deploy.js:88:38)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
...

so i want to know how can i do

@oblakov0372
Copy link

if i this, ... await contract.deployTransaction.wait(1); ...

will be ... 部署中,请稍等... TypeError: Cannot read properties of undefined (reading 'wait') at main (/home/nibo/hh-fcc/ethers-simple-storage-fcc/deploy.js:88:38) at processTicksAndRejections (node:internal/process/task_queues:96:5) ...

so i want to know how can i do

i also have this error, contract don't have this property

@oblakov0372
Copy link

if i this, ... await contract.deployTransaction.wait(1); ...

will be ... 部署中,请稍等... TypeError: Cannot read properties of undefined (reading 'wait') at main (/home/nibo/hh-fcc/ethers-simple-storage-fcc/deploy.js:88:38) at processTicksAndRejections (node:internal/process/task_queues:96:5) ...

so i want to know how can i do

here is a answer
const deploymentReceipt = await contract.deploymentTransaction().wait(1);

@ni1boo
Copy link
Author

ni1boo commented Nov 19, 2023

if i this, ... await contract.deployTransaction.wait(1); ...
will be ... 部署中,请稍等... TypeError: Cannot read properties of undefined (reading 'wait') at main (/home/nibo/hh-fcc/ethers-simple-storage-fcc/deploy.js:88:38) at processTicksAndRejections (node:internal/process/task_queues:96:5) ...
so i want to know how can i do

here is a answer const deploymentReceipt = await contract.deploymentTransaction().wait(1);

but i want to get the contract address。
if use deploymentTransaction(),the return is not the contract address,it will only be Contract Address:undefined

@Okpainmo
Copy link

Okpainmo commented Feb 12, 2024

You can get the contract address like so:

const contract = await contractFactory.deploy();
console.log(`Contract address: ${contract.target}`);

or like so:

const transactionReceipt = await contract.deploymentTransaction().wait(1);
console.log(`Contract address: ${transactionReceipt.contractAddress}`);

Preferably the second option.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants