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

get_contract function makes me confused #83

Open
dankorea opened this issue Oct 4, 2022 · 6 comments
Open

get_contract function makes me confused #83

dankorea opened this issue Oct 4, 2022 · 6 comments

Comments

@dankorea
Copy link

dankorea commented Oct 4, 2022

I have read it several times both in lottery and NFT course. Does it mean when we deploy on test or real network, e.g., goerli, we still keep on using the mocks? since we will always excute the mock mapping for Aggregator, vrf_coordinator and linktoken
" contract_type = contract_to_mock[contract_name]"
whenever we start the function, and we also use the "contract_type" variable in our "else" part codes in the function
"contract = Contract.from_abi(
contract_type._name, contract_address, contract_type.abi
)"

----raw codes from the course----
contract_type = contract_to_mock[contract_name]
if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
if len(contract_type) <= 0:
deploy_mocks()
contract = contract_type[-1]

else:
contract_address = config["networks"][network.show_active()][contract_name]
# address
# ABI
contract = Contract.from_abi(
contract_type._name, contract_address, contract_type.abi
)
# MockV3Aggregator.abi

@Ayangoel17
Copy link

No , its a function that is used for mapping
contract_to_mock = {
"eth_usd_price_feed": MockV3Aggregator,
"vrf_coordinator": VRFCoordinatorMock,
"link_token": LinkToken,
}
And when we have to depoly on real or test net it uses the address from our config file

@dankorea
Copy link
Author

dankorea commented Oct 4, 2022

but if we check the deploy scripts: https://github.com/PatrickAlphaC/smartcontract-lottery/blob/main/scripts/deploy_lottery.py
we would find "eth_usd_price_feed" is transferred to get_contract function and mapped to MockV3Aggregator no matter what our network setting is.
Did i make sth. wrong?

def deploy_lottery():
account = get_account()
lottery = Lottery.deploy(
get_contract("eth_usd_price_feed").address,
get_contract("vrf_coordinator").address,
get_contract("link_token").address,

@Ayangoel17
Copy link

If you see get_contract function else statement its uses config file address and contract name rather than mapping

    else:
        contract_address = config["networks"][network.show_active()][contract_name]
        # address
        # ABI
        contract = Contract.from_abi(
            contract_type._name, contract_address, contract_type.abi
        )
        # MockV3Aggregator.abi
    return contract

@dankorea
Copy link
Author

dankorea commented Oct 4, 2022

yes, it uses contract_type again for _name and abi parameters...
this is just what makes me confused...

contract_type._name, contract_address, contract_type.abi

@Ayangoel17
Copy link

  • See it is not deploying the mock contract
  • contract_type._name is used to just get the name or contract
  • contract_type.abi - is used for abi functionality .
    Role of abi : Is to send a request directly in JSON format to a smart contract and expect a response since a contract only communicates in bytecode. To translate this into something the EVM understands, this information is encoded via ABI encoding. These encodings include function signatures and variable declarations so that the EVM knows exactly which function to execute within the smart contract.
    To understand the more about abi : https://www.alchemy.com/overviews/what-is-an-abi-of-a-smart-contract-examples-and-usage

@dankorea
Copy link
Author

dankorea commented Oct 4, 2022

the reason why we can use contract_type.abi here is that , for example, MockV3Aggregator in fact has same abi as AggregatorV3Interface because MockV3Aggregator inherits from AggregatorV2V3Interface, right?
the reason we use contract_type.abi is just for convenience, and there are same reasons for VRFCoordinator(Mock) and LinkToken, am I right?
Here the _name, in fact, can be replaced by any string, right?

Thanks for your time and patience to me, a newbie, thank you !

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

2 participants