Archivi tag: cryptocurrency

Check Cryptocurrencies Price and Amount Coinbase Wallet with Powershell

Powershell script to check cryptocurrencies current prices and wallet amounts of Coinbase account wallet through Coinbase API.
Save the script and before run fill mandatory parameters (api key and secret key generated from Coinbase account) and change base currency to account default currency.

Example:

.\cryptobalance.ps1 -api_key 1234567890abcdef -secret_key 1234567890abcdefghijklmnopqrstuv -basecurrency EUR -cryptos "BTC", "ETH", "LTC", "BCH"
<#
.SYNOPSIS 
Retrieve current price and wallet amount of 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 (to the default currency of your account) before running cryptobalance.ps1.
To create API and SECRET key go to your account preferences in Coinbase and follow the procedure.

.EXAMPLE
Run .\cryptobalance.ps1 -api_key 1234567890abcdef -secret_key 1234567890abcdefghijklmnopqrstuv -basecurrency EUR -cryptos "BTC", "ETH", "LTC", "BCH"

.NOTES 
Name: bitbalance.ps1
Author: Marco Braidotti 
Version History 
    3.0 - 21/02/2018
#>

    Param(
        [Parameter(Mandatory=$true)][String] $api_key = '',  #change with your api key generated on your Coinbase account settings API section
        [Parameter(Mandatory=$true)][String] $secret_key = '', #change with your secret key generated on your Coinbase account settings API section
        [String] $basecurrency = 'EUR', #change with your desired fiat currency
        [String[]] $cryptos = @("BTC", "ETH", "LTC", "BCH") #change with your desired criptos
        )

try {
    
$totalbalance = 0

foreach ($crypto in $cryptos) {

    $urlAPICryptoPrice = 'https://api.coinbase.com/v2/prices/' + $crypto + '-' + $basecurrency + '/spot'
    $CryptoPrice = ((Invoke-WebRequest $urlAPICryptoPrice | 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"
    $wallets = $result.Content | ConvertFrom-Json


    foreach ($wallet in $wallets.data.balance) {
        if ($wallet.currency -eq $crypto) {
            $total = [math]::Round([Double]$wallet.amount * $CryptoPrice, 2)
        }
    }

    $totalbalance += $total
    Write-Host $crypto "Current Price:" $CryptoPrice $basecurrency "- Wallet Amount:" $total $basecurrency -ForegroundColor Yellow

}
$totalfiat = (((Invoke-WebRequest $accounts -Headers $header -Method Get -ContentType "application/json").Content | ConvertFrom-Json).data | Where-Object {$_.type -match 'fiat'  } | Select-Object balance).balance.amount
write-host "Fiat Total Amount:" $totalfiat $basecurrency -ForegroundColor Yellow

$amount = $totalbalance + $totalfiat
write-host "Wallet Total Amount:" $amount $basecurrency -ForegroundColor Green

}
catch [Exception] {
    Write-Host $_.Exception.Message
  exit
}

 

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