You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error: Bad Request: 139:8: extract s beyond 255: 256
The goal is to have test()bytes<256> return a "clamped" string at 256 bytes.
It's a bit of a convoluted example. For context, I'm messing around with the ARC-72 example and trying to get the URI to include the index/token ID as a suffix. So yes, I'm aware the above example works if bytes<256> is replaced with string. Due to the ARC-72 spec, even if I compute this in arc72_tokenURI I'd still run into this error as it also must return bytes<256>.
As a side-note, would be great if that itoa utility was included out-of-the-box, I believe there's a PyTeal utillity equivalent?
Let me know if I can provide anything else. Thanks!
Update:
This sort of works:
import{Contract}from'@algorandfoundation/tealscript';typeBytes256=bytes<256>;exportclassSimpleextendsContract{/** * Map single-digit integer to its string representation. */privatedigitToASCII(value: uint64): string{returnextract3('0123456789',value,1);}/** * Convert a number to its human-readable string representation. */privatenumberToString(value: uint256): string{if(value===0)return'0';constprefix=value/10><uint256>0 ? this.numberToString(value/10) : '';constsuffix=this.digitToASCII(<uint64>(value%10));returnconcat(prefix,suffix);}/** * Concat string with zero bytes to achieve 256 byte length */privatestringToBytes256(value: string): Bytes256{returncastBytes<Bytes256>(value+bzero(256-len(value)));}test(): bytes<256>{returnthis.stringToBytes256(this.numberToString(<uint256>1337));}}
The downside with this approach is that the output, once decoded, is a string with null-terminators. This is probably fine for most scenarios though, just need to be mindful in JS due to how it handles '\x00' when in a string. Most systems will not render that part.
/** Utility to decode a zero-byte padded bytes[256] to a string */functiondecodeBytes256ToString(bytes: Uint8Array): string{returnnewTextDecoder().decode(newUint8Array(bytes.filter((n)=>n!==0)));}
From what I can tell, the core issue is that casting to bytes<256> on a plain string works, e.g.:
'some string'asbytes<256>
But casting a string value, like after doing some concatenation, does not work, e.g.:
('hello'+' '+'world')asbytes<256>// errors with "extract l beyond 255: 256"
Reporting in case this is a bug, but guessing it's just not possible or could be done in a different way:
Getting this when I build it:
The goal is to have
test()bytes<256>
return a "clamped" string at 256 bytes.It's a bit of a convoluted example. For context, I'm messing around with the ARC-72 example and trying to get the URI to include the index/token ID as a suffix. So yes, I'm aware the above example works if
bytes<256>
is replaced withstring
. Due to the ARC-72 spec, even if I compute this inarc72_tokenURI
I'd still run into this error as it also must returnbytes<256>
.As a side-note, would be great if that
itoa
utility was included out-of-the-box, I believe there's a PyTeal utillity equivalent?Let me know if I can provide anything else. Thanks!
Update:
This sort of works:
The downside with this approach is that the output, once decoded, is a string with null-terminators. This is probably fine for most scenarios though, just need to be mindful in JS due to how it handles
'\x00'
when in a string. Most systems will not render that part.From what I can tell, the core issue is that casting to
bytes<256>
on a plain string works, e.g.:But casting a string value, like after doing some concatenation, does not work, e.g.:
output
With concatenation:
Without:
The text was updated successfully, but these errors were encountered: