Skip to content

Commit 07421df

Browse files
committed
pool+mobx: add RegisterSidecar RPC and mobx state
1 parent bcc52dc commit 07421df

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

app/src/api/pool.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ class PoolApi extends BaseApi<PoolEvents> {
271271
return res.toObject();
272272
}
273273

274+
/**
275+
* call the pool `RegisterSidecar` RPC and return the response
276+
*/
277+
async registerSidecar(ticket: string): Promise<POOL.SidecarTicket.AsObject> {
278+
const req = new POOL.RegisterSidecarRequest();
279+
req.setTicket(ticket);
280+
req.setAutoNegotiate(true);
281+
const res = await this._grpc.request(Trader.RegisterSidecar, req, this._meta);
282+
return res.toObject();
283+
}
284+
274285
//
275286
// Utility functions to convert user-facing units to API units
276287
//

app/src/store/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
FundNewAccountView,
2727
OrderFormView,
2828
OrderListView,
29+
RegisterSidecarView,
2930
RenewAccountView,
3031
} from './views';
3132

@@ -61,6 +62,7 @@ export class Store {
6162
orderFormView = new OrderFormView(this);
6263
orderListView = new OrderListView(this);
6364
batchesView = new BatchesView(this);
65+
registerSidecarView = new RegisterSidecarView(this);
6466

6567
/** the backend api services to be used by child stores */
6668
api: {

app/src/store/stores/accountStore.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,17 @@ export default class AccountStore {
252252
this._store.appView.handleError(error, 'Unable to withdraw funds');
253253
}
254254
}
255+
256+
/**
257+
* submits a sidecar ticket registration to the pool api
258+
*/
259+
async registerSidecar(ticket: string) {
260+
try {
261+
this._store.log.info('registering sidecar ticket');
262+
263+
await this._store.api.pool.registerSidecar(ticket);
264+
} catch (error) {
265+
this._store.appView.handleError(error, 'Unable to register sidecar ticket');
266+
}
267+
}
255268
}

app/src/store/views/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export { default as OrderFormView } from './orderFormView';
99
export { default as OrderListView } from './orderListView';
1010
export { default as LeaseView } from './leaseView';
1111
export { default as BatchesView } from './batchesView';
12+
export { default as RegisterSidecarView } from './registerSidecarView';
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { makeAutoObservable, toJS } from 'mobx';
2+
import { SidecarRegisterSteps } from 'types/state';
3+
import { Store } from 'store';
4+
5+
// an artificial delay to allow the user to abort a ticket registration before it executed
6+
export const TICKET_ABORT_DELAY = 2000;
7+
8+
export default class RegisterSidecarView {
9+
private _store: Store;
10+
11+
/** the current step of the wizard to display */
12+
currentStep = SidecarRegisterSteps.Closed;
13+
14+
// the the sidecar ticket
15+
ticket = '';
16+
17+
/** the reference to the timeout used to allow aborting */
18+
processingTimeout?: NodeJS.Timeout;
19+
20+
constructor(store: Store) {
21+
makeAutoObservable(this, {}, { deep: false, autoBind: true });
22+
23+
this._store = store;
24+
}
25+
26+
//
27+
// Computed properties
28+
//
29+
30+
/** determines whether to show the wizard UI */
31+
get showWizard(): boolean {
32+
return [
33+
SidecarRegisterSteps.EnterTicket,
34+
SidecarRegisterSteps.ConfirmTicket,
35+
SidecarRegisterSteps.Processing,
36+
SidecarRegisterSteps.Complete,
37+
].includes(this.currentStep);
38+
}
39+
40+
//
41+
// Actions
42+
//
43+
44+
/**
45+
* Show the first screen of the Sidecar Wizard
46+
*/
47+
startRegister() {
48+
this.currentStep = SidecarRegisterSteps.EnterTicket;
49+
}
50+
51+
/**
52+
* Navigate to the next step in the wizard
53+
*/
54+
goToNextStep() {
55+
if (this.currentStep === SidecarRegisterSteps.ConfirmTicket) {
56+
this.registerTicket();
57+
}
58+
this.currentStep++;
59+
this._store.log.info(`updated registerSidecarView.currentStep`, this.currentStep);
60+
}
61+
62+
/**
63+
* Navigate to the previous step in the wizard
64+
*/
65+
goToPrevStep() {
66+
if (this.currentStep === SidecarRegisterSteps.Processing) {
67+
// if back is clicked on the processing step
68+
this.abort();
69+
}
70+
if (this.currentStep === SidecarRegisterSteps.Complete) {
71+
this.cancel();
72+
return;
73+
}
74+
this.currentStep--;
75+
this._store.log.info(`updated registerSidecarView.currentStep`, this.currentStep);
76+
}
77+
78+
/**
79+
* hide the sidecar wizard
80+
*/
81+
cancel() {
82+
this.currentStep = SidecarRegisterSteps.Closed;
83+
this.ticket = '';
84+
this._store.log.info(`reset registerSidecarView`, toJS(this));
85+
}
86+
87+
/**
88+
* sets the value of the ticket
89+
*/
90+
setTicket(ticket: string) {
91+
this.ticket = ticket;
92+
}
93+
94+
/**
95+
* submit a request to the Pool API to perform the ticket registration
96+
*/
97+
registerTicket() {
98+
const delay =
99+
process.env.NODE_ENV === 'test'
100+
? 1 // use a 1 ms delay for unit tests
101+
: TICKET_ABORT_DELAY;
102+
103+
this.processingTimeout = setTimeout(async () => {
104+
try {
105+
this._store.log.info(`registering sidecar ticket`, toJS(this.ticket));
106+
const res = await this._store.api.pool.registerSidecar(this.ticket);
107+
this.goToNextStep();
108+
this._store.log.info(`registered ticket successfully`, toJS(res.ticket));
109+
} catch (error) {
110+
this._store.appView.handleError(error, `Unable to register ticket`);
111+
this.goToPrevStep();
112+
}
113+
}, delay);
114+
}
115+
116+
/**
117+
* abort a register that has been submitted
118+
*/
119+
abort() {
120+
if (this.processingTimeout) {
121+
clearTimeout(this.processingTimeout);
122+
this.processingTimeout = undefined;
123+
}
124+
}
125+
}

app/src/types/state.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export enum BuildSwapSteps {
3232
Processing = 4,
3333
}
3434

35+
export enum SidecarRegisterSteps {
36+
Closed = 0,
37+
EnterTicket = 1,
38+
ConfirmTicket = 2,
39+
Processing = 3,
40+
Complete = 4,
41+
}
42+
3543
export interface Alert {
3644
id: number;
3745
type: 'info' | 'success' | 'warning' | 'error' | 'default';

0 commit comments

Comments
 (0)