Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading