PSTruePool.psm1

function Get-tpCoinRecord {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [Alias("Launcher_ID")]
        [string]$farmed_by,

        [Parameter(ValueFromPipelineByPropertyName)]
        [int]$payout,

        [Parameter()]
        [string]$order,

        [Parameter()]
        [int]$limit,

        [Parameter()]
        [int]$offset
    )

    Process{
        $TruePoolParameters = @{
            APIEndPoint = "coinrecord"
            APIParameters = $PSBoundParameters
            Method = "GET"
        }
        $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
        if ($Null -ne $tpResponse.results){
            foreach ($coinrecord in $tpResponse.results){
                $coinrecord.psobject.TypeNames.Insert(0,"TruePool.CoinRecord")
                $coinrecord
            }
        }
        
    } #Process
}
function Get-tpFarmer {
    [CmdletBinding(DefaultParameterSetName="query")]
    param(
        [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName="Launcher_ID")]
        [string]$launcher_id,

        [Parameter(ParameterSetName="query")]
        [string]$search,

        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName="query")]
        [string]$display_name,

        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName="query")]
        [int]$difficulty,

        [Parameter(ParameterSetName="query")]
        [string]$order,

        [Parameter(ParameterSetName="query")]
        [int]$limit,

        [Parameter(ParameterSetName="query")]
        [int]$offset
    )

    Process{
        $TruePoolParameters = @{
            APIEndPoint = "farmer"
            APIParameters = $PSBoundParameters
            Method = "GET"
        }
        $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
        if ($Null -ne $tpResponse.results){
            foreach ($farmer in $tpResponse.results){
                $farmer.psobject.TypeNames.Insert(0,"TruePool.Farmer")
                $farmer
            }
        }
        
    } #Process
}
function Get-tpInfo {
    [CmdletBinding()]
    param()

    $tpResponse = Invoke-TruePoolAPI -APIEndPoint "info" -Method "GET"
    $tpResponse
}
function Get-tpPartial {
    [CmdletBinding(DefaultParameterSetName="query")]
    param(
        [Parameter(Mandatory,ParameterSetName="partial_id")]
        [int]$id,

        [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName="query")]
        [string]$launcher_id,

        [Parameter(ParameterSetName="query")]
        [int]$timestamp,

        [Parameter(ParameterSetName="query")]
        [int]$start_timestamp,

        [Parameter(ParameterSetName="query")]
        [string]$order,

        [Parameter(ParameterSetName="query")]
        [int]$limit,

        [Parameter(ParameterSetName="query")]
        [int]$offset
    )

    Process{
        $TruePoolParameters = @{
            APIEndPoint = "partial"
            Method = "GET"
        }
        if ($PSCmdlet.ParameterSetName -eq "query"){
            $TruePoolParameters["APIParameters"] = $PSBoundParameters
        }
        else{
            $TruePoolParameters["Single"] = $id
        }
        $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
        if ($PSCmdlet.ParameterSetName -eq "query"){
            if ($Null -ne $tpResponse.results){
                foreach ($partial in $tpResponse.results){
                    $partial.psobject.TypeNames.Insert(0,"TruePool.Partial")
                    $partial
                }
            }
        }
        else{
            $tpResponse
        }
        
    } #Process
}
function Get-tpPayout {
    [CmdletBinding(DefaultParameterSetName="query")]
    param(
        [Parameter(Mandatory,ParameterSetName="id")]
        [int]$id,

        [Parameter(ParameterSetName="query")]
        [string]$ordering,

        [Parameter(ParameterSetName="query")]
        [int]$limit,

        [Parameter(ParameterSetName="query")]
        [int]$offset
    )

    Process{
        $TruePoolParameters = @{
            APIEndPoint = "payout"
            Method = "GET"
        }
        if ($PSCmdlet.ParameterSetName -eq "query"){
            $TruePoolParameters["APIParameters"] = $PSBoundParameters
        }
        else{
            $TruePoolParameters["Single"] = $id
        }
        $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
        if ($PSCmdlet.ParameterSetName -eq "query"){
            if ($Null -ne $tpResponse.results){
                foreach ($payout in $tpResponse.results){
                    $payout.psobject.TypeNames.Insert(0,"TruePool.Payout")
                    $payout
                }
            }
        }
        else{
            $tpResponse
        }
        
    } #Process
}
function Get-tpPayoutAddress {
    [CmdletBinding(DefaultParameterSetName="query")]
    param(
        [Parameter(Mandatory,ParameterSetName="id")]
        [int]$id,

        [Parameter(ParameterSetName="query")]
        [string]$payout,

        [Parameter(ParameterSetName="query")]
        [string]$puzzle_hash,

        [Parameter(ParameterSetName="query")]
        [string]$farmer,

        [Parameter(ParameterSetName="query")]
        [string]$ordering,

        [Parameter(ParameterSetName="query")]
        [int]$limit,

        [Parameter(ParameterSetName="query")]
        [int]$offset
    )

    Process{
        $TruePoolParameters = @{
            APIEndPoint = "payout_address"
            Method = "GET"
        }
        if ($PSCmdlet.ParameterSetName -eq "query"){
            $TruePoolParameters["APIParameters"] = $PSBoundParameters
        }
        else{
            $TruePoolParameters["Single"] = $id
        }
        $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
        if ($PSCmdlet.ParameterSetName -eq "query"){
            if ($Null -ne $tpResponse.results){
                foreach ($payoutAddress in $tpResponse.results){
                    $payoutAddress.psobject.TypeNames.Insert(0,"TruePool.PayoutAddress")
                    $payoutAddress
                }
            }
        }
        else{
            $tpResponse
        }
        
    } #Process
}
function Get-tpSize {
    [CmdletBinding()]
    param(
        [Parameter()]
        [int]$days
    )

    $TB = 1gb * 1024
    $PB = $TB * 1024
    $TruePoolParameters = @{
        APIEndPoint = "size"
        APIParameters = $PSBoundParameters
        Method = "GET"
    }
    $tpResponse = Invoke-TruePoolAPI @TruePoolParameters
    foreach ($datapoint in $tpResponse){
        $datapoint.datetime = [datetime]::Parse($datapoint.datetime)
        $datapoint | Add-Member -NotePropertyMembers @{
            Size_TB = [math]::Round($datapoint.size / $TB,2)
            Size_PB = [math]::Round($datapoint.size / $PB,2)
        }
    }
    $tpResponse
}
function Get-tpStats {
    [CmdletBinding()]
    param()

    $TruePoolParameters = @{
        APIEndPoint = "stats"
        Method = "GET"
    }
    Invoke-TruePoolAPI @TruePoolParameters
}
function Invoke-TruePoolAPI {
    [CmdletBinding()]
    param(
        [string]$APIEndPoint,
        [hashtable]$APIParameters,
        [string]$Method,
        [string]$Single
    )


    $uri = "https://truepool.io/v1/pool/$APIEndPoint"

    if ($APIParameters.Keys.Count){
        $QueryString = ($APIParameters.Keys | foreach {
            if (![string]::IsNullOrEmpty($_)){
                $value = $APIParameters[$_]
                "$_=$value"
            }
        }) -join '&'

        if (-not[string]::IsNullOrEmpty($QueryString)){
            $uri = [string]::Join("?",@($uri,$QueryString))
        }
    }
    elseif ($PSBoundParameters.ContainsKey("Single")){
        $uri = [string]::Join("/",@($uri,$Single))
    }

    $RestMethodParameters = @{
        Method = $Method
        Uri = $uri
        ErrorAction = "Stop"
    }

    Invoke-RestMethod @RestMethodParameters
}
Export-ModuleMember -function Get-tpCoinRecord, Get-tpFarmer, Get-tpInfo, Get-tpPartial, Get-tpPayout, Get-tpPayoutAddress, Get-tpSize, Get-tpStats