forked from ava-labs/avalanche-faucet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelineChecks.ts
73 lines (66 loc) · 2.67 KB
/
pipelineChecks.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
import { CouponService } from '../CouponService/couponService'
import { MainnetCheckService } from '../mainnetCheckService'
import { checkMainnetBalance } from './mainnetBalanceCheck'
export enum PIPELINE_CHECKS {
MAINNET_BALANCE = 'mainnet_check',
COUPON = 'coupon_check'
}
export type PipelineCheckValidity = {
isValid: boolean,
checkPassedType?: PIPELINE_CHECKS,
errorMessage?: string,
dripAmount: number,
mainnetBalance?: number,
}
export function pipelineFailureMessage(mainnetBalanceCheckEnabled: boolean | string, couponCheckEnabled: boolean): string {
if (mainnetBalanceCheckEnabled && couponCheckEnabled) {
return "Either mainnet balance or a valid coupon is required!"
} else if (mainnetBalanceCheckEnabled) {
return "Mainnet balance is required!"
} else if (couponCheckEnabled) {
return "A valid coupon is required!"
}
return ""
}
export async function checkCouponPipeline(
couponService: CouponService,
pipelineCheckValidity: PipelineCheckValidity,
faucetConfigId: string,
coupon?: string,
) {
// if coupon is required but not passed in request
if (coupon === undefined) {
return
}
const couponValidity = await couponService.consumeCouponAmount(coupon, faucetConfigId, pipelineCheckValidity.dripAmount)
if (!couponValidity.isValid) {
pipelineCheckValidity.errorMessage = "Invalid or expired coupon provided. Contact support team on Discord! "
return
} else {
pipelineCheckValidity.isValid = true
pipelineCheckValidity.dripAmount = couponValidity.amount
pipelineCheckValidity.checkPassedType = PIPELINE_CHECKS.COUPON
}
}
export async function checkMainnetBalancePipeline(
mainnetCheckService: MainnetCheckService,
pipelineCheckValidity: PipelineCheckValidity,
rpc: string,
address: string,
) {
const {isValid, balance} = await checkMainnetBalance(rpc, address)
if (isValid) {
const mainnetAddressValidity = await mainnetCheckService.checkAddressValidity(address)
if (mainnetAddressValidity.isValid) {
pipelineCheckValidity.isValid = true
pipelineCheckValidity.checkPassedType = PIPELINE_CHECKS.MAINNET_BALANCE
pipelineCheckValidity.mainnetBalance = balance
} else if (mainnetAddressValidity.internalError) {
pipelineCheckValidity.errorMessage = "Some internal error occurred during mainnet check. "
} else {
pipelineCheckValidity.errorMessage = "Address has exhausted maximum balance checks! Please do some mainnet transactions. "
}
} else {
pipelineCheckValidity.errorMessage = "Mainnet balance check failed! "
}
}