sdk.md 8.79 KB
Newer Older
1
# AttestationStation sdk docs
Will Cory's avatar
Will Cory committed
2 3 4

Typescript sdk for interacting with the ATST based on [@wagmi/core](https://wagmi.sh/core/getting-started)

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

## Table of content 

- [Installation](#installation)
- [Basic usage](#basic-usage)
  - [Setup](#setup)
  - [Reading an attestation](#reading-an-attestation)
  - [Reading multiple attestation](#reading-multiple-attestations)
  - [Writing an attestation](#writing-an-attestation)
- [API](#api)
  - [High level API](#high-level-api)
  - [Low level API](#low-level-api)
  - [React API](#react-api)
- [Tutorial](#tutorial)


Will Cory's avatar
Will Cory committed
21 22 23

## Installation

24
Install atst and its peer dependencies.
Will Cory's avatar
Will Cory committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

npm

```bash
npm i @eth-optimism/atst @wagmi/core ethers@5.7.0
```

pnpm

```bash
pnpm i @eth-optimism/atst @wagmi/core ethers@5.7.0
```

yarn

```bash
yarn add @eth-optimism/atst @wagmi/core ethers@5.7.0
```

44 45
**Note:** As ethers v6 is not yet stable we only support ethers v5 at this time

Will Cory's avatar
Will Cory committed
46 47 48

## Basic usage

49
### Setup
Will Cory's avatar
Will Cory committed
50

51 52
atst uses `@wagmi/core` under the hood. 
[See their documentation for more information](https://wagmi.sh/core/getting-started).
Will Cory's avatar
Will Cory committed
53

54 55 56 57 58
```javascript
import ethers from "ethers"
const wagmiCore = await import("@wagmi/core")
const wagmiAlchemy = await import("@wagmi/core/providers/alchemy")
const wagmiChains = await import("@wagmi/core/chains")
Will Cory's avatar
Will Cory committed
59

60 61 62 63
const { chains, provider, webSocketProvider } = wagmiCore.configureChains(
  [wagmiChains.optimismGoerli],
  [wagmiAlchemy.alchemyProvider({ apiKey: process.env.ALCHEMY_API_KEY })],
)
Will Cory's avatar
Will Cory committed
64

65
wagmiCore.createClient({
Will Cory's avatar
Will Cory committed
66
  provider,
67
  webSocketProvider
Will Cory's avatar
Will Cory committed
68 69 70
})
```

Will Cory's avatar
Will Cory committed
71

72
### Reading an attestation
Will Cory's avatar
Will Cory committed
73

Will Cory's avatar
Will Cory committed
74 75
Here is an example of reading an attestation used by the optimist nft

76 77
```javascript
import { readAttestation } from '@eth-optimism/atst'
Will Cory's avatar
Will Cory committed
78 79 80 81 82

const creator = '0x60c5C9c98bcBd0b0F2fD89B24c16e533BaA8CdA3'
const about = '0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5'
const key = 'optimist.base-uri'

Will Cory's avatar
Will Cory committed
83
const str = await readAttestationString(creator, about, key)
84 85
console.log(attestation) 
// https://assets.optimism.io/4a609661-6774-441f-9fdb-453fdbb89931-bucket/optimist-nft/attributes
Will Cory's avatar
Will Cory committed
86 87
```

88
### Reading multiple attestations
Will Cory's avatar
Will Cory committed
89

90
If reading more than one attestation you can use [`readAttestations`](#readattestations) to read them with [multicall](https://www.npmjs.com/package/ethereum-multicall).
Will Cory's avatar
Will Cory committed
91 92 93

### Writing an attestation

94 95
To write to an attestation you must [connect](https://wagmi.sh/core/connectors/metaMask) your wagmi client if not already connected. 
If using Node.js use the [mock connector](https://wagmi.sh/core/connectors/mock).
Will Cory's avatar
Will Cory committed
96 97

```typescript
98
import { prepareWriteAttestation, writeAttestation } from '@eth-optimism/atst'
Will Cory's avatar
Will Cory committed
99 100 101 102 103 104 105 106 107

const preparedTx = await prepareWriteAttestation(about, key, 'hello world')
console.log(preparedTx.gasLimit)
await writeAttestation(preparedTx)
```

## API


108 109 110 111 112 113 114 115
### High level API

These functions are the easiest way to interact with the AttestationStation contract.
For a more detailed explanation, [see the tutorial](https://github.com/ethereum-optimism/optimism-tutorial/tree/main/ecosystem/attestation-station/using-sdk).

#### `getEvents`

Use `getEvents` to get attestation events using a provider and filters. 
Will Cory's avatar
Will Cory committed
116 117

```typescript
118 119 120 121 122 123 124 125 126
const events = await getEvents({
  creator,
  about,
  key,
  value,
  provider: new ethers.providers.JsonRpcProvider('http://localhost:8545'),
  fromBlockOrBlockhash,
  toBlock,
})
Will Cory's avatar
Will Cory committed
127 128
```

129
Set `key`, `about`, `creator`, or `value` to `null` to not filter that value.
Will Cory's avatar
Will Cory committed
130

131 132 133 134
#### `prepareWriteAttestation`

[Prepares](https://wagmi.sh/core/actions/prepareWriteContract) an attestation to be written.
This function creates the transaction data, estimates the gas cost, etc.
Will Cory's avatar
Will Cory committed
135 136

```typescript
137 138
const preparedTx = await prepareWriteAttestation(about, key, 'hello world')
console.log(preparedTx.gasFee)
Will Cory's avatar
Will Cory committed
139 140
```

141 142 143
**Return Value:** A prepared transaction, ready for [`writeAttestation`](#writeattestation).

#### `readAttestation`
Will Cory's avatar
Will Cory committed
144

145
[Reads](https://wagmi.sh/core/actions/readContract) and parses an attestation based on its data type.
Will Cory's avatar
Will Cory committed
146 147 148

```typescript
const attestation = await readAttestation(
149 150 151 152 153
  creator,        // Address: The creator of the attestation
  about,          // Address: The about topic of the attestation
  key,            // string: The key of the attestation
  dataType,       // Optional, the data type of the attestation, 'string' | 'bytes' | 'number' | 'bool' | 'address'
  contractAddress // Optional address: the contract address of AttestationStation
Will Cory's avatar
Will Cory committed
154 155 156
)
```

157 158
**Return Value:** The value attested by the `creator` on the `about` address concerning the `key`, when interpreted as the `dataType`.
If there is no such attestation the result is zero, `false`, or an empty string.
Will Cory's avatar
Will Cory committed
159

160
#### `readAttestations`
Will Cory's avatar
Will Cory committed
161

162 163 164
Similar to `readAttestation` but reads multiple attestations at once. 
Pass in a variadic amount of attestations to read.
The parameters are all the same structure type, the one shown below.
Will Cory's avatar
Will Cory committed
165 166

```typescript
167 168 169 170 171 172 173
const attestationList = await readAttestations({
  creator: <creator address>,
  about: <about address>,
  key: <attestation key (string)>,
  [dataType: <data type - 'string' | 'bytes' | 'number' | 'bool' | 'address'>,]
  [contractAddress: <contract address, if not the default>]
}, {...}, {...})
Will Cory's avatar
Will Cory committed
174 175
```

176 177
**Return Value:** A list of values attested by the `creator` on the `about` address concerning the `key`, when interpreted as the `dataType`.

Will Cory's avatar
Will Cory committed
178

Will Cory's avatar
Will Cory committed
179

180
#### `writeAttestation`
Will Cory's avatar
Will Cory committed
181

182
[Writes the prepared tx](https://wagmi.sh/core/actions/writeContract).
Will Cory's avatar
Will Cory committed
183 184

```typescript
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
const preparedTx = await prepareWriteAttestation(about, key, 'hello world')
await writeAttestation(preparedTx)
```

### Low level API

These definitions allow you to communicate with AttestationStation, but are not as easy to use as the high level API.

#### `ATTESTATION_STATION_ADDRESS`


The deployment address for AttestationStation currently deployed with create2 on Optimism and Optimism Goerli `0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77`.

```typescript
import { ATTESTATION_STATION_ADDRESS } from '@eth-optimism/atst'
Will Cory's avatar
Will Cory committed
200 201
```

202
#### `abi`
Will Cory's avatar
Will Cory committed
203

204 205 206 207 208 209 210 211 212 213
The abi of the AttestationStation contract

```typescript
import { abi } from '@eth-optimism/atst'
```

#### `createKey`


`createKey` hashes keys longer than 31 bytes, because the atst key size is limited to 32 bytes.
Will Cory's avatar
Will Cory committed
214 215

```typescript
216
const key = await createKey(
Will Cory's avatar
Will Cory committed
217 218 219 220
  'i.am.a.key.much.longer.than.32.bytes.long'
)
```

221
createKey will keep the key as is if it is shorter than 32 bytes and otherwise run it through keccak256.
Will Cory's avatar
Will Cory committed
222

Will Cory's avatar
Will Cory committed
223

224
#### `parseAddress`
Will Cory's avatar
Will Cory committed
225

226
Turn bytes into an address.
Will Cory's avatar
Will Cory committed
227

228 229
**Note:** `readAttestation` and `readAttestations` already do this for you.
This is only needed if talking to the contracts directly, or through a different library.
Will Cory's avatar
Will Cory committed
230

231 232 233 234 235 236 237 238 239
#### `parseBool`

Turn bytes into a boolean value.

**Note:** `readAttestation` and `readAttestations` already do this for you.
This is only needed if talking to the contracts directly, or through a different library.


#### `parseNumber`
Will Cory's avatar
Will Cory committed
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
Turn bytes into a number.

**Note:** `readAttestation` and `readAttestations` already do this for you.
This is only needed if talking to the contracts directly, or through a different library.


#### `parseString`

Turn bytes into a string.

**Note:** `readAttestation` and `readAttestations` already do this for you.
This is only needed if talking to the contracts directly, or through a different library.


#### `stringifyAttestationBytes`

Stringifys an attestation into raw bytes.
Will Cory's avatar
Will Cory committed
258 259

```typescript
260
const stringAttestation = stringifyAttestationBytes('hello world')
Will Cory's avatar
Will Cory committed
261 262 263 264 265 266 267
const numberAttestation = stringifyAttestationBytes(500)
const hexAttestation = stringifyAttestationBytes('0x1')
const bigNumberAttestation = stringifyAttestationBytes(
  BigNumber.from('9999999999999999999999999')
)
```

268
**Note:** `writeAttestation` already does this for you so this is only needed if using a library other than `atst`.
Will Cory's avatar
Will Cory committed
269

270
### React API
Will Cory's avatar
Will Cory committed
271

272
For react hooks we recomend using the [wagmi cli](https://wagmi.sh/cli/getting-started) with the [etherscan plugin](https://wagmi.sh/cli/plugins/etherscan) and [react plugin](https://wagmi.sh/cli/plugins/react) to automatically generate react hooks around AttestationStation.
Will Cory's avatar
Will Cory committed
273

274 275
Use `createKey` and `createValue` to convert your raw keys and values into bytes that can be used in AttestationStation contract calls.
Use `parseString`, `parseBool`, `parseAddress` and `parseNumber` to convert values returned by AttestationStation to their correct data type.
Will Cory's avatar
Will Cory committed
276

277 278 279 280 281 282
For convenience we also [export the hooks here](../src/react.ts):
- `useAttestationStationAttestation` - Reads attestations with useContractRead
- `useAttestationStationVersion` - Reads attestation version
- `useAttestationStationAttest` - Wraps useContractWrite with AttestationStation abi calling attest
- `usePrepareAttestationStationAttest` - Wraps usePrepare with AttestationStation abi calling attest
- `useAttestationStationAttestationCreatedEvent` - Wraps useContractEvents for Created events
Will Cory's avatar
Will Cory committed
283 284 285



Will Cory's avatar
Will Cory committed
286 287
## Tutorial

288 289
- [General atst tutorial](https://github.com/ethereum-optimism/optimism-tutorial/tree/main/ecosystem/attestation-station).
- [React atst starter](https://github.com/ethereum-optimism/optimism-starter). 
290