Skip to content

Commit 7f2e734

Browse files
authored
feat: add ARC22 and ARC28 interfaces for ABI contracts and methods (#856)
1 parent 5b6eef5 commit 7f2e734

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/abi/contract.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ABIMethod, ABIMethodParams, getMethodByName } from './method';
2+
import { ARC28Event } from './event';
23

34
export interface ABIContractNetworkInfo {
45
appID: number;
@@ -13,13 +14,16 @@ export interface ABIContractParams {
1314
desc?: string;
1415
networks?: ABIContractNetworks;
1516
methods: ABIMethodParams[];
17+
events?: ARC28Event[];
1618
}
1719

1820
export class ABIContract {
1921
public readonly name: string;
2022
public readonly description?: string;
2123
public readonly networks: ABIContractNetworks;
2224
public readonly methods: ABIMethod[];
25+
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) events that MAY be emitted by this contract */
26+
public readonly events?: ARC28Event[];
2327

2428
constructor(params: ABIContractParams) {
2529
if (
@@ -34,6 +38,7 @@ export class ABIContract {
3438
this.description = params.desc;
3539
this.networks = params.networks ? { ...params.networks } : {};
3640
this.methods = params.methods.map((method) => new ABIMethod(method));
41+
this.events = params.events;
3742
}
3843

3944
toJSON(): ABIContractParams {
@@ -42,6 +47,7 @@ export class ABIContract {
4247
desc: this.description,
4348
networks: this.networks,
4449
methods: this.methods.map((method) => method.toJSON()),
50+
events: this.events,
4551
};
4652
}
4753

src/abi/event.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) event description */
2+
export interface ARC28Event {
3+
/** The name of the event */
4+
name: string;
5+
/** Optional, user-friendly description for the event */
6+
desc?: string;
7+
/** The arguments of the event, in order */
8+
args: Array<{
9+
/** The type of the argument */
10+
type: string;
11+
/** Optional, user-friendly name for the argument */
12+
name?: string;
13+
/** Optional, user-friendly description for the argument */
14+
desc?: string;
15+
}>;
16+
}

src/abi/method.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { genericHash } from '../nacl/naclWrappers';
22
import { ABIType, ABITupleType } from './abi_type';
33
import { ABITransactionType, abiTypeIsTransaction } from './transaction';
44
import { ABIReferenceType, abiTypeIsReference } from './reference';
5+
import { ARC28Event } from './event';
56

67
function parseMethodSignature(
78
signature: string
@@ -61,6 +62,10 @@ export interface ABIMethodParams {
6162
desc?: string;
6263
args: ABIMethodArgParams[];
6364
returns: ABIMethodReturnParams;
65+
/** Optional, is it a read-only method (according to [ARC-22](https://arc.algorand.foundation/ARCs/arc-0022)) */
66+
readonly?: boolean;
67+
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) events that MAY be emitted by this method */
68+
events?: ARC28Event[];
6469
}
6570

6671
export type ABIArgumentType = ABIType | ABITransactionType | ABIReferenceType;
@@ -77,6 +82,8 @@ export class ABIMethod {
7782
}>;
7883

7984
public readonly returns: { type: ABIReturnType; description?: string };
85+
public readonly events?: ARC28Event[];
86+
public readonly readonly?: boolean;
8087

8188
constructor(params: ABIMethodParams) {
8289
if (
@@ -111,6 +118,9 @@ export class ABIMethod {
111118
: ABIType.from(params.returns.type),
112119
description: params.returns.desc,
113120
};
121+
122+
this.events = params.events;
123+
this.readonly = params.readonly;
114124
}
115125

116126
getSignature(): string {
@@ -147,6 +157,8 @@ export class ABIMethod {
147157
type: this.returns.type.toString(),
148158
desc: this.returns.description,
149159
},
160+
events: this.events,
161+
readonly: this.readonly,
150162
};
151163
}
152164

0 commit comments

Comments
 (0)