| 1 | package Finance::CoinbasePro::API::CLI::Converter; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Finance::CoinbasePro::API::CLI::Value; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Finance::CoinbasePro::API::CLI::ConversionFactor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | has 'conversions' => ( is=>'rw', isa=>"ArrayRef[Finance::CoinbasePro::API::CLI::ConversionFactor]", required=>1 ); | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # my $converter = Finance::CoinbasePro::API::CLI::Converter->new( conversions=>[{from=>"BTC", to=>"USD", factor=>7884}] ); | ||||||||||||||||||||||||||||
| 9 | # my $converted = $converter->convert( Finance::CoinbasePro::API::CLI:Value->new( num=>10, currency=>"BTC" ), "USD" ); | ||||||||||||||||||||||||||||
| 10 | sub convert { | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | my ($self, $from_value, $to_currency) = @_; | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=7 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | if ($from_value->currency eq $to_currency) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 0 | return $from_value; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | 2 | for my $conversion (@{$self->conversions}) { | |||||||||||||||||||||||||||
| 16 | # from and to match | ||||||||||||||||||||||||||||
| 17 | 2 | ▶ | if ($from_value->currency eq $conversion->from_currency && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$from_value->currency eq $conversion->from_currency and $to_currency eq $conversion->to_currency
MC/DC50%1/2
$from_value->currency eq $conversion->from_currency and $to_currency eq $conversion->to_currency
$from_value->currency eq $conversion->from_currency
$to_currency eq $conversion->to_currency
Truth table66%2/3
A and B
A= $from_value->currency eq $conversion->from_currencyB= $to_currency eq $conversion->to_currency
| |||||||||||||||||||||||||||||
| 18 | $to_currency eq $conversion->to_currency) { | ||||||||||||||||||||||||||||
| 19 | 1 | return $self->_convert_value_with_factor( $conversion, $from_value, $to_currency ); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # from and to are reversed | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | if ($from_value->currency eq $conversion->to_currency && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$from_value->currency eq $conversion->to_currency and $to_currency eq $conversion->from_currency
MC/DC0%0/2
$from_value->currency eq $conversion->to_currency and $to_currency eq $conversion->from_currency
$from_value->currency eq $conversion->to_currency
$to_currency eq $conversion->from_currency
Truth table33%1/3
A and B
A= $from_value->currency eq $conversion->to_currencyB= $to_currency eq $conversion->from_currency
| |||||||||||||||||||||||||||||
| 24 | $to_currency eq $conversion->from_currency) { | ||||||||||||||||||||||||||||
| 25 | 1 | return $self->_convert_value_with_factor( $conversion, $from_value, $to_currency ); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | #if ($from_value->currency eq $conversion->from_currency ) { | ||||||||||||||||||||||||||||
| 28 | # return $from_value; # special case, no conversion needed | ||||||||||||||||||||||||||||
| 29 | #} | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | die "$0: Couldn't find conversion for " . $from_value->to_str() . " to $to_currency\n"; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub _convert_value_with_factor { | ||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my ($self, $conversion, $from_value, $to_currency) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_convert_value_with_factor: called CC=6 SCAR=21.4
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if ($from_value->currency eq $conversion->from_currency && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$from_value->currency eq $conversion->from_currency and $to_currency eq $conversion->to_currency
MC/DC50%1/2
$from_value->currency eq $conversion->from_currency and $to_currency eq $conversion->to_currency
$from_value->currency eq $conversion->from_currency
$to_currency eq $conversion->to_currency
Truth table66%2/3
A and B
A= $from_value->currency eq $conversion->from_currencyB= $to_currency eq $conversion->to_currency
| |||||||||||||||||||||||||||||
| 39 | $to_currency eq $conversion->to_currency) { | ||||||||||||||||||||||||||||
| 40 | 1 | my $new_num = $from_value->num * $conversion->factor; | |||||||||||||||||||||||||||
| 41 | 1 | return Finance::CoinbasePro::API::CLI::Value->new( num=>$new_num, currency=>$to_currency ); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | if ($from_value->currency eq $conversion->to_currency && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$from_value->currency eq $conversion->to_currency and $to_currency eq $conversion->from_currency
MC/DC0%0/2
$from_value->currency eq $conversion->to_currency and $to_currency eq $conversion->from_currency
$from_value->currency eq $conversion->to_currency
$to_currency eq $conversion->from_currency
Truth table33%1/3
A and B
A= $from_value->currency eq $conversion->to_currencyB= $to_currency eq $conversion->from_currency
| |||||||||||||||||||||||||||||
| 44 | $to_currency eq $conversion->from_currency) { | ||||||||||||||||||||||||||||
| 45 | 1 | my $new_num = $from_value->num / $conversion->factor; | |||||||||||||||||||||||||||
| 46 | 1 | return Finance::CoinbasePro::API::CLI::Value->new( num=>$new_num, currency=>$to_currency ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($from_value->currency ne $conversion->from_currency ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | return $from_value; # special case, no conversion needed | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | die "$0: can't use converter for " . $conversion->from . " for value in " . $from_value->{currency}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | 1; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Finance::CoinbasePro::API::CLI::Converter - converts between currencies | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my $converter = Finance::CoinbasePro::API::CLI::Converter->new( conversions=>[{from=>"BTC", to=>"USD", factor=>7884}] ); | ||||||||||||||||||||||||||||
| 69 | my $converted = $converter->convert( Finance::CoinbasePro::API::CLI:Value->new( num=>10, currency=>"BTC" ), "USD" ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 REQUIRES | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | L<Finance::CoinbasePro::API::CLI::ConversionFactor> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | L<Finance::CoinbasePro::API::CLI::Value> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | L<Mouse> | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 METHODS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 convert | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | convert(); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | my $converter = Finance::CoinbasePro::API::CLI::Converter->new( conversions=>[{from=>"BTC", to=>"USD", factor=>7884}] ); | ||||||||||||||||||||||||||||
| 88 | my $converted = $converter->convert( Finance::CoinbasePro::API::CLI:Value->new( num=>10, currency=>"BTC" ), "USD" ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||