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

feat: add ARC22 and ARC28 interfaces for ABI contracts and methods #856

Merged
merged 4 commits into from
Jul 29, 2024
Merged
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
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;
@@ -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 (
@@ -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 {
@@ -42,6 +47,7 @@ export class ABIContract {
desc: this.description,
networks: this.networks,
methods: this.methods.map((method) => method.toJSON()),
events: this.events,
};
}

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
@@ -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
@@ -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;
@@ -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 (
@@ -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 {
@@ -147,6 +157,8 @@ export class ABIMethod {
type: this.returns.type.toString(),
desc: this.returns.description,
},
events: this.events,
readonly: this.readonly,
};
}