Skip to content

Commit

Permalink
ts: Correctly construct field layout for type aliases (#2821)
Browse files Browse the repository at this point in the history
  • Loading branch information
halfbakedsneed authored Feb 19, 2024
1 parent 5d5cade commit 253501a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions ts/packages/anchor/src/coder/borsh/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions ts/packages/anchor/tests/coder-instructions.spec.ts
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);
});
});

0 comments on commit 253501a

Please sign in to comment.