import { addTransaction , getApiKey , getDailyUsage } from "../db" ; import { getAddressChain , sendDrop , sendDropX } from "./helper" ; import { BN } from "avalanche" ; import { CONFIG } from "../ava" ; import axios , { AxiosResponse } from "axios" ; import { CONFIG_C } from "../eth" ; class ApiHelper { constructor ( ) { } static async tokenCustom ( apiKey , amount , to ) { if ( ! apiKey || ! amount || ! to ) { throw new Error ( 'Invalid request parameters.' ) ; } let key = await getApiKey ( apiKey ) ; if ( ! key ) { throw new Error ( 'Invalid API key.' ) ; } let amtBN = new BN ( amount . toString ( ) ) ; let usage = await getDailyUsage ( key ) ; let maxUsage = new BN ( key . daily_limit ) ; let available = maxUsage . sub ( usage ) ; if ( available . lt ( amtBN ) ) { throw new Error ( `Failed to send drop. Daily token quota is reached. Remaining Quota: ${ available . toString ( ) } nAVAX` ) ; } let txID = await sendDrop ( to , amtBN ) ; let tx = await addTransaction ( key , amtBN , to ) ; return txID ; } static async token ( address , captchaResponse ) { if ( ! captchaResponse ) { throw new Error ( 'Invalid Captcha' ) ; } let params = new URLSearchParams ( ) ; params . append ( 'secret' , CONFIG . CAPTCHA_SECRET ! ) ; params . append ( 'response' , captchaResponse ) ; let axios_res = await axios ( { method : 'post' , url : "https://www.google.com/recaptcha/api/siteverify" , data : params , } ) ; let data = axios_res . data ; if ( data . success ) { let amtX = new BN ( CONFIG . DROP_SIZE ) ; let amxC = new BN ( CONFIG_C . DROP_SIZE ) . divRound ( new BN ( 1000000000 ) ) ; let chain = getAddressChain ( address ) ; let amtBN = chain === 'X' ? amtX : amxC ; let txID = await sendDrop ( address , amtBN ) ; return txID ; } else { throw new Error ( 'Invalid Captcha' ) ; } } } export default ApiHelper ;