Commit a4e0d11c authored by Kenny Tran's avatar Kenny Tran Committed by Chi Kei Chan

Connect send page with exchange utils (#75)

* Connect send page with exchange utils

* Add exchangeRate and fix txId
parent b1a5a6c8
...@@ -9,6 +9,13 @@ class AddressInputPanel extends Component { ...@@ -9,6 +9,13 @@ class AddressInputPanel extends Component {
title: PropTypes.string, title: PropTypes.string,
description: PropTypes.string, description: PropTypes.string,
extraText: PropTypes.string, extraText: PropTypes.string,
onChange: PropTypes.func,
value: PropTypes.string,
};
static defaultProps = {
onChange() {},
value: '',
}; };
render() { render() {
...@@ -16,6 +23,8 @@ class AddressInputPanel extends Component { ...@@ -16,6 +23,8 @@ class AddressInputPanel extends Component {
title, title,
description, description,
extraText, extraText,
onChange,
value
} = this.props; } = this.props;
return ( return (
...@@ -31,6 +40,8 @@ class AddressInputPanel extends Component { ...@@ -31,6 +40,8 @@ class AddressInputPanel extends Component {
type="text" type="text"
className="address-input-panel__input" className="address-input-panel__input"
placeholder="0x1234..." placeholder="0x1234..."
onChange={e => onChange(e.target.value)}
value={value}
/> />
</div> </div>
</div> </div>
......
...@@ -37,7 +37,7 @@ class CurrencyInputPanel extends Component { ...@@ -37,7 +37,7 @@ class CurrencyInputPanel extends Component {
onCurrencySelected: PropTypes.func, onCurrencySelected: PropTypes.func,
onValueChange: PropTypes.func, onValueChange: PropTypes.func,
tokenAddresses: PropTypes.shape({ tokenAddresses: PropTypes.shape({
address: PropTypes.array.isRequired, addresses: PropTypes.array.isRequired,
}).isRequired, }).isRequired,
exchangeAddresses: PropTypes.shape({ exchangeAddresses: PropTypes.shape({
fromToken: PropTypes.object.isRequired, fromToken: PropTypes.object.isRequired,
......
...@@ -4,6 +4,7 @@ import addresses from './addresses'; ...@@ -4,6 +4,7 @@ import addresses from './addresses';
import exchangeContracts from './exchange-contract'; import exchangeContracts from './exchange-contract';
import tokenContracts from './token-contract'; import tokenContracts from './token-contract';
import exchange from './exchange'; import exchange from './exchange';
import send from './send';
import swap from './swap'; import swap from './swap';
import web3connect from './web3connect'; import web3connect from './web3connect';
...@@ -12,6 +13,7 @@ export default combineReducers({ ...@@ -12,6 +13,7 @@ export default combineReducers({
exchangeContracts, exchangeContracts,
tokenContracts, tokenContracts,
exchange, exchange,
send,
swap, swap,
web3connect, web3connect,
...drizzleReducers, ...drizzleReducers,
......
const UPDATE_FIELD = 'app/send/updateField';
const ADD_ERROR = 'app/send/addError';
const REMOVE_ERROR = 'app/send/removeError';
const RESET_SEND = 'app/send/resetSend';
const getInitialState = () => {
return {
input: '',
output: '',
inputCurrency: '',
outputCurrency: '',
recipient: '',
lastEditedField: '',
inputErrors: [],
outputErrors: [],
};
};
export const isValidSend = (state) => {
const { send } = state;
return send.outputCurrency !== '' &&
send.inputCurrency !== '' &&
send.input !== '' &&
send.output !== '' &&
send.recipient !== '' &&
send.inputErrors.length === 0 &&
send.outputErrors.length === 0;
};
export const updateField = ({ name, value }) => ({
type: UPDATE_FIELD,
payload: { name, value },
});
export const addError = ({ name, value }) => ({
type: ADD_ERROR,
payload: { name, value },
});
export const removeError = ({ name, value }) => ({
type: REMOVE_ERROR,
payload: { name, value },
});
export const resetSend = () => ({
type: RESET_SEND,
});
function reduceAddError(state, payload) {
const { name, value } = payload;
let nextErrors = state[name];
if (nextErrors.indexOf(value) === -1) {
nextErrors = [...nextErrors, value];
}
return {
...state,
[name]: nextErrors,
};
}
function reduceRemoveError(state, payload) {
const { name, value } = payload;
return {
...state,
[name]: state[name].filter(error => error !== value),
};
}
export default function sendReducer(state = getInitialState(), { type, payload }) {
switch (type) {
case UPDATE_FIELD:
return {
...state,
[payload.name]: payload.value,
};
case ADD_ERROR:
return reduceAddError(state, payload);
case REMOVE_ERROR:
return reduceRemoveError(state, payload);
case RESET_SEND:
return getInitialState();
default:
return state;
}
}
...@@ -273,7 +273,7 @@ const ETH_TO_ERC20 = { ...@@ -273,7 +273,7 @@ const ETH_TO_ERC20 = {
value: maxInput.toFixed(0), value: maxInput.toFixed(0),
}); });
}, },
sendInput: async ({drizzleCtx, contractStore, input, output, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => { sendInput: async ({drizzleCtx, contractStore, input, output, outputDecimals, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => {
if (!validEthToErc20(inputCurrency, outputCurrency)) { if (!validEthToErc20(inputCurrency, outputCurrency)) {
return; return;
} }
...@@ -287,7 +287,6 @@ const ETH_TO_ERC20 = { ...@@ -287,7 +287,6 @@ const ETH_TO_ERC20 = {
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.025); const ALLOWED_SLIPPAGE = BN(0.025);
const outputDecimals = await getDecimals({ address: outputCurrency, contractStore, drizzleCtx });
const minOutput = BN(output).multipliedBy(10 ** outputDecimals).multipliedBy(BN(1).minus(ALLOWED_SLIPPAGE)); const minOutput = BN(output).multipliedBy(10 ** outputDecimals).multipliedBy(BN(1).minus(ALLOWED_SLIPPAGE));
return exchange.methods.ethToTokenTransferInput.cacheSend( return exchange.methods.ethToTokenTransferInput.cacheSend(
...@@ -300,7 +299,7 @@ const ETH_TO_ERC20 = { ...@@ -300,7 +299,7 @@ const ETH_TO_ERC20 = {
} }
); );
}, },
sendOutput: async ({drizzleCtx, contractStore, input, output, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => { sendOutput: async ({drizzleCtx, contractStore, input, output, outputDecimals, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => {
if (!validEthToErc20(inputCurrency, outputCurrency)) { if (!validEthToErc20(inputCurrency, outputCurrency)) {
return; return;
} }
...@@ -314,7 +313,6 @@ const ETH_TO_ERC20 = { ...@@ -314,7 +313,6 @@ const ETH_TO_ERC20 = {
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.025); const ALLOWED_SLIPPAGE = BN(0.025);
const outputDecimals = await getDecimals({ address: outputCurrency, contractStore, drizzleCtx });
const outputAmount = BN(output).multipliedBy(BN(10 ** outputDecimals)); const outputAmount = BN(output).multipliedBy(BN(10 ** outputDecimals));
const maxInput = BN(input).multipliedBy(10 ** 18).multipliedBy(BN(1).plus(ALLOWED_SLIPPAGE)); const maxInput = BN(input).multipliedBy(10 ** 18).multipliedBy(BN(1).plus(ALLOWED_SLIPPAGE));
return exchange.methods.ethToTokenTransferOutput.cacheSend( return exchange.methods.ethToTokenTransferOutput.cacheSend(
...@@ -481,7 +479,7 @@ const ERC20_TO_ETH = { ...@@ -481,7 +479,7 @@ const ERC20_TO_ETH = {
{ from: account }, { from: account },
); );
}, },
sendInput: async ({drizzleCtx, contractStore, input, output, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => { sendInput: async ({drizzleCtx, contractStore, input, output, inputDecimals, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => {
if (!validErc20ToEth(inputCurrency, outputCurrency)) { if (!validErc20ToEth(inputCurrency, outputCurrency)) {
return; return;
} }
...@@ -495,7 +493,6 @@ const ERC20_TO_ETH = { ...@@ -495,7 +493,6 @@ const ERC20_TO_ETH = {
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.025); const ALLOWED_SLIPPAGE = BN(0.025);
const inputDecimals = await getDecimals({ address: inputCurrency, contractStore, drizzleCtx });
const minOutput = BN(output).multipliedBy(10 ** 18).multipliedBy(BN(1).minus(ALLOWED_SLIPPAGE)); const minOutput = BN(output).multipliedBy(10 ** 18).multipliedBy(BN(1).minus(ALLOWED_SLIPPAGE));
const inputAmount = BN(input).multipliedBy(10 ** inputDecimals); const inputAmount = BN(input).multipliedBy(10 ** inputDecimals);
...@@ -507,7 +504,7 @@ const ERC20_TO_ETH = { ...@@ -507,7 +504,7 @@ const ERC20_TO_ETH = {
{ from: account, value: '0x0' }, { from: account, value: '0x0' },
); );
}, },
sendOutput: async ({drizzleCtx, contractStore, input, output, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => { sendOutput: async ({drizzleCtx, contractStore, input, output, inputDecimals, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => {
if (!validErc20ToEth(inputCurrency, outputCurrency)) { if (!validErc20ToEth(inputCurrency, outputCurrency)) {
return; return;
} }
...@@ -521,7 +518,6 @@ const ERC20_TO_ETH = { ...@@ -521,7 +518,6 @@ const ERC20_TO_ETH = {
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.025); const ALLOWED_SLIPPAGE = BN(0.025);
const inputDecimals = await getDecimals({ address: inputCurrency, contractStore, drizzleCtx });
const maxInput = BN(input).multipliedBy(10 ** inputDecimals).multipliedBy(BN(1).plus(ALLOWED_SLIPPAGE)); const maxInput = BN(input).multipliedBy(10 ** inputDecimals).multipliedBy(BN(1).plus(ALLOWED_SLIPPAGE));
const outputAmount = BN(output).multipliedBy(10 ** 18); const outputAmount = BN(output).multipliedBy(10 ** 18);
...@@ -676,7 +672,7 @@ const ERC20_TO_ERC20 = { ...@@ -676,7 +672,7 @@ const ERC20_TO_ERC20 = {
{ from: account }, { from: account },
); );
}, },
sendInput: async ({drizzleCtx, contractStore, input, output, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => { sendInput: async ({drizzleCtx, contractStore, input, output, inputDecimals, outputDecimals, account, inputCurrency, outputCurrency, exchangeAddresses, recipient}) => {
if (!validErc20ToErc20(inputCurrency, outputCurrency)) { if (!validErc20ToErc20(inputCurrency, outputCurrency)) {
return; return;
} }
...@@ -688,8 +684,6 @@ const ERC20_TO_ERC20 = { ...@@ -688,8 +684,6 @@ const ERC20_TO_ERC20 = {
} }
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.04); const ALLOWED_SLIPPAGE = BN(0.04);
const inputDecimals = await getDecimals({ address: inputCurrency, contractStore, drizzleCtx });
const outputDecimals = await getDecimals({ address: outputCurrency, contractStore, drizzleCtx });
const inputAmount = BN(input).multipliedBy(BN(10 ** inputDecimals)); const inputAmount = BN(input).multipliedBy(BN(10 ** inputDecimals));
const outputAmount = BN(input).multipliedBy(BN(10 ** outputDecimals)); const outputAmount = BN(input).multipliedBy(BN(10 ** outputDecimals));
...@@ -719,6 +713,8 @@ const ERC20_TO_ERC20 = { ...@@ -719,6 +713,8 @@ const ERC20_TO_ERC20 = {
outputCurrency, outputCurrency,
exchangeAddresses, exchangeAddresses,
recipient, recipient,
inputDecimals,
outputDecimals,
} = opts; } = opts;
const exchangeRateA = await ETH_TO_ERC20.calculateInput({ ...opts, inputCurrency: 'ETH' }); const exchangeRateA = await ETH_TO_ERC20.calculateInput({ ...opts, inputCurrency: 'ETH' });
if (!exchangeRateA) { if (!exchangeRateA) {
...@@ -738,8 +734,6 @@ const ERC20_TO_ERC20 = { ...@@ -738,8 +734,6 @@ const ERC20_TO_ERC20 = {
const deadline = await getDeadline(drizzleCtx, 300); const deadline = await getDeadline(drizzleCtx, 300);
const ALLOWED_SLIPPAGE = BN(0.04); const ALLOWED_SLIPPAGE = BN(0.04);
const inputDecimals = await getDecimals({ address: inputCurrency, contractStore, drizzleCtx });
const outputDecimals = await getDecimals({ address: outputCurrency, contractStore, drizzleCtx });
const inputAmount = BN(input).multipliedBy(BN(10 ** inputDecimals)); const inputAmount = BN(input).multipliedBy(BN(10 ** inputDecimals));
const outputAmount = BN(output).multipliedBy(BN(10 ** outputDecimals)); const outputAmount = BN(output).multipliedBy(BN(10 ** outputDecimals));
const inputAmountB = BN(output).dividedBy(exchangeRateA).multipliedBy(BN(10 ** 18)); const inputAmountB = BN(output).dividedBy(exchangeRateA).multipliedBy(BN(10 ** 18));
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment