stellar / js-soroban-client

Main Soroban client library for the Javascript language
https://stellar.github.io/js-soroban-client/
Apache License 2.0
22 stars 17 forks source link

XdrWriterError [TypeError]: XDR Write Error: 0 is not a Hyper #83

Closed lmorgan824 closed 1 year ago

lmorgan824 commented 1 year ago

Describe the bug XdrWriterError [TypeError]: XDR Write Error: 0 is not a Hyper

We're getting this when trying to call .prepareTransaction or when calling .toXDR() on a transaction for soroban. This call works with soroban-client 0.5.0.

What version are you on? soroban-client 0.6.1

To Reproduce Steps to reproduce the behavior:

  1. github.com/blocktimefinancial/option
  2. See the opxpump.js code.
  3. Scroll down to line 128
  4. See error

Expected behavior Print .toXDR() for the transaction and call .prepareTransaction

Additional context Dump from pxpump.js which shows the parameter array is valid ScVal I128's Converted quote:

[
  ChildUnion {
    _switch: ChildEnum { name: 'scvI128', value: 10 },
    _arm: 'i128',
    _armType: [class ChildStruct extends Struct] {
      structName: 'Int128Parts',
      _fields: [
        [
          'hi',
          [class Hyper extends XdrPrimitiveType] {
            MAX_VALUE: [Hyper],
            MIN_VALUE: [Hyper]
          }
        ],
        [
          'lo',
          [class UnsignedHyper extends XdrPrimitiveType] {
            MAX_VALUE: [UnsignedHyper],
            MIN_VALUE: [UnsignedHyper]
          }
        ]
      ]
    },
    _value: ChildStruct {
      _attributes: {
        lo: UnsignedHyper { _value: 1n },
        hi: UnsignedHyper { _value: 0n }
      }
    }
  },
  ChildUnion {
    _switch: ChildEnum { name: 'scvI128', value: 10 },
    _arm: 'i128',
    _armType: [class ChildStruct extends Struct] {
      structName: 'Int128Parts',
      _fields: [
        [
          'hi',
          [class Hyper extends XdrPrimitiveType] {
            MAX_VALUE: [Hyper],
            MIN_VALUE: [Hyper]
          }
        ],
        [
          'lo',
          [class UnsignedHyper extends XdrPrimitiveType] {
            MAX_VALUE: [UnsignedHyper],
            MIN_VALUE: [UnsignedHyper]
          }
        ]
      ]
    },
    _value: ChildStruct {
      _attributes: {
        lo: UnsignedHyper { _value: 42035n },
        hi: UnsignedHyper { _value: 0n }
      }
    }
  },
  ChildUnion {
    _switch: ChildEnum { name: 'scvI128', value: 10 },
    _arm: 'i128',
    _armType: [class ChildStruct extends Struct] {
      structName: 'Int128Parts',
      _fields: [
        [
          'hi',
          [class Hyper extends XdrPrimitiveType] {
            MAX_VALUE: [Hyper],
            MIN_VALUE: [Hyper]
          }
        ],
        [
          'lo',
          [class UnsignedHyper extends XdrPrimitiveType] {
            MAX_VALUE: [UnsignedHyper],
            MIN_VALUE: [UnsignedHyper]
          }
        ]
      ]
    },
    _value: ChildStruct {
      _attributes: {
        lo: UnsignedHyper { _value: 1684508428000n },
        hi: UnsignedHyper { _value: 0n }
      }
    }
  },
  ChildUnion {
    _switch: ChildEnum { name: 'scvI128', value: 10 },
    _arm: 'i128',
    _armType: [class ChildStruct extends Struct] {
      structName: 'Int128Parts',
      _fields: [
        [
          'hi',
          [class Hyper extends XdrPrimitiveType] {
            MAX_VALUE: [Hyper],
            MIN_VALUE: [Hyper]
          }
        ],
        [
          'lo',
          [class UnsignedHyper extends XdrPrimitiveType] {
            MAX_VALUE: [UnsignedHyper],
            MIN_VALUE: [UnsignedHyper]
          }
        ]
      ]
    },
    _value: ChildStruct {
      _attributes: {
        lo: UnsignedHyper { _value: 4n },
        hi: UnsignedHyper { _value: 0n }
      }
    }
  }
]
Creating contract transaction for cd0ca2f721d91df334b79fb1e043920919ed0c6b09f930af5048a50930fb7f44
Preparing transaction for cd0ca2f721d91df334b79fb1e043920919ed0c6b09f930af5048a50930fb7f44
/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:611
    if (!(value instanceof this)) throw new _errors__WEBPACK_IMPORTED_MODULE_1__.XdrWriterError(`${value} is not a Hyper`);                                        ^

XdrWriterError [TypeError]: XDR Write Error: 0 is not a Hyper
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:611:41)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1349:12)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1547:21)
    at VarArray.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1800:23)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1547:21)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1349:12)
    at VarArray.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1800:23)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1349:12)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1547:21)
    at Function.write (/home/lj/src/soroban/soroban-examples/option/pxpump/node_modules/stellar-base/node_modules/js-xdr/lib/xdr.js:1349:12)
Shaptic commented 1 year ago

I believe this is because the top half of an I128Parts is now Hyper, not UnsignedHyper.

Shaptic commented 1 year ago

This was done as part of stellar/stellar-xdr#78.

Shaptic commented 1 year ago

In other words, this does not work:

    let i128 = StellarBase.xdr.ScVal.scvI128(
      new StellarBase.xdr.Int128Parts({
        hi: new StellarBase.xdr.Uint64(0, 0),
        lo: new StellarBase.xdr.Uint64(0, 1),
      })
    );
    i128.toXDR();

but this does:

    let i128 = StellarBase.xdr.ScVal.scvI128(
      new StellarBase.xdr.Int128Parts({
        hi: new StellarBase.xdr.Int64(0, 0),
        lo: new StellarBase.xdr.Uint64(0, 1),
      })
    );
    i128.toXDR();
lmorgan824 commented 1 year ago

Right on, I'll change that up. I know Paul was talking about adding the encode/decode for ScVal's to js-stellar-base, I guess that would eliminate these types of errors.

Thanks, Lou

Shaptic commented 1 year ago

Yeah, this sucks, and nobody downstream should have to care about stuff like this.

I've opened stellar/js-stellar-base#610 to track improving this.