Archivi tag: amount

Bitcoin, Ethereum, Litecoin Coinbase Prices and Wallet Amounts with Powershell

<#
.SYNOPSIS 
Retrieve current price and wallet amount of BTC, ETH, LTC cryptocurrencies
from Coinbase account and coinbase API.
See Coinbase API documentation here: https://developers.coinbase.com/api/v2.
REQUIREMENTS: Powershell and Windows 7 at least.
Simply change api_key, secret_key and basecurrency before running bitbalance.ps1.
To create API and SECRET key go to your account preferences in Coinbase
and follow the prucedure.

.EXAMPLE
Run bitbalance.ps1

.NOTES 
Name: bitbalance.ps1
Author: Marco Braidotti 
Version History 
    1.0 - 10/12/2017 
#>

$api_key = 'abcdefghi123456' #change with your api key generated on your Coinbase account settings API section
$secret_key = 'abcdefghijklmno123456789101234' #change with your secret key generated from on your Coinbase account settings API section
$basecurrency = 'EUR' #change with your desired fiat currency

#do not change anything below!!!!
$urlBTCprice = 'https://api.coinbase.com/v2/prices/BTC-' + $basecurrency + '/spot'
$urlETHprice = 'https://api.coinbase.com/v2/prices/ETH-' + $basecurrency + '/spot'
$urlLTCprice = 'https://api.coinbase.com/v2/prices/LTC-' + $basecurrency + '/spot'
$BTCprice = ((Invoke-WebRequest $urlBTCprice | ConvertFrom-Json).data).amount
$ETHprice = ((Invoke-WebRequest $urlETHprice | ConvertFrom-Json).data).amount
$LTCprice = ((Invoke-WebRequest $urlLTCprice | ConvertFrom-Json).data).amount

$accounts = 'https://api.coinbase.com/v2/accounts'
$time = 'https://api.coinbase.com/v2/time'
$epochtime = [string]((Invoke-WebRequest $time | ConvertFrom-Json).data).epoch

$method = 'GET'
$requestpath = '/v2/accounts'

$sign = $epochtime + $method + $requestpath
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret_key)
$computeSha = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($sign))

$signature = ([System.BitConverter]::ToString($computeSha) -replace "-").ToLower()

$header = @{
    "CB-ACCESS-SIGN"      = $signature
    "CB-ACCESS-TIMESTAMP" = $epochtime
    "CB-ACCESS-KEY"       = $api_key
}

$result = Invoke-WebRequest $accounts -Headers $header -Method Get -ContentType "application/json"
$accounts = $result.Content | ConvertFrom-Json

foreach ($wallet in $accounts.data.balance) {   
    switch ($wallet.currency) {   
        $basecurrency {$totalfiat = [Double]$wallet.amount} 
        BTC {$totalBTC = [math]::Round([Double]$wallet.amount * $BTCprice, 2)} 
        ETH {$totalETH = [math]::Round([Double]$wallet.amount * $ETHprice, 2)} 
        LTC {$totalLTC = [math]::Round([Double]$wallet.amount * $LTCprice, 2)} 
        default {"N.D."}
    }
}

$totalamount = $totalfiat + $totalBTC + $totalETH + $totalLTC

Write-Host "Price BTC/$basecurrency : $BTCprice" -background red -ForegroundColor black
Write-Host "Price ETH/$basecurrency : $ETHprice" -background red -ForegroundColor black
Write-Host "Price LTC/$basecurrency : $LTCprice" -background red -ForegroundColor black
Write-Host "Balance $basecurrency : $totalfiat $basecurrency" -background yellow -ForegroundColor black
Write-Host "Balance BTC : $totalBTC $basecurrency" -background yellow -ForegroundColor black
Write-Host "Balance ETH : $totalETH $basecurrency" -background yellow -ForegroundColor black
Write-Host "Balance LTC : $totalLTC $basecurrency" -background yellow -ForegroundColor black
Write-Host "Total Balance : $totalamount $basecurrency" -ForegroundColor green