TokenScript #42

How to read a data object in TS

setting the context of the need to read a data object

<card action="bid">
    <input>
        <token name="kitty1" …>
<!-- a reference to an instance of attestation which is an offer to buy kitty -->
        <attestation name="offer-to-buy-kitty.price" …>
    </input>

</card>

Suppose offer-to-buy-kitty.price is 0.802ETH

Compare existing methods:

1. Our method: always return a string

console.log(offer-to-buy-kitty.price == "80280000000000000000000000")
// true

2. Truffle method

console.log( (offer-to-buy-kitty.price == 0.802) )
//console: type mismatch

console.log(offer-to-buy-kitty.price === new BigNumber(0.802).mul(18,10))
// console: true

console.log(offer-to-buy-kitty.price.div("1e18",10).toNumber() == 0.802)
// console: true

3. Our decision: use .value as in DOM

console.log( (offer-to-buy-kitty.price.value == 0.802) )
//console: true
// get the name of the attribute in local langauge:
offer.price.name == "Price"

// get the type information (especially if there was a CHOICE)
//  e.g. figure out if this is a block height or a universalTime

console.log(offer.expiry.constructor.name)
// You might get "UniversalTime" or "INTEGER"

console.log(offer.expiry instanceof UniversalTime)
// console: true

console.log( (offer-to-buy-kitty.price === new INTEGER("0.802e10"))
// console: true

console.log(offer-to-buy-kitty.price.div("1e18",10).toNumber() == 0.802)
// console: true

4. Alternative candidate plan decided to abandon

console.log( (offer-to-buy-kitty.price.$ == "0.802") )
//console: true
console.log( (offer-to-buy-kitty.price.# == 0.802) )
//console: true

This is modelled after Microsoft BASIC, where a variable with suffix

!  Exclamation point (single-precision data-type suffix)
#  Number sign (double-precision data-type suffix)
$  Dollar sign (suffix for string data type)