Skip to content

Commit

Permalink
Merge pull request #15 from Schwartz10/cash-out
Browse files Browse the repository at this point in the history
Cash out
  • Loading branch information
Schwartz10 authored Jan 21, 2018
2 parents 2502a03 + d66fb0e commit cd4d19e
Show file tree
Hide file tree
Showing 6 changed files with 1,757 additions and 823 deletions.
2,519 changes: 1,704 additions & 815 deletions build/contracts/CapCoin.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/contracts/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,9 @@
"5777": {
"events": {},
"links": {},
"address": "0x8cdaf0cd259887258bc13a92c0a6da92698644c0"
"address": "0x2eca6fcfef74e2c8d03fbaf0ff6712314c9bd58b"
}
},
"schemaVersion": "1.0.1",
"updatedAt": "2018-01-20T19:30:59.025Z"
"updatedAt": "2018-01-21T16:59:00.389Z"
}
2 changes: 1 addition & 1 deletion build/contracts/SimpleStorage.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,5 @@
}
},
"schemaVersion": "1.0.1",
"updatedAt": "2018-01-20T19:30:59.025Z"
"updatedAt": "2018-01-21T16:59:00.388Z"
}
13 changes: 13 additions & 0 deletions contracts/CapCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract CapCoin is Ownable {
event LikedPost(int lotteryAmount, string posterName,
address poster, uint posterCoinbalance, string likerName,
address liker, uint likerCoinbalance);
event CashedOut(uint newCoinbalance, address user);

uint coinSupply = 1000000;
uint coinsBought = 0;
Expand Down Expand Up @@ -94,6 +95,18 @@ contract CapCoin is Ownable {
createPost(url, caption);
}

function cashOut(uint weiAmount, uint coinAmount) public {
assert(addressToUser[msg.sender].coinBalance > coinAmount);
addressToUser[msg.sender].coinBalance -= coinAmount + 1;
value -= weiAmount;
msg.sender.transfer(weiAmount);
CashedOut(addressToUser[msg.sender].coinBalance, msg.sender);
}

function getContractBal() public view returns (uint) {
return value;
}

function kill() {
if (msg.sender == owner) {
selfdestruct(owner);
Expand Down
31 changes: 27 additions & 4 deletions src/components/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import {connect} from 'react-redux'
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import RaisedButton from 'material-ui/RaisedButton';
import { buyTokens } from '../store/user';
import { buyTokens, cashOut } from '../store/user';
import TextField from 'material-ui/TextField';

class Exchange extends Component {

constructor(props) {
super(props);
this.state = {value: 1};
this.state = {value: 1, cashOutAmount: ""};
}

handleChange = (event, index, value) => this.setState({value});
handleCashOutTextChange = (event) => this.setState({cashOutAmount: event.target.value})

render() {
return (
Expand All @@ -35,7 +37,24 @@ class Exchange extends Component {
this.props.buyTokens(e, this.state.value, this.props.contract.buyTokens, this.props.accounts[0], this.props.web3.toWei)}
label="Buy Tokens" primary={true}
/>
<h3>You have {this.props.user.coinBalance} Coins</h3>
<h3>You have {this.props.user.coinBalance} Coins</h3><br />
{this.props.user.coinBalance > 0 &&
<div>
<TextField
hintText="Cash Out Amount"
value={this.state.cashOutAmount}
onChange={this.handleCashOutTextChange}
errorText={this.state.cashOutAmount > this.props.user.coinBalance - 1 && "Amount Needs to be less than your current coin balance"}
/><br />
<RaisedButton
disabled={Number(this.state.cashOutAmount) <= 0}
onClick={(e) =>
this.props.cashOut(e, Number(this.state.cashOutAmount - 1), this.props.contract.cashOut, this.props.accounts[0], this.props.web3.toWei)}
label="Cash Out" primary={true}
/>
<p>*NOTE: There is a 1 coin fee for cashing out*</p>
</div>
}
</div>
);
}
Expand All @@ -54,7 +73,11 @@ const mapDispatch = (dispatch) => {
return {
buyTokens: function(e, amount, contractFunc, account, conversionFunc){
e.preventDefault()
return dispatch(buyTokens(amount, contractFunc, account, conversionFunc))
return dispatch(buyTokens(amount, contractFunc, account, conversionFunc));
},
cashOut: function(e, amount, contractFunc, account, conversionFunc) {
e.preventDefault()
return dispatch(cashOut(amount, contractFunc, account, conversionFunc));
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const CREATE_USER = 'CREATE_USER';
const BUY_TOKENS = 'BUY_TOKENS';
const LIKED_POST = 'LIKED_POST';
const CREATE_POST = 'CREATE_POST';
const CASHED_OUT = 'CASHED_OUT';

/**
* ACTION CREATORS
*/
const getUser = user => ({type: GET_USER, user})
const createUser = user => ({type: CREATE_USER, user})
const boughtTokens = amount => ({type: BUY_TOKENS, amount})
const cashedOut = amount => ({type: CASHED_OUT, amount})

/**
* THUNK CREATORS
Expand Down Expand Up @@ -54,10 +56,15 @@ export const addUser = (name, contractFunc, account) =>
export const buyTokens = (amount, contractFunc, account, conversionFunc) =>
dispatch =>
contractFunc(amount, {from: account, gas: 1000000, value: conversionFunc(amount/10, 'ether')})
// contractFunc(amount, {from: account})
.then(res => dispatch(boughtTokens(res.logs[0].args.coinBalance.c[0])))
.catch(err => console.log(err));

export const cashOut = (amount, contractFunc, account, conversionFunc) =>
dispatch =>
contractFunc(conversionFunc(amount/10, 'ether'), amount, {from: account, gas: 1000000})
.then(res => dispatch(cashedOut(res.logs[0].args.newCoinbalance.c[0])))
.catch(err => console.log(err));

/**
* REDUCER
*/
Expand All @@ -77,6 +84,8 @@ export default function (state = defaultUser, action) {
updatedUser.postLottery = action.post.tokenPot;
updatedUser.coinBalance -= 5;
return updatedUser;
case CASHED_OUT:
return Object.assign({}, state, {coinBalance: action.amount - 1});
default:
return state
}
Expand Down

0 comments on commit cd4d19e

Please sign in to comment.