1import { useCallback, useEffect, useMemo, useState } from 'react';
2import * as wg from 'lib/walletgen';
3import * as need from 'lib/need';
4import * as azimuth from 'azimuth-js';
5import * as ob from 'urbit-ob';
14} from '@urbit/roller-api';
15import { isDevelopment, isRopsten } from './flags';
16import { ROLLER_HOSTS } from './constants';
17import { useRollerStore } from 'store/roller';
18import { getTimeToNextBatch } from './utils/roller';
19import { useWallet } from 'store/wallet';
20import { usePointCursor } from 'store/pointCursor';
21import { useNetwork } from 'store/network';
23export default function useRoller() {
24 const { wallet, walletType, walletHdPath, authToken }: any = useWallet();
25 const { pointCursor }: any = usePointCursor();
26 const { web3, contracts }: any = useNetwork();
28 const { nextBatchTime, setNextBatchTime, setNextRoll } = useRollerStore();
29 const [config, setConfig] = useState<Config | null>(null);
31 const options: Options = useMemo(() => {
32 const type = isRopsten || !isDevelopment ? 'https' : 'http';
33 const host = isRopsten
34 ? ROLLER_HOSTS.ROPSTEN
37 : ROLLER_HOSTS.MAINNET;
38 const port = isDevelopment ? 8080 : 80;
39 const path = '/v1/roller';
51 const api = useMemo(() => {
52 return new RollerRPCAPI(options);
55 const fetchConfig = useCallback(async () => {
60 setNextBatchTime(response.nextBatch);
63 // TODO: more elegant error handling
65 '[fetchConfig:failed] is roller running on localhost?\n',
69 }, [api]); // eslint-disable-line react-hooks/exhaustive-deps
71 const generateInviteCodes = useCallback(
72 async (numInvites: number) => {
73 const _point = need.point(pointCursor);
74 const _contracts = contracts.getOrElse(null);
75 const _web3 = web3.getOrElse(null);
76 const _wallet = wallet.getOrElse(null);
77 const _authToken = authToken.getOrElse(null);
78 if (!_contracts || !_web3 || !_wallet || !_authToken) {
79 // not using need because we want a custom error
80 throw new Error('Internal Error: Missing Contracts/Web3/Wallet');
83 const planets: number[] = await azimuth.azimuth.getUnspawnedChildren(
88 // const planets: any = await azimuth.delegatedSending.getPlanetsToSend(
94 const starInfo = await api.getPoint(_point);
96 const tickets: { ticket: string; planet: string }[] = [];
97 const requests: Promise<string>[] = [];
99 const nonce = starInfo?.ownership?.owner?.nonce!;
100 const address = starInfo?.ownership?.owner?.address!;
102 for (let i = 0; i < numInvites && planets[i]; i++) {
103 const planet = planets[i];
105 const { ticket, owner } = await wg.generateTemporaryDeterministicWallet(
111 ship: _point, //ship that is spawning the planet
112 proxy: 'own', // this should be either "own" or "proxy"
116 address: owner.keys.address, // the new owner of the star (invite wallet)
117 ship: planet, // ship to spawn
120 const txHash = await api.hashTransaction(
126 const { signature } = _web3.eth.accounts.sign(
128 _wallet.privateKey.toString('hex')
131 requests.push(api.spawn(signature, from, address, data));
132 tickets.push({ ticket, planet: ob.patp(planet) });
135 const hashes = await Promise.all(requests);
137 return hashes.map((hash, i) => ({ hash, ...tickets[i] }));
151 const getPoints = useCallback(
152 async (proxy: Proxy, address: EthAddress) => {
153 const points: Ship[] =
155 ? await api.getOwnedPoints(address)
157 ? await api.getManagerFor(address)
159 ? await api.getVotingFor(address)
160 : proxy === 'transfer'
161 ? await api.getTransferringFor(address)
163 ? await api.getSpawningFor(address)
171 // On load, get initial config
178 }, [config, fetchConfig]);
181 const interval = setInterval(() => {
182 const nextRoll = getTimeToNextBatch(nextBatchTime, new Date().getTime());
183 setNextRoll(nextRoll);
185 if (nextBatchTime <= new Date().getTime()) {
186 api.getRollerConfig().then(response => {
187 setNextBatchTime(response.nextBatch);
192 return () => clearInterval(interval);
193 }, [nextBatchTime]); // eslint-disable-line react-hooks/exhaustive-deps
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