Skip to content

Commit

Permalink
Merge pull request #467 from multiversx/TOOL-220-add-check-for-boolea…
Browse files Browse the repository at this point in the history
…n-value-to-increase-clarity

Add convert to bool if string or number receive for boolean value
  • Loading branch information
danielailie authored Aug 6, 2024
2 parents a86346c + 56386ac commit 9f0c630
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-core",
"version": "13.2.2",
"version": "13.3.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
57 changes: 56 additions & 1 deletion src/smartcontracts/nativeSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe("test native serializer", () => {
assert.deepEqual(typedValues[1].valueOf(), [Buffer.from("a"), Buffer.from("b"), Buffer.from("c")]);
});

it("should should handle optionals in a strict manner (but it does not)", async () => {
it("should handle optionals in a strict manner (but it does not)", async () => {
const endpoint = AbiRegistry.create({
endpoints: [
{
Expand Down Expand Up @@ -610,4 +610,59 @@ describe("test native serializer", () => {
variadic: false,
});
});

it("should accept a mixed of values for boolen type", async () => {
const endpoint = AbiRegistry.create({
endpoints: [
{
name: "foo",
inputs: [
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
{
type: "bool",
},
],
outputs: [],
},
],
}).getEndpoint("foo");

let typedValues = NativeSerializer.nativeToTypedValues(
[true, "true", "TRUE", 1, false, "false", "falseBar", 0, 5],
endpoint,
);

assert.deepEqual(typedValues[0].valueOf(), true);
assert.deepEqual(typedValues[1].valueOf(), true);
assert.deepEqual(typedValues[2].valueOf(), true);
assert.deepEqual(typedValues[3].valueOf(), true);
assert.deepEqual(typedValues[4].valueOf(), false);
assert.deepEqual(typedValues[5].valueOf(), false);
assert.deepEqual(typedValues[6].valueOf(), false);
assert.deepEqual(typedValues[7].valueOf(), false);
assert.deepEqual(typedValues[8].valueOf(), false);
});
});
3 changes: 2 additions & 1 deletion src/smartcontracts/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ export namespace NativeSerializer {
return new AddressValue(convertNativeToAddress(native, errorContext));
}
if (type instanceof BooleanType) {
return new BooleanValue(native);
const boolValue = native.toString().toLowerCase() === "true" || native.toString() === "1";
return new BooleanValue(boolValue);
}
if (type instanceof TokenIdentifierType) {
return new TokenIdentifierValue(convertNativeToString(native, errorContext));
Expand Down

0 comments on commit 9f0c630

Please sign in to comment.