Skip to content

synced with matic repo and added amount of days field #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export default function App() {
});
}

const { width } = Dimensions.get('window');

return (
<Home stravaOauth={stravaOauth} code={code}></Home>
)
Expand Down
66 changes: 51 additions & 15 deletions MakeCommitment.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React, { Component } from "react";
import { View, StyleSheet, Image, Text, Button, TouchableOpacity, TextInput } from "react-native";
import { View, Text, TouchableOpacity, TextInput } from "react-native";
import { ethers, utils } from 'ethers';
import abi from './abi.json'
import daiAbi from './daiAbi.json'
import { Dimensions } from 'react-native';
import DropDownPicker from 'react-native-dropdown-picker';

export default class MakeCommitment extends Component <{next: any, account: any, code: any}, {txSent: Boolean, loading: Boolean, distance: Number, stake: Number, activity: {}, activities: any}> {
export default class MakeCommitment extends Component <{next: any, account: any, code: any}, {txSent: Boolean, loading: Boolean, distance: Number, stake: Number, daysToStart: Number, duration: Number, activity: {}, activities: any}> {
contract: any;
daiContract: any;

constructor(props) {
super(props);

this.state = {
distance: 0,
stake: 0,
daysToStart: 0,
duration: 0,
loading: false,
txSent: false,
activity: {},
Expand All @@ -41,7 +43,6 @@ export default class MakeCommitment extends Component <{next: any, account: any,
this.contract = contract.connect(wallet);
this.daiContract = daiContract.connect(wallet);


let activities = [];
let exists = true;
let index = 0;
Expand Down Expand Up @@ -82,24 +83,46 @@ export default class MakeCommitment extends Component <{next: any, account: any,
this.setState({activities: formattedActivities, activity: formattedActivities[0]})
}

addDays = (date: Date, days: number) => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}

calculateStart = (_daysToStart: number) => {
const result = _daysToStart === 0 ?
new Date() :
this.addDays(new Date(), _daysToStart)
result.setHours(0,0,0,0); //start at next day 00:00
return result;
}

calculateEnd = (_startTime: Date, _duration: number) => {
const result = this.addDays(_startTime, _duration)
result.setHours(24,0,0,0); //give until end of day
return result;
}

async createCommitment() {
const distanceInMiles = Math.floor(this.state.distance);
const startTime = Math.ceil(new Date().getTime() / 1000) + 60;
const startTime = this.calculateStart(this.state.daysToStart);
const startTimestamp = Math.ceil(startTime.valueOf() /1000); //to seconds
const endTimestamp = Math.ceil(this.calculateEnd(startTime, this.state.duration).valueOf() /1000); //to seconds

const stakeAmount = utils.parseEther(this.state.stake.toString());
this.setState({loading: true})

const allowance = await this.daiContract.allowance(this.props.account.signingKey.address, '0x251B6f95F6A17D2aa350456f616a84b733380eBE');
if(allowance.gte(stakeAmount)) {
await this.contract.depositAndCommit(this.state.activity, distanceInMiles * 100, startTime, stakeAmount, stakeAmount, String(this.props.code.athlete.id), {gasLimit: 5000000});
await this.contract.depositAndCommit(this.state.activity, distanceInMiles * 100, startTimestamp, endTimestamp, stakeAmount, stakeAmount, String(this.props.code.athlete.id), {gasLimit: 5000000});
} else {
await this.daiContract.approve('0x251B6f95F6A17D2aa350456f616a84b733380eBE', stakeAmount)
await this.contract.depositAndCommit(this.state.activity, distanceInMiles * 100, startTime, stakeAmount, stakeAmount, String(this.props.code.athlete.id), {gasLimit: 5000000});
await this.contract.depositAndCommit(this.state.activity, distanceInMiles * 100, startTimestamp, endTimestamp, stakeAmount, stakeAmount, String(this.props.code.athlete.id), {gasLimit: 5000000});
}

this.setState({loading: false, txSent: true})
}


getActivityName() {
return this.state.activities.find((act: any) => act.value === this.state.activity).label;
}
Expand Down Expand Up @@ -147,10 +170,20 @@ export default class MakeCommitment extends Component <{next: any, account: any,
</View>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Deadline:</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>2 Days</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Starting in</Text>
<View style={{flex: 1, flexDirection: 'row', marginLeft: 10}}>
<TextInput style={{textAlign:'center', borderRadius: 5, backgroundColor: 'white', fontSize: 28, color: 'black', width: 30 + '%'}} onChangeText={text => this.setState({daysToStart: Number(text)})}></TextInput><Text style={{flex: 1, color: 'white', fontSize: 28}}> day(s)</Text>
</View>
</View>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>for</Text>
<View style={{flex: 1, flexDirection: 'row', marginLeft: 10}}>
<TextInput style={{textAlign:'center', borderRadius: 5, backgroundColor: 'white', fontSize: 28, color: 'black', width: 30 + '%'}} onChangeText={text => this.setState({duration: Number(text)})}></TextInput><Text style={{flex: 1, color: 'white', fontSize: 28}}> day(s)</Text>
</View>
</View>
<View>
</View>
</View>

<TouchableOpacity
style={{width: 300, height: 50, backgroundColor: '#D45353', alignItems: 'center', justifyContent: 'center'}}
Expand All @@ -177,15 +210,18 @@ export default class MakeCommitment extends Component <{next: any, account: any,
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>{this.state.stake} Dai</Text>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Deadline:</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>2 Days</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Starting in </Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>{this.state.daysToStart} day(s)</Text>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>for</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>{this.state.duration} day(s)</Text>
</View>
<Text style={{color: 'white', fontSize: 30, marginTop: 25}}>View on Etherscan <Image style={{width: 20, height: 20}} source={require('./assets/arrow-popout.svg')}></Image></Text>
</View>

<TouchableOpacity
style={{width: 300, height: 50, backgroundColor: '#D45353', alignItems: 'center', justifyContent: 'center'}}
onPress={() => this.props.next(7)}>
onPress={() => this.props.next(6)}>
<Text style={{fontSize: 30, color:'white'}}>Track Progress</Text>
</TouchableOpacity>
</View>}
Expand Down
2 changes: 1 addition & 1 deletion Track.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class Track extends Component <{next: any, account: any, code: st
console.log(this.props.account.signingKey.address)
await contractWithSigner.requestActivityDistance(this.props.account.signingKey.address, '0x1cf7D49BE7e0c6AC30dEd720623490B64F572E17', 'd8fcf41ee8984d3b8b0eae7b74eca7dd', {gasLimit: 500000});
this.setState({loading: false})
this.props.next(8)
this.props.next(7)
} catch (error) {
console.log(error)
this.setState({loading: false})
Expand Down
2 changes: 1 addition & 1 deletion Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Wallet extends Component <{next: any, account: any}, {balan
try {
const commitment = await commitPoolContract.commitments(this.props.account.signingKey.address);
if(commitment.exists){
this.props.next(7)
this.props.next(6)
} else {
this.props.next(5)
}
Expand Down
Loading