ts: Correctly construct field layout for type aliases (#2821)

This commit is contained in:
Jayden Park 2024-02-20 06:59:48 +11:00 committed by GitHub
parent 5d5cadebdd
commit 253501aa89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 4 deletions

View File

@ -44,6 +44,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: Fix formatting enums ([#2763](https://github.com/coral-xyz/anchor/pull/2763)).
- cli: Fix `migrate` command not working without global `ts-node` installation ([#2767](https://github.com/coral-xyz/anchor/pull/2767)).
- client, lang, spl, syn: Enable all features for docs.rs build ([#2774](https://github.com/coral-xyz/anchor/pull/2774)).
- ts: Fix construction of field layouts for type aliased instruction arguments ([#2821](https://github.com/coral-xyz/anchor/pull/2821))
### Breaking

View File

@ -164,10 +164,7 @@ export class IdlCoder {
}
case "alias": {
return IdlCoder.fieldLayout(
{ type: typeDef.type.value, name: typeDef.name },
types
);
return IdlCoder.fieldLayout({ type: typeDef.type.value, name }, types);
}
}
}

View File

@ -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);
});
});