Skip to content

Commit

Permalink
added more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDoge committed Oct 15, 2024
1 parent 05e1ae0 commit aedfcb6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nomadshiba/typed-contracts",
"version": "0.2.4",
"version": "0.2.5",
"exports": {
"./abi": "./types/abi.ts",
"./ethers6": "./types/ethers6/exports.ts"
Expand Down
89 changes: 79 additions & 10 deletions types/abi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
/**
* Shape of Solidity ABI
* `Abi` represents the structure of an Ethereum contract's Application Binary Interface (ABI),
* which defines the functions, events, and errors that can be interacted with in the contract.
* It is a readonly array of `Abi.Item`, which can represent functions, events, or errors.
*
* @example
* Example ABI structure:
* ```ts
* const exampleAbi: Abi = [
* {
* type: "function",
* name: "transfer",
* inputs: [
* { name: "to", type: "address", internalType: "address" },
* { name: "value", type: "uint256", internalType: "uint256" },
* ],
* outputs: [],
* stateMutability: "nonpayable",
* },
* {
* type: "event",
* name: "Transfer",
* inputs: [
* { name: "from", type: "address", internalType: "address", indexed: true },
* { name: "to", type: "address", internalType: "address", indexed: true },
* { name: "value", type: "uint256", internalType: "uint256", indexed: false },
* ],
* anonymous: false,
* },
* ];
* ```
*/
export type Abi = readonly Abi.Item[]

export namespace Abi {
/**
* `Abi.Item` represents a single item in an ABI, which can be either a function, event, or error.
*/
export type Item = FunctionItem | EventItem | ErrorItem

type Argument = {
type: string
name: string
internalType: string
}

/**
* `Abi.FunctionItem` defines a function in a contract's ABI, including its name, inputs, outputs,
* and state mutability.
*/
export type FunctionItem =
| {
type: "function"
Expand All @@ -24,10 +55,24 @@ export namespace Abi {
inputs: readonly FunctionItem.Input[]
stateMutability: "nonpayable"
}

/**
* `Abi.FunctionItem.Input` and `Abi.FunctionItem.Output` represent the arguments and return values of a function.
* They both share the same structure, defined by `Abi.Argument`, which includes the argument's type, name,
* and internal type used in Solidity.
*/
export namespace FunctionItem {
export type Type = FunctionItem["type"]
export type Input = Argument & {}
export type Output = Argument & {}

/**
* `Abi.FunctionItem.StateMutability` defines the mutability of a function, which can be:
* - `pure`: does not read or modify the blockchain state.
* - `view`: reads but does not modify the blockchain state.
* - `constant`: synonym for `view` (legacy usage).
* - `nonpayable`: does not accept Ether but may modify the state.
* - `payable`: accepts Ether and may modify the state.
*/
export type StateMutability =
| "pure"
| "view"
Expand All @@ -36,24 +81,48 @@ export namespace Abi {
| "payable"
}

/**
* `Abi.EventItem` defines an event in the contract's ABI, which can be emitted by the contract.
* Each event has a name, inputs, and an `anonymous` flag indicating whether the event is anonymous.
*/
export type EventItem = {
type: "event"
name: string
inputs: readonly EventItem.Input[]
anonymous: boolean
}

export namespace EventItem {
export type Type = EventItem["type"]
/**
* `Abi.EventItem.Input` represents an argument to an event, with an additional `indexed` flag
* indicating whether the argument can be used to filter event logs.
*/
export type Input = Argument & { indexed: boolean }
}

/**
* `Abi.ErrorItem` defines an error in the contract's ABI, which can be used for custom error handling.
*/
export type ErrorItem = {
type: "error"
name: string
inputs: readonly ErrorItem.Input[]
}

export namespace ErrorItem {
export type Type = ErrorItem["type"]
/**
* `Abi.ErrorItem.Input` represents an argument to an error.
*/
export type Input = Argument & {}
}

/**
* `Abi.Argument` represents a single argument in a function, event, or error.
* It includes the argument's `type`, `name`, and `internalType` used by Solidity.
*/
type Argument = {
type: string
name: string
internalType: string
}
}

0 comments on commit aedfcb6

Please sign in to comment.