I’m calling a function that creates an instance of a smart contract. I have another function that uses the address of that smart contract as an argument. The problem: I’m not sure how I can go about “capturing” this address so I can use it as an argument. Do I use an event listener? Or how should I just create a function on my smart contract to return this contract address instead and do it this way?
This is my example code:
async function createContractInstance(arg1, arg2, arg3) {
if (typeof window.ethereum !== 'undefined') {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, contract.abi, signer)
try {
const val = await contract.createContractInstance(arg1, arg2, arg3);
console.log('val: ', val)
} catch (err) {
console.log('Error: ', err)
}
}
}
async function useContractInstance(contractInstanceAddress) {
if (typeof window.ethereum !== 'undefined') {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const differentContract = new ethers.Contract(differentContractAddress, differentContract.abi, signer)
try {
const val = await differentContract.useContractInstance(contractInstanceAddress);
console.log('val: ', val)
} catch (err) {
console.log('Error: ', err)
}
}
}
<div className={buttonContainer}>
<button className={buttonStyle}
type='button'
onClick={() => createContractInstance(1,2,3)}>Create Contract Instance
</button>
</div>
<div className={buttonContainer}>
<button className={buttonStyle}
type='button'
onClick={() => useContractInstance("contract Address")}> Use Contract Instance
</button>
</div>