-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
@@ -0,0 +1,62 @@ | |||
import BigNumber from "bignumber.js"; | |||
import { numberToPaddedHex } from "@multiversx/sdk-core/out/utils.codec"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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;
}
package.json
Outdated
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"version": "0.0.1", |
There was a problem hiding this comment.
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
.
|
||
describe('addArgs Function', () => { | ||
test('Correctly encodes the arguments` lengths and values and adds them to the call data.', () => { | ||
let callData = addArgs("", args); |
There was a problem hiding this comment.
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.
@@ -0,0 +1,62 @@ | |||
import BigNumber from "bignumber.js"; | |||
import { numberToPaddedHex } from "@multiversx/sdk-core/out/utils.codec"; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
helpers/encodeCallData.ts
Outdated
addEndpointName, | ||
addGasLimit, | ||
addArgs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only for tests, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
9bf5af6
No description provided.