2

Is it possible to use a variable as the entry_point for a callback in smartpy?

This works:

    @sp.entry_point
    def getProofs(self, address, callback_address):
        c = sp.contract(TGetProofsResponsePayload, callback_address, entry_point="getProofsCallback").open_some()
        sp.transfer(sp.record(address=address, proofs=self.data.identities[address]), sp.mutez(0), c)

This does not:

    @sp.entry_point
    def getProofs(self, address, callback_address, callback_entrypoint):
        c = sp.contract(TGetProofsResponsePayload, callback_address, entry_point=callback_entrypoint).open_some()
        sp.transfer(sp.record(address=address, proofs=self.data.identities[address]), sp.mutez(0), c)

Getting the following error:

Error: Error in Scenario
Expression format error (a 4) (contract (attr (params 64) callback_entrypoint 170)
(record
((address address) (callback_entrypoint string)
(proofs
(map string
(record
((meta (map string string)) (register_date timestamp) (verified bool))
None))))
None)
(attr (params 64) callback_address 170) 68) 

Any ideas? 🤷‍♂️

I guess I can pass a routing_key or callback_id to handle different scenarios for the callback, but seems cleaner not to force the callback entrypoint name? 😅

Thanks ❤️

2

The callback_address can be tz1...%getProofsCallback.

The smartpy code would then look like:

c = sp.contract(TGetProofsResponsePayload, callback_address).open_some()

The CONTRACT instruction CONTRACT %name t has annotation as you can see and those cannot be dynamic.

To get the address with entry_point information you can do:

# when building the callback
callback_address = sp.self_entry_point(entry_point = 'getProofsCallback')
4
  • Added an example on how to build the callback with entrypoint information May 21 at 13:41
  • 1
    Fantastic 🎉 Works like charm 🙌 I had to use sp.self_entry_point_address to get what I needed, but yeah... thanks a bunch!! May 21 at 14:49
  • Out of curiosity, are there any functions to build such a callback_address from a testcase? 😅 Would like to make a test to verify that contract X cannot trigger unwanted behaviour on contract A via calling getProofs on contract B 🤔 (getProofs is on B and getProofsCallback is on A in this case) May 21 at 15:12
  • 1
    Yes. You can do the following: smartpy.io/… May 21 at 15:32

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.