1
- import Spark from "@compolabs/spark-orderbook-ts-sdk" ;
1
+ import SparkOrderBookSdk , {
2
+ AssetType ,
3
+ CreateOrderParams ,
4
+ DepositParams ,
5
+ GetMatchOrderEventsParams ,
6
+ GetOrdersParams ,
7
+ OrderType ,
8
+ WriteTransactionResponse ,
9
+ } from "@compolabs/spark-orderbook-ts-sdk" ;
2
10
import { makeObservable } from "mobx" ;
3
11
import { Nullable } from "tsdef" ;
4
12
@@ -17,7 +25,6 @@ import {
17
25
TOKENS_LIST ,
18
26
} from "./constants" ;
19
27
import {
20
- FetchOrdersParams ,
21
28
FetchTradesParams ,
22
29
MarketCreateEvent ,
23
30
PerpMaxAbsPositionSize ,
@@ -30,14 +37,14 @@ export class FuelNetwork {
30
37
private static instance : Nullable < FuelNetwork > = null ;
31
38
32
39
private walletManager = new WalletManager ( ) ;
33
- private sdk : Spark ;
40
+ private orderbookSdk : SparkOrderBookSdk ;
34
41
35
42
public network = NETWORKS [ 0 ] ;
36
43
37
44
private constructor ( ) {
38
45
makeObservable ( this . walletManager ) ;
39
46
40
- this . sdk = new Spark ( {
47
+ this . orderbookSdk = new SparkOrderBookSdk ( {
41
48
networkUrl : NETWORKS [ 0 ] . url ,
42
49
contractAddresses : CONTRACT_ADDRESSES ,
43
50
indexerApiUrl : INDEXER_URL ,
@@ -68,6 +75,7 @@ export class FuelNetwork {
68
75
return this . walletManager . wallet ;
69
76
} ;
70
77
78
+ // TODO: Fix for mobile wallets connected to desktop
71
79
getIsExternalWallet = ( ) => false ;
72
80
73
81
getTokenList = ( ) : Token [ ] => {
@@ -84,34 +92,46 @@ export class FuelNetwork {
84
92
85
93
setWallet = async ( account : string , wallet ?: any ) : Promise < void > => {
86
94
await this . walletManager . setWallet ( account , wallet ) ;
87
- this . sdk . setActiveWallet ( this . walletManager . wallet ?? undefined ) ;
95
+ this . orderbookSdk . setActiveWallet ( ( this . walletManager . wallet as any ) ?? undefined ) ;
88
96
} ;
89
97
90
98
connectWalletByPrivateKey = async ( privateKey : string ) : Promise < void > => {
91
- await this . walletManager . connectByPrivateKey ( privateKey , await this . sdk . getProvider ( ) ) ;
92
- this . sdk . setActiveWallet ( this . walletManager . wallet ?? undefined ) ;
99
+ await this . walletManager . connectByPrivateKey ( privateKey , ( await this . orderbookSdk . getProvider ( ) ) as any ) ;
100
+ this . orderbookSdk . setActiveWallet ( ( this . walletManager . wallet as any ) ?? undefined ) ;
93
101
} ;
94
102
95
103
disconnectWallet = ( ) : void => {
96
104
this . walletManager . disconnect ( ) ;
97
- this . sdk . setActiveWallet ( this . walletManager . wallet ?? undefined ) ;
105
+ this . orderbookSdk . setActiveWallet ( ( this . walletManager . wallet as any ) ?? undefined ) ;
98
106
} ;
99
107
100
108
addAssetToWallet = async ( assetId : string ) : Promise < void > => {
101
109
await this . walletManager . addAsset ( assetId ) ;
102
110
} ;
103
111
104
- createSpotOrder = async ( assetAddress : string , size : string , price : string ) : Promise < any > => {
105
- const baseToken = this . getTokenByAssetId ( assetAddress ) ;
106
- const baseAsset = this . getAssetFromToken ( baseToken ) ;
107
- const quoteToken = this . getTokenBySymbol ( "USDC" ) ;
108
- const quoteAsset = this . getAssetFromToken ( quoteToken ) ;
112
+ createSpotOrder = async ( deposit : DepositParams , order : CreateOrderParams ) : Promise < WriteTransactionResponse > => {
113
+ // const token = this.getTokenByAssetId(assetAddress);
114
+ // const asset = this.getAssetFromToken(token);
109
115
110
- return this . sdk . createSpotOrder ( baseAsset , quoteAsset , size , price ) ;
116
+ return this . orderbookSdk . createOrder ( deposit , order ) ;
111
117
} ;
112
118
113
- cancelSpotOrder = async ( orderId : string ) : Promise < void > => {
114
- await this . sdk . cancelSpotOrder ( orderId ) ;
119
+ cancelSpotOrder = async ( order : SpotMarketOrder ) : Promise < void > => {
120
+ const withdrawAmount = order . orderType === OrderType . Buy ? order . currentQuoteAmount : order . currentAmount ;
121
+ const assetType = order . orderType === OrderType . Buy ? AssetType . Quote : AssetType . Base ;
122
+
123
+ console . log ( {
124
+ amount : withdrawAmount . toString ( ) ,
125
+ assetType : assetType ,
126
+ } ) ;
127
+
128
+ await this . orderbookSdk . cancelOrder (
129
+ {
130
+ amount : withdrawAmount . toString ( ) ,
131
+ assetType : assetType ,
132
+ } ,
133
+ order . id ,
134
+ ) ;
115
135
} ;
116
136
117
137
mintToken = async ( assetAddress : string ) : Promise < void > => {
@@ -120,7 +140,7 @@ export class FuelNetwork {
120
140
121
141
const amount = FAUCET_AMOUNTS [ token . symbol ] . toString ( ) ;
122
142
123
- await this . sdk . mintToken ( asset , amount ) ;
143
+ await this . orderbookSdk . mintToken ( asset , amount ) ;
124
144
} ;
125
145
126
146
depositPerpCollateral = async ( assetAddress : string , amount : string ) : Promise < void > => {
@@ -167,30 +187,49 @@ export class FuelNetwork {
167
187
} ;
168
188
169
189
fetchSpotMarkets = async ( limit : number ) : Promise < MarketCreateEvent [ ] > => {
170
- return this . sdk . fetchSpotMarkets ( limit ) ;
190
+ return this . orderbookSdk . fetchMarkets ( limit ) ;
171
191
} ;
172
192
173
193
fetchSpotMarketPrice = async ( baseTokenAddress : string ) : Promise < BN > => {
174
194
const token = this . getTokenByAssetId ( baseTokenAddress ) ;
175
195
const asset = this . getAssetFromToken ( token ) ;
176
196
177
- return this . sdk . fetchSpotMarketPrice ( asset ) ;
197
+ return this . orderbookSdk . fetchMarketPrice ( asset ) ;
178
198
} ;
179
199
180
- fetchSpotOrders = async ( params : FetchOrdersParams ) : Promise < SpotMarketOrder [ ] > => {
181
- const orders = await this . sdk . fetchSpotOrders ( params ) ;
200
+ fetchSpotOrders = async ( params : GetOrdersParams ) : Promise < SpotMarketOrder [ ] > => {
201
+ const orders = await this . orderbookSdk . fetchOrders ( params ) ;
182
202
183
- return orders . map ( ( obj ) => new SpotMarketOrder ( obj ) ) ;
203
+ return orders . map (
204
+ ( order ) =>
205
+ new SpotMarketOrder ( {
206
+ ...order ,
207
+ quoteAssetId : TOKENS_BY_SYMBOL . USDC . assetId ,
208
+ } ) ,
209
+ ) ;
184
210
} ;
185
211
186
- fetchSpotTrades = async ( params : FetchTradesParams ) : Promise < SpotMarketTrade [ ] > => {
187
- const trades = await this . sdk . fetchSpotTrades ( params ) ;
212
+ fetchSpotTrades = async ( params : GetMatchOrderEventsParams ) : Promise < SpotMarketTrade [ ] > => {
213
+ const trades = await this . orderbookSdk . getMatchOrderEvents ( params ) ;
188
214
189
- return trades . map ( ( obj ) => new SpotMarketTrade ( { ...obj , userAddress : params . trader } ) ) ;
215
+ return trades . map (
216
+ ( trade ) =>
217
+ new SpotMarketTrade ( {
218
+ ...trade ,
219
+ user : this . getAddress ( ) ?? "" ,
220
+ quoteAssetId : TOKENS_BY_SYMBOL . USDC . assetId ,
221
+ } ) ,
222
+ ) ;
190
223
} ;
191
224
192
225
fetchSpotVolume = async ( ) : Promise < SpotMarketVolume > => {
193
- return this . sdk . fetchSpotVolume ( ) ;
226
+ const data = await this . orderbookSdk . fetchVolume ( ) ;
227
+
228
+ return {
229
+ low : new BN ( data . low24h ) ,
230
+ high : new BN ( data . high24h ) ,
231
+ volume : new BN ( data . volume24h ) ,
232
+ } ;
194
233
} ;
195
234
196
235
matchPerpOrders = async ( order1 : string , order2 : string ) : Promise < unknown > => {
0 commit comments