Skip to content

Commit

Permalink
Merge branch 'feat/next' into TOOL-276-implement-facade
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 13, 2024
2 parents 6bc9456 + 404b714 commit bb29def
Show file tree
Hide file tree
Showing 36 changed files with 604 additions and 141 deletions.
16 changes: 8 additions & 8 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.12.0",
"version": "13.15.0",
"description": "MultiversX SDK for JavaScript and TypeScript",
"author": "MultiversX",
"homepage": "https://multiversx.com",
Expand Down
2 changes: 1 addition & 1 deletion src/abi/codeMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("CodeMetadata Class Tests", function () {
CodeMetadata.fromBuffer(buffer);
},
Error,
"Buffer is too short.",
"code metadata buffer has length 1, expected 2",
);
});

Expand Down
7 changes: 4 additions & 3 deletions src/abi/codeMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CodeMetadataLength = 2;

/**
* The metadata of a Smart Contract, as an abstraction.
*/
Expand All @@ -6,7 +8,6 @@ export class CodeMetadata {
public readable: boolean;
public payable: boolean;
public payableBySc: boolean;
private static readonly codeMetadataLength = 2;

static ByteZero = {
Upgradeable: 1,
Expand Down Expand Up @@ -48,8 +49,8 @@ export class CodeMetadata {
* Creates a metadata object from a buffer.
*/
static fromBuffer(buffer: Buffer): CodeMetadata {
if (buffer.length < this.codeMetadataLength) {
throw new Error("Buffer is too short.");
if (buffer.length != CodeMetadataLength) {
throw new Error(`code metadata buffer has length ${buffer.length}, expected ${CodeMetadataLength}`);
}

const byteZero = buffer[0];
Expand Down
62 changes: 39 additions & 23 deletions src/abi/codec/binary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import * as errors from "../../errors";
import BigNumber from "bignumber.js";
import { assert } from "chai";
import { BinaryCodec, BinaryCodecConstraints } from "./binary";
import { Address } from "../../address";
import * as errors from "../../errors";
import {
AddressType,
AddressValue,
ArrayVec,
ArrayVecType,
BigIntType,
BigIntValue,
BigUIntType,
BigUIntValue,
BooleanType,
BooleanValue,
EnumType,
EnumValue,
EnumVariantDefinition,
Field,
I16Type,
I16Value,
I32Type,
I32Value,
I64Type,
I64Value,
I8Type,
I8Value,
List,
ListType,
NumericalType,
NumericalValue,
StringType,
StringValue,
Struct,
Field,
StructType,
TokenIdentifierType,
TokenIdentifierValue,
TypedValue,
U16Type,
U16Value,
U32Type,
U32Value,
U64Type,
U64Value,
U8Type,
U8Value,
List,
ListType,
EnumType,
EnumVariantDefinition,
EnumValue,
ArrayVec,
ArrayVecType,
U16Value,
TokenIdentifierType,
TokenIdentifierValue,
StringValue,
StringType,
BigIntValue,
I64Value,
I32Value,
I16Value,
I8Value,
} from "../typesystem";
import { isMsbOne } from "./utils";
import { Address } from "../../address";
import { BytesType, BytesValue } from "../typesystem/bytes";
import BigNumber from "bignumber.js";
import { ExplicitEnumType, ExplicitEnumValue, ExplicitEnumVariantDefinition } from "../typesystem/explicit-enum";
import { FieldDefinition } from "../typesystem/fields";
import { BinaryCodec, BinaryCodecConstraints } from "./binary";
import { isMsbOne } from "./utils";

describe("test binary codec (basic)", () => {
let codec = new BinaryCodec();
Expand Down Expand Up @@ -175,6 +176,21 @@ describe("test binary codec (basic)", () => {
]);
assert.deepEqual(codec.decodeTopLevel<StringValue>(Buffer.from(payload), new StringType()), stringValue);
});

it("should create explicit-enums, encode and decode", async () => {
let length = [0x00, 0x00, 0x00, 0x04];
let payload = [0x74, 0x65, 0x73, 0x74];
let enumType = new ExplicitEnumType("Colour", [new ExplicitEnumVariantDefinition("test")]);
let enumValue = ExplicitEnumValue.fromName(enumType, "test");

assert.deepEqual(codec.encodeNested(enumValue), Buffer.from([...length, ...payload]));
assert.deepEqual(codec.encodeTopLevel(enumValue), Buffer.from(payload));
assert.deepEqual(codec.decodeNested<ExplicitEnumValue>(Buffer.from([...length, ...payload]), enumType), [
enumValue,
8,
]);
assert.deepEqual(codec.decodeTopLevel<ExplicitEnumValue>(Buffer.from(payload), enumType), enumValue);
});
});

describe("test binary codec (advanced)", () => {
Expand Down
41 changes: 25 additions & 16 deletions src/abi/codec/binary.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import * as errors from "../../errors";
import { guardTrue } from "../../utils";
import {
Type,
ArrayVec,
ArrayVecType,
EnumType,
EnumValue,
ExplicitEnumType,
ExplicitEnumValue,
List,
ManagedDecimalSignedType,
ManagedDecimalSignedValue,
ManagedDecimalType,
ManagedDecimalValue,
onTypedValueSelect,
onTypeSelect,
OptionValue,
PrimitiveType,
PrimitiveValue,
Struct,
StructType,
TypedValue,
EnumValue,
TupleType,
Tuple,
ArrayVecType,
ArrayVec,
ManagedDecimalType,
ManagedDecimalValue,
ManagedDecimalSignedType,
ManagedDecimalSignedValue,
TupleType,
Type,
TypedValue,
} from "../typesystem";
import { guardTrue } from "../../utils";
import { ArrayVecBinaryCodec } from "./arrayVec";
import { EnumBinaryCodec } from "./enum";
import { ExplicitEnumBinaryCodec } from "./explicit-enum";
import { ListBinaryCodec } from "./list";
import { ManagedDecimalCodec } from "./managedDecimal";
import { ManagedDecimalSignedCodec } from "./managedDecimalSigned";
import { OptionValueBinaryCodec } from "./option";
import { PrimitiveBinaryCodec } from "./primitive";
import { ListBinaryCodec } from "./list";
import { StructBinaryCodec } from "./struct";
import { EnumBinaryCodec } from "./enum";
import { TupleBinaryCodec } from "./tuple";
import { ArrayVecBinaryCodec } from "./arrayVec";
import { ManagedDecimalCodec } from "./managedDecimal";
import { ManagedDecimalSignedCodec } from "./managedDecimalSigned";

export class BinaryCodec {
readonly constraints: BinaryCodecConstraints;
Expand All @@ -41,6 +44,7 @@ export class BinaryCodec {
private readonly structCodec: StructBinaryCodec;
private readonly tupleCodec: TupleBinaryCodec;
private readonly enumCodec: EnumBinaryCodec;
private readonly explicitEnumCodec: ExplicitEnumBinaryCodec;
private readonly managedDecimalCodec: ManagedDecimalCodec;
private readonly managedDecimalSignedCodec: ManagedDecimalSignedCodec;

Expand All @@ -53,6 +57,7 @@ export class BinaryCodec {
this.structCodec = new StructBinaryCodec(this);
this.tupleCodec = new TupleBinaryCodec(this);
this.enumCodec = new EnumBinaryCodec(this);
this.explicitEnumCodec = new ExplicitEnumBinaryCodec();
this.managedDecimalCodec = new ManagedDecimalCodec(this);
this.managedDecimalSignedCodec = new ManagedDecimalSignedCodec(this);
}
Expand All @@ -68,6 +73,7 @@ export class BinaryCodec {
onStruct: () => this.structCodec.decodeTopLevel(buffer, <StructType>type),
onTuple: () => this.tupleCodec.decodeTopLevel(buffer, <TupleType>type),
onEnum: () => this.enumCodec.decodeTopLevel(buffer, <EnumType>type),
onExplicitEnum: () => this.explicitEnumCodec.decodeTopLevel(buffer, <ExplicitEnumType>type),
onManagedDecimal: () => this.managedDecimalCodec.decodeTopLevel(buffer, <ManagedDecimalType>type),
onManagedDecimalSigned: () =>
this.managedDecimalSignedCodec.decodeTopLevel(buffer, <ManagedDecimalSignedType>type),
Expand All @@ -87,6 +93,7 @@ export class BinaryCodec {
onStruct: () => this.structCodec.decodeNested(buffer, <StructType>type),
onTuple: () => this.tupleCodec.decodeNested(buffer, <TupleType>type),
onEnum: () => this.enumCodec.decodeNested(buffer, <EnumType>type),
onExplicitEnum: () => this.explicitEnumCodec.decodeNested(buffer, <ExplicitEnumType>type),
onManagedDecimal: () => this.managedDecimalCodec.decodeNested(buffer, <ManagedDecimalType>type),
onManagedDecimalSigned: () =>
this.managedDecimalSignedCodec.decodeNested(buffer, <ManagedDecimalSignedType>type),
Expand All @@ -106,6 +113,7 @@ export class BinaryCodec {
onStruct: () => this.structCodec.encodeNested(<Struct>typedValue),
onTuple: () => this.tupleCodec.encodeNested(<Tuple>typedValue),
onEnum: () => this.enumCodec.encodeNested(<EnumValue>typedValue),
onExplicitEnum: () => this.explicitEnumCodec.encodeNested(<ExplicitEnumValue>typedValue),
onManagedDecimal: () => this.managedDecimalCodec.encodeNested(<ManagedDecimalValue>typedValue),
onManagedDecimalSigned: () =>
this.managedDecimalSignedCodec.encodeNested(<ManagedDecimalSignedValue>typedValue),
Expand All @@ -123,6 +131,7 @@ export class BinaryCodec {
onStruct: () => this.structCodec.encodeTopLevel(<Struct>typedValue),
onTuple: () => this.tupleCodec.encodeTopLevel(<Tuple>typedValue),
onEnum: () => this.enumCodec.encodeTopLevel(<EnumValue>typedValue),
onExplicitEnum: () => this.explicitEnumCodec.encodeTopLevel(<ExplicitEnumValue>typedValue),
onManagedDecimal: () => this.managedDecimalCodec.encodeTopLevel(<ManagedDecimalValue>typedValue),
onManagedDecimalSigned: () =>
this.managedDecimalSignedCodec.encodeTopLevel(<ManagedDecimalSignedValue>typedValue),
Expand Down
8 changes: 3 additions & 5 deletions src/abi/codec/codemetadata.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { CodeMetadata } from "../codeMetadata";
import { CodeMetadata, CodeMetadataLength } from "../codeMetadata";
import { CodeMetadataValue } from "../typesystem/codeMetadata";

export class CodeMetadataCodec {
decodeNested(buffer: Buffer): [CodeMetadataValue, number] {
const codeMetadata = CodeMetadata.fromBuffer(buffer);

return [new CodeMetadataValue(codeMetadata), length];
const codeMetadata = CodeMetadata.fromBuffer(buffer.slice(0, CodeMetadataLength));
return [new CodeMetadataValue(codeMetadata), CodeMetadataLength];
}

decodeTopLevel(buffer: Buffer): CodeMetadataValue {
const codeMetadata = CodeMetadata.fromBuffer(buffer);

return new CodeMetadataValue(codeMetadata);
}

Expand Down
33 changes: 33 additions & 0 deletions src/abi/codec/explicit-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { StringValue } from "../typesystem";
import { ExplicitEnumType, ExplicitEnumValue, ExplicitEnumVariantDefinition } from "../typesystem/explicit-enum";
import { StringBinaryCodec } from "./string";

export class ExplicitEnumBinaryCodec {
private readonly stringCodec: StringBinaryCodec;

constructor() {
this.stringCodec = new StringBinaryCodec();
}

decodeTopLevel(buffer: Buffer, type: ExplicitEnumType): ExplicitEnumValue {
const stringValue = this.stringCodec.decodeTopLevel(buffer);
return new ExplicitEnumValue(type, new ExplicitEnumVariantDefinition(stringValue.valueOf()));
}

decodeNested(buffer: Buffer, type: ExplicitEnumType): [ExplicitEnumValue, number] {
const [value, length] = this.stringCodec.decodeNested(buffer);
const enumValue = new ExplicitEnumValue(type, new ExplicitEnumVariantDefinition(value.valueOf()));

return [enumValue, length];
}

encodeNested(enumValue: ExplicitEnumValue): Buffer {
const buffer = this.stringCodec.encodeNested(new StringValue(enumValue.valueOf().name));
return buffer;
}

encodeTopLevel(enumValue: ExplicitEnumValue): Buffer {
const buffer = this.stringCodec.encodeTopLevel(new StringValue(enumValue.valueOf().name));
return buffer;
}
}
3 changes: 1 addition & 2 deletions src/abi/interaction.local.net.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import { ContractController } from "../testutils/contractController";
import { createLocalnetProvider } from "../testutils/networkProviders";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig";
import { TransactionsFactoryConfig } from "../transactionsFactoryConfig";
import { TransactionWatcher } from "../transactionWatcher";
import { Interaction } from "./interaction";
import { ResultsParser } from "./resultsParser";
import { ReturnCode } from "./returnCode";
import { SmartContract } from "./smartContract";
import { ManagedDecimalSignedValue, ManagedDecimalValue } from "./typesystem";

describe("test smart contract interactor", function () {
let provider = createLocalnetProvider();
Expand Down
5 changes: 2 additions & 3 deletions src/abi/interaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractQueryResponse } from "../networkProviders";
import BigNumber from "bignumber.js";
import { assert } from "chai";
import { Address } from "../address";
import { ContractQueryResponse } from "../networkProviders";
import {
loadAbiRegistry,
loadTestWallets,
Expand All @@ -16,8 +16,7 @@ import { ContractFunction } from "./function";
import { Interaction } from "./interaction";
import { ReturnCode } from "./returnCode";
import { SmartContract } from "./smartContract";
import { BigUIntValue, OptionalValue, OptionValue, TokenIdentifierValue, U32Value } from "./typesystem";
import { BytesValue } from "./typesystem/bytes";
import { BigUIntValue, BytesValue, OptionalValue, OptionValue, TokenIdentifierValue, U32Value } from "./typesystem";

describe("test smart contract interactor", function () {
let dummyAddress = new Address("erd1qqqqqqqqqqqqqpgqak8zt22wl2ph4tswtyc39namqx6ysa2sd8ss4xmlj3");
Expand Down
2 changes: 1 addition & 1 deletion src/abi/interactionChecker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { SmartContract } from "./smartContract";
import {
BigUIntType,
BigUIntValue,
BytesValue,
OptionalType,
OptionalValue,
OptionValue,
TokenIdentifierValue,
U32Value,
} from "./typesystem";
import { BytesValue } from "./typesystem/bytes";

describe("integration tests: test checker within interactor", function () {
let dummyAddress = new Address("erd1qqqqqqqqqqqqqpgqak8zt22wl2ph4tswtyc39namqx6ysa2sd8ss4xmlj3");
Expand Down
Loading

0 comments on commit bb29def

Please sign in to comment.