Class: MVM::Registry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url: MVM::RPC_URL, registry_address: MVM::REGISTRY_ADDRESS) ⇒ Registry

Returns a new instance of Registry.



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

def initialize(rpc_url: MVM::RPC_URL, registry_address: MVM::REGISTRY_ADDRESS)
  @rpc = Eth::Client.create rpc_url
  @registry = Eth::Contract.from_abi name: 'Registry', address: registry_address,
                                     abi: File.read(File.expand_path('./abis/registry.json', __dir__))
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



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

def registry
  @registry
end

#rpcObject (readonly)

Returns the value of attribute rpc.



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

def rpc
  @rpc
end

Instance Method Details

#asset_from_contract(contract) ⇒ Object



22
23
24
25
# File 'lib/mvm/registry.rb', line 22

def asset_from_contract(contract)
  hex = @rpc.call(@registry, 'assets', contract).to_s(16)
  MixinBot::UUID.new(hex:).unpacked
end

#contract_from_asset(asset_id) ⇒ Object



46
47
48
# File 'lib/mvm/registry.rb', line 46

def contract_from_asset(asset_id)
  @rpc.call @registry, 'contracts', asset_id.gsub('-', '').to_i(16)
end

#contract_from_multisig(user_ids, threshold) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mvm/registry.rb', line 54

def contract_from_multisig(user_ids, threshold)
  bytes = []
  bytes += MixinBot.utils.encode_uint16(user_ids.length)
  bytes += [user_ids.sort.join.gsub('-', '')].pack('H*').bytes
  bytes += MixinBot.utils.encode_uint16(threshold)

  hash = Eth::Util.bin_to_prefixed_hex(Eth::Util.keccak256(bytes.pack('C*')))
  @rpc.call @registry, 'contracts', hash.to_i(16)
end

#contract_from_user(user_id) ⇒ Object



50
51
52
# File 'lib/mvm/registry.rb', line 50

def contract_from_user(user_id)
  contract_from_multisig [user_id], 1
end

#pidObject



13
14
15
16
# File 'lib/mvm/registry.rb', line 13

def pid
  hex = @rpc.call(@registry, 'PID').to_s(16)
  MixinBot::UUID.new(hex:).unpacked
end

#user_from_contract(contract) ⇒ Object



41
42
43
44
# File 'lib/mvm/registry.rb', line 41

def user_from_contract(contract)
  group = users_from_contract contract
  group[:members].first
end

#users_from_contract(contract) ⇒ Object



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

def users_from_contract(contract)
  bytes = @rpc.call(@registry, 'users', contract).bytes
  members = []
  length = bytes.shift(2).reverse.pack('C*').unpack1('S*')
  length.times do
    members << MixinBot::UUID.new(raw: bytes.shift(16).pack('C*')).unpacked
  end
  threshold = bytes.shift(2).reverse.pack('C*').unpack1('S*')
  {
    members:,
    threshold:
  }.with_indifferent_access
end

#versionObject



18
19
20
# File 'lib/mvm/registry.rb', line 18

def version
  @rpc.call @registry, 'VERSION'
end