-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ts: Correctly construct field layout for type aliases (#2821)
- Loading branch information
1 parent
5d5cade
commit 253501a
Showing
3 changed files
with
51 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as assert from "assert"; | ||
import { BorshCoder } from "../src"; | ||
import { IdlType } from "../src/idl"; | ||
import { toInstruction } from "../src/program/common"; | ||
|
||
describe("coder.instructions", () => { | ||
test("Can encode and decode type aliased instruction arguments (byte array)", () => { | ||
const idl = { | ||
version: "0.1.0", | ||
name: "test", | ||
instructions: [ | ||
{ | ||
name: "initialize", | ||
accounts: [], | ||
args: [ | ||
{ | ||
name: "arg", | ||
type: { | ||
defined: "AliasTest", | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
types: [ | ||
{ | ||
name: "AliasTest", | ||
type: { | ||
kind: "alias" as const, | ||
value: { | ||
array: ["u8", 3] as [IdlType, number], | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const idlIx = idl.instructions[0]; | ||
const expected = [1, 2, 3]; | ||
|
||
const coder = new BorshCoder(idl); | ||
const ix = toInstruction(idlIx, expected); | ||
|
||
const encoded = coder.instruction.encode(idlIx.name, ix); | ||
const decoded = coder.instruction.decode(encoded, "hex", idlIx.name); | ||
|
||
assert.deepStrictEqual(decoded?.data[idlIx.args[0].name], expected); | ||
}); | ||
}); |