fix(core): update

This commit is contained in:
2022-02-11 17:49:12 +01:00
commit 688ce7b912
19 changed files with 28150 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import * as plugins from './helium.plugins';
import { HeliumWallet } from './helium.classes.heliumwallet';
export class Helium {
public baseApiUrl = 'https://helium-api.stakejoy.com';
public wallets: HeliumWallet[] = [];
public async addWalletByAddress(walletAddressArg: string) {
const newWallet = new HeliumWallet(this, walletAddressArg);
await newWallet.update();
this.wallets.push(newWallet);
return newWallet;
}
public async request<T = any>(urlArg: string): Promise<T> {
const webrequest = new plugins.webrequest.WebRequest();
const response = await webrequest.request(urlArg, {
method: 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36'
}
})
return response.json();
}
}

View File

@ -0,0 +1,48 @@
import * as plugins from './helium.plugins';
import * as interfaces from './interfaces';
import { Helium } from './helium.classes.helium';
import { HeliumHotspot } from './helium.classes.hotspot';
export class HeliumWallet {
heliumRef: Helium;
lastUpdate: plugins.smarttime.ExtendedDate;
originalData: interfaces.IWalletResponse['data'];
public walletAddress: string;
public earningsHnt24h: number;
public earningsHnt7d: number;
public earningsHnt14d: number;
public earningsHnt30d: number;
public hotspots: HeliumHotspot[] = [];
constructor(heliumRefArg: Helium, walletAddressArg: string) {
this.heliumRef = heliumRefArg;
this.walletAddress = walletAddressArg;
}
public async update() {
const currentDate = new plugins.smarttime.ExtendedDate();
const walletResponse: interfaces.IWalletResponse = await this.heliumRef.request(`${this.heliumRef.baseApiUrl}/v1/accounts/${this.walletAddress}`)
this.originalData = walletResponse.data;
const getHntEarningsForHours = async (hoursArg: number) => {
const earningsResponse: interfaces.IWalletEarningsResponse = await this.heliumRef.request(`${this.heliumRef.baseApiUrl}/v1/accounts/${this.walletAddress}/rewards/sum?min_time=-${hoursArg}%20hour&max_time=${currentDate.toISOString()}&bucket=hour`);
const reducedData:number = earningsResponse.data.map(dataArg => dataArg.sum).reduce((previous = 0, currentArg) => {
return previous + currentArg;
})
return reducedData / 100000000;
}
this.hotspots = await HeliumHotspot.getAllHotspotsForWallet(this)
this.earningsHnt24h = await getHntEarningsForHours(24);
this.earningsHnt7d = await getHntEarningsForHours(24 * 7);
this.earningsHnt14d = await getHntEarningsForHours(24 * 14);
this.earningsHnt30d = await getHntEarningsForHours(24 * 30);
}
}

View File

@ -0,0 +1,40 @@
import * as plugins from './helium.plugins';
import * as interfaces from './interfaces';
import { HeliumWallet } from './helium.classes.heliumwallet';
/**
* a helium hotspot
*/
export class HeliumHotspot {
public static async getAllHotspotsForWallet(walletArg: HeliumWallet) {
const hotspotsResponse: interfaces.IWalletHotspotResponse = await walletArg.heliumRef.request(
`${walletArg.heliumRef.baseApiUrl}/v1/accounts/13L7By1BmboTx1hRUuN3MtMjNmTFaWj5C6EbRZCK2sceaRCWZev/hotspots`
);
const returnHotspots: HeliumHotspot[] = [];
for (const hotspotArg of hotspotsResponse.data) {
const newHotspot = new HeliumHotspot(walletArg, hotspotArg.address);
await newHotspot.update();
returnHotspots.push(newHotspot);
}
return returnHotspots;
}
// INSTANCE
heliumWalletRef: HeliumWallet;
hotspotAddress: string;
public originalData: interfaces.IHotspotResponse['data'];
constructor(walletArg: HeliumWallet, hotspotAddress: string) {
this.heliumWalletRef = walletArg;
this.hotspotAddress = hotspotAddress;
}
public async update() {
const hotspotResponse: interfaces.IHotspotResponse = await this.heliumWalletRef.heliumRef.request(
`${this.heliumWalletRef.heliumRef.baseApiUrl}/v1/hotspots/${this.hotspotAddress}`
);
this.originalData = hotspotResponse.data
}
}

7
ts/helium.plugins.ts Normal file
View File

@ -0,0 +1,7 @@
import * as smarttime from '@pushrocks/smarttime';
import * as webrequest from '@pushrocks/webrequest';
export {
smarttime,
webrequest
}

1
ts/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './helium.classes.helium';

View File

@ -0,0 +1,5 @@
import { IWalletHotspotResponse } from './wallet';
export interface IHotspotResponse {
data: IWalletHotspotResponse['data'][0];
}

2
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './wallet';
export * from './hotspots';

71
ts/interfaces/wallet.ts Normal file
View File

@ -0,0 +1,71 @@
export interface IWalletResponse {
data: {
validator_count: number;
staked_balance: number;
speculative_sec_nonce: number;
speculative_nonce: number;
sec_nonce: number;
sec_balance: number;
nonce: number;
hotspot_count: number;
dc_nonce: number;
dc_balance: number;
block: number;
balance: number;
address: string;
};
}
export interface IWalletEarningsResponse {
meta: { min_time: string; max_time: string; bucket: 'hour' };
data: {
total: number;
timestamp: string;
sum: number;
stddev: number;
min: number;
median: number;
max: number;
avg: number;
}[];
}
export interface IWalletHotspotResponse {
data: {
lng: number;
lat: number;
timestamp_added: string;
status: {
timestamp: string;
online: 'online' | 'offline';
listen_addrs: string[];
height: number;
};
reward_scale: number;
payer: string;
owner: string;
nonce: number;
name: string;
mode: 'full' | 'light';
location_hex: string;
location: string;
last_poc_challenge: number;
last_change_block: number;
geocode: {
short_street: string;
short_state: string;
short_country: string;
short_city: string;
long_street: string;
long_state: string;
long_country: string;
long_city: string;
city_id: string;
};
gain: number;
elevation: number;
block_added: number;
block: number;
address: string;
}[];
}