0

Can someone, please, explain, how this works:

I have contract MigrationAgent which has migrateFrom() function (not implemented). And I have NewContract is MigrationAgent which implements migrateFrom(). I deployed that contract, have its address 0x*****.

To call this function from other contract, as I see in example, I have to do this: MigrationAgent(0x*****).migrateFrom();

Why is this? I can't understand logic of this syntax. Why can't I write 0x*****.migrateFrom() and what if I write MigrationAgent(0x*****).migrateFrom() when 0x***** is not MigrationAgent?

1

Why is this? I can't understand logic of this syntax. Why can't I write 0x*****.migrateFrom()

0x***** is just an address, you have to write

MigrationAgent(0x*****).migrateFrom();

in order to instantiate a MigrationAgent contract. Once you have done so you can call its defined functions.

and what if I write MigrationAgent(0x*****).migrateFrom() when 0x***** is not MigrationAgent?

The execution would fail, unless said contract did have the function signature defined. In that case, it would do whatever the function defined on that contract does, which can be dangerous. That's why you should avoid calling functions on other contracts, specially if you don't know what those contracts are.

  • Thanks a lot for your answer So when I write ContractName(address) it means smth like type casting, and fails if it's not possible. And if I use new do I deploy new contract to the blockchain? – Олеся Костеева Nov 14 '17 at 13:53
  • If you do MigrationAgent m = MigrationAgent(0X...); you are instantiating an existing contract deployed to that address. On the other hand, if you do MigrationAgent m = new MigrationAgent(constructor_param_1,constructor_param_2,constructor_param_etc); you are creating a new contract and deploying it. – pabloruiz55 Nov 14 '17 at 14:00

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.