Skip to content

Commit 7b82fc8

Browse files
integrate #1143 to fix #1141
1 parent 920919e commit 7b82fc8

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

src/lib/useRoller.ts

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -145,47 +145,66 @@ export default function useRoller() {
145145
}
146146

147147
const pointNum = Number(point);
148-
const rawDetails = await api.getPoint(pointNum);
149-
const isL2 = isL2Spawn(rawDetails?.dominion);
150-
151-
const details = toL1Details(rawDetails);
152148

153149
try {
154-
if (isL2) {
155-
const l2Quota = isL2 ? await api.getRemainingQuota(pointNum) : 0;
156-
const l2Allowance = isL2 ? await api.getAllowance(pointNum) : 0;
150+
// Try fetching from the roller
151+
const rawDetails = await api.getPoint(pointNum); // this fails for newly spawned L1 point until roller updates
152+
const details = toL1Details(rawDetails);
153+
const isL2 = isL2Spawn(rawDetails?.dominion);
157154

155+
if (isL2) {
156+
// Scenario 1: L2 point exists in roller state - use roller data
157+
const l2Quota = await api.getRemainingQuota(pointNum);
158+
const l2Allowance = await api.getAllowance(pointNum);
158159
return new Point({
159160
value: pointNum,
160161
details,
161162
address: _wallet.address,
162163
l2Quota,
163164
l2Allowance,
164165
});
166+
} else {
167+
// Scenario 2: L1 point exists in roller state - use combination of chain and roller data
168+
const _contracts = need.contracts(contracts);
169+
const l1Details = await azimuth.azimuth.getPoint(
170+
_contracts,
171+
pointNum
172+
);
173+
// Override with roller data for accuracy, L1 data here could be stale
174+
l1Details.sponsor = details.sponsor;
175+
l1Details.escapeRequested = details.escapeRequested;
176+
l1Details.escapeRequestedTo = details.escapeRequestedTo;
177+
return new Point({
178+
value: pointNum,
179+
details: l1Details,
180+
address: _wallet.address,
181+
});
165182
}
166-
167-
const _contracts = need.contracts(contracts);
168-
const l1Details = await azimuth.azimuth.getPoint(_contracts, point);
169-
170-
l1Details.sponsor = details.sponsor;
171-
l1Details.escapeRequested = details.escapeRequested;
172-
l1Details.escapeRequestedTo = details.escapeRequestedTo;
173-
174-
return new Point({
175-
value: pointNum,
176-
details: l1Details,
177-
address: _wallet.address,
178-
});
179183
} catch (e) {
180-
console.warn(e);
181-
// Just return a placeholder Point
182-
const details: L1Point = toL1Details();
183-
return new Point({
184-
value: pointNum,
185-
details,
186-
address: _wallet.address,
187-
isPlaceholder: true,
188-
});
184+
// roller failed to fetch point
185+
try {
186+
// Scenario 3: Newly spawned L1 point - not in roller state yet, use chain data only
187+
const _contracts = need.contracts(contracts);
188+
const l1Details = await azimuth.azimuth.getPoint(
189+
_contracts,
190+
pointNum
191+
);
192+
return new Point({
193+
value: pointNum,
194+
details: l1Details,
195+
address: _wallet.address,
196+
});
197+
} catch (e2) {
198+
// Scenario 4: Some other issue, point doesn’t exist on chain or in roller state. Just return placeholder.
199+
console.warn('Failed to fetch point data:', e2);
200+
const details: L1Point = toL1Details();
201+
return new Point({
202+
value: pointNum,
203+
details,
204+
address: _wallet.address,
205+
isPlaceholder: true,
206+
});
207+
}
189208
}
190209
},
191210
[api, wallet, contracts]
@@ -309,14 +328,14 @@ export default function useRoller() {
309328
proxy === 'own'
310329
? await api.getOwnedPoints(address)
311330
: proxy === 'manage'
312-
? await api.getManagerFor(address)
313-
: proxy === 'vote'
314-
? await api.getVotingFor(address)
315-
: proxy === 'transfer'
316-
? await api.getTransferringFor(address)
317-
: proxy === 'spawn'
318-
? await api.getSpawningFor(address)
319-
: [];
331+
? await api.getManagerFor(address)
332+
: proxy === 'vote'
333+
? await api.getVotingFor(address)
334+
: proxy === 'transfer'
335+
? await api.getTransferringFor(address)
336+
: proxy === 'spawn'
337+
? await api.getSpawningFor(address)
338+
: [];
320339

321340
return points;
322341
},
@@ -365,14 +384,14 @@ export default function useRoller() {
365384
const networkSeed = customNetworkSeed
366385
? customNetworkSeed
367386
: await attemptNetworkSeedDerivation({
368-
urbitWallet,
369-
wallet,
370-
authMnemonic,
371-
details: point,
372-
authToken,
373-
point: point.value,
374-
revision: nextRevision,
375-
});
387+
urbitWallet,
388+
wallet,
389+
authMnemonic,
390+
details: point,
391+
authToken,
392+
point: point.value,
393+
revision: nextRevision,
394+
});
376395
const txHash = await submitL2Transaction({
377396
api,
378397
wallet: _wallet,
@@ -422,7 +441,7 @@ export default function useRoller() {
422441
let nonce = await api.getNonce({ ship: point, proxy });
423442
const progress = onUpdate
424443
? (state: number) => onUpdate({ type: 'progress', state })
425-
: () => {};
444+
: () => { };
426445

427446
let requests = [];
428447

0 commit comments

Comments
 (0)