Skip to content

Commit

Permalink
fix tokenamount
Browse files Browse the repository at this point in the history
submodule fix
  • Loading branch information
wsdt committed Oct 7, 2023
1 parent 6824c72 commit bbdd6d0
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 288 deletions.
6 changes: 3 additions & 3 deletions boba_community/hc-captcha-metafaucet/api/hc_sendMetaTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const FAUCET_ABI = [{
{ internalType: 'string', name: '_key', type: 'string' },
{ internalType: 'address', name: '_to', type: 'address' }
],
name: 'getNativeFaucet',
name: 'getFaucet',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
Expand Down Expand Up @@ -74,8 +74,8 @@ exports.handler = async function hc_sendMetaTx(event, context) {
const faucetContract = new ethers.Contract(faucetAddr, FAUCET_ABI, wallet);

try {
await faucetContract.estimateGas.getNativeFaucet(body.uuid, body.key, body.to);
const tx = await faucetContract.getNativeFaucet(body.uuid, body.key, body.to);
await faucetContract.estimateGas.getFaucet(body.uuid, body.key, body.to);
const tx = await faucetContract.getFaucet(body.uuid, body.key, body.to);
const receipt = await tx.wait()

return await return_payload(redisClient, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,44 @@
pragma solidity ^0.8.9;

import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import './TuringHelper.sol';

contract BobaMainnetFaucet is Ownable {
address public NativeAddress = 0x4200000000000000000000000000000000000006;
contract BobaFaucet is Ownable {
uint256 public nativeFaucetAmount;
uint256 public tokenFaucetAmount;
uint256 public waitingPeriod;

string public hcBackendUrl;
IERC20 public token;
TuringHelper public hcHelper;

string public hcBackendUrl;

uint256 private constant SAFE_GAS_STIPEND = 6000;

mapping(address => uint256) public nativeClaimRecords;
mapping(address => uint256) public claimRecords;

event Configure(
address turingHelperAddress,
address tokenAddress,
string turingUrl,
uint256 waitingPeriod,
uint256 nativeFaucetAmount
uint256 nativeFaucetAmount,
uint256 tokenFaucetAmount
);

event WithdrawNative(
event Withdraw(
address receiver,
uint256 amount
uint256 nativeAmount,
uint256 tokenAmount
);

event IssuedNative(
event Issued(
bytes32 uuid,
bytes32 key,
address receiver,
uint256 amount,
uint256 tokenAmount,
uint256 timestamp
);

Expand All @@ -45,75 +52,97 @@ contract BobaMainnetFaucet is Ownable {

constructor(
address _hcHelperAddress,
address _token,
string memory _hcUrl,
uint256 _waitingPeriod,
uint256 _nativeFaucetAmount
uint256 _nativeFaucetAmount,
uint256 _tokenFaucetAmount
) {
hcHelper = TuringHelper(_hcHelperAddress);
token = IERC20(_token);
hcBackendUrl = _hcUrl;
waitingPeriod = _waitingPeriod;
nativeFaucetAmount = _nativeFaucetAmount;
tokenFaucetAmount = _tokenFaucetAmount;
}

// allow contract to receive ETH
receive() external payable {}

function configure(
address _turingHelperAddress,
address _tokenAddress,
string memory _turingUrl,
uint256 _waitingPeriod,
uint256 _nativeFaucetAmount
uint256 _nativeFaucetAmount,
uint256 _tokenFaucetAmount
) public onlyOwner {
require(_turingHelperAddress != address(0), "HCHelper cannot be ZeroAddr");
require(_nativeFaucetAmount > 0, "Native amount too small");
// tokenAmount can be 0 (e.g. for mainnet)

hcHelper = TuringHelper(_turingHelperAddress);
token = IERC20(token);

hcBackendUrl = _turingUrl;
waitingPeriod = _waitingPeriod;
nativeFaucetAmount = _nativeFaucetAmount;
tokenFaucetAmount = _tokenFaucetAmount;

emit Configure(
_turingHelperAddress,
_tokenAddress,
_turingUrl,
_waitingPeriod,
_nativeFaucetAmount
_nativeFaucetAmount,
_tokenFaucetAmount
);
}

function withdrawNative(
uint256 _amount
function withdraw(
uint256 _nativeAmount,
uint256 _tokenAmount
) public onlyOwner {
(bool sent,) = (msg.sender).call{gas: SAFE_GAS_STIPEND, value: _amount}("");
require(sent, "Failed to send native");
(bool sent,) = (msg.sender).call{gas: SAFE_GAS_STIPEND, value: _nativeAmount}("");
require(sent, "Failed to send native");

sent = token.transfer(msg.sender, _tokenAmount);
require(sent, "Failed to send token");

emit WithdrawNative(msg.sender, _amount);
emit Withdraw(msg.sender, _nativeAmount, _tokenAmount);
}

function getNativeFaucet(
function getFaucet(
bytes32 _uuid,
string memory _key,
address _to
) external {
require(nativeClaimRecords[_to] + waitingPeriod <= block.timestamp, 'Invalid request');
require(claimRecords[_to] + waitingPeriod <= block.timestamp, 'Invalid request');

bytes32 hashedKey = keccak256(abi.encodePacked(_key));
uint256 result = _verifyKey(_uuid, hashedKey, _to);

require(result == 1, 'Captcha wrong');

nativeClaimRecords[_to] = block.timestamp;
claimRecords[_to] = block.timestamp;

(bool sent,) = (_to).call{gas: SAFE_GAS_STIPEND, value: nativeFaucetAmount}("");
require(sent, "Failed to send native");

emit IssuedNative(_uuid, hashedKey, _to, nativeFaucetAmount, block.timestamp);
if (tokenFaucetAmount > 0 && token.balanceOf(address(this)) >= tokenFaucetAmount) {
sent = token.transfer(_to, tokenFaucetAmount);
require(sent, "Failed to send token");
emit Issued(_uuid, hashedKey, _to, nativeFaucetAmount, tokenFaucetAmount, block.timestamp);
} else {
emit Issued(_uuid, hashedKey, _to, nativeFaucetAmount, 0, block.timestamp);
}
}

function _verifyKey(bytes32 _uuid, bytes32 _key, address _to) private returns (uint256) {
bytes memory encRequest = abi.encode(_uuid, _key, _to);
bytes memory encResponse = hcHelper.TuringTxV2(hcBackendUrl, encRequest);

uint256 result = abi.decode(encResponse,(uint256));
uint256 result = abi.decode(encResponse, (uint256));

emit VerifyKey(hcBackendUrl, _uuid, _key, result);

Expand Down
10 changes: 10 additions & 0 deletions boba_community/hc-captcha-metafaucet/contracts/BobaMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract BobaMock is ERC20 {
constructor() ERC20("Boba Network", "BOBA") {
_mint(msg.sender, 100 ether);
}
}
Loading

0 comments on commit bbdd6d0

Please sign in to comment.