1

There is the standard way of creating contract instances with the new keyword where you pass arguments like:

MyContract mine = new MyContract(arg1, arg2);

Then there is another way, like in this example where create a contract instance at a given address, without the new keyword, but without arguments, like this:

MyContract mine = MyContract(address);

How do I create a contract instance at a given address (like the second example), and with the arguments I want (like the first example)?

I'm new to this, sorry if I'm misunderstanding something here. `

1

You can't.

In the first example, you are not merely instantiating a contract that is there. You are deploying a new instance and passing arguments into the constructor function. Constructors run one time only, by design, and are not even included in the deployed bytecode (because they can't be run again).

In the second example, you are instantiating a contract that is already there. There is no need or possible use of arguments because the contract is already deployed and the constructor cannot be run again.

Hope it helps.

0

The second example is creating an instance of the contract at a specific address that already exists on the blockchain. You pass in the address to MyContract to tell the EVM where to look when executing the code.

To call a function on this contract you will do reference the name you gave it (in this case, mine) and call the function using that. This is where you can pass in the desired parameters.

So an example would be:

MyContract mine = MyContract(0x1234)
uint256 number = mine.squareTheNumber(2) // number = 4

In this example, you are calling the squareTheNumber(uint256) function on mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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