Skip to content

Commit

Permalink
Add Native currency support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomarsachin2271 committed Aug 28, 2021
1 parent b4e914c commit b63c94a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 55 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@apollo/client": "^3.3.21",
"@biconomy/hyphen": "^0.0.10",
"@biconomy/hyphen": "^0.0.11",
"@biconomy/mexa": "^1.5.10",
"@material-ui/core": "^4.9.0",
"@material-ui/icons": "^4.11.2",
Expand Down
39 changes: 33 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,19 @@ function App() {
console.log(`User address is ${userAddress}`)
let tokenAddress = selectedToken.address;
if(tokenAddress) {
let balance;
let userBalance;
if(tokenAddress.toLowerCase() === config.NATIVE_ADDRESS) {
userBalance = await ethersProvider.getBalance(userAddress);
let decimals = selectedFromChain.nativeDecimal;
balance = userBalance.toString() / BigNumber.from(10).pow(decimals).toString();

} else {
let tokenContract = new ethers.Contract(tokenAddress, config.abi.erc20, signer);
let userBalance = await tokenContract.balanceOf(userAddress);
userBalance = await tokenContract.balanceOf(userAddress);
let decimals = await tokenContract.decimals();
let balance = userBalance.toString() / BigNumber.from(10).pow(decimals).toString();
balance = userBalance.toString() / BigNumber.from(10).pow(decimals).toString();
}
let displayBalance = "-";
if(balance != undefined) {
displayBalance = toFixed(balance, 4);
Expand Down Expand Up @@ -893,9 +902,13 @@ function App() {
}
}
if(selectedToken && selectedToken.tokenSymbol && selectedToChain && selectedToChain.chainId) {
let fixedDecimalPoint = 2;
if(config.isNativeAddress(selectedToken.address)) {
fixedDecimalPoint = 5;
}
let lpFeeAmount = (parseFloat(lpFee)*parseFloat(amount))/100;
if(lpFeeAmount) {
lpFeeAmount = lpFeeAmount.toFixed(2);
lpFeeAmount = lpFeeAmount.toFixed(fixedDecimalPoint);
setLpFeeAmount(lpFeeAmount.toString());
}

Expand All @@ -906,7 +919,9 @@ function App() {
// Check the balance again using tokenAmount
let userBalanceCheck = await checkUserBalance(amount);
if(userBalanceCheck) {
if(!config.isNativeAddress(selectedToken.address)) {
await checkTokenApproval(amount);
}
if(fetchResponse && fetchResponse.json) {
let response = await fetchResponse.json();
if (response && response.tokenGasPrice != undefined) {
Expand All @@ -925,9 +940,12 @@ function App() {
if(amountToGet > 0) {
dispatch(updateTransactionFee(transactionFee, selectedToken.tokenSymbol));
setShowEstimation(true);
amountToGet = amountToGet.toFixed(2);
amountToGet = amountToGet.toFixed(fixedDecimalPoint);
setEstimatedTokensToGet(amountToGet);
dispatch(updateEstimatedAmountToGet(amountToGet));
if(config.isNativeAddress(selectedToken.address)) {
dispatch(updateTransferButtonState(true, "Transfer"));
}
} else {
dispatch(updateTransferButtonState(false, "Amount to transfer too low"));
}
Expand Down Expand Up @@ -1070,7 +1088,12 @@ function App() {

// showFeedbackMessage("Initiaiting Transfer");
dispatch(updateTransferButtonState(false, "Transfer"));
let tokenDecimals = await hyphen.getERC20TokenDecimals(selectedToken.address);
let tokenDecimals;
if(config.isNativeAddress(selectedToken.address)) {
tokenDecimals = selectedFromChain.nativeDecimal;
} else {
tokenDecimals = await hyphen.getERC20TokenDecimals(selectedToken.address);
}

amount = amount * Math.pow(10, tokenDecimals);
amount = amount.toLocaleString('fullwide', {useGrouping:false})
Expand Down Expand Up @@ -1245,7 +1268,11 @@ function App() {
let token = config.tokensMap[selectedToken.tokenSymbol][selectedToChain.chainId];
let tokenDecimal = token.decimal;
amount = parseFloat(amount)/parseFloat(ethers.BigNumber.from(10).pow(tokenDecimal))
if(amount) amount = amount.toFixed(2);
let fixedDecimalPoint = 2;
if(config.isNativeAddress(selectedToken.address)) {
fixedDecimalPoint = 5;
}
if(amount) amount = amount.toFixed(fixedDecimalPoint);
}

if(amount) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function Header(props) {

<UseBiconomySwitchWrapper >
<LightTooltip title={`This button will toggle Hyphen gasless feature for your wallet. Users with hardware wallets should keep this setting turned off.`}
placement="down">
placement="bottom">
<InfoIcon className={`${classes.infoIcon}`} />
</LightTooltip>
<div className={classes.gaslessModeLable}>{`Gasless Mode`}</div>
Expand Down
14 changes: 2 additions & 12 deletions src/components/TokenListContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ import Select from '@material-ui/core/Select';
import { useSelector, useDispatch } from 'react-redux'
import { updateSupportedTokens, updateSelectedToken, updateApproveButtonState, updateTransferButtonState } from '../redux'
import { useState } from 'react';


let USDCLogo = require("../assets/usdc.png");
let USDTLogo = require("../assets/usdt.png");
let DAILogo = require("../assets/dai.png");

let tokenLogoMap = {
"USDC": USDCLogo,
"USDT": USDTLogo,
"DAI": DAILogo
}
import { config } from '../config';

const useStyles = makeStyles((theme) => ({
formControl: {
Expand Down Expand Up @@ -87,7 +77,7 @@ function TokenListContainer(props) {
>
{tokenList && tokenList.map((token, index) =>
<MenuItem value={token.tokenSymbol} key={`${token.tokenSymbol}${index}`}>
<img src={tokenLogoMap[token.tokenSymbol]} className={classes.tokenLogo}/>
<img src={config.tokenLogoMap[token.tokenSymbol]} className={classes.tokenLogo}/>
{token.tokenSymbol}
</MenuItem>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/user/UserDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export default function UserDetails(props) {
let getTokenInfo = (rawAmount, tokenAddress, chainId) => {
console.log("getting token info");
let formattedAmount = rawAmount;
let tokenInfo = config.tokenAddressMap[tokenAddress.toLowerCase()][chainId];
let tokenInfo = config.tokenAddressMap[chainId][tokenAddress.toLowerCase()];
let symbol;
if(tokenInfo) {
symbol = tokenInfo.symbol
Expand Down
85 changes: 68 additions & 17 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ const PROD_ENVIRONMENT = "prod";
let USDCLogo = require("./assets/usdc.png");
let USDTLogo = require("./assets/usdt.png");
let DAILogo = require("./assets/dai.png");

let ETHLogo = require("./assets/Ethereum.png");

let getEnv = () => {
return process.env.REACT_APP_ENV;
}

let config = {};
config.getEnv = getEnv;
config.NATIVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";

config.tokenLogoMap = {
"USDC": USDCLogo,
"USDT": USDTLogo,
"DAI": DAILogo
"DAI": DAILogo,
"ETH": ETHLogo
}

let walletSelect = {
Expand All @@ -30,6 +32,8 @@ let MUMBAI = {
chainId: 80001,
rpcUrl: "https://rpc-mumbai.maticvigil.com",
currency: "Test MATIC",
nativeToken: config.NATIVE_ADDRESS,
nativeDecimal: 18,
nativeFaucetURL: "https://faucet.matic.network/",
biconomy: {
enable: true,
Expand All @@ -50,6 +54,8 @@ let ETHEREUM = {
chainId: 1,
rpcUrl: "https://mainnet.infura.io/v3/d126f392798444609246423b06116c77",
currency: "ETH",
nativeToken: config.NATIVE_ADDRESS,
nativeDecimal: 18,
nativeFaucetURL: "",
assetSentTopicId: "0xec1dcc5633614eade4a5730f51adc7444a5103a8477965a32f2e886f5b20f694",
onboardConfig : {
Expand All @@ -69,6 +75,8 @@ let MATIC = {
chainId: 137,
rpcUrl: "https://rpc-mainnet.maticvigil.com",
currency: "MATIC",
nativeToken: config.NATIVE_ADDRESS,
nativeDecimal: 18,
nativeFaucetURL: "",
biconomy: {
enable: true,
Expand All @@ -89,6 +97,8 @@ let GOERLI = {
chainId: 5,
rpcUrl: "https://goerli.infura.io/v3/d126f392798444609246423b06116c77",
currency: "Goerli ETH",
nativeToken: config.NATIVE_ADDRESS,
nativeDecimal: 18,
nativeFaucetURL: "https://faucet.goerli.mudit.blog/",
assetSentTopicId: "0xec1dcc5633614eade4a5730f51adc7444a5103a8477965a32f2e886f5b20f694",
biconomy: {
Expand Down Expand Up @@ -230,24 +240,59 @@ config.tokensMap = {
symbol: "DAI"
}
},

"ETH" : {
80001: {
address: "0xa6fa4fb5f76172d178d61b04b0ecd319c5d1c0aa",
transferOverhead: 29766,
decimal: 18,
symbol: "ETH"
},
5: {
address: config.NATIVE_ADDRESS,
transferOverhead: 29766,
decimal: 18,
symbol: "ETH"
},
137: {
address: "0x7ceb23fd6bc0add59e62ac25578270cff1b9f619",
transferOverhead: 29766,
decimal: 18,
symbol: "ETH"
},
1: {
address: config.NATIVE_ADDRESS,
transferOverhead: 29766,
decimal: 18,
symbol: "ETH"
}
}
}

config.tokenAddressMap = {
"0x64ef393b6846114bad71e2cb2ccc3e10736b5716" : {5 : config.tokensMap["USDT"][5]},
"0xeabc4b91d9375796aa4f69cc764a4ab509080a58" : {80001 : config.tokensMap["USDT"][80001]},
"0xdac17f958d2ee523a2206206994597c13d831ec7" : {1 : config.tokensMap["USDT"][1]},
"0xc2132d05d31c914a87c6611c10748aeb04b58e8f" : {137 : config.tokensMap["USDT"][137]},

"0xb5b640e6414b6def4fc9b3c1eef373925effeccf" : {5 : config.tokensMap["USDC"][5]},
"0xda5289fcaaf71d52a80a254da614a192b693e977" : {80001 : config.tokensMap["USDC"][80001]},
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" : {1 : config.tokensMap["USDC"][1]},
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174" : {137 : config.tokensMap["USDC"][137]},

"0x2686eca13186766760a0347ee8eeb5a88710e11b" : {5 : config.tokensMap["DAI"][5]},
"0x27a44456bEDb94DbD59D0f0A14fE977c777fC5C3" : {80001 : config.tokensMap["DAI"][80001]},
"0x6b175474e89094c44da98b954eedeac495271d0f" : {1 : config.tokensMap["DAI"][1]},
"0x8f3cf7ad23cd3cadbd9735aff958023239c6a063" : {137 : config.tokensMap["DAI"][137]},
1 : {
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : config.tokensMap["ETH"][1],
"0xdac17f958d2ee523a2206206994597c13d831ec7" : config.tokensMap["USDT"][1],
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" : config.tokensMap["USDC"][1],
"0x6b175474e89094c44da98b954eedeac495271d0f" : config.tokensMap["DAI"][1]
},
5 : {
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : config.tokensMap["ETH"][5],
"0x64ef393b6846114bad71e2cb2ccc3e10736b5716" : config.tokensMap["USDT"][5],
"0xb5b640e6414b6def4fc9b3c1eef373925effeccf" : config.tokensMap["USDC"][5],
"0x2686eca13186766760a0347ee8eeb5a88710e11b" : config.tokensMap["DAI"][5]
},
80001 : {
"0xa6fa4fb5f76172d178d61b04b0ecd319c5d1c0aa" : config.tokensMap["ETH"][80001],
"0xeabc4b91d9375796aa4f69cc764a4ab509080a58" : config.tokensMap["USDT"][80001],
"0xda5289fcaaf71d52a80a254da614a192b693e977" : config.tokensMap["USDC"][80001],
"0x27a44456bEDb94DbD59D0f0A14fE977c777fC5C3" : config.tokensMap["DAI"][80001]
},
137 : {
"0x7ceb23fd6bc0add59e62ac25578270cff1b9f619" : config.tokensMap["ETH"][137],
"0xc2132d05d31c914a87c6611c10748aeb04b58e8f" : config.tokensMap["USDT"][137],
"0x2791bca1f2de4661ed88a30c99a7a9449aa84174" : config.tokensMap["USDC"][137],
"0x8f3cf7ad23cd3cadbd9735aff958023239c6a063" : config.tokensMap["DAI"][137]
}
}

config.changeRPCPayload = {
Expand Down Expand Up @@ -276,6 +321,7 @@ config.changeRPCPayload = {

}


config.explorerURLMap = {
80001: "https://mumbai.polygonscan.com/tx/",
137: "https://polygonscan.com/tx/",
Expand Down Expand Up @@ -318,4 +364,9 @@ config.transactionStatus = {
CONFIRMED: "Confirmed"
}
config.useBiconomyKey = "USE_BICONOMY";

config.isNativeAddress = (address) => {
return address && (address.toLowerCase() === config.NATIVE_ADDRESS);
}

module.exports = { config }

0 comments on commit b63c94a

Please sign in to comment.