Skip to content

Commit aa9defa

Browse files
committed
update manifest contract naming
1 parent fedab02 commit aa9defa

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# v0.4.7 (unreleased)
2+
- feat: add overload to .contract() method of the manifest builder to allow for passing in a name to identify the contract instead of the digest of the contract ABI
23
- chore: update `arkiver deploy` command to include serialized manifest
34
- chore: add entity name field to manifest
5+
- chore: add block handler name field to manifest
6+
- chore: use digest instead of uuid for contract id
47

58
# v0.4.6
69
- fix: add minor version to data source

deno.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/arkiver/manifest-builder.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from './types.ts'
1414
import {
1515
Abi,
16+
crypto,
1617
ExtractAbiEvent,
1718
ExtractAbiEventNames,
1819
mongoose,
@@ -110,11 +111,27 @@ export class DataSourceBuilder<TName extends string> {
110111

111112
public contract<const TAbi extends Abi>(
112113
abi: TAbi,
114+
): ContractBuilder<TAbi, TName>
115+
116+
public contract<const TAbi extends Abi>(
117+
nameOrAbi: string | TAbi,
118+
abi: TAbi,
119+
): ContractBuilder<TAbi, TName>
120+
121+
public contract<const TAbi extends Abi>(
122+
nameOrAbi: string | TAbi,
123+
abi?: TAbi,
113124
) {
114125
if (this.dataSource.contracts == undefined) {
115126
this.dataSource.contracts = []
116127
}
117-
return new ContractBuilder<TAbi, TName>(this, abi)
128+
if (typeof nameOrAbi === 'string') {
129+
if (abi === undefined) {
130+
throw new Error('ABI is required when passing a name.')
131+
}
132+
return new ContractBuilder<TAbi, TName>(this, abi, nameOrAbi)
133+
}
134+
return new ContractBuilder<TAbi, TName>(this, nameOrAbi)
118135
}
119136

120137
public addBlockHandler(
@@ -134,6 +151,7 @@ export class DataSourceBuilder<TName extends string> {
134151
handler,
135152
startBlockHeight,
136153
blockInterval: BigInt(blockInterval),
154+
name: handler.name,
137155
})
138156
return this
139157
}
@@ -148,6 +166,7 @@ export class ContractBuilder<
148166
constructor(
149167
private builder: DataSourceBuilder<TName>,
150168
abi: TAbi,
169+
name?: string,
151170
) {
152171
const existing = this.builder.dataSource.contracts?.find(
153172
(contract) => contract.abi === abi,
@@ -159,7 +178,7 @@ export class ContractBuilder<
159178
abi,
160179
sources: [],
161180
events: [],
162-
id: crypto.randomUUID(),
181+
id: name ?? hashAbi(abi),
163182
}
164183
this.builder.dataSource.contracts!.push(this.contract)
165184
}
@@ -250,3 +269,15 @@ export class ContractBuilder<
250269
return this
251270
}
252271
}
272+
273+
const hashAbi = (abi: Abi) => {
274+
const textEncoder = new TextEncoder()
275+
const str = JSON.stringify(abi)
276+
const hash = crypto.subtle.digestSync('SHA-256', textEncoder.encode(str))
277+
const uint8Array = new Uint8Array(hash)
278+
const hexString = Array.from(
279+
uint8Array,
280+
(byte) => byte.toString(16).padStart(2, '0'),
281+
).join('')
282+
return hexString
283+
}

src/arkiver/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface IBlockHandler {
3838
handler: BlockHandler
3939
startBlockHeight: bigint | 'live'
4040
blockInterval: bigint
41+
name: string
4142
}
4243

4344
export interface ChainOptions {

src/deps.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ export {
3131
BaseHandler,
3232
ConsoleHandler,
3333
} from 'https://deno.land/std@0.181.0/log/handlers.ts'
34+
export { crypto } from 'https://deno.land/std@0.186.0/crypto/mod.ts'

0 commit comments

Comments
 (0)