Skip to content

Commit

Permalink
Changed dontation system
Browse files Browse the repository at this point in the history
Donations are now directly deposited to the recievers address rather than being stored on the contract.
  • Loading branch information
Harnick Khera authored and Harnick Khera committed Jan 23, 2019
1 parent b073125 commit 9834920
Show file tree
Hide file tree
Showing 5 changed files with 10,282 additions and 14,631 deletions.
58 changes: 1 addition & 57 deletions React/src/components/Account.js
Expand Up @@ -9,40 +9,20 @@
import React, { Component } from 'react';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
import {DepositTrx, withdrawTrx, ChangeUsername} from "../utils/tronweb";
import {ChangeUsername} from "../utils/tronweb";

class Account extends React.Component {
constructor(props) {
super(props);
this.state = {
DepositValue: 0,
Username: ""
};

this.handleDepositChange = this.handleDepositChange.bind(this);
this.handleDepositSubmit = this.handleDepositSubmit.bind(this);
this.handleWithdrawSubmit = this.handleWithdrawSubmit.bind(this);

this.handleUserChange = this.handleUserChange.bind(this);
this.handleUserSubmit = this.handleUserSubmit.bind(this);

}

handleDepositChange(event) {
this.setState({DepositValue: event.target.value});
}

handleDepositSubmit(event) {
DepositTrx(this.state.DepositValue);
this.state.DepositValue = 0;
event.preventDefault();
}

handleWithdrawSubmit(event) {
withdrawTrx(true, 0);
this.state.WithdrawValue = 0;
event.preventDefault();
}

handleUserChange(event) {
this.setState({Username: event.target.value});
Expand All @@ -66,8 +46,6 @@ class Account extends React.Component {
<TabList>
<Tab>Current User Information</Tab>
<Tab>Update Username</Tab>
<Tab>Deposit Trx</Tab>
<Tab>Withdraw Trx</Tab>
</TabList>


Expand All @@ -76,8 +54,6 @@ class Account extends React.Component {
<h3>Current User Information</h3>
<p></p>
<strong>Username: </strong> {userData['UserName']}
<p></p>
<strong>Balance: </strong> {Number(userData['SunBalance'])/1000000} Trx
<p></p>
<strong>Address: </strong> {userData['TronAddress']}
<p></p>
Expand Down Expand Up @@ -107,38 +83,6 @@ class Account extends React.Component {
</div>
</TabPanel>

<TabPanel>
<div className="Deposit">
<h3>Deposit Trx</h3>

<form onSubmit={this.handleDepositSubmit}>
<label> Deposit Value </label>

<div>
<input type="number" value={this.state.DepositValue} onChange={this.handleDepositChange} />
</div>

<p></p>
<input type="submit" class="btn btn-outline-dark" value="Deposit" />
<p></p>
</form>

</div>
</TabPanel>

<TabPanel>
<div className="Withdraw">
<h3>Withdraw Trx</h3>

<form onSubmit={this.handleWithdrawSubmit}>

<p></p>
<input type="submit" class="btn btn-outline-dark" value={ "Withdrawal Your " + (Number(userData['SunBalance'])/1000000).toString() + " Trx"} />
<p></p>
</form>
</div>
</TabPanel>

</Tabs>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions React/src/components/Donate.js
Expand Up @@ -48,6 +48,7 @@ class Donate extends React.Component {

<p></p>
<strong>Post Has Earned: {this.props.donation['TrxDonation']}Trx </strong>

<p></p>
</form>
</div>
Expand Down
91 changes: 10 additions & 81 deletions React/src/utils/tronweb.js
Expand Up @@ -23,8 +23,8 @@ const tronWebDefault = new TronWeb(

//address of the contract
// const contractAddress = "TGX6LGnhFgSUyG4oR7iU4bVTWUnMY9B7mP"; MAINNET
const contractAddress = "TEQebZr8wuhyYMLXCdWi6LQA7z2gN1fEfY" //Shasta
//const contractAddress = "TH4TJ961DbZ6fuJNf3gHwNgfwNcRxxYE6a"; // Quickstart
//const contractAddress = "TEQebZr8wuhyYMLXCdWi6LQA7z2gN1fEfY" //Shasta
const contractAddress = "TM2sSVEvbSGYjR5d6oS8cv529vqWZLF2h3"; // Quickstart


function dynamicTronlink(){
Expand Down Expand Up @@ -366,77 +366,7 @@ export async function VoteOnComment(postid, commentid, votetype) {

}

// DEPOSIT, DONATION and WITHDRAW SYSTEM
export async function DepositTrx(trxAmount) {

//load the contract
const contract = await window.tronWeb.contract().at(contractAddress);

//convert tron amount into a sun value as sun is used as the call value
let sunAmount = Number(trxAmount * 1000000) // 1 trx is 1 million sun, call value is in sun.

//notify the user that the deposit has been attempted
Swal({title:'transaction to deposit ' + sunAmount.toString() + "Sun (" + trxAmount.toString() + " trx) has been sent",
type: 'info'
});

//submit the data to the blockchain
contract.deposit().send({
shouldPollResponse:true,
callValue: sunAmount

}).then(res => Swal({
title:'Deposit Made Successfully',
type: 'success'

})).catch(err => Swal(
{
title:'Deposit Failed',
type: 'error'
}
));
}

export async function withdrawTrx(takeAll, trxAmount) {

//load the contract
const contract = await window.tronWeb.contract().at(contractAddress);

//convert the postid into a useable form
let sunAmount = Number(trxAmount * 1000000) // 1 trx is 1 million sun, call value is in sun.
let sunHexValue = "0x" + Number(sunAmount).toString(16);

//notify the user that the deposit has been attempted
if(takeAll == true){

Swal({title:'transaction to withdraw Current trx balance has been sent',
type: 'info'
});

}else {

Swal({title:'transaction to withdraw ' + sunAmount.toString() + "Sun (" + trxAmount.toString() + " trx) has been sent",
type: 'info'
});

}

//submit the data to the blockchain
contract.withdraw(tronWeb.toHex(takeAll), sunHexValue).send({
shouldPollResponse:true,
callValue: 0

}).then(res => Swal({
title:'Withdrawal Successful',
type: 'success'

})).catch(err => Swal(
{
title:'Withdrawal Failed',
type: 'error'
}
));
}
//DONATION SYSTEM

export async function DonateTrx(postid, trxAmount) {

Expand All @@ -449,15 +379,15 @@ export async function DonateTrx(postid, trxAmount) {
let id = "0x" + Number(postid).toString(16);


Swal({title:'Transaction to Donate ' + trxAmount.toString() + "trx from your contract balance sent",
Swal({title:'Transaction to Donate ' + trxAmount.toString() + "trx sent",
type: 'info'
});


//submit the data to the blockchain
contract.makeDonation(id, sunHexValue).send({
contract.makeDonation(id).send({
shouldPollResponse:true,
callValue: 0
callValue: sunAmount

}).then(res => Swal({
title:'Donation Successful',
Expand Down Expand Up @@ -551,18 +481,17 @@ export async function getUserData() {
let senderAddress = await contract.getSenderAddress().call();
let hexAdd = senderAddress;
let add = tronWeb.address.fromHex(hexAdd);

let ContractBalance = await contract.getBalance(hexAdd).call();
let balance = tronWeb.toBigNumber(ContractBalance['_hex']).toNumber();

let ContractUsername = await contract.getUsername(hexAdd).call();
let username = hex2a(ContractUsername);

let balance = await tronWeb.trx.getBalance(add);

user = {
TronAddress : add,
HexAddress : "0x" + hexAdd,
SunBalance : balance,
UserName : username
UserName : username,
SunBalance : balance
}

localStorage.setItem("User", JSON.stringify(user));
Expand Down

0 comments on commit 9834920

Please sign in to comment.