Skip to content

Commit

Permalink
Merge pull request #64 from ava-labs/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
rajranjan0608 authored Sep 29, 2022
2 parents fa21553 + 0e6bec4 commit 85fd582
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 41 deletions.
25 changes: 20 additions & 5 deletions client/src/components/FaucetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const FaucetForm = (props: any) => {
const [faucetAddress, setFaucetAddress] = useState<string | null>(null)
const [options, setOptions] = useState<DropdownOption[]>([])
const [tokenOptions, setTokenOptions] = useState<DropdownOption[]>([]);
const [balance, setBalance] = useState<number>(0)
const [balance, setBalance] = useState<string>("0")
const [shouldAllowSend, setShouldAllowSend] = useState<boolean>(false)
const [isLoading, setIsLoading] = useState<boolean>(false)
const [isFetchingBalance, setIsFetchingBalance] = useState<AbortController | null>(null)
Expand All @@ -49,12 +49,12 @@ const FaucetForm = (props: any) => {
// Update balance whenver chain changes or after transaction is processed
useEffect(() => {
updateBalance()
}, [chain, sendTokenResponse, chainConfigs])
}, [chain, token, sendTokenResponse, chainConfigs])

// Make REQUEST button disabled if either address is not valid or balance is low
useEffect(() => {
if(address) {
if(balance > chainConfigs[token!]?.DRIP_AMOUNT) {
if(BigInt(balance) > calculateBaseUnit(chainConfigs[token!]?.DRIP_AMOUNT, chainConfigs[token!]?.DECIMALS)) {
setShouldAllowSend(true)
return
}
Expand Down Expand Up @@ -230,6 +230,21 @@ const FaucetForm = (props: any) => {
}
}

function calculateBaseUnit(amount: string = "0", decimals: number = 18): BigInt {
for(let i = 0; i < decimals; i++) {
amount += "0"
}
return BigInt(amount)
}

function calculateLargestUnit(amount: string = "0", decimals: number = 18): string {
let base = "1"
for(let i = 0; i < decimals; i++) {
base += "0"
}
return (BigInt(amount) / BigInt(base)).toString()
}

function chainToIndex(id: any): number | null {
if(chainConfigs?.length > 0) {
if(typeof id == "string") {
Expand Down Expand Up @@ -468,7 +483,7 @@ const FaucetForm = (props: any) => {
<div>
<div style={{width: "100%"}}>
<span style={{color: "grey", fontSize: "12px", float: "right"}}>
Faucet balance: {Math.round(balance/1e9 * 100) / 100} {chainConfigs[token!]?.TOKEN}
Faucet balance: {calculateLargestUnit(balance, chainConfigs[token!]?.DECIMALS)} {chainConfigs[token!]?.TOKEN}
</span>

<span style={{color: "grey", fontSize: "12px"}}>
Expand Down Expand Up @@ -517,7 +532,7 @@ const FaucetForm = (props: any) => {
?
<ClipLoader size="20px" speedMultiplier={0.3} color="403F40"/>
:
<span>Request {chainConfigs[token || 0]?.DRIP_AMOUNT / 1e9} {chainConfigs[token || 0]?.TOKEN}</span>
<span>Request {chainConfigs[token || 0]?.DRIP_AMOUNT} {chainConfigs[token || 0]?.TOKEN}</span>
}
</button>
</div>
Expand Down
56 changes: 28 additions & 28 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/43113/chain-logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"DECIMALS": 18,
"RECALIBRATE": 30,
"RATELIMIT": {
"MAX_LIMIT": 1,
Expand All @@ -37,7 +38,7 @@
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/11111/chain-logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -53,7 +54,7 @@
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/335/chain-logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -69,7 +70,7 @@
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/73773/chain-logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "11000000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -85,23 +86,7 @@
"IMAGE": "https://pirateverse.finance/images/pirateverse.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
}
},
{
"ID": "DIGARD",
"NAME": "Digard Network Test",
"TOKEN": "DIGA",
"RPC": "https://api.digard.network/ext/bc/sNUvM8CDbYbwi99ih238cbwyRiDYsX6GPKnFF1NzignfKfte4/rpc",
"CHAINID": 960006,
"EXPLORER": "https://testnet.avascan.info/blockchain/digard",
"IMAGE": "https://cdn.digard.io/digard.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -117,7 +102,7 @@
"IMAGE": "https://rotw.games/assets/icons/icon_nmac_fill.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -133,7 +118,7 @@
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/808/chain-logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -149,7 +134,7 @@
"IMAGE": "https://i.imgur.com/6ZG2tIW.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
Expand All @@ -165,7 +150,7 @@
"IMAGE": "https://cdn-production.joinhighrise.com/blockchain/highrise_blockchain_testnet_logo.png",
"MAX_PRIORITY_FEE": "2000000000",
"MAX_FEE": "100000000000",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"RECALIBRATE": 30,
"RATELIMIT": {
"MAX_LIMIT": 1,
Expand All @@ -174,13 +159,28 @@
}
],
"erc20tokens": [
{
"ID": "USDCC",
"HOSTID": "C",
"NAME": "USDC (Circle)",
"TOKEN": "USDC",
"IMAGE": "https://cryptologos.cc/logos/usd-coin-usdc-logo.png",
"CONTRACTADDRESS": "0x5425890298aed601595a70ab815c96711a31bc65",
"DRIP_AMOUNT": 1,
"GASLIMIT": "100000",
"DECIMALS": 6,
"RATELIMIT": {
"MAX_LIMIT": 1,
"WINDOW_SIZE": 1440
}
},
{
"ID": "WAVAXC",
"HOSTID": "C",
"NAME": "Wrapped AVAX",
"TOKEN": "WAVAX",
"CONTRACTADDRESS": "0xd00ae08403B9bbb9124bB305C09058E32C39A48c",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"GASLIMIT": "150000",
"DECIMALS": 18,
"RATELIMIT": {
Expand All @@ -195,7 +195,7 @@
"TOKEN": "ALOT",
"IMAGE": "https://glacier-api.avax.network/proxy/chain-assets/main/chains/432201/chain-logo.png",
"CONTRACTADDRESS": "0x9983F755Bbd60d1886CbfE103c98C272AA0F03d6",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"GASLIMIT": "150000",
"DECIMALS": 18,
"RATELIMIT": {
Expand Down Expand Up @@ -224,7 +224,7 @@
"NAME": "Wrapped WAGMI",
"TOKEN": "WWGM",
"CONTRACTADDRESS": "0x3Ee7094DADda15810F191DD6AcF7E4FFa37571e4",
"DRIP_AMOUNT": 2000000000,
"DRIP_AMOUNT": 2,
"GASLIMIT": "150000",
"DECIMALS": 18,
"RATELIMIT": {
Expand Down
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ router.get('/getBalance', (req: any, res: any) => {
let balance: BN = evm?.instance.getBalance(erc20)

if(balance) {
balance = balance.div(new BN(1e9))
balance = balance
} else {
balance = new BN(0)
}
Expand Down
1 change: 1 addition & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type ChainType = {
CHAINID: number,
EXPLORER: string,
IMAGE: string,
DECIMALS?: number,
MAX_PRIORITY_FEE: string,
MAX_FEE: string,
DRIP_AMOUNT: number,
Expand Down
16 changes: 9 additions & 7 deletions vms/evm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { BN } from 'avalanche'
import Web3 from 'web3'

import { calculateBaseUnit } from './utils'
import Log from './Log'
import ERC20Interface from './ERC20Interface.json'

import { ChainType, SendTokenResponse, RequestType } from './evmTypes'

export default class EVM {
web3: any
account: any
NAME: string
DRIP_AMOUNT: number | BN
DRIP_AMOUNT: BN
DECIMALS: number
LEGACY: boolean
MAX_PRIORITY_FEE: string
MAX_FEE: string
Expand All @@ -35,7 +36,8 @@ export default class EVM {
this.contracts = new Map()

this.NAME = config.NAME
this.DRIP_AMOUNT = (new BN(config.DRIP_AMOUNT)).mul(new BN(1e9))
this.DECIMALS = config.DECIMALS || 18
this.DRIP_AMOUNT = calculateBaseUnit(config.DRIP_AMOUNT.toString(), this.DECIMALS)
this.MAX_PRIORITY_FEE = config.MAX_PRIORITY_FEE
this.MAX_FEE = config.MAX_FEE
this.RECALIBRATE = config.RECALIBRATE || 30
Expand Down Expand Up @@ -98,13 +100,13 @@ export default class EVM {
return
}

let amount: BN | number = this.DRIP_AMOUNT
let amount: BN = this.DRIP_AMOUNT

// If id is provided, then it is ERC20 token transfer, so update the amount
if(this.contracts.get(id)) {
const dripAmount: number = this.contracts.get(id).config.DRIP_AMOUNT
if(dripAmount) {
amount = (new BN(dripAmount)).mul(new BN(1e9))
amount = calculateBaseUnit(dripAmount.toString(), this.contracts.get(id).config.DECIMALS || 18)
}
}

Expand Down Expand Up @@ -227,7 +229,7 @@ export default class EVM {
this.nonce++
this.executeQueue()
} else {
this.log.warn("Faucet balance too low!")
this.log.warn("Faucet balance too low!" + this.balance)
this.hasError.set(req.receiver, "Faucet balance too low! Please try after sometime.")
}
}
Expand Down
1 change: 1 addition & 0 deletions vms/evmTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ChainType = {
IMAGE: string,
MAX_PRIORITY_FEE: string,
MAX_FEE: string,
DECIMALS?: number,
DRIP_AMOUNT: number,
RECALIBRATE?: number,
RATELIMIT: {
Expand Down
9 changes: 9 additions & 0 deletions vms/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BN } from 'avalanche'

export function calculateBaseUnit(amount: string, decimals: number): BN {
for(let i = 0; i < decimals; i++) {
amount += "0"
}

return new BN(amount)
}

0 comments on commit 85fd582

Please sign in to comment.