Class: MVM::Nft

Inherits:
Object
  • Object
show all
Defined in:
lib/mvm/nft.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url: MVM::RPC_URL, mirror_address: MVM::MIRROR_ADDRESS) ⇒ Nft

Returns a new instance of Nft.



7
8
9
10
# File 'lib/mvm/nft.rb', line 7

def initialize(rpc_url: MVM::RPC_URL, mirror_address: MVM::MIRROR_ADDRESS)
  @rpc = Eth::Client.create rpc_url
  @mirror = Eth::Contract.from_abi name: 'Mirror', address: mirror_address, abi: File.read(File.expand_path('./abis/mirror.json', __dir__))
end

Instance Attribute Details

#mirrorObject (readonly)

Returns the value of attribute mirror.



5
6
7
# File 'lib/mvm/nft.rb', line 5

def mirror
  @mirror
end

#rpcObject (readonly)

Returns the value of attribute rpc.



5
6
7
# File 'lib/mvm/nft.rb', line 5

def rpc
  @rpc
end

Instance Method Details

#collection_from_contract(address) ⇒ Object



12
13
14
15
16
17
# File 'lib/mvm/nft.rb', line 12

def collection_from_contract(address)
  collection = @rpc.call @mirror, 'collections', address
  return if collection.zero?

  MixinBot::UUID.new(hex: collection.to_fs(16)).unpacked
end

#contract_from_collection(uuid) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mvm/nft.rb', line 19

def contract_from_collection(uuid)
  collection = uuid.to_s.gsub('-', '').to_i(16)
  contract = @rpc.call @mirror, 'contracts', collection
  address = Eth::Address.new contract
  return unless address.valid?

  address.checksummed
end

#owner_of(collection_id, token_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mvm/nft.rb', line 28

def owner_of(collection_id, token_id)
  address = contract_from_collection collection_id
  return if address.blank? || address.to_i(16).zero?

  contract = Eth::Contract.from_abi name: 'Collectible', address:, abi: File.read(File.expand_path('./abis/erc721.json', __dir__))
  owner = @rpc.call contract, 'ownerOf', token_id.to_i
  address = Eth::Address.new owner
  return unless address.valid?

  address.checksummed
rescue IOError
  nil
end

#token_of_owner_by_index(contract, owner, index) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/mvm/nft.rb', line 42

def token_of_owner_by_index(contract, owner, index)
  contract = Eth::Contract.from_abi name: 'Collectible', address: contract, abi: File.read(File.expand_path('./abis/erc721.json', __dir__))

  @rpc.call contract, 'tokenOfOwnerByIndex', owner, index
rescue IOError
  nil
end