Skip to content

Commit

Permalink
faucet api final
Browse files Browse the repository at this point in the history
  • Loading branch information
carrion9 committed Sep 11, 2021
1 parent 89edc16 commit 8a7359e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion locale/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ msgstr "Go Back"
#: src/features/farm/FarmListItem.tsx:97
#: src/features/farm/FarmListItemDetails.tsx:235
#: src/features/vault/VaultListItemDetails.tsx:293
#: src/pages/farm/index.tsx:224
#: src/pages/farm/index.tsx:226
msgid "Harvest"
msgstr "Harvest"

Expand Down
2 changes: 1 addition & 1 deletion locale/zh-CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ msgstr "返回"
#: src/features/farm/FarmListItem.tsx:97
#: src/features/farm/FarmListItemDetails.tsx:235
#: src/features/vault/VaultListItemDetails.tsx:293
#: src/pages/farm/index.tsx:224
#: src/pages/farm/index.tsx:226
msgid "Harvest"
msgstr "收获"

Expand Down
36 changes: 17 additions & 19 deletions src/pages/api/faucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function handler(req, res) {
let inOrder = false
let history = {
ips: {},
wallets: {},
wallets: {}
}

async function faucet(request) {
Expand All @@ -25,37 +25,37 @@ async function faucet(request) {
const receipt = await web3Request(address, amount)
ret.push({
status: 200,
message: `You have successfuly been sent ${amount} MOVR`,
message: `You have successfuly been sent ${amount} MOVR`
})
} catch (error) {
ret.push({
status: 429,
message: error.message,
message: error.message
})
}
inOrder = false
} else {
ret.push({
status: 429,
message: 'Processing too many orders, please try again in a moment',
message: 'Processing too many orders, please try again in a moment'
})
}
return ret
}

function checkLimit(request) {
const address = request.query.address.toLowerCase()
const ip = request.connection.remoteAddress
const timeLimit = parseInt(process.env.FAUCET_TIME_LIMIT_MIN) * 60 * 1000
if (
// TODO need to find ip from request
(history.ips.hasOwnProperty(request.ip) && history.ips[request.ip] > Date.now() - timeLimit) ||
(history.ips.hasOwnProperty(ip) && history.ips[ip] > Date.now() - timeLimit) ||
(history.wallets.hasOwnProperty(address) && history.wallets[address] > Date.now() - timeLimit)
) {
throw new Error(
`You have reached the daily limit. <br> <b>Limit expires</b> in ${timeLeft(history.ips[request.ip])}`
`You have reached the daily limit. <br> <b>Limit expires</b> in ${timeLeft(history.ips[ip])}`
)
}
history.ips[request.ip] = Date.now()
history.ips[ip] = Date.now()
history.wallets[address] = Date.now()
}

Expand All @@ -75,36 +75,34 @@ function secondsToString(uptime) {
}

function timeLeft(timestamp) {
const timeNeeded = process.env.REDIS_EXPIRE_SECONDS * 1000
const timeNeeded = process.env.FAUCET_TIME_LIMIT_MIN * 60 * 1000
const timePassed = Date.now() - timestamp
const timeLeft = timeNeeded - timePassed
return secondsToString(timeLeft / 1000)
}

async function web3Request(to, value) {
const wallet = await web3.eth.accounts.wallet.add(process.env.FAUCET_WALLET_PRIVATE_KEY)
const nonce = await web3.eth.getTransactionCount(wallet.address, 'pending')
const gasPrice = await web3.utils.toWei(process.env.FAUCET_GAS_PRICE_GWEI, 'gwei')
return new Promise(async (resolve, reject) => {
const transactionParams = {
//TODO fix nonce
nonce: this.nonce,
gasPrice: web3.utils.toHex(this.gasPrice),
nonce: web3.utils.toBN(nonce),
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: '0x9C40',
to: to,
//TODO fix address
from: this.wallet.address,
value: web3.utils.toWei(`${value}`, 'ether'),
from: wallet.address,
value: web3.utils.toWei(`${value}`, 'ether')
}
// TODO manage wallet privateKey
const signedTransaction = await web3.eth.accounts.signTransaction(transactionParams, this.wallet.privateKey)
const signedTransaction = await web3.eth.accounts.signTransaction(transactionParams, wallet.privateKey)

web3.eth
.sendSignedTransaction(signedTransaction.rawTransaction)
.then((tx) => {
this.nonce = `${parseInt(this.nonce, 10) + 1}`
console.log(tx)
resolve(tx)
})
.catch((error) => {
this.nonce = `${parseInt(this.nonce, 10) + 1}`
console.log(error)
reject(error)
})
Expand Down

0 comments on commit 8a7359e

Please sign in to comment.