From b419005aa445cb7895b8509b1283336ec52f7559 Mon Sep 17 00:00:00 2001 From: ixje Date: Wed, 30 Aug 2023 08:47:30 +0200 Subject: [PATCH] fix(sc): support emitting Maps in ScriptBuilder (#912) --- docs/changelog/v5.md | 4 ++++ .../neon-core/__tests__/sc/ScriptBuilder.ts | 16 +++++++++++++ packages/neon-core/src/sc/ScriptBuilder.ts | 24 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/docs/changelog/v5.md b/docs/changelog/v5.md index dba4fff3..7f49fcd1 100644 --- a/docs/changelog/v5.md +++ b/docs/changelog/v5.md @@ -5,6 +5,10 @@ sidebar_label: Latest (v5) sidebar_position: 1 --- +# 5.5.1 +- sc + - Add support for `Map` type in `emitContractParam` function. + # 5.5.0 - wallet diff --git a/packages/neon-core/__tests__/sc/ScriptBuilder.ts b/packages/neon-core/__tests__/sc/ScriptBuilder.ts index 9c6df012..91c66ad1 100644 --- a/packages/neon-core/__tests__/sc/ScriptBuilder.ts +++ b/packages/neon-core/__tests__/sc/ScriptBuilder.ts @@ -103,6 +103,14 @@ describe("emitPush", () => { ], ["ContractParam(integer) -1", ContractParam.integer(-1), "0f"], ["ContractParam(integer) -12345", ContractParam.integer(-12345), "01c7cf"], + [ + "ContractParam(map) {1: 2}", + ContractParam.map({ + key: ContractParam.integer(1), + value: ContractParam.integer(2), + }), + "121111be", + ], ] as [string, ContractParam | string | boolean | number, string][])( "%s", ( @@ -214,6 +222,14 @@ describe("emitContractParam", () => { ContractParam.string("hello world"), "0c0b68656c6c6f20776f726c64", ], + [ + "ContractParam(map)", + ContractParam.map({ + key: ContractParam.integer(1), + value: ContractParam.integer(2), + }), + "121111be", + ], ])("%s", (_msg: string, data: ContractParam, expected: string) => { const result = new ScriptBuilder().emitContractParam(data).build(); expect(result).toBe(expected); diff --git a/packages/neon-core/src/sc/ScriptBuilder.ts b/packages/neon-core/src/sc/ScriptBuilder.ts index e73c8329..a472c6b3 100644 --- a/packages/neon-core/src/sc/ScriptBuilder.ts +++ b/packages/neon-core/src/sc/ScriptBuilder.ts @@ -8,6 +8,7 @@ import { } from "../u"; import { ContractParam, + ContractParamMap, ContractParamType, likeContractParam, } from "./ContractParam"; @@ -122,6 +123,27 @@ export class ScriptBuilder extends StringStream { return this.emitNumber(arr.length).emit(OpCode.PACK); } + /** + * Private method to append a map + */ + private emitMap(m: ContractParamMap): this { + for (let i = 0; i < m.length; i++) { + const keyType = m[i].key.type; + if ( + keyType !== ContractParamType.Boolean && + keyType !== ContractParamType.Integer && + keyType !== ContractParamType.String && + keyType !== ContractParamType.ByteArray + ) { + throw new Error(`Unsupported key type: ${keyType}`); + } + this.emitPush(m[i].value); + this.emitPush(m[i].key); + } + this.emitPush(m.length); + return this.emit(OpCode.PACKMAP); + } + /** * Appends a bytearray. */ @@ -290,6 +312,8 @@ export class ScriptBuilder extends StringStream { return this.emitHexString(param.value as HexString); case ContractParamType.PublicKey: return this.emitPublicKey(param.value as HexString); + case ContractParamType.Map: + return this.emitMap(param.value as ContractParamMap); default: throw new Error(`Unaccounted ContractParamType!: ${param.type}`); }