0

Seems like such a basic thing. A similar question was posted 9 months back in the Cadence discord and the answer was no - just checking that is still the case.

I'm trying to pass a serial number encoded as a string in the NFT metadata as an argument to MetadataViews.Serial() which accepts a UInt64 - so I need to get the string value in the meta into a UInt64.

N8P
  • 393
  • 1
  • 4
  • 11

1 Answers1

0

So, there is no official way to do it, but it can be implemented in cadence:

pub fun parseUInt64(_ string: String) : UInt64? {
            let chars : {Character : UInt64} = {
                "0" : 0 , 
                "1" : 1 , 
                "2" : 2 , 
                "3" : 3 , 
                "4" : 4 , 
                "5" : 5 , 
                "6" : 6 , 
                "7" : 7 , 
                "8" : 8 , 
                "9" : 9 
            }
            var number : UInt64 = 0
            var i = 0
            while i < string.length {
                if let n = chars[string[i]] {
                        number = number * 10 + n
                } else {
                    return nil 
                }
                i = i + 1
            }
            return number 
        }

    }

This was borrowed from https://flowscan.org/contract/A.097bafa4e0b48eef.CharityNFT/overview

N8P
  • 393
  • 1
  • 4
  • 11