-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.js
243 lines (228 loc) · 9.24 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint-disable */
import { create } from 'zustand';
import { AgoricChainStoragePathKind, makeAgoricChainStorageWatcher } from '@agoric/rpc';
import { isBorrowOffer, isCreateOffer } from "../utils/helpers.js";
import { RentalPhase } from "../utils/constants.js";
const useStore = create((set, get) => ({
watcher: null,
brands: [],
boardAuxChildren: [],
crabbleOffers: [],
catalog: {},
brandToKeyword: {},
keywordToBrand: {},
brandToDisplayInfo: {},
smartWalletPurses: [],
vbankPurses: [],
wallet: null,
crabbleInstance: null,
watchedRentals: {},
ownedRentals: {},
borrowedRentals: {},
notifierState: { open: false, severity: '', message: '' },
setWatcher: (rpcAddr, chainId) => {
const watcher = makeAgoricChainStorageWatcher(rpcAddr, chainId);
set(() => ({ watcher }));
},
updateBrands: (brandArray) => {
const brandToKeyword = {};
const keywordToBrand = {};
[...brandArray].forEach(([keyword, brand]) => {
brandToKeyword[brand] = keyword;
keywordToBrand[keyword] = brand;
});
set(() => ({ brandToKeyword, keywordToBrand, brands: brandArray }));
},
updateVBank: vbankPurses => {
if (!vbankPurses) return;
const brandToDisplayInfo = {};
[...vbankPurses].forEach(({ brand, displayInfo }) => brandToDisplayInfo[brand] = displayInfo);
set(() => ({ vbankPurses }));
},
updateBrandToDisplayInfo: async boardAuxChildren => {
const { watcher, keywordToBrand } = get();
const brandToDisplayInfo = {};
try {
const queries = [...boardAuxChildren].map(id => watcher.queryOnce([
AgoricChainStoragePathKind.Data,
`published.boardAux.${id}`,
]));
const results = await Promise.all(queries);
[...results].forEach(({ allegedName, displayInfo }) => {
const brand = keywordToBrand[allegedName];
brandToDisplayInfo[brand] = displayInfo;
});
console.log('Results', { results });
set(() => ({ brandToDisplayInfo }))
} catch (e) {
console.log('Error fetching boardAux', e);
}
},
registerRentals: paths => {
const produceWatchedRentals = state => {
const newWatchedRentals = {};
// We filter out the paths we already registered along with the special 'governance' path
[...paths].filter(path => path !== 'governance' && !state.watchedRentals.hasOwnProperty(path))
.forEach(path => {
const stopWatching = state.watcher.watchLatest(
[AgoricChainStoragePathKind.Data, `published.crabble.rentals.${path}`],
rental => state.updateRental(rental, path),
error => {
console.error(`Error when fetching ${path}: `, error);
}
);
newWatchedRentals[path] = stopWatching;
console.log('PATHS', path);
console.log('Stop', stopWatching);
});
console.log('NEW_WATCHED_RENTALS', { newWatchedRentals })
return newWatchedRentals;
};
set(state => ({ watchedRentals: { ...state.watchedRentals, ...produceWatchedRentals(state) } }));
},
/**
* Stop watching if removed
* Update catalog for other cases
* Update owned rentals if rental is owned
* Update borrowed rentals if rental is borrowed
*/
updateRental: (rental, path) => {
const { watchedRentals } = get();
stopIfPhaseMatches(watchedRentals[path], () => rental.phase === 'removed');
console.log('Watched Rentals', watchedRentals);
set(state => ({ catalog: { ...state.catalog, [path]: rental } }));
},
registerRentalsLeg: subscriberPaths => {
const produceWatchedRentalsLeg = state => {
let newWatchedRentals = {};
[...subscriberPaths].filter(
([id]) => !state.watchedRentals.hasOwnProperty(id)
&& (isCreateOffer(id) || isBorrowOffer(id))
)
.forEach(([id, { rental }]) => {
const updateFunction = isCreateOffer(id) ?
state.updateOwnedRentals : state.updateBorrowedRentals;
const stopWatching = state.watcher.watchLatest(
[AgoricChainStoragePathKind.Data, rental],
rental => updateFunction(id, rental),
error => console.log('ERROR', { id, error })
);
newWatchedRentals[id] = stopWatching;
});
return newWatchedRentals;
}
set(state => ({
watchedRentals: { ...state.watchedRentals, ...produceWatchedRentalsLeg(state) }
}));
},
updateOwnedRentals: (id, rental) => {
stopIfPhaseMatches(get().watchedRentals[id],
() => rental.phase === 'removed');
set(state => ({
ownedRentals: { ...state.ownedRentals, [id]: rental }
}))
},
updateBorrowedRentals: (id, rental) => {
stopIfPhaseMatches(get().watchedRentals[id],
() => rental.phase === 'available' || rental.phase === 'liquidation');
set(state => ({
borrowedRentals: { ...state.borrowedRentals, [id]: rental }
}));
},
updateSmartWallet: smartWalletData => {
const { crabbleInstance } = get();
console.log('Crabble Instance', crabbleInstance);
const { purses, offerToUsedInvitation, offerToPublicSubscriberPaths } = smartWalletData;
const pathEntries = Object.fromEntries(offerToPublicSubscriberPaths);
const crabbleOffers = [...offerToUsedInvitation].filter(([,invitation]) => {
const { value: [{
instance
}]} = invitation;
return instance === crabbleInstance;
}).map(([id]) => {
return [pathEntries[id].rental, { id, type: isCreateOffer(id) ? 'owned' : (isBorrowOffer(id) ? 'borrowed' : 'error') }]
});
console.log('Crabble Offers', { crabbleOffers });
set(() => ({ crabbleOffers, smartWalletPurses: purses }));
},
getCatalog: () => {
const { catalog } = get();
return [...Object.entries(catalog)].map(([_, rental]) => rental);
},
getOwnedRentals: () => {
const { crabbleOffers, catalog } = get();
return getUserRentalByType(crabbleOffers, catalog, 'owned');
},
getBorrowedRentals: () => {
const { crabbleOffers, catalog } = get();
return getUserRentalByType(crabbleOffers, catalog, 'borrowed');
},
getActiveBorrows: () => {
const { crabbleOffers, catalog } = get();
return getUserRentalByType(crabbleOffers, catalog, 'borrowed')
.filter(({ phase }) => phase === RentalPhase.RENTED || phase === RentalPhase.GRACE_PERIOD);
},
notifyUser: (severity, message) => {
set(() => ({
notifierState: { open: true, severity, message }
}));
},
closeNotifier: () => {
set(() => ({
notifierState: { open: false, severity: '', message: '' }
}));
},
getUtilityBrands: () => {
const { smartWalletPurses } = get();
return [...smartWalletPurses].filter(({ balance, brand }) => {
if (balance.value.length <= 0) {
return false;
}
const value = balance.value[0];
return value && typeof value !== 'bigint';
});
},
getDisplayableUtilityBrands: () => {
const { vbankPurses } = get();
return [...vbankPurses]
.filter(({
displayInfo,
brandPetname
}) => displayInfo.assetKind === 'set' && brandPetname !== 'Invitation');
},
getKeywordFromBrand: brand => {
const { brandToKeyword } = get();
return brandToKeyword[brand];
},
getRentalBalances: () => {
const { getOwnedRentals } = get();
const ownedRentals = getOwnedRentals();
return [...Object.values(ownedRentals)].map(value => [...Object.entries(value.rentalBalance)]).flat()
},
getCollateralBalances: () => {
const { getOwnedRentals } = get();
const ownedRentals = getOwnedRentals();
return [...Object.values(ownedRentals)].map(value => [...Object.entries(value.collateralBalance)]).flat()
},
getDisplayInfo: brand => {
const { brandToDisplayInfo } = get();
return brandToDisplayInfo[brand];
},
getFungibleBrands: () => {
const { vbankPurses } = get();
if (!vbankPurses) return [];
return vbankPurses.filter(({ displayInfo }) => displayInfo.assetKind === 'nat');
}
}));
const stopIfPhaseMatches = (stopWatching, phaseChecker) => {
if (phaseChecker()) {
console.log('stopping')
stopWatching()
}
};
const getUserRentalByType = (crabbleOffers, catalog, wantedType) => {
return [...crabbleOffers]
.filter(([path, { type }]) => type === wantedType && catalog[path.split('.').pop()])
.map(([path, { id }]) => ({ id, ...catalog[path.split('.').pop()] }));
};
export default useStore;