@@ -3,6 +3,70 @@ import { hideBin } from "yargs/helpers";
3
3
import { checkHex , Client } from "../index" ;
4
4
import { privateKeyToAccount } from "viem/accounts" ;
5
5
import { isHex } from "viem" ;
6
+ import { BidStatusUpdate , Opportunity } from "../types" ;
7
+
8
+ const DAY_IN_SECONDS = 60 * 60 * 24 ;
9
+
10
+ class SimpleSearcher {
11
+ private client : Client ;
12
+ constructor (
13
+ public endpoint : string ,
14
+ public chainId : string ,
15
+ public privateKey : string
16
+ ) {
17
+ this . client = new Client (
18
+ { baseUrl : endpoint } ,
19
+ undefined ,
20
+ this . opportunityHandler . bind ( this ) ,
21
+ this . bidStatusHandler . bind ( this )
22
+ ) ;
23
+ }
24
+
25
+ async bidStatusHandler ( bidStatus : BidStatusUpdate ) {
26
+ console . log (
27
+ `Bid status for bid ${ bidStatus . id } : ${ bidStatus . status } ${
28
+ bidStatus . status == "submitted" ? bidStatus . result : ""
29
+ } `
30
+ ) ;
31
+ }
32
+
33
+ async opportunityHandler ( opportunity : Opportunity ) {
34
+ const bid = BigInt ( argv . bid ) ;
35
+ // Bid info should be generated by evaluating the opportunity
36
+ // here for simplicity we are using a constant bid and 24 hours of validity
37
+ const bidParams = {
38
+ amount : bid ,
39
+ validUntil : BigInt ( Math . round ( Date . now ( ) / 1000 + DAY_IN_SECONDS ) ) ,
40
+ } ;
41
+ const opportunityBid = await this . client . signOpportunityBid (
42
+ opportunity ,
43
+ bidParams ,
44
+ checkHex ( argv . privateKey )
45
+ ) ;
46
+ try {
47
+ const bidId = await this . client . submitOpportunityBid ( opportunityBid ) ;
48
+ console . log (
49
+ `Successful bid. Opportunity id ${ opportunityBid . opportunityId } Bid id ${ bidId } `
50
+ ) ;
51
+ } catch ( error ) {
52
+ console . error (
53
+ `Failed to bid on opportunity ${ opportunity . opportunityId } : ${ error } `
54
+ ) ;
55
+ }
56
+ }
57
+
58
+ async start ( ) {
59
+ try {
60
+ await this . client . subscribeChains ( [ argv . chainId ] ) ;
61
+ console . log (
62
+ `Subscribed to chain ${ argv . chainId } . Waiting for opportunities...`
63
+ ) ;
64
+ } catch ( error ) {
65
+ console . error ( error ) ;
66
+ this . client . websocket ?. close ( ) ;
67
+ }
68
+ }
69
+ }
6
70
7
71
const argv = yargs ( hideBin ( process . argv ) )
8
72
. option ( "endpoint" , {
@@ -30,49 +94,19 @@ const argv = yargs(hideBin(process.argv))
30
94
. help ( )
31
95
. alias ( "help" , "h" )
32
96
. parseSync ( ) ;
33
-
34
97
async function run ( ) {
35
- const client = new Client ( { baseUrl : argv . endpoint } ) ;
36
98
if ( isHex ( argv . privateKey ) ) {
37
99
const account = privateKeyToAccount ( argv . privateKey ) ;
38
100
console . log ( `Using account: ${ account . address } ` ) ;
39
101
} else {
40
102
throw new Error ( `Invalid private key: ${ argv . privateKey } ` ) ;
41
103
}
42
- const DAY_IN_SECONDS = 60 * 60 * 24 ;
43
- client . setOpportunityHandler ( async ( opportunity ) => {
44
- const bid = BigInt ( argv . bid ) ;
45
- // Bid info should be generated by evaluating the opportunity
46
- // here for simplicity we are using a constant bid and 24 hours of validity
47
- const bidInfo = {
48
- amount : bid ,
49
- validUntil : BigInt ( Math . round ( Date . now ( ) / 1000 + DAY_IN_SECONDS ) ) ,
50
- } ;
51
- const opportunityBid = await client . signOpportunityBid (
52
- opportunity ,
53
- bidInfo ,
54
- checkHex ( argv . privateKey )
55
- ) ;
56
- try {
57
- await client . submitOpportunityBid ( opportunityBid ) ;
58
- console . log (
59
- `Successful bid ${ bid } on opportunity ${ opportunity . opportunityId } `
60
- ) ;
61
- } catch ( error ) {
62
- console . error (
63
- `Failed to bid on opportunity ${ opportunity . opportunityId } : ${ error } `
64
- ) ;
65
- }
66
- } ) ;
67
- try {
68
- await client . subscribeChains ( [ argv . chainId ] ) ;
69
- console . log (
70
- `Subscribed to chain ${ argv . chainId } . Waiting for opportunities...`
71
- ) ;
72
- } catch ( error ) {
73
- console . error ( error ) ;
74
- client . websocket ?. close ( ) ;
75
- }
104
+ const searcher = new SimpleSearcher (
105
+ argv . endpoint ,
106
+ argv . chainId ,
107
+ argv . privateKey
108
+ ) ;
109
+ await searcher . start ( ) ;
76
110
}
77
111
78
112
run ( ) ;
0 commit comments