Skip to content

Commit 7089711

Browse files
committed
Fixing lint and type errors for relayer package
1 parent 1d44c4d commit 7089711

13 files changed

Lines changed: 335 additions & 516 deletions

File tree

packages/services/relayer/src/preconditions/codec.ts

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export function decodePrecondition(p: TransactionPrecondition): Precondition | u
3636
return undefined
3737
}
3838

39+
if (typeof p.minAmount !== 'bigint') {
40+
console.warn(`Failed to decode precondition: minAmount must be a bigint`)
41+
return undefined
42+
}
43+
3944
let precondition: Precondition | undefined
4045

4146
try {
@@ -118,73 +123,92 @@ export function decodePrecondition(p: TransactionPrecondition): Precondition | u
118123
}
119124

120125
export function encodePrecondition(p: Precondition): string {
121-
const data: any = {}
122-
123126
switch (p.type()) {
124127
case 'native-balance': {
125128
const native = p as NativeBalancePrecondition
126-
data.address = native.address.toString()
127-
if (native.min !== undefined) data.min = native.min.toString()
128-
if (native.max !== undefined) data.max = native.max.toString()
129-
break
129+
const data = {
130+
address: native.address.toString(),
131+
...(native.min !== undefined && { min: native.min.toString() }),
132+
...(native.max !== undefined && { max: native.max.toString() }),
133+
}
134+
135+
return JSON.stringify(data)
130136
}
131137

132138
case 'erc20-balance': {
133139
const erc20 = p as Erc20BalancePrecondition
134-
data.address = erc20.address.toString()
135-
data.token = erc20.token.toString()
136-
if (erc20.min !== undefined) data.min = erc20.min.toString()
137-
if (erc20.max !== undefined) data.max = erc20.max.toString()
138-
break
140+
const data = {
141+
address: erc20.address.toString(),
142+
token: erc20.token.toString(),
143+
...(erc20.min !== undefined && { min: erc20.min.toString() }),
144+
...(erc20.max !== undefined && { max: erc20.max.toString() }),
145+
}
146+
147+
return JSON.stringify(data)
139148
}
140149

141150
case 'erc20-approval': {
142151
const erc20 = p as Erc20ApprovalPrecondition
143-
data.address = erc20.address.toString()
144-
data.token = erc20.token.toString()
145-
data.operator = erc20.operator.toString()
146-
data.min = erc20.min.toString()
147-
break
152+
const data = {
153+
address: erc20.address.toString(),
154+
token: erc20.token.toString(),
155+
operator: erc20.operator.toString(),
156+
min: erc20.min.toString(),
157+
}
158+
159+
return JSON.stringify(data)
148160
}
149161

150162
case 'erc721-ownership': {
151163
const erc721 = p as Erc721OwnershipPrecondition
152-
data.address = erc721.address.toString()
153-
data.token = erc721.token.toString()
154-
data.tokenId = erc721.tokenId.toString()
155-
if (erc721.owned !== undefined) data.owned = erc721.owned
156-
break
164+
const data = {
165+
address: erc721.address.toString(),
166+
token: erc721.token.toString(),
167+
tokenId: erc721.tokenId.toString(),
168+
...(erc721.owned !== undefined && { owned: erc721.owned }),
169+
}
170+
171+
return JSON.stringify(data)
157172
}
158173

159174
case 'erc721-approval': {
160175
const erc721 = p as Erc721ApprovalPrecondition
161-
data.address = erc721.address.toString()
162-
data.token = erc721.token.toString()
163-
data.tokenId = erc721.tokenId.toString()
164-
data.operator = erc721.operator.toString()
165-
break
176+
const data = {
177+
address: erc721.address.toString(),
178+
token: erc721.token.toString(),
179+
tokenId: erc721.tokenId.toString(),
180+
operator: erc721.operator.toString(),
181+
}
182+
183+
return JSON.stringify(data)
166184
}
167185

168186
case 'erc1155-balance': {
169187
const erc1155 = p as Erc1155BalancePrecondition
170-
data.address = erc1155.address.toString()
171-
data.token = erc1155.token.toString()
172-
data.tokenId = erc1155.tokenId.toString()
173-
if (erc1155.min !== undefined) data.min = erc1155.min.toString()
174-
if (erc1155.max !== undefined) data.max = erc1155.max.toString()
175-
break
188+
const data = {
189+
address: erc1155.address.toString(),
190+
token: erc1155.token.toString(),
191+
tokenId: erc1155.tokenId.toString(),
192+
...(erc1155.min !== undefined && { min: erc1155.min.toString() }),
193+
...(erc1155.max !== undefined && { max: erc1155.max.toString() }),
194+
}
195+
196+
return JSON.stringify(data)
176197
}
177198

178199
case 'erc1155-approval': {
179200
const erc1155 = p as Erc1155ApprovalPrecondition
180-
data.address = erc1155.address.toString()
181-
data.token = erc1155.token.toString()
182-
data.tokenId = erc1155.tokenId.toString()
183-
data.operator = erc1155.operator.toString()
184-
data.min = erc1155.min.toString()
185-
break
201+
const data = {
202+
address: erc1155.address.toString(),
203+
token: erc1155.token.toString(),
204+
tokenId: erc1155.tokenId.toString(),
205+
operator: erc1155.operator.toString(),
206+
min: erc1155.min.toString(),
207+
}
208+
209+
return JSON.stringify(data)
186210
}
187211
}
188212

189-
return JSON.stringify(data)
213+
return JSON.stringify({})
190214
}

packages/services/relayer/src/relayer/relayer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export interface Relayer {
2727
checkPrecondition(precondition: Precondition.Precondition): Promise<boolean>
2828
}
2929

30-
export function isRelayer(relayer: any): relayer is Relayer {
30+
export function isRelayer(relayer: unknown): relayer is Relayer {
3131
return (
32+
typeof relayer === 'object' &&
33+
relayer !== null &&
3234
'isAvailable' in relayer &&
3335
'feeOptions' in relayer &&
3436
'relay' in relayer &&

packages/services/relayer/src/relayer/rpc-relayer/index.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ import {
1010
import { Address, Hex, AbiFunction } from 'ox'
1111
import { Constants, Payload, Network } from '@0xsequence/wallet-primitives'
1212
import { FeeOption, FeeQuote, OperationStatus, Relayer } from '../index.js'
13-
import { decodePrecondition } from '../../preconditions/index.js'
13+
import {
14+
decodePrecondition,
15+
Erc1155ApprovalPrecondition,
16+
Erc1155BalancePrecondition,
17+
Erc20ApprovalPrecondition,
18+
Erc20BalancePrecondition,
19+
Erc721ApprovalPrecondition,
20+
Erc721OwnershipPrecondition,
21+
NativeBalancePrecondition,
22+
} from '../../preconditions/index.js'
1423
import {
1524
erc20BalanceOf,
1625
erc20Allowance,
@@ -69,7 +78,9 @@ export const getChain = (chainId: number): Chain => {
6978
}
7079

7180
// Fall back to viem's built-in chains
72-
const viemChain = Object.values(chains).find((c: any) => typeof c === 'object' && 'id' in c && c.id === chainId)
81+
const viemChain = Object.values(chains).find(
82+
(c: unknown) => typeof c === 'object' && c !== null && 'id' in c && c.id === chainId,
83+
)
7384
if (viemChain) {
7485
return viemChain as Chain
7586
}
@@ -78,7 +89,7 @@ export const getChain = (chainId: number): Chain => {
7889
}
7990

8091
export class RpcRelayer implements Relayer {
81-
public readonly kind: 'relayer' = 'relayer'
92+
public readonly kind = 'relayer'
8293
public readonly type = 'rpc'
8394
public readonly id: string
8495
public readonly chainId: number
@@ -239,7 +250,7 @@ export class RpcRelayer implements Relayer {
239250
return { opHash: `0x${result.txnHash}` }
240251
}
241252

242-
async status(opHash: Hex.Hex, chainId: number): Promise<OperationStatus> {
253+
async status(opHash: Hex.Hex, _chainId: number): Promise<OperationStatus> {
243254
try {
244255
const cleanedOpHash = opHash.startsWith('0x') ? opHash.substring(2) : opHash
245256
const result = await this.client.getMetaTxnReceipt({ metaTxID: cleanedOpHash })
@@ -291,9 +302,9 @@ export class RpcRelayer implements Relayer {
291302

292303
switch (decoded.type()) {
293304
case 'native-balance': {
294-
const native = decoded as any
305+
const native = decoded as NativeBalancePrecondition
295306
try {
296-
const balance = await this.provider.getBalance({ address: native.address.toString() as `0x${string}` })
307+
const balance = await this.provider.getBalance({ address: native.address })
297308
const minWei = native.min !== undefined ? BigInt(native.min) : undefined
298309
const maxWei = native.max !== undefined ? BigInt(native.max) : undefined
299310

@@ -316,9 +327,9 @@ export class RpcRelayer implements Relayer {
316327
}
317328

318329
case 'erc20-balance': {
319-
const erc20 = decoded as any
330+
const erc20 = decoded as Erc20BalancePrecondition
320331
try {
321-
const data = AbiFunction.encodeData(erc20BalanceOf, [erc20.address.toString()])
332+
const data = AbiFunction.encodeData(erc20BalanceOf, [erc20.address])
322333
const result = await this.provider.call({
323334
to: erc20.token.toString() as `0x${string}`,
324335
data: data as `0x${string}`,
@@ -345,9 +356,9 @@ export class RpcRelayer implements Relayer {
345356
}
346357

347358
case 'erc20-approval': {
348-
const erc20 = decoded as any
359+
const erc20 = decoded as Erc20ApprovalPrecondition
349360
try {
350-
const data = AbiFunction.encodeData(erc20Allowance, [erc20.address.toString(), erc20.operator.toString()])
361+
const data = AbiFunction.encodeData(erc20Allowance, [erc20.address, erc20.operator])
351362
const result = await this.provider.call({
352363
to: erc20.token.toString() as `0x${string}`,
353364
data: data as `0x${string}`,
@@ -362,12 +373,12 @@ export class RpcRelayer implements Relayer {
362373
}
363374

364375
case 'erc721-ownership': {
365-
const erc721 = decoded as any
376+
const erc721 = decoded as Erc721OwnershipPrecondition
366377
try {
367378
const data = AbiFunction.encodeData(erc721OwnerOf, [erc721.tokenId])
368379
const result = await this.provider.call({
369-
to: erc721.token.toString() as `0x${string}`,
370-
data: data as `0x${string}`,
380+
to: erc721.token,
381+
data: data,
371382
})
372383
const resultHex = result.toString() as `0x${string}`
373384
const owner = resultHex.slice(-40)
@@ -381,7 +392,7 @@ export class RpcRelayer implements Relayer {
381392
}
382393

383394
case 'erc721-approval': {
384-
const erc721 = decoded as any
395+
const erc721 = decoded as Erc721ApprovalPrecondition
385396
try {
386397
const data = AbiFunction.encodeData(erc721GetApproved, [erc721.tokenId])
387398
const result = await this.provider.call({
@@ -398,9 +409,9 @@ export class RpcRelayer implements Relayer {
398409
}
399410

400411
case 'erc1155-balance': {
401-
const erc1155 = decoded as any
412+
const erc1155 = decoded as Erc1155BalancePrecondition
402413
try {
403-
const data = AbiFunction.encodeData(erc1155BalanceOf, [erc1155.address.toString(), erc1155.tokenId])
414+
const data = AbiFunction.encodeData(erc1155BalanceOf, [erc1155.address, erc1155.tokenId])
404415
const result = await this.provider.call({
405416
to: erc1155.token.toString() as `0x${string}`,
406417
data: data as `0x${string}`,
@@ -427,15 +438,12 @@ export class RpcRelayer implements Relayer {
427438
}
428439

429440
case 'erc1155-approval': {
430-
const erc1155 = decoded as any
441+
const erc1155 = decoded as Erc1155ApprovalPrecondition
431442
try {
432-
const data = AbiFunction.encodeData(erc1155IsApprovedForAll, [
433-
erc1155.address.toString(),
434-
erc1155.operator.toString(),
435-
])
443+
const data = AbiFunction.encodeData(erc1155IsApprovedForAll, [erc1155.address, erc1155.operator])
436444
const result = await this.provider.call({
437-
to: erc1155.token.toString() as `0x${string}`,
438-
data: data as `0x${string}`,
445+
to: erc1155.token,
446+
data: data,
439447
})
440448
return BigInt(result.toString()) === 1n
441449
} catch (error) {

packages/services/relayer/src/relayer/standard/eip6963.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Payload } from '@0xsequence/wallet-primitives'
66
import { FeeToken, TransactionPrecondition } from '../rpc-relayer/relayer.gen.js'
77

88
export class EIP6963Relayer implements Relayer {
9-
public readonly kind: 'relayer' = 'relayer'
9+
public readonly kind = 'relayer'
1010
public readonly type = 'eip6963'
1111
public readonly id: string
1212
public readonly info: EIP6963ProviderInfo
@@ -59,7 +59,7 @@ export function getEIP6963Store() {
5959
return store
6060
}
6161

62-
let relayers: Map<string, EIP6963Relayer> = new Map()
62+
const relayers: Map<string, EIP6963Relayer> = new Map()
6363

6464
export function getRelayers(): EIP6963Relayer[] {
6565
const store = getEIP6963Store()

0 commit comments

Comments
 (0)