2018-09-11 09:16:57 -07:00
|
|
|
import { TransactionObject } from 'web3/eth/types';
|
|
|
|
|
|
|
|
type Web3Method<T> = (index: number) => TransactionObject<T>;
|
|
|
|
|
|
|
|
export async function collectArrayElements<T>(
|
|
|
|
method: Web3Method<T>,
|
|
|
|
account: string,
|
|
|
|
): Promise<T[]> {
|
2018-09-10 09:55:26 -07:00
|
|
|
const arrayElements = [];
|
|
|
|
let noError = true;
|
|
|
|
let index = 0;
|
|
|
|
while (noError) {
|
|
|
|
try {
|
|
|
|
arrayElements.push(await method(index).call({ from: account }));
|
|
|
|
index += 1;
|
|
|
|
} catch (e) {
|
|
|
|
noError = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arrayElements;
|
|
|
|
}
|
2018-10-30 16:18:26 -07:00
|
|
|
|
|
|
|
interface Web3ErrorResponse {
|
|
|
|
code: number;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function web3ErrorToString(err: Web3ErrorResponse): string {
|
|
|
|
return err.message
|
|
|
|
.split('\n')[0]
|
|
|
|
.split(':')
|
|
|
|
.slice(-1)[0]
|
|
|
|
.trim();
|
|
|
|
}
|
2018-11-06 10:42:45 -08:00
|
|
|
|
|
|
|
export function safeEnable(): Promise<void> {
|
|
|
|
const w = typeof window === 'undefined' ? {} : (window as any);
|
|
|
|
if (!w.ethereum || !w.ethereum.enable) {
|
|
|
|
return Promise.reject('No web3 client installed');
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.ethereum.enable();
|
|
|
|
}
|