Skip to content

Commit

Permalink
Merge recent changes (6c0eb0f) from RaidGuild repo
Browse files Browse the repository at this point in the history
  • Loading branch information
akolotov authored Feb 18, 2021
2 parents 7696fc2 + 3d000fb commit 9991650
Show file tree
Hide file tree
Showing 11 changed files with 870 additions and 657 deletions.
12 changes: 6 additions & 6 deletions packages/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
"@chakra-ui/theme": "^1.2.2",
"@emotion/react": "^11.1.4",
"@emotion/styled": "^11.0.0",
"@ethersproject/contracts": "^5.0.8",
"@ethersproject/providers": "^5.0.17",
"@ethersproject/contracts": "^5.0.11",
"@ethersproject/providers": "^5.0.23",
"@testing-library/dom": "^6.12.2",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@types/react": "^16.9.19",
"@uniswap/token-lists": "^1.0.0-beta.17",
"@walletconnect/web3-provider": "^1.3.1",
"@walletconnect/web3-provider": "^1.3.6",
"ajv": "^6.12.6",
"eslint": "^6.8.0",
"eslint-plugin-flowtype": "^4.6.0",
"ethers": "^5.0.24",
"ethers": "^5.0.31",
"fast-memoize": "^2.5.2",
"focus-visible": "^5.2.0",
"framer-motion": "^2.9.5",
Expand All @@ -44,8 +44,8 @@
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
"rxjs": "^6.6.3",
"web3": "^1.3.1",
"web3modal": "^1.9.2"
"web3": "^1.3.4",
"web3modal": "^1.9.3"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
25 changes: 17 additions & 8 deletions packages/react-app/src/components/ClaimTransferModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import { getBridgeNetwork, getNetworkName, logError } from '../lib/helpers';
import { LoadingModal } from './LoadingModal';

export const ClaimTransferModal = () => {
const { account, ethersProvider } = useContext(Web3Context);
const { account, ethersProvider, providerChainId } = useContext(Web3Context);
const { txHash, setTxHash } = useContext(BridgeContext);
const [isOpen, setOpen] = useState(false);
const [claiming, setClaiming] = useState(false);
const [message, setMessage] = useState(false);
const [executed, setExecuted] = useState(false);
const [loadingText, setLoadingText] = useState('');
const chainId = getBridgeNetwork(HOME_NETWORK);

const onClose = () => {
setOpen(false);
Expand All @@ -49,7 +50,12 @@ export const ClaimTransferModal = () => {
}, [message, account, setTxHash]);

const claimable =
!claiming && message && message.msgData && message.signatures && !executed;
!claiming &&
message &&
message.msgData &&
message.signatures &&
!executed &&
providerChainId === chainId;

const toast = useToast();
const showError = errorMsg => {
Expand All @@ -70,16 +76,19 @@ export const ClaimTransferModal = () => {
} else if (claimable) {
try {
setClaiming(true);
await executeSignatures(
ethersProvider,
getBridgeNetwork(HOME_NETWORK),
message,
);
await executeSignatures(ethersProvider, chainId, message);
setLoadingText('Waiting for Execution');
} catch (executeError) {
setClaiming(false);
setLoadingText('');
logError({ executeError });
logError({ executeError, chainId, message });
if (executeError && executeError.message) {
showError(executeError.message);
} else {
showError(
'Impossible to perform the operation. Reload the application and try again.',
);
}
}
}
};
Expand Down
9 changes: 8 additions & 1 deletion packages/react-app/src/components/HistoryItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export const HistoryItem = ({
setTxHash(tx.hash);
} catch (executeError) {
setLoading(false);
logError({ executeError });
logError({ executeError, chainId: providerChainId, message });
if (executeError && executeError.message) {
showError(executeError.message);
} else {
showError(
'Impossible to perform the operation. Reload the application and try again.',
);
}
}
}
};
Expand Down
12 changes: 11 additions & 1 deletion packages/react-app/src/components/UnlockButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ export const UnlockButton = () => {
return true;
};
const onClick = () => {
if (!unlockLoading && !allowed && valid()) approve();
if (!unlockLoading && !allowed && valid()) {
approve().catch(error => {
if (error && error.message) {
showError(error.message);
} else {
showError(
'Impossible to perform the operation. Reload the application and try again.',
);
}
});
}
};
return (
<Flex
Expand Down
33 changes: 23 additions & 10 deletions packages/react-app/src/contexts/BridgeContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,28 @@ export const BridgeProvider = ({ children }) => {
const [approvalTxHash, setApprovalTxHash] = useState();
const approve = useCallback(async () => {
setUnlockLoading(true);
const approvalAmount =
window.localStorage.getItem('infinite-unlock') === 'true'
? LARGEST_UINT256
: fromAmount;
try {
const approvalAmount =
window.localStorage.getItem('infinite-unlock') === 'true'
? LARGEST_UINT256
: fromAmount;
const tx = await approveToken(ethersProvider, fromToken, approvalAmount);
setApprovalTxHash(tx.hash);
await tx.wait();
setAllowance(approvalAmount);
} catch (error) {
logError({ approveError: error });
} catch (approveError) {
logError({
approveError,
fromToken,
approvalAmount: approvalAmount.toString(),
account,
});
throw approveError;
} finally {
setApprovalTxHash();
setUnlockLoading(false);
}
setApprovalTxHash();
setUnlockLoading(false);
}, [fromAmount, fromToken, ethersProvider]);
}, [fromAmount, fromToken, ethersProvider, account]);

const transfer = useCallback(async () => {
setLoading(true);
Expand All @@ -138,7 +145,13 @@ export const BridgeProvider = ({ children }) => {
setTxHash(tx.hash);
} catch (transferError) {
setLoading(false);
logError({ transferError });
logError({
transferError,
fromToken,
receiver: receiver || account,
fromAmount: fromAmount.toString(),
account,
});
throw transferError;
}
}, [fromToken, account, receiver, ethersProvider, fromAmount]);
Expand Down
21 changes: 7 additions & 14 deletions packages/react-app/src/lib/amb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { gql, request } from 'graphql-request';

import { getGasPrice } from './gasPrice';
import { getAMBAddress, getGraphEndpoint, logError } from './helpers';
import { isEIP1193 } from './providers';

export const fetchConfirmations = async (chainId, ethersProvider) => {
const abi = ['function requiredBlockConfirmations() view returns (uint256)'];
Expand Down Expand Up @@ -49,22 +50,14 @@ export const executeSignatures = async (ethersProvider, chainId, message) => {
);
const address = getAMBAddress(chainId);
const ambContract = new Contract(address, abi, ethersProvider.getSigner());
const gasPrice = getGasPrice(chainId);
return ambContract.executeSignatures(message.msgData, signatures, {
gasPrice,
});
const options = isEIP1193(ethersProvider)
? undefined
: { gasPrice: getGasPrice(chainId) };
return options
? ambContract.executeSignatures(message.msgData, signatures, options)
: ambContract.executeSignatures(message.msgData, signatures);
};

// const messagesTXQuery = gql`
// query getMessage($txHash: String!) {
// messages(where: { txHash_contains: $txHash }, first: 1) {
// msgId
// msgData
// signatures
// }
// }
// `;

const messagesTXQuery = gql`
query getRequests($txHash: String!) {
requests: userRequests(where: { txHash_contains: $txHash }, first: 1) {
Expand Down
22 changes: 13 additions & 9 deletions packages/react-app/src/lib/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
logError,
} from './helpers';
import { getOverriddenToToken, isOverridden } from './overrides';
import { getEthersProvider } from './providers';
import { getEthersProvider, isEIP1193 } from './providers';
import { fetchTokenDetails } from './token';

const getToName = (fromName, fromxDai) => {
Expand Down Expand Up @@ -242,27 +242,31 @@ export const fetchTokenLimits = async (
export const relayTokens = async (ethersProvider, token, receiver, amount) => {
const signer = ethersProvider.getSigner();
const { chainId, mode, mediator, address } = token;
const gasPrice = getGasPrice(chainId);
const options = isEIP1193(ethersProvider)
? undefined
: { gasPrice: getGasPrice(chainId) };
switch (mode) {
case 'erc677': {
const abi = ['function transferAndCall(address, uint256, bytes)'];
const tokenContract = new Contract(address, abi, signer);
return tokenContract.transferAndCall(mediator, amount, receiver, {
gasPrice,
});
return options
? tokenContract.transferAndCall(mediator, amount, receiver, options)
: tokenContract.transferAndCall(mediator, amount, receiver);
}
case 'dedicated-erc20': {
const abi = ['function relayTokens(address, uint256)'];
const mediatorContract = new Contract(mediator, abi, signer);
return mediatorContract.relayTokens(receiver, amount, { gasPrice });
return options
? mediatorContract.relayTokens(receiver, amount, options)
: mediatorContract.relayTokens(receiver, amount);
}
case 'erc20':
default: {
const abi = ['function relayTokens(address, address, uint256)'];
const mediatorContract = new Contract(mediator, abi, signer);
return mediatorContract.relayTokens(token.address, receiver, amount, {
gasPrice,
});
return options
? mediatorContract.relayTokens(token.address, receiver, amount, options)
: mediatorContract.relayTokens(token.address, receiver, amount);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-app/src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const networkLabels = {

export const chainUrls = {
100: {
rpc: process.env.REACT_APP_HOME_RPC_URL || 'https://xdai.poanetwork.dev',
rpc: process.env.REACT_APP_HOME_RPC_URL || 'https://rpc.xdaichain.com',
explorer:
process.env.REACT_APP_HOME_EXPLORER_PREFIX ||
'https://blockscout.com/poa/xdai',
Expand Down
6 changes: 6 additions & 0 deletions packages/react-app/src/lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export const getEthersProvider = chainId => {
const rpcURL = localRPCUrl || getRPCUrl(chainId);
return memoized(rpcURL);
};

export const isEIP1193 = ethersProvider =>
ethersProvider &&
ethersProvider.connection &&
ethersProvider.connection.url &&
ethersProvider.connection.url.includes('eip-1193');
10 changes: 7 additions & 3 deletions packages/react-app/src/lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getOverriddenMode,
isOverridden,
} from './overrides';
import { getEthersProvider } from './providers';
import { getEthersProvider, isEIP1193 } from './providers';

export const fetchAllowance = async (
{ mediator, address },
Expand Down Expand Up @@ -104,9 +104,13 @@ export const approveToken = async (
amount,
) => {
const abi = ['function approve(address, uint256)'];
const gasPrice = getGasPrice(chainId);
const options = isEIP1193(ethersProvider)
? undefined
: { gasPrice: getGasPrice(chainId) };
const tokenContract = new Contract(address, abi, ethersProvider.getSigner());
return tokenContract.approve(mediator, amount, { gasPrice });
return options
? tokenContract.approve(mediator, amount.toString(), options)
: tokenContract.approve(mediator, amount.toString());
};

export const fetchTokenBalance = async (token, account) => {
Expand Down
Loading

0 comments on commit 9991650

Please sign in to comment.