Skip to content

Commit

Permalink
feat: add helper method to Abi Interface (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight committed Jul 25, 2023
1 parent 70c065e commit b4d78c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-apples-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": patch
---

feat: add helper method "getTypeById" to Abi Interface
10 changes: 10 additions & 0 deletions packages/abi-coder/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value);
}

getTypeById(typeId: number) {
return findOrThrow(
this.jsonAbi.types,
(t) => t.typeId === typeId,
() => {
throw new Error(`type with typeId '${typeId}' doesn't exist`);
}
);
}
}
15 changes: 15 additions & 0 deletions packages/abi-coder/test/interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,19 @@ describe('Abi interface', () => {
});
});
});

describe('abi types', () => {
it('should return the correct type when it exists', () => {
const abiType = exhaustiveExamplesInterface.getTypeById(22);
expect(abiType.type).toEqual('enum EnumWithStructs');
expect(abiType.components).toBeDefined();
expect(abiType.typeParameters).toBeNull();
});

it('should throw an error when type does not exist', () => {
expect(() => exhaustiveExamplesInterface.getTypeById(999)).toThrowError(
"type with typeId '999' doesn't exist"
);
});
});
});

0 comments on commit b4d78c1

Please sign in to comment.