Call contract from contract with the same signature #301

Closed
opened 2021-03-08 23:07:30 +09:00 by zxq9 · 2 comments
zxq9 commented 2021-03-08 23:07:30 +09:00 (Migrated from gitlab.com)

Created by: cytadela8

Currently one can call contract from another by using an variable with type of that contract like:

contract Remote =
  function get : () => int
  function can_resolve : (string, string) => bool

contract RemoteCall =

    function remote_resolve(r : Remote, name: string, key: string) : bool =
        r.can_resolve(name, key)

But what if we want to call another instance of a contract with the same code. Following example will result in an error:

contract Detector =
  entrypoint detect() : int=
    switch(Chain.block_hash(Chain.block_height))
      None => 1
      Some(_hash) => 2
  entrypoint call_detect(detector : Detector) : (int * int) =
    let d_1 = detector.detect()
    (detect(), d_1)

  entrypoint call_call_detect(detector1 : Detector, detector2 : Detector) : (int * int * int) =
    let (d_1, d_2) = detector1.call_detect(detector2)
    (detect(), d_1, d_2)

One has to use workaround like this:

@compiler >= 4.3

contract DetectorInterface2 =
  entrypoint detect : () => int

contract DetectorInterface =
  entrypoint detect : () => int
  entrypoint call_detect : (DetectorInterface2) => (int * int)

contract Detector =
  entrypoint detect() : int =
    switch(Chain.block_hash(Chain.block_height))
      None => 1
      Some(_hash) => 2
  entrypoint call_detect(detector : DetectorInterface) : (int * int) =
    let d_1 = detector.detect()
    (detect(), d_1)

  entrypoint call_call_detect(detector1 : DetectorInterface, detector2 : DetectorInterface2) : (int * int * int) =
    let (d_1, d_2) = detector1.call_detect(detector2)
    (detect(), d_1, d_2)

We should make it so the workaround is not needed.

*Created by: cytadela8* Currently one can call contract from another by using an variable with type of that contract like: ``` contract Remote = function get : () => int function can_resolve : (string, string) => bool contract RemoteCall = function remote_resolve(r : Remote, name: string, key: string) : bool = r.can_resolve(name, key) ``` But what if we want to call another instance of a contract with the same code. Following example will result in an error: ``` contract Detector = entrypoint detect() : int= switch(Chain.block_hash(Chain.block_height)) None => 1 Some(_hash) => 2 entrypoint call_detect(detector : Detector) : (int * int) = let d_1 = detector.detect() (detect(), d_1) entrypoint call_call_detect(detector1 : Detector, detector2 : Detector) : (int * int * int) = let (d_1, d_2) = detector1.call_detect(detector2) (detect(), d_1, d_2) ``` One has to use workaround like this: ``` @compiler >= 4.3 contract DetectorInterface2 = entrypoint detect : () => int contract DetectorInterface = entrypoint detect : () => int entrypoint call_detect : (DetectorInterface2) => (int * int) contract Detector = entrypoint detect() : int = switch(Chain.block_hash(Chain.block_height)) None => 1 Some(_hash) => 2 entrypoint call_detect(detector : DetectorInterface) : (int * int) = let d_1 = detector.detect() (detect(), d_1) entrypoint call_call_detect(detector1 : DetectorInterface, detector2 : DetectorInterface2) : (int * int * int) = let (d_1, d_2) = detector1.call_detect(detector2) (detect(), d_1, d_2) ``` We should make it so the workaround is not needed.
ghallak commented 2022-03-01 19:44:00 +09:00 (Migrated from gitlab.com)

@UlfNorell @hanssv Are there any reasons for why it is not possible to call a different instance of the same contract? Was this banned on purpose? Or should it be allowed?

@UlfNorell @hanssv Are there any reasons for why it is not possible to call a different instance of the same contract? Was this banned on purpose? Or should it be allowed?
zxq9 commented 2022-03-02 06:50:26 +09:00 (Migrated from gitlab.com)

Created by: hanssv

I can't think of any reason and I think I remember us discussing adding it in the past - just never made it to the top of the list I guess 🤔

I guess the hard question would be to figure out a good name for the implictly defined type then. Self or This might be popular options 😅

*Created by: hanssv* I can't think of any reason and I think I remember us discussing adding it in the past - just never made it to the top of the list I guess 🤔 I guess the hard question would be to figure out a good name for the implictly defined type then. `Self` or `This` might be popular options 😅
Sign in to join this conversation.
No Milestone
No project
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: QPQ-AG/sophia#301
No description provided.