Move.

Post

Share your knowledge.

Steven.
Nov 16, 2023
Expert Q&A

How to Read a Constant Return Value from a Move Function Using BCS in Sui TS SDK?

I have a Move function that returns a constant value as follows:

const FEE: u64 = 1_000_000

public fun fee(): u64 {
    FEE
}
When I make a moveCall using the Sui TS SDK, I receive encoded values like this:

javascript
Copy code
returnValues: [
    [
        [ 64, 66, 15, 0, 0, 0, 0, 0 ], "u64"
    ]
]

I understand that I need to decode these values using the BCS library to obtain a readable output. However, I am unsure how to achieve this. I attempted to use bcs.registerStructType, but it seems this approach does not work because the return value is not a struct.

Could someone please provide guidance on how to decode and read this constant return value using the BCS library in the Sui TS SDK?

  • Move
  • Move Script
0
2
Share
Comments
.

Answers

2
Jeremy.
Nov 16 2023, 13:49

You don't need to register a struct type for decoding a u64 value using the BCS (Binary Canonical Serialization) library. You can directly decode the encoded value using the bcs.decode method. Here's how you can decode and read the constant return value in your case:

const bcs = require('bcs'); // Import the BCS library

const encodedValue = Uint8Array.from([ 64, 66, 15, 0, 0, 0, 0, 0 ]); // Encoded value received from the moveCall

const decodedValue = bcs.decode('u64', encodedValue); // Decode the u64 value

console.log('Decoded result:', decodedValue); // Output: Decoded result: 1000000

In the code above, the bcs.decode function takes the type 'u64' and the encoded value as input and returns the decoded result. The decoded value will be 1000000 in this case, corresponding to the constant value returned by your Move function.

0
Comments
.
Steven.
Nov 16 2023, 13:53

The Binary Canonical Serialization (BCS) library in Move smart contract language provides a way to encode and decode data structures. In your case, you want to decode a u64 value from an encoded byte array. You can do this directly using the bcs.decode method, as you have shown in your code snippet.

const bcs = require('bcs'); // Import the BCS library

const encodedValue = Uint8Array.from([ 64, 66, 15, 0, 0, 0, 0, 0 ]); // Encoded value received from the moveCall

const decodedValue = bcs.decode('u64', encodedValue); // Decode the u64 value

console.log('Decoded result:', decodedValue); // Output: Decoded result: 1000000

In this code, you are using the bcs.decode function to decode a u64 value from an encoded byte array. The bcs.decode function takes two arguments: the type of the value to decode ('u64' in this case) and the encoded byte array. It returns the decoded value.

The bcs.decode function is part of the BCS library, which is a JavaScript implementation of the Move language's binary serialization format. This library allows you to encode and decode Move data structures in JavaScript.

Please note that this code assumes that the encoded byte array represents a u64 value. If the encoded byte array represents a different type of value, you will need to change the type argument to bcs.decode.

Also, the bcs.decode function throws an error if the encoded byte array is not a valid encoding of the specified type. Therefore, you should use a try/catch block to handle potential errors digitalocean.com.

In conclusion, you can decode a u64 value from an encoded byte array using the bcs.decode method of the BCS library in Move smart contract language. This allows you to interact with Move data structures in JavaScript.

0
Comments
.

Do you know the answer?

Please log in and share it.

We use cookies to ensure you get the best experience on our website.
More info