Skip to content

Commit

Permalink
fix(sc): support emitting Maps in ScriptBuilder (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje authored Aug 30, 2023
1 parent adf1859 commit b419005
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/changelog/v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions packages/neon-core/__tests__/sc/ScriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
(
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions packages/neon-core/src/sc/ScriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../u";
import {
ContractParam,
ContractParamMap,
ContractParamType,
likeContractParam,
} from "./ContractParam";
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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}`);
}
Expand Down

0 comments on commit b419005

Please sign in to comment.