Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encode call data #1

Merged
merged 13 commits into from
Mar 22, 2024
Merged

Add encode call data #1

merged 13 commits into from
Mar 22, 2024

Conversation

dragos-rebegea
Copy link
Collaborator

No description provided.

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
helpers/encodeCallData.js Outdated Show resolved Hide resolved
helpers/encodeCallData.js Outdated Show resolved Hide resolved
@@ -0,0 +1,62 @@
import BigNumber from "bignumber.js";
import { numberToPaddedHex } from "@multiversx/sdk-core/out/utils.codec";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe import directly from @multiversx/sdk-core/utils.codec, not from the /out dir.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS2307: Cannot find module  @multiversx/sdk-core/utils.codec  or its corresponding type declarations.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. We'll keep that in mind.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, feel free to:

export function numberToPaddedHex(value: bigint | number | BigNumber.Value) {
    let hexableNumber: { toString(radix?: number): string };

    if (typeof value === "bigint" || typeof value === "number") {
        hexableNumber = value;
    } else {
        hexableNumber = new BigNumber(value);
    }

    const hex = hexableNumber.toString(16);
    return zeroPadStringIfOddLength(hex);
}

export function zeroPadStringIfOddLength(input: string): string {
    input = input || "";

    if (input.length % 2 == 1) {
        return "0" + input;
    }

    return input;
}

helpers/encodeCallData.ts Outdated Show resolved Hide resolved
helpers/encodeCallData.ts Outdated Show resolved Hide resolved
index.ts Outdated Show resolved Hide resolved
jest.config.js Outdated Show resolved Hide resolved
tests/encodeCallData.test.ts Outdated Show resolved Hide resolved
tests/encodeCallData.test.ts Outdated Show resolved Hide resolved
popenta
popenta previously approved these changes Mar 18, 2024
popenta
popenta previously approved these changes Mar 18, 2024
andreibancioiu
andreibancioiu previously approved these changes Mar 19, 2024
package.json Outdated
"version": "1.0.0",
"description": "",
"main": "index.js",
"version": "0.0.1",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, "first" usable version is v0.1.0.

https://stackoverflow.com/a/24154727/1475331


describe('addArgs Function', () => {
test('Correctly encodes the arguments` lengths and values and adds them to the call data.', () => {
let callData = addArgs("", args);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are a bit more explicit and readable if you use proper / actual test values (both input values and expected output) within each individual test.

index.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,62 @@
import BigNumber from "bignumber.js";
import { numberToPaddedHex } from "@multiversx/sdk-core/out/utils.codec";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. We'll keep that in mind.

return callData;
}

export function addEndpointName(callData: string, endpointName: string): string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for internal use and tests, right (as the ones below)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@@ -0,0 +1,62 @@
import BigNumber from "bignumber.js";
import { numberToPaddedHex } from "@multiversx/sdk-core/out/utils.codec";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, feel free to:

export function numberToPaddedHex(value: bigint | number | BigNumber.Value) {
    let hexableNumber: { toString(radix?: number): string };

    if (typeof value === "bigint" || typeof value === "number") {
        hexableNumber = value;
    } else {
        hexableNumber = new BigNumber(value);
    }

    const hex = hexableNumber.toString(16);
    return zeroPadStringIfOddLength(hex);
}

export function zeroPadStringIfOddLength(input: string): string {
    input = input || "";

    if (input.length % 2 == 1) {
        return "0" + input;
    }

    return input;
}

}

export function addGasLimit(callData: string, gasLimit: number): string {
const gasLimitHex = new BigNumber(gasLimit).toString(16).padStart(uint64ArgBytes, '0');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So only nested encoding, but maybe this is the context, all right.

return callData + gasLimitHex;
}

export function addArgs(callData: string, args: any[]): string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you are referring sdk-core. Perhaps BinaryCodec would have helped a bit?

https://github.com/multiversx/mx-sdk-js-core/blob/main/src/smartcontracts/codec/binary.spec.ts#L210

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the time being will let it like this

Comment on lines 61 to 63
addEndpointName,
addGasLimit,
addArgs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for tests, correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@dragos-rebegea dragos-rebegea merged commit 612c272 into main Mar 22, 2024
3 checks passed
@dragos-rebegea dragos-rebegea deleted the add-encodeCallData branch March 22, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants