1import { SpawnedPoints, UnspawnedPoints } from '@urbit/roller-api';
2import { POINT_DOMINIONS } from 'lib/constants';
3import { deriveNetworkSeedFromUrbitWallet } from 'lib/keys';
4import * as need from 'lib/need';
5import * as azimuth from 'azimuth-js';
6import { Invite, Invites } from 'lib/types/Invite';
7import useRoller from 'lib/useRoller';
8import { useWalletConnect } from 'lib/useWalletConnect';
9import { showNotification } from 'lib/utils/notifications';
15} from 'lib/utils/roller';
16import { useCallback } from 'react';
17import { useNetwork } from 'store/network';
18import { usePointCursor } from 'store/pointCursor';
19import { useRollerStore } from 'store/rollerStore';
25} from 'store/storage/roller';
26import { useWallet } from 'store/wallet';
27import { usePointCache } from 'store/pointCache';
28import create from 'zustand';
29import { generateCsvLine } from 'lib/utils/invite';
30import Point from 'lib/types/Point';
32const inviteTemplate = (
44export type InviteGeneratingStatus =
51 generatingNum: number;
52 status: InviteGeneratingStatus;
55interface InviteStore {
58 [point: number]: InviteJob;
60 addInvite: (point: number, invite: Invite) => void;
61 setInvites: (points: number, invites: Invite[]) => void;
62 updateJob: (point: number, updates: Partial<InviteJob>) => void;
65export const useInviteStore = create<InviteStore>((set, get) => ({
68 addInvite: (point: number, invite: Invite) => {
70 const newInvites: Invites = {};
71 const invites = state.invites[point];
73 if (!invites.find(inv => inv.planet === invite.planet)) {
74 newInvites[point] = [...invites, invite];
76 return { invites: Object.assign(state.invites, newInvites) };
79 setInvites: (point: number, invites: Invite[]) =>
81 const newInvites: Invites = {};
82 newInvites[point] = invites;
83 return { invites: Object.assign(state.invites, newInvites) };
85 updateJob: (point: number, updates: Partial<InviteJob>) => {
86 const jobs = get().inviteJobs;
87 const job = jobs[point] || { status: 'initial', generatingNum: 0 };
88 const newJob = Object.assign({}, job, updates);
91 set({ inviteJobs: jobs });
95export function useInvites() {
96 const { wallet, authToken, walletType }: any = useWallet();
97 const { connector } = useWalletConnect();
98 const { pointCursor }: any = usePointCursor();
99 const { web3, contracts }: any = useNetwork();
100 const allPoints: any = usePointCache();
101 const getDetails = allPoints?.getDetails;
102 const { api, ls, performL2Reticket } = useRoller();
103 const { point, pointList, setPendingTransactions } = useRollerStore();
110 } = useInviteStore();
112 const generateInviteInfo = async (planet: number, _authToken: string) => {
113 const { ticket, inviteWallet } = await generateInviteWallet(
118 return inviteTemplate(planet, ticket, inviteWallet.ownership.keys.address);
121 const getInvites = useCallback(async () => {
122 const inviteJob = inviteJobs[point.value];
123 if (inviteJob?.status === 'generating') {
128 updateJob(point.value, { status: 'generating' });
130 const curPoint: number = Number(need.point(pointCursor));
131 const _authToken = authToken.getOrElse(null);
132 const _contracts = contracts.getOrElse(null);
134 if (_authToken && _contracts) {
135 const spawnedPoints = point.isL2Spawn
136 ? await api.getSpawned(curPoint)
139 const newPending = await api.getPendingByShip(curPoint);
140 setPendingTransactions(newPending);
141 const pendingSpawns = getPendingSpawns(newPending);
143 const availablePoints = await azimuth.azimuth.getUnspawnedChildren(
148 const outgoingPoints = pointList
150 ({ value, isPlanet }) => isPlanet && availablePoints.includes(value)
152 .map(({ value }) => value);
154 const invitePlanets = spawnedPoints
156 outgoingPoints.filter((p: number) => !spawnedPoints.includes(p))
158 .filter((p: number) => !pendingSpawns.has(p))
161 setInvites(curPoint, []);
163 const newInvites: Invite[] = [];
164 // Iterate over all of the stored invites, generating wallet info as necessary
165 const storedInvites = getStoredInvites(ls);
167 for (let i = 0; i < invitePlanets.length; i++) {
168 updateJob(point.value, { generatingNum: i + 1 });
169 const planet = invitePlanets[i];
170 const storedInvite = storedInvites[planet];
172 storedInvite || (await generateInviteInfo(planet, _authToken));
174 const planetInfo = await api.getPoint(planet);
175 // unclaimed point: if still owned by the deterministic invite wallet
177 invite.owner.toLowerCase() ===
178 planetInfo.ownership?.owner?.address &&
179 planetInfo.dominion === POINT_DOMINIONS.L2
181 setStoredInvite(ls, invite);
182 addInvite(curPoint, invite);
183 newInvites.push(invite);
185 // The point has been claimed, remove if it's in the cache
187 removeStoredInvite(ls, planet);
192 updateJob(point.value, { status: 'finished', generatingNum: 0 });
196 console.warn('ERROR GETTING INVITES', error);
197 updateJob(point.value, { status: 'errored', generatingNum: 0 });
209 setPendingTransactions,
213 const generateInviteCodes = useCallback(
216 planets: SpawnedPoints | UnspawnedPoints,
217 spawn: boolean = true
219 const _contracts = contracts.getOrElse(null);
220 const _web3 = web3.getOrElse(null);
221 const _wallet = wallet.getOrElse(null);
222 const _authToken = authToken.getOrElse(null);
224 if (!_contracts || !_web3 || !_wallet || !_authToken) {
225 // not using need because we want a custom error
226 throw new Error('Internal Error: Missing Contracts/Web3/Wallet');
229 const invites: Invite[] = [];
230 const storedInvites = getStoredInvites(ls);
232 const proxy = point.getSpawnProxy();
234 if (proxy === undefined)
235 throw new Error("Error: Address doesn't match proxy");
237 const nonce = await api.getNonce({
242 for (let i = 0; i < planets.length; i++) {
243 updateJob(point.value, { generatingNum: i + 1 });
244 const planet = planets[i];
245 const nonceInc = i + nonce;
247 // When reticketing a planet in a browser that previously generated
248 // the invite, we need to remove it from the cache.
249 const storedInvite = storedInvites[planet];
251 removeStoredInvite(ls, planet);
254 const { ticket, inviteWallet } = await generateInviteWallet(
260 await performL2Reticket({
262 to: inviteWallet.ownership.keys.address,
263 manager: inviteWallet.management.keys.address,
265 toWallet: inviteWallet,
268 await submitL2Transaction({
270 address: _wallet.address,
275 pointToSpawn: planet,
282 const networkSeed = await deriveNetworkSeedFromUrbitWallet(
286 await submitL2Transaction({
293 type: 'configureKeys',
299 await registerProxyAddress(
306 inviteWallet.management.keys.address,
312 await submitL2Transaction({
317 type: 'transferPoint',
319 address: inviteWallet.ownership.keys.address,
330 owner: inviteWallet.ownership.keys.address,
334 setStoredInvites(ls, invites);
335 console.log('getting invites from code generation');
338 `Your invite${invites.length > 1 ? 's have' : ' has'} been generated!`
359 const generateCsv = useCallback(() => {
360 const pointInvites = invites[point.value];
363 throw new Error('There was an error creating the CSV');
366 return pointInvites.reduce(
367 (csvData, { ticket, planet }, ind) =>
368 (csvData += generateCsvLine(ind, ticket, planet)),
369 'Number,Planet,Invite URL,Point,Ticket\n'
371 }, [invites, point]);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list