Skip to content

Commit

Permalink
feat: add ARC22 and ARC28 interfaces for ABI contracts and methods (#856
Browse files Browse the repository at this point in the history
)
  • Loading branch information
joe-p authored Jul 29, 2024
1 parent 5b6eef5 commit 7f2e734
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/abi/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ABIMethod, ABIMethodParams, getMethodByName } from './method';
import { ARC28Event } from './event';

export interface ABIContractNetworkInfo {
appID: number;
Expand All @@ -13,13 +14,16 @@ export interface ABIContractParams {
desc?: string;
networks?: ABIContractNetworks;
methods: ABIMethodParams[];
events?: ARC28Event[];
}

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

constructor(params: ABIContractParams) {
if (
Expand All @@ -34,6 +38,7 @@ export class ABIContract {
this.description = params.desc;
this.networks = params.networks ? { ...params.networks } : {};
this.methods = params.methods.map((method) => new ABIMethod(method));
this.events = params.events;
}

toJSON(): ABIContractParams {
Expand All @@ -42,6 +47,7 @@ export class ABIContract {
desc: this.description,
networks: this.networks,
methods: this.methods.map((method) => method.toJSON()),
events: this.events,
};
}

Expand Down
16 changes: 16 additions & 0 deletions src/abi/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) event description */
export interface ARC28Event {
/** The name of the event */
name: string;
/** Optional, user-friendly description for the event */
desc?: string;
/** The arguments of the event, in order */
args: Array<{
/** The type of the argument */
type: string;
/** Optional, user-friendly name for the argument */
name?: string;
/** Optional, user-friendly description for the argument */
desc?: string;
}>;
}
12 changes: 12 additions & 0 deletions src/abi/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { genericHash } from '../nacl/naclWrappers';
import { ABIType, ABITupleType } from './abi_type';
import { ABITransactionType, abiTypeIsTransaction } from './transaction';
import { ABIReferenceType, abiTypeIsReference } from './reference';
import { ARC28Event } from './event';

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

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

public readonly returns: { type: ABIReturnType; description?: string };
public readonly events?: ARC28Event[];
public readonly readonly?: boolean;

constructor(params: ABIMethodParams) {
if (
Expand Down Expand Up @@ -111,6 +118,9 @@ export class ABIMethod {
: ABIType.from(params.returns.type),
description: params.returns.desc,
};

this.events = params.events;
this.readonly = params.readonly;
}

getSignature(): string {
Expand Down Expand Up @@ -147,6 +157,8 @@ export class ABIMethod {
type: this.returns.type.toString(),
desc: this.returns.description,
},
events: this.events,
readonly: this.readonly,
};
}

Expand Down

0 comments on commit 7f2e734

Please sign in to comment.