-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMakeCommitment.tsx
More file actions
234 lines (206 loc) · 12.1 KB
/
MakeCommitment.tsx
File metadata and controls
234 lines (206 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React, { Component } from "react";
import { View, StyleSheet, Image, Text, Button, TouchableOpacity, TextInput } from "react-native";
import { ethers, utils } from 'ethers';
import abi from '../CommitPoolContract/out/abi/contracts/SinglePlayerCommit.sol/SinglePlayerCommit.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,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: {},
activities: []
};
}
async componentDidMount() {
const url = 'https://rpc-mumbai.maticvigil.com/v1/e121feda27b4c1387cd0bf9a441e8727f8e86f56'
const provider = new ethers.providers.JsonRpcProvider(url);
let privateKey = this.props.account.signingKey.privateKey;
let wallet = new ethers.Wallet(privateKey);
wallet = wallet.connect(provider);
let contractAddress = '0xc129A3E263e05b73685b87cffC69695eB6240eaf';
let contract = new ethers.Contract(contractAddress, abi, provider);
let daiAddress = '0x70d1f773a9f81c852087b77f6ae6d3032b02d2ab';
let daiContract = new ethers.Contract(daiAddress, daiAbi, provider);
this.contract = contract.connect(wallet);
this.daiContract = daiContract.connect(wallet);
let activities = [];
let exists = true;
let index = 0;
while (exists){
try {
const key = await this.contract.activityKeyList(index);
const activity = await this.contract.activities(key);
const clone = Object.assign({}, activity)
clone.key = key;
activities.push(clone);
index++;
} catch (error) {
console.log(error)
exists = false;
}
}
const formattedActivities = activities.map(act => {
if(act[0] === 'Run') {
return {
label: 'Run 🏃♂️',
value: act.key
}
} else if (act[0] === 'Ride') {
return {
label: 'Ride 🚲',
value: act.key
}
} else {
return {
label: act[0],
value: act.key
}
}
})
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) => {
if (_daysToStart === 0) {
const result = new Date();
return result;
} else {
const result = 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 = 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, '0xc129A3E263e05b73685b87cffC69695eB6240eaf');
if(allowance.gte(stakeAmount)) {
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('0xc129A3E263e05b73685b87cffC69695eB6240eaf', stakeAmount)
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;
}
render() {
const { width } = Dimensions.get('window');
return (
<View style={{flex: 1, width, alignItems: 'center', justifyContent: 'space-around'}}>
{this.state.loading ? <View style={{flex: 1, alignItems: 'center', justifyContent: 'center', position: 'absolute', right: 0, left: 0, top: 0, bottom: 0, backgroundColor: 'rgba(0,0,0,0.5)', zIndex: 2}}><Text style={{fontSize: 25}}>⌛</Text></View> : undefined}
{!this.state.txSent ?
<View style={{flex: 1, alignItems: 'center', justifyContent: 'space-around'}}>
<View style={{alignItems: 'center'}}>
<Text style={{ color: 'white', fontSize: 30, textAlign: 'center', marginBottom: 25}}>
{"Now that you've connected Strava and have funds in your wallet, you can set up your commitment!"}
</Text>
<Text style={{fontSize: 30, color: 'white', marginBottom: 25, textAlign: 'center'}}>Create Commitment</Text>
<View style={{flexDirection: "row", width: 300, padding: 10, zIndex: 5000}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Activity:</Text>
<DropDownPicker
items={this.state.activities}
containerStyle={{height: 40}}
style={{backgroundColor: '#fafafa', width: 135}}
itemStyle={{
justifyContent: 'flex-start'
}}
dropDownStyle={{backgroundColor: '#fafafa'}}
onChangeItem={item => {
console.log("change", item)
this.setState({activity: item.value})
}}
/>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Distance:</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({distance: Number(text)})}></TextInput><Text style={{flex: 1, color: 'white', fontSize: 28}}> Miles</Text>
</View>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Stake:</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({stake: Number(text)})}></TextInput><Text style={{flex: 1, color: 'white', fontSize: 28}}> Dai</Text>
</View>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<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 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>
<TouchableOpacity
style={{width: 300, height: 50, backgroundColor: '#D45353', alignItems: 'center', justifyContent: 'center'}}
onPress={() => this.createCommitment()}>
<Text style={{fontSize: 30, color: 'white'}}>Stake and Commit</Text>
</TouchableOpacity>
</View>
:
<View style={{backgroundColor: '#D45353', flex: 1, alignItems: 'center', justifyContent: 'space-around'}}>
{this.state.loading ? <View style={{alignItems: 'center', justifyContent: 'center', position: 'absolute', right: 0, left: 0, top: 0, bottom: 0, backgroundColor: 'rgba(0,0,0,0.5)', zIndex: 2}}><Text style={{fontSize: 25}}>⌛</Text></View> : undefined}
<View style={{alignItems: 'center'}}>
<Text style={{fontSize: 50, color: 'white', marginBottom: 25, textAlign: 'center'}}>Commitment Created</Text>
<Text style={{fontSize: 50, marginBottom: 25}}>✔️</Text>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Activity:</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>{this.getActivityName()}</Text>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Distance:</Text>
<Text style={{flex: 1, color: 'white', fontSize: 28, marginLeft: 10}}>{this.state.distance} Miles</Text>
</View>
<View style={{flexDirection: "row", width: 300, padding: 10}}>
<Text style={{flex: 1, color: 'white', fontSize: 28, fontWeight: 'bold'}}>Stake:</Text>
<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'}}>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>
</View>
<TouchableOpacity
style={{width: 300, height: 50, backgroundColor: '#D45353', alignItems: 'center', justifyContent: 'center'}}
onPress={() => this.props.next(6)}>
<Text style={{fontSize: 30, color:'white'}}>Track Progress</Text>
</TouchableOpacity>
</View>}
</View>
);
}
}