-
Notifications
You must be signed in to change notification settings - Fork 0
/
usd_inr.js
224 lines (193 loc) · 7.57 KB
/
usd_inr.js
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
import React, { Component } from 'react'
import {Route, Link} from 'react-router-dom';
import Web3 from 'web3'
import { newKitFromWeb3 } from '@celo/contractkit';
import BigNumber from "bignumber.js";
import Tokenaddress from 'tokenaddress.json';
import Currency from 'CurrencyToken.json';
//Importing Utilities
import {connectWallet} from '../../../Utilis/connectWallet.js';
import {loadContract} from '../../../Utilis/ContractUtilis.js';
import {formatBalance} from '../../../Utilis/ContractUtilis.js';
import {loadTokens} from '../../../Utilis/ContractUtilis.js';
// token contracts
import ERCToken from 'abis/IERC20Token.json' //ERC20
import MutateToken from 'abis/MutateToken.json'
// components
import TokenMain from '../main-components/Main'
//variables
let kit
//currencies to convert with currency code
let a_currency = "USD"
let b_currency = "COP"
// tokens for that currencies
//let a_token = "hUSD"
//let b_token = "hPESO"
//contracts address
const ERC20_DECIMALS = 18
const mutatetokenaddress = Tokenaddress.MUTATE
const aTokenaddress = Tokenaddress.USDT
const bTokenaddress = Tokenaddress.cPESO
class USD_cPESO extends Component {
constructor(props) {
super(props)
this.state = {
account: '0x0',
a: "hUSD",
b: "hINR",
atoken: {},
btoken: {},
aTokenaddress: Currency["hUSD"].address,
bTokenaddress: Currency["hINR"].address,
mutatetoken: {},
aTokenBalance: '0',
bTokenBalance: '0',
apoolBalance: '0',
bpoolBalance: '0',
exchangerate: '74.98',
loading: true
};
this.ahandleChange = this.ahandleChange.bind(this)
this.bhandleChange = this.bhandleChange.bind(this)
}
// This will call the celo blockchain data functions function and load the web3
async componentWillMount() {
let accounts = await connectWallet();
this.setState({account: accounts})
await this.loadingContracts()
await this.loadingTokens(this.state.aTokenaddress, this.state.bTokenaddress)
//await this.getExchangeRate()
}
getExchangeRate = async function () {
var URL = Tokenaddress.URL + Currency[this.state.a].symbol + "&to=" + Currency[this.state.a].symbol
const response = await fetch(URL);
const data = await response.json();
this.setState({ exchangerate: data.result.toFixed(2) })
}
loadingContracts = async function () {
try {
const web3 = new Web3(window.celo);
kit = newKitFromWeb3(web3);
//contract = new kit.web3.eth.Contract(marketplaceAbi, MPContractAddress)
// tokenswitch address
const mutatetoken = await loadContract(MutateToken.abi, mutatetokenaddress)
this.setState({ mutatetoken })
console.log("Mutate Token loaded")
} catch (error) {
console.log("Error! - Main Contract section")
console.log({ error })
}
}
loadingTokens = async function (address_a, address_b) {
try {
const web3 = new Web3(window.celo);
kit = newKitFromWeb3(web3);
this.setState({ loading: false })
let aToken = await loadTokens(address_a, this.state.account, mutatetokenaddress)
this.setState({aToken: aToken.contract})
this.setState({aTokenBalance: aToken.accountBalance})
this.setState({apoolBalance: aToken.contractBalance})
let bToken = await loadTokens(address_b, this.state.account, mutatetokenaddress)
this.setState({bToken: bToken.contract})
this.setState({bTokenBalance: bToken.accountBalance})
this.setState({bpoolBalance: bToken.contractBalance})
} catch (error) {
console.log("Error! - Token section")
console.log({ error })
}
}
updateValue = async function (atoken, btoken){
console.log(atoken, btoken)
this.setState({aTokenaddress: Currency[atoken].address})
this.setState({bTokenaddress: Currency[btoken].address})
let exchangevalue = Currency[btoken].exchange_rate / Currency[atoken].exchange_rate
exchangevalue = exchangevalue.toFixed(2)
this.setState({exchangerate: exchangevalue})
this.loadingTokens(Currency[atoken].address, Currency[btoken].address)
console.log("We are in the update function")
}
// Function sections=================================================================================
// USDT(a) - cPESO(b)
a_b = (ato, aamount) => {
this.setState({ loading: true})
aamount = BigNumber(aamount).shiftedBy(ERC20_DECIMALS)
this.state.aToken.methods.approve(this.state.mutatetoken._address, aamount).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.state.mutatetoken.methods.mutate(aTokenaddress, bTokenaddress, ato, aamount, this.state.exchangerate, true).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.setState({ loading: false})
})
})
}
b_a = (bto, bamount) => {
this.setState({ loading: true})
bamount = BigNumber(bamount).shiftedBy(ERC20_DECIMALS)
this.state.aToken.methods.approve(this.state.mutatetoken._address, bamount).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.state.mutatetoken.methods.mutate(aTokenaddress, bTokenaddress, bto, bamount, this.state.exchangerate, false).send({ from: this.state.account }).on('transactionHash', (hash) => {
this.setState({ loading: false})
})
})
}
ahandleChange = event => {
let a = event.target.value
this.setState({a: event.target.value})
this.updateValue(event.target.value ,this.state.b)
}
bhandleChange = event => {
this.setState({b: event.target.value})
this.updateValue(this.state.a ,event.target.value)
}
//Render section=====================================================================================
render() {
let content
if(this.state.loading){
content = <p id="loader" className="text-center">Loading...</p>
}else{
content = <TokenMain
a = {this.state.a}
b = {this.state.b}
aTokenBalance = {this.state.aTokenBalance}
bTokenBalance = {this.state.bTokenBalance}
apoolBalance = {this.state.apoolBalance}
bpoolBalance = {this.state.bpoolBalance}
exchangerate = {this.state.exchangerate}
a_b = {this.a_b}
b_a = {this.b_a}
/>
}
let a =
<select class="flex w-1/2 rounded-none text-center text-lg text-white bg-red-400" onChange={this.ahandleChange}>
<option class="bg-white text-black" value="hUSD">hUSD</option>
<option class="bg-white text-black" value="hINR">hINR</option>
<option class="bg-white text-black"value="hPESO">hPESO</option>
</select>
let b =
<select class="flex w-1/2 rounded-none text-center text-lg" onChange={this.bhandleChange}>
<option value="hINR">hINR</option>
<option value="hUSD">hUSD</option>
<option value="hPESO">hPESO</option>
</select>
return (
<div>
<div className="container-fluid mt-5">
<div className="row">
<main role="main" className="col-lg-12 ml-auto mr-auto" style={{ maxWidth: '80%' }}>
<div className="">
<div>
<div class="container mx-auto">
<div class="flex flex-row mx-auto mb-6">
<div class="flex w-1/2 mx-auto border-2 border-white-400 shadow-lg rounded-xl">
{a}
{b}
</div>
</div>
</div>
{content}
</div>
</div>
</main>
</div>
</div>
</div>
);
}
}
export default USD_cPESO;