-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for fetching an address' reputation lazily. TODO: * Add unit tests Example: ``` risky_address = Coinbase::Address.new(:ethereum_mainnet, '0x12846c6Fd6baBFE4bC6F761eB871eFfFDEb26913') // Returns the reputation of the address risky_address.reputation // Returns whether the address itself is deemed "risky" risky_address.risky? => true ```
- Loading branch information
1 parent
1f6d6ce
commit 39ca4a4
Showing
3 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
module Coinbase | ||
# A representation of the reputation of a blockchain address. | ||
class AddressReputation | ||
# A metadata object associated with an address reputation. | ||
Metadata = Struct.new( | ||
*Client::AddressReputationMetadata.attribute_map.keys.map(&:to_sym), | ||
keyword_init: true | ||
) do | ||
def to_s | ||
Coinbase.pretty_print_object( | ||
self.class, | ||
**to_h | ||
) | ||
end | ||
end | ||
|
||
class << self | ||
def fetch(address_id:, network: Coinbase.default_network) | ||
network = Coinbase::Network.from_id(network) | ||
|
||
model = Coinbase.call_api do | ||
reputation_api.get_address_reputation(network.normalized_id, address_id) | ||
end | ||
|
||
new(model) | ||
end | ||
|
||
private | ||
|
||
def reputation_api | ||
Coinbase::Client::ReputationApi.new(Coinbase.configuration.api_client) | ||
end | ||
end | ||
|
||
def initialize(model) | ||
unless model.is_a?(Coinbase::Client::AddressReputation) | ||
raise ArgumentError, | ||
'must be an AddressReputation client object' | ||
end | ||
|
||
@model = model | ||
end | ||
|
||
def score | ||
@model.score | ||
end | ||
|
||
def metadata | ||
@metadata ||= Metadata.new(**@model.metadata) | ||
end | ||
|
||
def risky? | ||
score.negative? | ||
end | ||
|
||
def to_s | ||
Coinbase.pretty_print_object( | ||
self.class, | ||
score: score, | ||
**metadata.to_h | ||
) | ||
end | ||
|
||
def inspect | ||
to_s | ||
end | ||
end | ||
end |