-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsfuel.ts
92 lines (81 loc) · 2.73 KB
/
sfuel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @license
* SKALE Metaport
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @file sfuel.ts
* @copyright SKALE Labs 2022-Present
*/
import debug from 'debug'
import { Provider } from 'ethers'
import MetaportCore from './metaport'
import { AddressType } from './interfaces'
import { isFaucetAvailable, getSFuel } from './faucet'
import { MAINNET_CHAIN_NAME, DEFAULT_MIN_SFUEL_WEI } from '../core/constants'
debug.enable('*')
const log = debug('metaport:sfuel')
export interface StationData {
balance: bigint
ok: boolean
}
export interface StationPowRes {
message: string
ok: boolean
}
export class Station {
endpoint: string
provider: Provider
constructor(
public chainName: string,
public mpc: MetaportCore
) {
this.chainName = chainName
this.mpc = mpc
this.provider = mpc.provider(chainName)
}
async getData(address: AddressType): Promise<StationData> {
try {
const balance = await this.provider.getBalance(address)
return { balance, ok: balance >= DEFAULT_MIN_SFUEL_WEI }
} catch (e) {
log(`ERROR: getSFuelData for ${this.chainName} failed!`)
log(e)
return { balance: undefined, ok: undefined }
}
}
isFaucetAvailable(): boolean {
return isFaucetAvailable(this.chainName, this.mpc.config.skaleNetwork)
}
async doPoW(address: AddressType): Promise<StationPowRes> {
// return { ok: true, message: 'PoW is not available for Ethereum Mainnet' };
if (!this.chainName || !isFaucetAvailable(this.chainName, this.mpc.config.skaleNetwork)) {
log('WARNING: PoW is not available for this chain')
if (this.chainName === MAINNET_CHAIN_NAME) {
return { ok: true, message: 'PoW is not available for Ethereum Mainnet' }
}
return { ok: false, message: 'PoW is not available for this chain' }
}
log('Mining sFUEL for ' + address + ' on ' + this.chainName + '...')
try {
await getSFuel(this.chainName, address, this.mpc)
return { ok: true, message: 'PoW finished successfully' }
} catch (e) {
log('ERROR: PoW failed!')
log(e)
return { ok: false, message: e.message }
}
}
}