handle funding of tokens different from SNT

This commit is contained in:
Richard Ramos 2019-03-29 20:36:18 -04:00
parent 1854c559af
commit 539a8fa71b
6 changed files with 49 additions and 39 deletions

View File

@ -2,16 +2,14 @@
import {
CREATE_ESCROW, RESET_CREATE_ESCROW_STATUS, LOAD_ESCROWS, RELEASE_ESCROW, CANCEL_ESCROW,
RATE_TRANSACTION, PAY_ESCROW, OPEN_CASE, OPEN_CASE_SIGNATURE, PAY_ESCROW_SIGNATURE, CLOSE_DIALOG,
ADD_USER_RATING, USER_RATING, GET_ESCROW, GET_FEE, FUND_ESCROW, RESET_FUND_STATUS
ADD_USER_RATING, USER_RATING, GET_ESCROW, GET_FEE, FUND_ESCROW, RESET_STATUS
} from './constants';
import Escrow from 'Embark/contracts/Escrow';
import SNT from 'Embark/contracts/SNT';
import { toTokenDecimals } from '../../utils/numbers';
import { zeroAddress } from '../../utils/address';
const toBN = web3.utils.toBN;
export const createEscrow = (buyerAddress, username, tradeAmount, assetPrice, statusContactCode, offer) => {
tradeAmount = toTokenDecimals(tradeAmount, offer.token.decimals);
@ -24,24 +22,34 @@ export const createEscrow = (buyerAddress, username, tradeAmount, assetPrice, st
export const fundEscrow = (escrow, feeAmount) => {
const token = web3.utils.toChecksumAddress(escrow.offer.asset);
const expirationTime = Math.floor((new Date()).getTime() / 1000) + (86400 * 2); // TODO: what will be the expiration time?
const value = escrow.tradeAmount;
let value = escrow.tradeAmount;
let toSend = Escrow.methods.fund(escrow.escrowId, value, expirationTime);
if(token !== zeroAddress){
if(token === zeroAddress){
return {
type: FUND_ESCROW,
toSend,
value
};
}
return {
type: FUND_ESCROW,
toSend
};
/*
TODO: attempt to remove SNT approval if token is different from SNT, and send an approveAndCall trx
let SNTAmount = feeAmount;
if(token === SNT.options.address){
SNTAmount = toBN(SNTAmount).add(toBN(value)).toString();
}
const encodedCall = toSend.encodeABI();
toSend = SNT.methods.approveAndCall(Escrow.options.address, SNTAmount, encodedCall);
}
return {
type: FUND_ESCROW,
toSend,
value
};
}*/
};
export const resetCreateEscrowStatus = () => ({
@ -76,4 +84,4 @@ export const checkUserRating = (address) => ({ type: USER_RATING, address });
export const addUserRating = () => ({ type: ADD_USER_RATING });
export const resetFundingStatus = () => ({type: RESET_FUND_STATUS});
export const resetStatus = () => ({type: RESET_STATUS});

View File

@ -68,5 +68,5 @@ export const GET_FEE = 'GET_FEE';
export const GET_FEE_SUCCEEDED = 'GET_FEE_SUCCEDED';
export const GET_FEE_FAILED = 'GET_FEE_FAILED';
export const RESET_FUND_STATUS = 'RESET_FUND_STATUS';
export const RESET_STATUS = 'RESET_FUND_STATUS';

View File

@ -5,7 +5,7 @@ import {
GET_ESCROW_SUCCEEDED,
GET_FEE_SUCCEEDED,
FUND_ESCROW_FAILED, FUND_ESCROW_SUCCEEDED, FUND_ESCROW,
RESET_FUND_STATUS
RESET_STATUS
// GET_ESCROWS_FAILED,
// RELEASE_ESCROW, RELEASE_ESCROW_SUCCEEDED, RELEASE_ESCROW_FAILED, RELEASE_ESCROW_PRE_SUCCESS,
// CANCEL_ESCROW_FAILED, CANCEL_ESCROW_SUCCEEDED, CANCEL_ESCROW, CANCEL_ESCROW_PRE_SUCCESS,
@ -88,10 +88,11 @@ function reducer(state = DEFAULT_STATE, action) {
...state,
fee: action.fee
};
case RESET_FUND_STATUS:
case RESET_STATUS:
return {
...state,
fundEscrowStatus: States.none
fundEscrowStatus: States.none,
createEscrowStatus: States.none
};
// Migrate to new UI
// case RELEASE_ESCROW_FAILED:

View File

@ -8,6 +8,8 @@ import { fromTokenDecimals } from '../../../utils/numbers';
import Reputation from '../../../components/Reputation';
import RoundedIcon from "../../../ui/RoundedIcon";
import escrow from '../../../features/escrow';
import one from "../../../../images/escrow/01.png";
import two from "../../../../images/escrow/02.png";
import three from "../../../../images/escrow/03.png";
@ -58,7 +60,7 @@ const PreFund = ({amount, asset, fee, showApproveScreen, showFundButton, fundAct
<span className="bg-dark text-white p-3 rounded-circle">
<img src={two} alt="two" />
</span>
<p className="h2 mt-4">{showFundButton ? 'You are about to approve' : 'You are about to send'}</p>
<p className="h2 mt-4">{!showFundButton ? 'You are about to approve' : 'You are about to send'}</p>
<p className="h2 text-success">{fromTokenDecimals(amount, asset.decimals)} {asset.symbol}</p>
{ fee !== "0" && <Fragment>
<p className="h2">+ our fee</p>
@ -99,16 +101,15 @@ class CardEscrowSeller extends Component {
}
componentDidMount(){
const escrow = this.props.escrow;
const showFundButton = this.props.showFundButton;
const trade = this.props.escrow;
let step;
switch(escrow.status){
case 'funded':
switch(trade.status){
case escrow.helpers.tradeStates.funded:
step = 4;
break;
case 'waiting':
case escrow.helpers.tradeStates.waiting:
default:
step = 1;
}
@ -131,8 +132,6 @@ class CardEscrowSeller extends Component {
if(showLoading) step = 3;
if(showFunded) step = 4;
console.log(showFunded);
let component;
switch(step){
case 4:

View File

@ -1,4 +1,5 @@
/* global web3 */
/* eslint-disable complexity */
import React, {Component} from 'react';
import {withRouter} from "react-router-dom";
import PropTypes from 'prop-types';
@ -22,6 +23,8 @@ import escrow from '../../features/escrow';
import network from '../../features/network';
import approval from '../../features/approval';
import { States } from '../../utils/transaction';
const {toBN, toChecksumAddress} = web3.utils;
class Escrow extends Component {
@ -33,7 +36,7 @@ class Escrow extends Component {
props.getEscrow(props.escrowId);
props.getFee();
props.getSNTAllowance();
props.resetFundingStatus();
props.resetStatus();
if(props.escrow) props.getTokenAllowance(props.escrow.offer.asset);
}
@ -89,25 +92,25 @@ class Escrow extends Component {
if(!escrow) return <Loading page={true} />;
if(loading) return <Loading mining={true} />;
const token = Object.keys(tokens).map(t => tokens[t]).find(x => toChecksumAddress(x.address) === toChecksumAddress(escrow.offer.asset));
const requiredSNT = this.calculateRequiredSNT();
const isSNTapproved = toBN(sntAllowance).gte(toBN(requiredSNT));
const shouldResetSNT = toBN(sntAllowance).gt(toBN(0)) && toBN(requiredSNT).lt(toBN(sntAllowance));
const token = Object.keys(tokens).map(t => tokens[t]).find(x => toChecksumAddress(x.address) === toChecksumAddress(escrow.offer.asset));
const requiredToken = escrow.tradeAmount;
const isTokenApproved = token.address === zeroAddress || (tokenAllowance !== null && toBN(tokenAllowance).gte(toBN(requiredToken)));
const shouldResetToken = token.address !== zeroAddress && tokenAllowance !== null && toBN(tokenAllowance).gt(toBN(0)) && toBN(requiredToken).lt(toBN(tokenAllowance));
const isBuyer = false; //escrow.buyer === address;
const isBuyer = escrow.buyer === address;
const offer = this.getOffer(escrow, isBuyer);
let showFundButton = isSNTapproved && isTokenApproved; // TODO: All required tokens approved. Show fund button and call the escrow function. See above
let showFundButton = isSNTapproved && isTokenApproved;
// Show token approval UI
if(showApproveFundsScreen) {
if((!isSNTapproved || shouldResetSNT)) return <ApproveSNTFunds handleApprove={this.handleApprove(requiredSNT, tokens.SNT.address)} handleReset={this.handleReset(tokens.SNT.address)} sntAllowance={sntAllowance} requiredSNT={requiredSNT} shouldResetSNT={shouldResetSNT} />;
if (!isSNTapproved || shouldResetSNT) return <ApproveSNTFunds handleApprove={this.handleApprove(requiredSNT, tokens.SNT.address)} handleReset={this.handleReset(tokens.SNT.address)} sntAllowance={sntAllowance} requiredSNT={requiredSNT} shouldResetSNT={shouldResetSNT} />;
if(escrow.offer.asset !== zeroAddress) { // A token
if(toChecksumAddress(escrow.offer.asset) === toChecksumAddress(tokens.SNT.address)){
@ -123,8 +126,8 @@ class Escrow extends Component {
return (
<div className="escrow">
{ isBuyer ? <CardEscrowBuyer /> : <CardEscrowSeller showLoading={fundStatus === "pending"}
showFunded={fundStatus === "success"}
{ isBuyer ? <CardEscrowBuyer /> : <CardEscrowSeller showLoading={fundStatus === States.pending}
showFunded={fundStatus === States.success}
escrow={escrow}
fee={fee}
showFundButton={showFundButton}
@ -162,7 +165,7 @@ Escrow.propTypes = {
loading: PropTypes.bool,
fundEscrow: PropTypes.func,
fundStatus: PropTypes.string,
resetFundingStatus: PropTypes.func
resetStatus: PropTypes.func
};
const mapStateToProps = (state, props) => {
@ -188,6 +191,6 @@ export default connect(
getTokenAllowance: approval.actions.getTokenAllowance,
approve: approval.actions.approve,
fundEscrow: escrow.actions.fundEscrow,
resetFundingStatus: escrow.actions.resetFundingStatus
resetStatus: escrow.actions.resetStatus
}
)(withRouter(Escrow));

View File

@ -2,7 +2,7 @@ module.exports = async (licensePrice, feeAmount, deps) => {
try {
const addresses = await deps.web3.eth.getAccounts();
const main = addresses[0];
const sntToken = 100;
const sntToken = 10000000;
const balance = await deps.contracts.SNT.methods.balanceOf(main).call();
if (balance !== '0') {
return;
@ -21,9 +21,8 @@ module.exports = async (licensePrice, feeAmount, deps) => {
}));
console.log('Generate SNT...');
const weiSnt = sntToken * Math.pow(10, 18);
await Promise.all(addresses.slice(0, 8).map(async (address) => {
const generateToken = deps.contracts.SNT.methods.generateTokens(address, weiSnt.toString());
const generateToken = deps.contracts.SNT.methods.generateTokens(address, sntToken + '000000000000000000');
const gas = await generateToken.estimateGas({from: main});
return generateToken.send({from: main, gas});
}));