Class: NftChecker::OpenSea

Inherits:
Object
  • Object
show all
Defined in:
lib/nft_checker/open_sea.rb

Overview

NFT Checker implementation for OpenSea

Instance Method Summary collapse

Constructor Details

#initialize(testnet: false) ⇒ OpenSea

Returns a new instance of OpenSea.

Parameters:

  • testnet (defaults to: false)

    Use OpenSea ‘testnets’ API (default false)



11
12
13
# File 'lib/nft_checker/open_sea.rb', line 11

def initialize(testnet: false)
  @url_base = testnet ? "https://testnets-api.opensea.io/" : "https://api.opensea.io/"
end

Instance Method Details

#fetch_nft_for_owner(owner_address, nft_metadata) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/nft_checker/open_sea.rb', line 52

def fetch_nft_for_owner(owner_address, )
  contract, token = .slice(:contract_address, :token_id).values
  rez = HTTParty.get(@url_base + "asset/#{contract}/#{token}/", query: { account_address: owner_address })
  handle_response_codes(rez, not_found: nil) do
    data = rez.parsed_response
    data if data["ownership"] &&
            data["ownership"]["owner"]["address"].casecmp(owner_address).zero? &&
            data["ownership"]["quantity"].to_i.positive?
  end
end

#in_collection?(collection_metadata, nft_metadata) ⇒ Boolean

Verify that the NFT is part of the referenced collection

Parameters:

  • collection_metadata
    • hash containing :slug for OpenSea collection

  • nft_metadata
    • hash containing :contract_address and :token_id values

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/nft_checker/open_sea.rb', line 25

def in_collection?(, )
  nft = fetch_nft()
  return false if nft.nil?

  .each_key do |key|
    return false unless nft["collection"][key.to_s].casecmp([key]).zero?
  end
  true
end

#list_nfts(collection_metadata, owner_address) ⇒ Object

List all NFTs in the collection owned by the given address

Parameters:

  • collection_metadata
    • hash containing :slug for OpenSea collection

  • owner_address
    • address to check for NFTs



45
46
47
48
49
50
# File 'lib/nft_checker/open_sea.rb', line 45

def list_nfts(, owner_address)
  rez = HTTParty.get("#{@url_base}assets", query: { owner: owner_address, collection: [:slug] })
  handle_response_codes(rez, not_found: []) do
    rez.parsed_response["assets"] || []
  end
end

#owner?(address, nft_metadata) ⇒ Boolean

Verify that the NFT is owned by the given address

Parameters:

  • address
    • address of presumed NFT owner

  • nft_metadata
    • hash containing :contract_address and :token_id values

Returns:

  • (Boolean)


18
19
20
# File 'lib/nft_checker/open_sea.rb', line 18

def owner?(address, )
  !fetch_nft_for_owner(address, ).nil?
end

#verify_owner(nft_metadata, owner_address) ⇒ Object

Deprecated

Verify that the NFT is owned by the given address

Parameters:

  • nft_metadata
    • hash containing :contract_address and :token_id values

  • owner_address
    • address of presumed NFT owner



38
39
40
# File 'lib/nft_checker/open_sea.rb', line 38

def verify_owner(, owner_address)
  owner?(owner_address, )
end