diff --git a/src/app/lib/action.ts b/src/app/lib/action.ts index 7fe91f3..970975c 100644 --- a/src/app/lib/action.ts +++ b/src/app/lib/action.ts @@ -18,6 +18,7 @@ interface IData { address: string captcha: CaptchaSolutionRequest, promoCode: string | undefined + isMainnetRns: boolean } //Job @@ -50,7 +51,7 @@ const TESTNET_CHAIN_ID = 31; export async function dispense(data: IData) { const faucetHistory: FaucetHistory = loadFaucetHistory(); - const { address, captcha, promoCode } = data; + const { address, captcha, promoCode, isMainnetRns } = data; const headersList = await headers(); const ip: string = headersList.get('x-forwarded-for') || headersList.get('x-user-ip') as string; @@ -58,7 +59,7 @@ export async function dispense(data: IData) { const faucetBalance: number = Number(await web3.eth.getBalance(faucetAddress())); try { - const dispenseAddress: string = await addressUtil.retriveAddressFromFrontend(address); + const dispenseAddress: string = await addressUtil.retriveAddressFromFrontend(address, isMainnetRns); const captchaSolutionRequest: CaptchaSolutionRequest = captcha; logger.event('dispensing to ' + dispenseAddress); @@ -74,7 +75,8 @@ export async function dispense(data: IData) { faucetBalance, ip!, promoCode, - faucetHistory + faucetHistory, + isMainnetRns ); if (!validationStatus.valid()) { @@ -183,12 +185,13 @@ const runValidations = ( faucetBalance: number, ip: string, promoCode: string | undefined, - faucetHistory: FaucetHistory + faucetHistory: FaucetHistory, + isMainnetRns: boolean ): ValidationStatus => { const validations: (() => string)[] = [ () => captchaRejected(captchaSolutionResponse), () => alreadyDispensed(dispenseAddress, ip, faucetHistory, promoCode), - () => invalidAddress(dispenseAddress), + () => invalidAddress(dispenseAddress, isMainnetRns), () => insuficientFunds(faucetBalance) ]; const errorMessages: string[] = validations.map(validate => validate()).filter(e => e != '' && e != '-'); diff --git a/src/components/container.tsx b/src/components/container.tsx index e4f35a1..4a477dc 100644 --- a/src/components/container.tsx +++ b/src/components/container.tsx @@ -15,6 +15,7 @@ function Container() { const captchaValue = useRef(null); const [dispenseAddress, setDispenseAddress] = useState(''); const [siteKeyCaptcha, setSiteKeyCaptcha] = useState(''); + const [isMainnetRns, setIsMainnetRns] = useState(true); useEffect(() => { setSiteKeyCaptcha(siteKey()); @@ -56,7 +57,8 @@ function Container() { promoCode: code, captcha: { token: captchaValue.current!.getValue()! - } + }, + isMainnetRns: isMainnetRns }) .then((res: DispenseResponse | undefined) => { const data: DispenseResponse = res!; @@ -88,6 +90,8 @@ function Container() { onAddressChange: handleDispenseAddressChange, onDispenseClick: handleFaucetButtonClick, siteKeyCaptcha, + isMainnetRns: isMainnetRns, + setIsMainnetRns: setIsMainnetRns }; return ( <> diff --git a/src/components/faucet.tsx b/src/components/faucet.tsx index 221feb3..552f0fa 100644 --- a/src/components/faucet.tsx +++ b/src/components/faucet.tsx @@ -1,4 +1,4 @@ -import React, { ChangeEvent, RefObject, useCallback, useState } from 'react'; +import React, { ChangeEvent, RefObject, useCallback, useEffect, useState } from 'react'; import ReCAPTCHA from "react-google-recaptcha"; import { debounce } from '../utils/debounce'; import Spinner from './control/Spinner'; @@ -12,6 +12,8 @@ export interface FaucetProps { captchaValue: RefObject; onAddressChange: (event: ChangeEvent) => void; onDispenseClick: (code: string | undefined) => void; + isMainnetRns: boolean; + setIsMainnetRns: (state: boolean) => void; } const Faucet = (props: FaucetProps) => { @@ -23,7 +25,7 @@ const Faucet = (props: FaucetProps) => { const [inputCode, setInputCode] = useState(''); const [validCode, setValidCode] = useState(); const [msgError, setMsgError] = useState(); - + const [isRNS, setIsRNS] = useState(false); const handleForm = () => { // validate form setError({ address: false, captchaValue: false }); @@ -57,6 +59,14 @@ const Faucet = (props: FaucetProps) => { debouncedGetCode(code); } + useEffect(() => { + if(props.dispenseAddress.includes('.rsk')) { + setIsRNS(true); + } else { + setIsRNS(false); + } + }, [props.dispenseAddress]); + return (
@@ -104,6 +114,26 @@ const Faucet = (props: FaucetProps) => { }
+ {isRNS && +
props.setIsMainnetRns(!props.isMainnetRns)} + > + +
+ props.setIsMainnetRns(!props.isMainnetRns)} + /> +
+
+
+
}
{ props.siteKeyCaptcha ? { + async retriveAddressFromFrontend(frontendAddress: string = 'Undefined address', mainnet: boolean): Promise { const posibleRnsAlias = frontendAddress.includes('.rsk'); if(posibleRnsAlias) { try { - const resolver = Resolver.forRskMainnet({}) + let resolver; + if(mainnet) { + resolver = Resolver.forRskMainnet({}) + } else { + resolver = Resolver.forRskTestnet({}) + } const address = await resolver.addr(frontendAddress); return address } catch(e) { logger.error(`Couldn't resolve address for this rnsAlias: ${frontendAddress}`); logger.error(e); - return 'INVALID RNS'; } } else { diff --git a/src/utils/validations.ts b/src/utils/validations.ts index 0df8620..d40a7e6 100644 --- a/src/utils/validations.ts +++ b/src/utils/validations.ts @@ -47,4 +47,4 @@ export const alreadyDispensed = (address: string, ip:string, faucetHistory: Fauc saveFaucetHistory(faucetHistory) return '' } -export const invalidAddress = (dispenseAddress: string): string => !isValidAddress(dispenseAddress, CHAIN_ID) ? 'Invalid address, provide a valid one.' : ''; \ No newline at end of file +export const invalidAddress = (dispenseAddress: string, isMainnetRns: boolean): string => !isValidAddress(dispenseAddress, isMainnetRns ? 30 : 31) ? 'Invalid address, provide a valid one.' : '';