1
1
// Setup provider (see anchor docs for more instructions on setting up a provider using your wallet)
2
- import * as anchor from "@project-serum/anchor" ;
3
- import * as zo from "@zero_one/client" ;
4
- import { Connection , Keypair , PublicKey , Transaction } from "@solana/web3.js" ;
2
+ import { default as zo } from "@zero_one/client" ;
3
+ import { ConfirmOptions , Connection , Keypair , PublicKey , Transaction } from "@solana/web3.js" ;
5
4
import { TpuClient } from "tpu-client" ;
6
5
import { config } from "dotenv" ;
7
- import { Program } from "@project-serum/anchor" ;
8
- import { FundingInfo , OrderType } from "@zero_one/client" ;
9
-
6
+ import { createProgram , createProvider , FundingInfo , IDL , OrderType , Zo } from "@zero_one/client" ;
7
+ import { fork , ChildProcess } from 'child_process'
8
+ import { Spreads , Spread } from "./tardis" ;
9
+ import * as anchor from "@project-serum/anchor" ;
10
10
11
11
config ( { path : './.env.local' } ) ;
12
12
@@ -29,12 +29,25 @@ export interface IWallet {
29
29
publicKey : PublicKey ;
30
30
}
31
31
32
- export declare class Wallet implements IWallet {
32
+ class Wallet {
33
33
readonly payer : Keypair ;
34
- constructor ( payer : Keypair ) ;
35
- signTransaction ( tx : Transaction ) : Promise < Transaction > ;
36
- signAllTransactions ( txs : Transaction [ ] ) : Promise < Transaction [ ] > ;
37
- get publicKey ( ) : PublicKey ;
34
+
35
+ constructor ( payer ) {
36
+ this . payer = payer ;
37
+ }
38
+ async signTransaction ( tx : Transaction ) {
39
+ tx . partialSign ( this . payer ) ;
40
+ return tx ;
41
+ }
42
+ async signAllTransactions ( txs : Array < Transaction > ) {
43
+ return txs . map ( ( t ) => {
44
+ t . partialSign ( this . payer ) ;
45
+ return t ;
46
+ } ) ;
47
+ }
48
+ get publicKey ( ) {
49
+ return this . payer . publicKey ;
50
+ }
38
51
}
39
52
40
53
// setup wallet
@@ -80,15 +93,17 @@ class MarketMaker {
80
93
tpuClient : TpuClient
81
94
provider : anchor . Provider
82
95
idl : zo . Zo
83
- program : Program < zo . Zo >
96
+ program : anchor . Program < zo . Zo >
84
97
state : zo . State
85
98
margin : zo . Margin
86
99
running : boolean
100
+ spreads : Spreads
87
101
spread : number
88
102
markets : Map < string , zo . ZoMarket >
89
103
orderbooks : Map < string , Orderbook >
104
+ tardis : ChildProcess
90
105
91
- constructor ( tpuClient : TpuClient , provider : anchor . Provider , idl : zo . Zo , program : Program < zo . Zo > , state : zo . State , margin : zo . Margin ) {
106
+ constructor ( tpuClient : TpuClient , provider : anchor . Provider , idl : zo . Zo , program : anchor . Program < zo . Zo > , state : zo . State , margin : zo . Margin ) {
92
107
this . tpuClient = tpuClient ;
93
108
this . provider = provider ;
94
109
this . idl = idl ;
@@ -100,16 +115,52 @@ class MarketMaker {
100
115
101
116
static async load ( ) {
102
117
const tpuClient = await TpuClient . load ( new Connection ( process . env . RPC_URL ) ) ;
103
- const provider = new anchor . Provider ( tpuClient . connection , botWallet , { commitment : 'processed' } ) ;
104
- const idl = await anchor . Program . fetchIdl ( new PublicKey ( zo . ZERO_ONE_MAINNET_PROGRAM_ID ) , provider ) as zo . Zo ;
105
- const program = new anchor . Program ( idl , zo . ZERO_ONE_MAINNET_PROGRAM_ID , provider ) as Program < zo . Zo > ;
106
- const state = await zo . State . load ( program , zo . ZO_MAINNET_STATE_KEY ) as zo . State ;
107
- const margin = await zo . Margin . create ( program , state ) as zo . Margin ;
118
+ const provider = createProvider ( tpuClient . connection , botWallet , { commitment : 'processed' } ) ;
119
+ const program = createProgram ( provider , zo . Cluster . Mainnet ) ;
120
+ const state = await ( zo . State ) . load ( program , zo . ZO_MAINNET_STATE_KEY ) as zo . State ;
121
+ const margin = await zo . Margin . load ( program , state ) as zo . Margin ;
108
122
109
- return new MarketMaker ( tpuClient , provider , idl , program , state , margin ) ;
123
+ return new MarketMaker ( tpuClient , provider , zo . IDL , program , state , margin ) ;
110
124
}
111
125
112
126
127
+ public startTardis ( ) {
128
+ if ( ! this . tardis )
129
+ this . tardis = fork ( './tardis.ts' , [ ] , { stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ] } )
130
+
131
+ if ( this . tardis . stderr )
132
+ this . tardis . stderr . on ( 'data' , ( data : Buffer ) => {
133
+ console . log ( data . toString ( ) ) ;
134
+ } )
135
+
136
+ this . tardis . on ( 'close' , ( code , sig ) => {
137
+ this . tardis . kill ( ) ;
138
+ delete this . tardis
139
+ this . startTardis ( ) ;
140
+ } )
141
+
142
+ if ( this . tardis . stdout )
143
+ this . tardis . stdout . on ( 'data' , ( data : Buffer ) => {
144
+ console . log ( data . toString ( ) ) ;
145
+ } )
146
+
147
+ this . tardis . on ( 'message' , ( data : string ) => {
148
+ let d = JSON . parse ( data ) ;
149
+ switch ( d . type ) {
150
+ case 'started' :
151
+ break ;
152
+ case 'data' :
153
+ break ;
154
+ case 'spreads' :
155
+ this . spreads = d . spreads as Spreads ;
156
+ console . log ( this . spreads ) ;
157
+ break ;
158
+ case 'error' :
159
+ console . error ( d . data ) ;
160
+ break ;
161
+ }
162
+ } )
163
+ }
113
164
114
165
public stop ( ) {
115
166
this . running = false ;
@@ -205,5 +256,8 @@ class MarketMaker {
205
256
206
257
}
207
258
259
+ MarketMaker . load ( ) . then ( marketMaker => {
260
+ marketMaker . startTardis ( ) ;
261
+ } ) ;
208
262
209
263
0 commit comments