2

I have a factory contract, that in a transaction:

  • Deploys a contract
  • Emits an event with the address of the contract it deployed

I want to be able to use the address returned to interface with the contract that was deployed. EX:

from brownie import AstroSwapExchange, AstroSwapFactory
[...]
tokenExchangeAddress = forgeTx.events["TokenExchangeAdded"][0]["tokenExchange"]
return tokenExchangeAddress

tokenExchangeAddress = deploy_new_exchange(factory, erc20)
CREATE A CONTRACT OBJECT HERE
factory = exchange.factory()

What functions could I use to take advantage of the returned address and turn it into a Contract object to interact with it like any other contract that I deploy myself?

2 Answers 2

2

I found the answer myself! (Not sure if it's the best thing to do but here's how)

Step 1: Get the ABI for the contract you want to create a source of. In my case, it was a contract that was importable through brownie so:

from brownie import AstroSwapExchange, AstroSwapFactory
[...]
exchangeAbi = AstroSwapExchange.abi

Step 2: Use Contract.from_abi to create the contract. The part that confused me in the documentation is that you need to use the contract from brownie.network.contract instead of the actual ContractContainer from brownie.

from brownie.network.contract import Contract
[...]
exchange = Contract.from_abi("AstroSwapExchange", exchangeAddress, exchangeAbi)

And there we go! A Contract variable we can use to interface with things like:

tokenExchangeFee = exchange.feeAmmount()
2
0

A cleaner and simpler approach is use the ContractContainer.at(<address>).

The compiled project contracts that you import from brownie are of the ContractContainer type. Thus, in your example, both AstroSwapExchange and AstroSwapFactory are instances of the ContractContainer type. As such, your code can be simplified to the following:

from brownie.network.contract import ProjectContract
from brownie import AstroSwapExchange

exchange: ProjectContract = AstroSwapExchange.at(exchangeAddress)

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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