2

Can we parse a varialble of type sp.TAddress to sp.TString like this:

strAddress = sp.string(sp.address("tz1-add")) or strAddress = str(sp.address("tz1-add"))?

Smart Contract Example:

import smartpy as sp

class SmartContract(sp.Contract):
    def init(self):
        self.init(fromAddress = "")

    @sp.entry_point
    def transferEvent(self, params):
        self.data.fromAddress = sp.string(params.fromAddress)

if "templates" not in name:
    @sp.add_test(name = "Smart Contract")
    def test():

        c1 = SmartContract()

        scenario = sp.test_scenario()
        scenario += c1

        scenario.h1("Smart Contract")
        scenario += c1.transferEvent(strAddress = sp.address("tz1MFYTrMMhh7QBq8NRP6QMU2gcrcGhjSTrv"))


        scenario.verify(c1.data.fromAddress == "tz1MFYTrMMhh7QBq8NRP6QMU2gcrcGhjSTrv")

As Error I get:

Error: Error in Scenario Literal format error: (string(attr(params 8)fromAddress 9)) Traceback (most recent call last):

enter image description here

Thanks.

1 Answer 1

2

No, you can't. Addresses are not convertible to strings in Michelson (short of really cheating with internals).

That being said, the error message could be a bit more explicit.

Also, what is your use case? The general understanding is that this operation is useless.

6
  • So, I want to call a method of a smart contract A from a smart contract B where the method accepts a string. The smart contract B, the caller, passes an address as param to the method called in smart contract A.
    – Daly
    Feb 6, 2020 at 14:13
  • 1
    Understood. You cannot do this (without using super weird hacks that you really don't want to hear about).
    – FFF
    Feb 6, 2020 at 15:11
  • Haha ok, I resolved it differently.
    – Daly
    Feb 6, 2020 at 15:51
  • I have a similar problem, but in this case i'm trying to assemble TZIP-021 metadata from an off-chain view. i need a way of populating the minter and creators fields with strings of the artist and minter addresses. any suggestions?
    – damian
    Oct 7, 2021 at 10:57
  • @FFF would you mind elaborating on the hacks to get it working? i have a requirement to do this inside a unit test (not inside a contract) and this would be very useful to know
    – 0x10
    May 2, 2022 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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