multiversx / mx-sdk-js-core

MultiversX SDK for interacting with the MultiversX blockchain (in general) and Smart Contracts (in particular).
https://multiversx.github.io/mx-sdk-js-core/
Other
60 stars 37 forks source link

Upgrade breaks Interaction serialization with native variadic arguments #306

Closed michavie closed 1 year ago

michavie commented 1 year ago

Upgrading from 12.1.0 breaks transactions using native variadic arguments, with this error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'belongsToTypesystem')
    at eu (_app-e1ba62606a0e66e8.js:5:31477)
    at Object.ec [as nativeToTypedValues] (_app-e1ba62606a0e66e8.js:5:31004)
    at methods.<computed> [as someMethod] (_app-e1ba62606a0e66e8.js:5:42871)
    at U (3032-5df52ac79f3887e8.js:1:27869)
    at async Object.S [as someOtherMethod] (3032-5df52ac79f3887e8.js:1:26988)

Worked before and fails after upgrade for Interactions:

const interactionArgs = [
    someValue,
    ...['a', 'b', 'c'], // <--- used to work, but now causes errors with latest versions
  ]

Using VariadicValue is a temporary fix, but ideally, native values would keep working

Likely caused by this change here: https://github.com/multiversx/mx-sdk-js-core/commit/973c823ac2d35eaf8fe269b3886dabb5874f6186

andreibancioiu commented 1 year ago

Hi @michavie,

Thank you for raising the issue :pray:

Do you think you can help us reproduce the issue, by altering the following test case (the one below passes)?

it.only("issue 306", async () => {
    const endpoint = AbiRegistry.create({
        "endpoints": [
            {
                "name": "foo",
                "inputs": [{
                    "type": "u64"
                }, {
                    "type": "variadic<bytes>"
                }],
                "outputs": []
            }
        ]
    }).getEndpoint("foo");

    // Using both native JavaScript objects and typed values
    const typedValues = NativeSerializer.nativeToTypedValues([
        42,
        ...['a', 'b', 'c'],
    ], endpoint);

    assert.deepEqual(typedValues[0].getType(), new U64Type());
    assert.deepEqual(typedValues[0].valueOf(), new BigNumber(42));
    assert.deepEqual(typedValues[1].getType(), new VariadicType(new BytesType()));
    assert.deepEqual(typedValues[1].valueOf(), [Buffer.from('a'), Buffer.from('b'), Buffer.from('c')]);
});

Thank you!

michavie commented 1 year ago

I could reproduce the error in the test results by not providing any values, as in spreading an empty array: ...[] or providing no value at all

Would make sense as ...[] evaluates to undefined and giving no value also equals undefined

Sorry for the initial sloppy example

Submitted a quick PR here: https://github.com/multiversx/mx-sdk-js-core/pull/307

andreibancioiu commented 1 year ago

Fixed by your PR, https://github.com/multiversx/mx-sdk-js-core/pull/307.

Thank you :)