Skip to content

Commit 12de6ef

Browse files
committed
Better typing of preconditions
1 parent 4e1f486 commit 12de6ef

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

packages/wallet/core/src/preconditions/codec.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,58 +120,68 @@ export function decodePrecondition(p: IntentPrecondition): Precondition | undefi
120120
}
121121
}
122122

123+
type PreconditionData = {
124+
address: Address.Checksummed
125+
token: Address.Checksummed
126+
operator: Address.Checksummed
127+
min: string
128+
max: string
129+
tokenId: string
130+
owned: boolean
131+
}
132+
123133
export function encodePrecondition(p: Precondition): string {
124-
const data: any = {}
134+
const data: Partial<PreconditionData> = {}
125135

126136
switch (p.type()) {
127137
case 'native-balance': {
128138
const native = p as NativeBalancePrecondition
129-
data.address = native.address.toString()
139+
data.address = native.address
130140
if (native.min !== undefined) data.min = native.min.toString()
131141
if (native.max !== undefined) data.max = native.max.toString()
132142
break
133143
}
134144

135145
case 'erc20-balance': {
136146
const erc20 = p as Erc20BalancePrecondition
137-
data.address = erc20.address.toString()
138-
data.token = erc20.token.toString()
147+
data.address = erc20.address
148+
data.token = erc20.token
139149
if (erc20.min !== undefined) data.min = erc20.min.toString()
140150
if (erc20.max !== undefined) data.max = erc20.max.toString()
141151
break
142152
}
143153

144154
case 'erc20-approval': {
145155
const erc20 = p as Erc20ApprovalPrecondition
146-
data.address = erc20.address.toString()
147-
data.token = erc20.token.toString()
148-
data.operator = erc20.operator.toString()
156+
data.address = erc20.address
157+
data.token = erc20.token
158+
data.operator = erc20.operator
149159
data.min = erc20.min.toString()
150160
break
151161
}
152162

153163
case 'erc721-ownership': {
154164
const erc721 = p as Erc721OwnershipPrecondition
155-
data.address = erc721.address.toString()
156-
data.token = erc721.token.toString()
165+
data.address = erc721.address
166+
data.token = erc721.token
157167
data.tokenId = erc721.tokenId.toString()
158168
if (erc721.owned !== undefined) data.owned = erc721.owned
159169
break
160170
}
161171

162172
case 'erc721-approval': {
163173
const erc721 = p as Erc721ApprovalPrecondition
164-
data.address = erc721.address.toString()
165-
data.token = erc721.token.toString()
174+
data.address = erc721.address
175+
data.token = erc721.token
166176
data.tokenId = erc721.tokenId.toString()
167-
data.operator = erc721.operator.toString()
177+
data.operator = erc721.operator
168178
break
169179
}
170180

171181
case 'erc1155-balance': {
172182
const erc1155 = p as Erc1155BalancePrecondition
173-
data.address = erc1155.address.toString()
174-
data.token = erc1155.token.toString()
183+
data.address = erc1155.address
184+
data.token = erc1155.token
175185
data.tokenId = erc1155.tokenId.toString()
176186
if (erc1155.min !== undefined) data.min = erc1155.min.toString()
177187
if (erc1155.max !== undefined) data.max = erc1155.max.toString()
@@ -180,10 +190,10 @@ export function encodePrecondition(p: Precondition): string {
180190

181191
case 'erc1155-approval': {
182192
const erc1155 = p as Erc1155ApprovalPrecondition
183-
data.address = erc1155.address.toString()
184-
data.token = erc1155.token.toString()
193+
data.address = erc1155.address
194+
data.token = erc1155.token
185195
data.tokenId = erc1155.tokenId.toString()
186-
data.operator = erc1155.operator.toString()
196+
data.operator = erc1155.operator
187197
data.min = erc1155.min.toString()
188198
break
189199
}

packages/wallet/core/src/relayer/standard/local.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { EIP1193Provider } from 'mipd'
33
import { AbiFunction, Bytes, Hex, TransactionReceipt } from 'ox'
44
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../relayer.js'
55
import { IntentPrecondition } from './rpc/relayer.gen.js'
6-
import { decodePrecondition } from '../../preconditions/index.js'
6+
import {
7+
decodePrecondition,
8+
Erc1155ApprovalPrecondition,
9+
Erc1155BalancePrecondition,
10+
Erc20ApprovalPrecondition,
11+
Erc20BalancePrecondition,
12+
Erc721ApprovalPrecondition,
13+
Erc721OwnershipPrecondition,
14+
NativeBalancePrecondition,
15+
} from '../../preconditions/index.js'
716
import {
817
erc20BalanceOf,
918
erc20Allowance,
@@ -171,8 +180,8 @@ export class LocalRelayer implements Relayer {
171180

172181
switch (decoded.type()) {
173182
case 'native-balance': {
174-
const native = decoded as any
175-
const balance = await this.provider.getBalance(native.address.toString())
183+
const native = decoded as NativeBalancePrecondition
184+
const balance = await this.provider.getBalance(native.address)
176185
if (native.min !== undefined && balance < native.min) {
177186
return false
178187
}
@@ -183,10 +192,10 @@ export class LocalRelayer implements Relayer {
183192
}
184193

185194
case 'erc20-balance': {
186-
const erc20 = decoded as any
187-
const data = AbiFunction.encodeData(erc20BalanceOf, [erc20.address.toString()])
195+
const erc20 = decoded as Erc20BalancePrecondition
196+
const data = AbiFunction.encodeData(erc20BalanceOf, [erc20.address])
188197
const result = await this.provider.call({
189-
to: erc20.token.toString(),
198+
to: erc20.token,
190199
data,
191200
})
192201
const balance = BigInt(result)
@@ -200,21 +209,21 @@ export class LocalRelayer implements Relayer {
200209
}
201210

202211
case 'erc20-approval': {
203-
const erc20 = decoded as any
204-
const data = AbiFunction.encodeData(erc20Allowance, [erc20.address.toString(), erc20.operator.toString()])
212+
const erc20 = decoded as Erc20ApprovalPrecondition
213+
const data = AbiFunction.encodeData(erc20Allowance, [erc20.address, erc20.operator])
205214
const result = await this.provider.call({
206-
to: erc20.token.toString(),
215+
to: erc20.token,
207216
data,
208217
})
209218
const allowance = BigInt(result)
210219
return allowance >= erc20.min
211220
}
212221

213222
case 'erc721-ownership': {
214-
const erc721 = decoded as any
223+
const erc721 = decoded as Erc721OwnershipPrecondition
215224
const data = AbiFunction.encodeData(erc721OwnerOf, [erc721.tokenId])
216225
const result = await this.provider.call({
217-
to: erc721.token.toString(),
226+
to: erc721.token,
218227
data,
219228
})
220229
const owner = Address.checksum(`0x${result.slice(26)}`)
@@ -223,21 +232,21 @@ export class LocalRelayer implements Relayer {
223232
}
224233

225234
case 'erc721-approval': {
226-
const erc721 = decoded as any
235+
const erc721 = decoded as Erc721ApprovalPrecondition
227236
const data = AbiFunction.encodeData(erc721GetApproved, [erc721.tokenId])
228237
const result = await this.provider.call({
229-
to: erc721.token.toString(),
238+
to: erc721.token,
230239
data,
231240
})
232241
const approved = Address.checksum(`0x${result.slice(26)}`)
233242
return Address.isEqual(approved, erc721.operator)
234243
}
235244

236245
case 'erc1155-balance': {
237-
const erc1155 = decoded as any
238-
const data = AbiFunction.encodeData(erc1155BalanceOf, [erc1155.address.toString(), erc1155.tokenId])
246+
const erc1155 = decoded as Erc1155BalancePrecondition
247+
const data = AbiFunction.encodeData(erc1155BalanceOf, [erc1155.address, erc1155.tokenId])
239248
const result = await this.provider.call({
240-
to: erc1155.token.toString(),
249+
to: erc1155.token,
241250
data,
242251
})
243252
const balance = BigInt(result)
@@ -251,13 +260,10 @@ export class LocalRelayer implements Relayer {
251260
}
252261

253262
case 'erc1155-approval': {
254-
const erc1155 = decoded as any
255-
const data = AbiFunction.encodeData(erc1155IsApprovedForAll, [
256-
erc1155.address.toString(),
257-
erc1155.operator.toString(),
258-
])
263+
const erc1155 = decoded as Erc1155ApprovalPrecondition
264+
const data = AbiFunction.encodeData(erc1155IsApprovedForAll, [erc1155.address, erc1155.operator])
259265
const result = await this.provider.call({
260-
to: erc1155.token.toString(),
266+
to: erc1155.token,
261267
data,
262268
})
263269
return BigInt(result) === 1n

0 commit comments

Comments
 (0)