Difference between .getContractAt || .getContract || deployments.get #6004
-
My main concern is not a difference, but the different execution of these functions doesn't allow me to procceed. My code where I have a problem: const { network, ethers } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
const VRF_SUB_FUND_AMOUNT = ethers.parseEther("1")
const BASE_FEE = ethers.parseEther("0.25")
const GAS_PRICE_LINK = 1e9
module.exports = async function ({ getNamedAccounts, deployments }) {
const { deploy, log } = deployments
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2Address, subscriptionId, vrfCoordinatorV2Mock
if (chainId == 31337) {
vrfCoordinatorV2Mock = await ethers.getContractAt("VRFCoordinatorV2Mock", deployer)
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.target
console.log(vrfCoordinatorV2Address)
const transactionResponse = await vrfCoordinatorV2Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait()
subscriptionId = transactionReceipt.logs[0].args.subId
await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT)
} else { The main concern is that (vrfCoordinatorV2Mock.createSubscription()) doesn't emit an event when executed, so logs[] and args are undefined.
On the other hand, fcc script runs just fine with .getContract(""), which takes only 1 param. Another guy uses deployments.get to get his MockV3Aggregator address. But as far as I have tested, the deploymends.get will give you not the contract instance, but the address. I think so, because it doesn't allow me to call any function: vrfCoordinatorV2Mock = await deployments.get("VRFCoordinatorV2Mock", deployer)
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
console.log(vrfCoordinatorV2Address)
const transactionResponse = await vrfCoordinatorV2Mock.createSubscription()
And I am stuck in this position for 4 days, trying to figure out a way with this new cool hardhat update. In case you need it: {
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
"@nomicfoundation/hardhat-verify": "^1.0.0",
"@typechain/ethers-v6": "^0.4.0",
"@typechain/hardhat": "^8.0.0",
"chai": "^4.2.0",
"dotenv": "^16.3.1",
"ethers": "^6.4.0",
"hardhat": "^2.17.1",
"hardhat-contract-sizer": "^2.10.0",
"hardhat-deploy": "^0.11.34",
"hardhat-gas-reporter": "^1.0.8",
"prettier": "^3.0.1",
"solhint": "^3.6.1",
"solidity-coverage": "^0.8.0",
"typechain": "^8.1.0"
},
"dependencies": {
"@chainlink/contracts": "^0.6.1"
}
}
hardhat-config.js: require("@nomicfoundation/hardhat-toolbox")
require("@nomicfoundation/hardhat-ethers")
require("hardhat-deploy")
require("hardhat-contract-sizer")
require("dotenv").config()
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY
module.exports = {
solidity: {
compilers: [{ version: "0.8.18" }],
},
defaultNetwork: "hardhat",
networks: {
hardhat: {
chainId: 31337,
blockConfirmations: 1,
},
sepolia: {
url: SEPOLIA_RPC_URL || "",
accounts: [PRIVATE_KEY],
chainId: 11155111,
blockConfirmations: 6,
},
},
gasReporter: {
enabled: false, // true
outputFile: "gas-report.txt",
noColors: true,
currency: "USD",
coinmarketcap: COINMARKETCAP_API_KEY,
token: "ETH", // or MATIC
},
namedAccounts: {
deployer: {
default: 0,
},
player: {
default: 1,
},
},
}
01-deploy const { network, ethers } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
// const { verify } = require("../helper-hardhat-config")
const VRF_SUB_FUND_AMOUNT = ethers.parseEther("1")
const BASE_FEE = ethers.parseEther("0.25")
const GAS_PRICE_LINK = 1e9
module.exports = async function ({ getNamedAccounts, deployments }) {
const { deploy, log } = deployments
// here we grap an account that we listed in config | row: 41
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2Address, subscriptionId, vrfCoordinatorV2Mock
if (chainId == 31337) {
vrfCoordinatorV2Mock = await deployments.get("VRFCoordinatorV2Mock", deployer)
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
console.log(vrfCoordinatorV2Address)
const transactionResponse = await vrfCoordinatorV2Mock.createSubscription()
const transactionReceipt = await transactionResponse.wait()
subscriptionId = transactionReceipt.logs[0].args.subId
await vrfCoordinatorV2Mock.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT)
// await vrfCoordinatorV2Mock.addConsumer(subscriptionId, raffle.address)
} else {
vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"]
subscriptionId = networkConfig[chainId]["subscriptionId"]
}
const enterenceFee = networkConfig[chainId]["enterenceFee"]
const gasLane = networkConfig[chainId]["gasLane"]
const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"]
const interval = networkConfig[chainId]["interval"]
const args = [
vrfCoordinatorV2Address,
enterenceFee,
gasLane,
subscriptionId,
callbackGasLimit,
interval,
]
const raffle = await deploy("Raffle", {
from: deployer,
args: args,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
// if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
// await verify(raffle.address, args)
// }
// log("_____________________________________________________")
}
module.exports.tags = ["all", "raffle"]
00-deploy const { network, ethers, deployments } = require("hardhat")
const BASE_FEE = ethers.parseEther("0.25")
// const BASE_FEE = 250000000000000000
const GAS_PRICE_LINK = 1e9
module.exports = async function ({ getNamedAccounts, deployments }) {
const { deploy, log } = deployments
// here we grap an account that we listed in config | row: 41
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
if (chainId == 31337) {
log("Local network detected! Deploying mocks...")
await deploy("VRFCoordinatorV2Mock", {
from: deployer,
log: true,
args: [BASE_FEE, GAS_PRICE_LINK],
})
log("Mocks deployed!")
log("____________________________________________________")
}
}
module.exports.tags = ["all", "mock"]
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
deployments.get doesn't give you contract instance, it contains address, abi, bytecode and other stuff related to contract. vrfCoordinatorV2Mock = await deployments.get("VRFCoordinatorV2Mock")
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
vrfCoordinatorInstance = await ethers.getContractAt("VRFCoordinatorV2Mock", vrfCoordinatorV2Address, deployer) And you can use vrfCoordinatorInstance to call the functions on the contract. |
Beta Was this translation helpful? Give feedback.
-
Working mock+deploy script with hardhat toolbox const { network, ethers } = require("hardhat")
const { networkConfig, developmentChains } = require("../helper-hardhat-config")
const { verify } = require("../utils/verify")
// const { verify } = require("../helper-hardhat-config")
const VRF_SUB_FUND_AMOUNT = ethers.parseEther("1")
const BASE_FEE = ethers.parseEther("0.25")
const GAS_PRICE_LINK = 1e9
module.exports = async function ({ getNamedAccounts, deployments }) {
const { deploy, log } = deployments
// here we grap an account that we listed in config | row: 41
const { deployer } = await getNamedAccounts()
const chainId = network.config.chainId
let vrfCoordinatorV2Address, subscriptionId, vrfCoordinatorV2Mock
if (chainId == 31337) {
const signer = await ethers.getSigner(deployer)
vrfCoordinatorV2Mock = await deployments.get("VRFCoordinatorV2Mock")
vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address
const vrfCoordinatorInstance = await ethers.getContractAt(
"VRFCoordinatorV2Mock",
vrfCoordinatorV2Address,
signer,
)
console.log(vrfCoordinatorV2Address)
const transactionResponse = await vrfCoordinatorInstance.createSubscription()
const transactionReceipt = await transactionResponse.wait()
subscriptionId = transactionReceipt.logs[0].args.subId
await vrfCoordinatorInstance.fundSubscription(subscriptionId, VRF_SUB_FUND_AMOUNT)
// await vrfCoordinatorV2Mock.addConsumer(subscriptionId, raffle.address)
} else {
vrfCoordinatorV2Address = networkConfig[chainId]["vrfCoordinatorV2"]
subscriptionId = networkConfig[chainId]["subscriptionId"]
}
const enterenceFee = networkConfig[chainId]["enterenceFee"]
const gasLane = networkConfig[chainId]["gasLane"]
const callbackGasLimit = networkConfig[chainId]["callbackGasLimit"]
const interval = networkConfig[chainId]["interval"]
const args = [
vrfCoordinatorV2Address,
enterenceFee,
gasLane,
subscriptionId,
callbackGasLimit,
interval,
]
const raffle = await deploy("Raffle", {
from: deployer,
args: args,
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
})
// if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
// await verify(raffle.address, args)
// }
// log("_____________________________________________________")
}
module.exports.tags = ["all", "raffle"] |
Beta Was this translation helpful? Give feedback.
vrfCoordinatorInstance = await ethers.getContractAt(
"VRFCoordinatorV2Mock",
vrfCoordinatorV2Address,
deployer,
)
Over here replace deployer with signer, and move the signer line up.