Skip to content

Commit

Permalink
Api: improve tx execution. App: improve API response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Licari committed Nov 26, 2023
1 parent d67c6d2 commit 490a90b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
7 changes: 7 additions & 0 deletions api/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def is_token_enabled(address, tokens_list):
return is_enabled


def get_balance(w3, address, format='ether'):
balance = w3.eth.get_balance(address)
return w3.from_wei(balance, format)


def create_app():
# Init Flask app
app = Flask(__name__)
Expand All @@ -32,12 +37,14 @@ def create_app():
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(app.config['FAUCET_PRIVATE_KEY']))

cache = Cache(app.config['FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS'])
faucet_balance = get_balance(w3, app.config['FAUCET_ADDRESS'])

# Set logger
logging.basicConfig(level=logging.INFO)
logging.info("="*60)
logging.info("RPC_URL = " + app.config['FAUCET_RPC_URL'])
logging.info("FAUCET ADDRESS = " + app.config['FAUCET_ADDRESS'])
logging.info("FAUCET BALANCE = %d %s" % (faucet_balance, app.config['FAUCET_CHAIN_NATIVE_TOKEN_SYMBOL']))
logging.info("="*60)


Expand Down
5 changes: 2 additions & 3 deletions api/api/services/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def claim_native(w3, sender, recipient, amount):
'to': recipient,
'value': amount
}
return w3.eth.send_transaction(tx_dict)
return w3.eth.send_transaction(tx_dict).hex()

def claim_token(w3, sender, recipient, amount, token_address):
"""
Expand All @@ -28,5 +28,4 @@ def claim_token(w3, sender, recipient, amount, token_address):
- token_address: String
"""
token = Token(token_address, w3)
tx_hash = token.transfer(sender, recipient, amount)
return tx_hash
return token.transfer(sender, recipient, amount)
4 changes: 2 additions & 2 deletions app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function App() {
<Container maxWidth="xl" sx={{ marginBottom: "2em" }} id="app-container">
<ToastContainer
position="top-right"
autoClose={50000}
autoClose={false}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
closeOnClick={false}
rtl={false}
pauseOnFocusLoss
draggable
Expand Down
20 changes: 13 additions & 7 deletions app/src/components/hcaptcha/hcaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Select,
MenuItem,
useMediaQuery,
InputLabel
} from "@mui/material";
import Card from "@mui/material/Card";
import CardContent from "@mui/material/CardContent";
Expand All @@ -31,7 +32,6 @@ export const HCaptchaForm = function () {
const [enabledTokens, setEnabledTokens] = useState([]);
const [tokenAmount, setTokenAmount] = useState(0);
const [showLoading, setShowLoading] = useState(false);
const [transactionHash, setTransactionHash] = useState("");

const getFaucetInfo = async () => {
return axios.get(`${process.env.REACT_APP_FAUCET_API_URL}/info`);
Expand Down Expand Up @@ -68,14 +68,20 @@ export const HCaptchaForm = function () {
const divs = []

for(let idx in errors) {
divs.push(<div>{errors[idx]}</div>)
divs.push(<div key={idx}>{errors[idx]}</div>)
}

return (
<div>{divs}</div>
)
}

const ToastTxSuccessful = (txHash) => (
<div>
Tokens sent to your wallet address. Hash: {txHash}
</div>
);

const sendRequest = async () => {
if (walletAddress.length <= 0) {
toast.error("Please provide a wallet address.");
Expand All @@ -97,10 +103,9 @@ export const HCaptchaForm = function () {
.post(apiURL, req)
.then((response) => {
setShowLoading(false);
setTransactionHash(response.data.transactionHash);
setWalletAddress("");
setCaptchaVerified(true);
toast("Tokens sent to your wallet address. Hash: " + transactionHash);
toast(ToastTxSuccessful(response.data.transactionHash));
})
.catch((error) => {
setShowLoading(false);
Expand Down Expand Up @@ -162,17 +167,18 @@ export const HCaptchaForm = function () {
<CardContent>
<Grid container spacing={2}>
<Grid item xs={12}>
<label>Wallet address</label>
<InputLabel id="input-wallet-address-label">Wallet address</InputLabel>
<TextField
onChange={handleWalletAddressChange}
value={walletAddress}
id="wallet-address"
labelId="input-wallet-address-label"
fullWidth
/>
</Grid>
<Grid item xs={12}>
<label>Token</label>
<Select value={tokenAddress} onChange={handleTokenChange}>
<InputLabel id="select-token-label">Token</InputLabel>
<Select value={tokenAddress} onChange={handleTokenChange} labelId="select-token-label">
{enabledTokens?.map((item) => {
return (
<MenuItem key={item.address} value={item.address}>
Expand Down

0 comments on commit 490a90b

Please sign in to comment.