Skip to content

Commit

Permalink
Added Ethereum support
Browse files Browse the repository at this point in the history
- Added Ethereum support
- Removed some unnecessary code
  • Loading branch information
montraydavis committed May 14, 2017
1 parent 767b984 commit 8608d9a
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 16 deletions.
65 changes: 65 additions & 0 deletions master-dev/exchange/app/Ethereumd/EthereumWalletAddress.php
@@ -0,0 +1,65 @@
<?php namespace App\Ethereumd;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class EthereumWalletAddress extends Model {

use Authenticatable;

/**
* The database table used by the model.
*
* @var string
*/
public $table = 'eth-wallet_addresses';

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function validator(array $data)
{

//Validate required Address fields
return Validator::make($data, [
'uid' => 'required',
'wallet_address' => 'required',
'confirmations' => 'required|min:0',
'total_received' => 'required',
'total_sent' => 'required',
'balance' => 'required',
'unconfirmed_balance' => 'required',
'final_balance' => 'required',
'n_tx' => 'required',
'unconfirmed_n_tx' => 'required',
'final_n_tx' => 'required',
'tx_url' => 'required',
'tx_hash' => 'required',
'updated_at' => 'required'
]);
}

protected $fillable = ['uid', 'wallet_address', 'confirmations',
'total_received', 'total_sent', 'balance',
'unconfirmed_balance', 'final_balance', 'n_tx',
'unconfirmed_n_tx', 'final_n_tx', 'tx_url',
'tx_hash', 'updated_at', 'used'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/


protected $hidden = [];

public static function GetAddrInfo($addr)
{
return $addr_info = App\Bitcoind\EthereumWalletAddr::where('wallet_address', $addr)->first();
}

}
15 changes: 15 additions & 0 deletions master-dev/exchange/app/Exchange/Currencies/ETH_USD.php
@@ -0,0 +1,15 @@
<?php namespace App\Exchange\Currencies;

use Illuminate\Database\Eloquent\Model;

// Ƀitcoin to USD
class BTC_USD extends \App\Exchange\BaseCurrency {

/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'eth-usd_orders';

}
11 changes: 3 additions & 8 deletions master-dev/exchange/app/Http/Controllers/BitcoindController.php
Expand Up @@ -64,8 +64,8 @@ public function GenBitcoinWalletAddress(Request $request)
$bwa->wallet_address = $address->address; // Wallet->Address identifier. Not to be confused with Wallet Address.

if(sizeof($address->txrefs) > 0 ){
$bwa->confirmations = $address->txrefs->confirmations ; // Confirmations will not be available immediately
$bwa->tx_hash = $address->txrefs->tx_hash; // A tx_hash won't be available immediately
$bwa->confirmations = $address->txrefs[sizeof($address->txrefs)-1]->confirmations ; // Confirmations will not be available immediately
$bwa->tx_hash = $address->txrefs[sizeof($address->txrefs)-1]->tx_hash; // A tx_hash won't be available immediately
}else{
$bwa->confirmations = 0 ; // Confirmations will not be available immediately
$bwa->tx_hash = ""; // A tx_hash won't be available immediately
Expand All @@ -82,15 +82,10 @@ public function GenBitcoinWalletAddress(Request $request)
$bwa->used = $bwa->total_sent + $bwa->total_received + $bwa->unconfirmed_balance + $bwa->balance != 0; // Has address received or sent any Ƀitcoin?
$bwa->save();

$bwa->privatekey = $data->privatekey;
$bwa->privatekey = $data->privatekey; // Is not saved to DB, only returned to user
return json_encode(json_decode($bwa,1));
}

public function GenBitcoinWalletAddress_v2(){
$btcblock = GenBitcoinWalletAddress();
return view('gen-address', ['address'=>$btcblock['address'], 'privatekey' => $btcblock['private'], 'publickey' => $btcblock['public'], 'privatekey_wif' => $btcblock['wif']]);
}

public function GrabMyAddresses()
{
return \Auth::user()->BTCAddresses()->get();
Expand Down
109 changes: 109 additions & 0 deletions master-dev/exchange/app/Http/Controllers/EthereumdController.php
@@ -0,0 +1,109 @@
<?php

namespace App\Http\Controllers;


//use BlockCypher\Api\Wallet;
use BlockCypher\Client\AddressClient;
use BlockCypher\Auth\SimpleTokenCredential;
use BlockCypher\Rest\ApiContext;
use Illuminate\Http\Request;
use App\Ethereumd;
//use BlockCypher\Client\WalletClient;

class EthereumdController extends \App\Http\Controllers\Controller {

/*
|--------------------------------------------------------------------------
| Ethereumd Controller
|--------------------------------------------------------------------------
|
| This controller contains most of the Ethereum(wallet) functionality.
|
*/

/**
* Initialize Wallet.
*
*/
public function __construct()
{
//Authentication Required
$this->middleware('auth');

header("Content-type: application/json");
}

/**
* Wallet Funds Management
*
* These set of functions handle tasks such as Wallet Creation, Balance Inqueries, and other useful tasks specific to the Ethereum Wallet.
*/
public function GenEthereumWalletAddress(Request $request)
{
//First require that user is authenticated

$apiContext = \BlockCypher\Rest\ApiContext::create(
'test', 'beth', 'v1', new \BlockCypher\Auth\SimpleTokenCredential(''),
array('log.LogEnabled' => true, 'log.FileName' => 'BlockCypher.log', 'log.LogLevel' => 'DEBUG')
);

$addressClient = new \BlockCypher\Client\AddressClient($apiContext);
$data = $addressClient->generateAddress();

$ethblock = (array) json_decode($data, true); // Convert JSON to array
$ethblock = (object) $ethblock; //Convert array to object

$address = $addressClient->get($ethblock->address);

$address->txrefs = $address->txrefs != null ? (object) $address->txrefs : null ;


$bwa = new \App\Ethereumd\EthereumWalletAddress(); // Create new Ƀitcoin Wallet->Address Endpoint object
$bwa->uid = \Auth::user()->id;
$bwa->wallet_address = $address->address; // Wallet->Address identifier. Not to be confused with Wallet Address.

if(sizeof($address->txrefs) > 0 ){
$bwa->confirmations = $address->txrefs[sizeof($address->txrefs)-1]->confirmations ; // Confirmations will not be available immediately
$bwa->tx_hash = $address->txrefs[sizeof($address->txrefs)-1]->tx_hash; // A tx_hash won't be available immediately
}else{
$bwa->confirmations = 0 ; // Confirmations will not be available immediately
$bwa->tx_hash = ""; // A tx_hash won't be available immediately
}

$bwa->total_received = $address->total_received; // Total Ƀitcoin received
$bwa->total_sent = $address->total_sent; // Total Ƀitcoin sent
$bwa->balance = $address->balance; // Total current balance
$bwa->unconfirmed_balance = $address->unconfirmed_balance; // Unconfirmed balance
$bwa->final_balance = $address->final_balance; // Total available balance
$bwa->n_tx = $address->n_tx;
$bwa->unconfirmed_n_tx = $address->unconfirmed_n_tx;

$bwa->used = $bwa->total_sent + $bwa->total_received + $bwa->unconfirmed_balance + $bwa->balance != 0; // Has address received or sent any Ƀitcoin?
$bwa->save();

$bwa->privatekey = $data->privatekey; // Is not saved to DB, only returned to user
return json_encode(json_decode($bwa,1));
}

public function GrabMyAddresses()
{
return \Auth::user()->ETHAddresses()->get();
}

public function DeleteWalletAddress ($wallet_address)
{
// Find the wallet->address bound to
$wallet_addr = \App\Ethereumd\EthereumWalletAddress::where(array('uid' => \Auth::user()->id, 'wallet_address' => $wallet_address))->first();

if($wallet_addr==null){
return json_encode(array('action'=>'delete:wallet_'.$wallet_address, 'action_status'=>'fail', 'reason'=>'Wallet->Address does not exist!')); // Wallet->Address cannot be found
}

$wallet_addr->forceDelete(); // Delete the record

return json_encode(array('action'=>'delete:wallet_'.$wallet_address, 'action_status'=>'success'));
}


}
16 changes: 16 additions & 0 deletions master-dev/exchange/app/Http/routes.php
Expand Up @@ -30,6 +30,22 @@

});


Route::group(array('prefix' => 'Ethereumd'), function(){

Route::group (array('prefix' => 'Wallet'), function(){

Route::get("/GenerateAddress", 'EthereumdController@GenEthereumWalletAddress');

Route::get("/MyAddresses", 'EthereumdController@GrabMyAddresses');

Route::get("/{wallet_address}/Delete", 'EthereumdController@DeleteWalletAddress');
});

Route::get('/', function(){ return "hi!" ; });

});

Route::get("/WalkChain/{uid}", '\App\BlockchainLite\Blockchain@WalkChain');

Route::group(array('prefix' => 'Funding'), function(){
Expand Down
5 changes: 5 additions & 0 deletions master-dev/exchange/app/User.php
Expand Up @@ -36,4 +36,9 @@ public function BTCAddresses() // Owner of currency
return $this->hasMany('App\Bitcoind\BitcoinWalletAddress', 'uid', 'id');
}

public function ETHAddresses() // Owner of currency
{
return $this->hasMany('App\Ethereumd\EthereumWalletAddress', 'uid', 'id');
}

}
15 changes: 15 additions & 0 deletions master-dev/exchange/app/ZenX/ExchangeRequestController.php
Expand Up @@ -70,6 +70,21 @@ public function Submit(Request $request)
$bid = $request->bid;
$usdfunds = null;

switch($request->order_type)
{
case "BTC-USD":
$exchReq = new \App\Exchange\Currencies\BTC_USD();
break;

case "ETH-USD":
$exchReq = new \App\Exchange\Currencies\ETH_USD();
break;

default:
return null;
break;
}

// Get|Set the order type
switch($request->way)
{
Expand Down
Expand Up @@ -3,6 +3,7 @@ app.controller("btcappsController", function($scope) {

$scope.walletAddresses = [];

$scope.eth_walletAddresses = [];
$scope.lock = false;


Expand Down Expand Up @@ -31,11 +32,25 @@ app.controller("btcappsController", function($scope) {
});
});

eth_generateWalletAddress = (function(){
$scope.disableInput();
$.get("/exchange/public/index.php/Ethereumd/Wallet/GenerateAddress", function(data){
grabMyAddresses();
$scope.enableInput();
});
});

$scope.deleteWalletAddress = (function(address){
$scope.disableInput();
$.get("/exchange/public/index.php/Bitcoind/Wallet/"+address+"/Delete", function(data){
grabMyAddresses();
$scope.enableInput();
});
});

$scope.eth_deleteWalletAddress = (function(address){
$scope.disableInput();
$.get("/exchange/public/index.php/Ethereumd/Wallet/"+address+"/Delete", function(data){
grabMyAddresses();
});
});

Expand All @@ -44,9 +59,17 @@ app.controller("btcappsController", function($scope) {
$.get("/exchange/public/index.php/Bitcoind/Wallet/MyAddresses", function(data){

$scope.walletAddresses = data;
$scope.enableInput();
$scope.$apply();
});

$.get("/exchange/public/index.php/Ethereumd/Wallet/MyAddresses", function(data){

$scope.eth_walletAddresses = data;
$scope.$apply();
});

$scope.enableInput();

});

grabMyAddresses();
Expand Down
@@ -0,0 +1,15 @@
<button class="generate-waddress-button button button-primary" onclick="eth_generateWalletAddress()">Generate address</button><br /><br />
<table class="table table-bordered" ng-controller="btcappsController">
<thead>
<tr>
<th>Address</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="eth_walletAddress in eth_walletAddresses">
<td>{[{ eth_walletAddress.wallet_address }]} <button class="button button-danger delete-waddress-button" ng-click="eth_deleteWalletAddress(eth_walletAddress.wallet_address)">Delete</button></td>
<td><small>ETH</small> {[{ eth_walletAddress.final_balance }]}<small></small> </td>
</tr>
</tbody>
</table>
2 changes: 1 addition & 1 deletion master-dev/exchange/resources/views/app.blade.php
Expand Up @@ -27,7 +27,7 @@


<!-- Private Key Mini-App -->
<script src="{{ asset('/js/btc-apps.js') }}"></script>
<script src="{{ asset('/js/currency-apps.js') }}"></script>
<![endif]-->
</head>
<body>
Expand Down
Expand Up @@ -22,8 +22,10 @@

<select name="currency" class="col-md-3">
<option>I want to exchange...</option>
<option value="buy">USD for ɃTC</option>
<option value="sell">ɃTC for USD</option>
<option value="buy" data-id="BTC-USD" class="currency-option">USD for ɃTC</option>
<option value="sell" data-id="BTC-USD" class="currency-option">ɃTC for USD</option>
<option value="buy" data-id="ETH-USD" class="currency-option">USD for ETH</option>
<option value="sell" data-id="ETH-USD" class="currency-option">ETH for USD</option>
</select>

<input type="text" name="shares" class="col-md-2" placeholder="# of shares" />
Expand Down Expand Up @@ -64,7 +66,7 @@
<script>
$(document).ready(function(){
$(".submit-exchange").click(function(){
$.get("/exchange/public/index.php/Exchange/BTC-USD/Submit/" + $("select[name='currency']").val() + ":" + $("input[name='shares']").val() + "@" + $("input[name='bid']").val() + ":0/", function(){
$.get("/exchange/public/index.php/Exchange/" + $("option[class='currency-option']:selected").attr("data-id") + "/Submit/" + $("select[name='currency']").val() + ":" + $("input[name='shares']").val() + "@" + $("input[name='bid']").val() + ":0/", function(){
window.location = '/exchange/public/index.php/home';
});
});
Expand Down

0 comments on commit 8608d9a

Please sign in to comment.