Module: Vendi::Minter

Included in:
Machine
Defined in:
lib/vendi/minter.rb

Overview

helper methods for minting NFT

Instance Method Summary collapse

Instance Method Details

#asset_name(asset_name) ⇒ Object

encode string asset_name to hex representation



8
9
10
# File 'lib/vendi/minter.rb', line 8

def asset_name(asset_name)
  asset_name.unpack1('H*')
end

#construct_sign_submit(wid, pass, metadata, mint_payload) ⇒ Object

Construct -> Sign -> Submit transaction



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vendi/minter.rb', line 85

def construct_sign_submit(wid, pass, , mint_payload)
  tx_constructed = @cw.shelley.transactions.construct(wid,
                                                      nil,
                                                      nil,
                                                      ,
                                                      nil,
                                                      mint_payload,
                                                      nil)
  # puts tx_constructed.request.options[:body]
  tx_signed = @cw.shelley.transactions.sign(wid, pass, tx_constructed['transaction'])
  # puts tx_signed
   = @cw.shelley.transactions.submit(wid, tx_signed['transaction'])
  # puts tx_submitted

  [tx_constructed, tx_signed, ]
end

#keys_to_mint(tx_amt, price, vend_max, collection_name) ⇒ Object

get list of keys of metadata to be minted



14
15
16
17
18
# File 'lib/vendi/minter.rb', line 14

def keys_to_mint(tx_amt, price, vend_max, collection_name)
  nfts = (collection_name)
  to_mint = (tx_amt / price) > vend_max.to_i ? vend_max.to_i : (tx_amt / price)
  nfts.keys.sample(to_mint)
end

#metadatas(keys, collection_name) ⇒ Object

get metadata for keys to be minted



22
23
24
25
26
27
# File 'lib/vendi/minter.rb', line 22

def metadatas(keys, collection_name)
  nfts = (collection_name)
  keys.to_h do |key|
    [key, nfts[key]]
  end
end

#mint_nft(collection_name, tx_amt, vend_max, dest_address) ⇒ Object

Mint NFT



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vendi/minter.rb', line 103

def mint_nft(collection_name, tx_amt, vend_max, dest_address)
  c = config(collection_name)
  wid = c[:wallet_id]
  pass = c[:wallet_pass]
  policy_id = c[:wallet_policy_id]
  price = c[:price]
  keys = keys_to_mint(tx_amt, price, vend_max, collection_name)
  @logger.info "Minting #{keys.size} NFT(s): #{keys} to #{dest_address}"
   = (keys, collection_name, policy_id)
  mint_payload = mint_payload(keys, dest_address, 1)
  tx_res = construct_sign_submit(wid, pass, , mint_payload)
  { keys: keys, tx_res: tx_res }
end

#mint_payload(keys, address, quantity = 1) ⇒ Object

Build mint payload for construct tx



75
76
77
78
79
80
81
82
# File 'lib/vendi/minter.rb', line 75

def mint_payload(keys, address, quantity = 1)
  keys.map do |key|
    { 'operation' => { 'mint' => { 'quantity' => quantity,
                                   'receiving_address' => address } },
      'policy_script_template' => Vendi::POLICY_SCRIPT_TEMPLATE,
      'asset_name' => asset_name(key.to_s) }
  end
end

#outgoing_tx_ok?(tx_res) ⇒ Boolean

check if NFT mint transaction is successful

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/vendi/minter.rb', line 119

def outgoing_tx_ok?(tx_res)
  tx_constructed, tx_signed,  = tx_res
  tx_constructed.code == 202 && tx_signed.code == 202 && .code == 202
end

#prepare_metadata(keys, collection_name, policy_id) ⇒ Object

prepare metadata for minting



31
32
33
34
35
36
37
# File 'lib/vendi/minter.rb', line 31

def (keys, collection_name, policy_id)
  {
    '721' => {
      policy_id => metadatas(keys, collection_name)
    }
  }
end

#update_failed_mints(collection_name, tx_id, reason, keys) ⇒ Object

update failed mints



63
64
65
66
67
68
69
70
71
72
# File 'lib/vendi/minter.rb', line 63

def update_failed_mints(collection_name, tx_id, reason, keys)
  failed_mints_file = failed_mints_path(collection_name)
  if File.exist? failed_mints_file
    failed_mints = from_json(failed_mints_file)
    failed_mints[tx_id] = [reason, keys]
    to_json(failed_mints_file, failed_mints)
  else
    to_json(failed_mints_file, tx_id => [reason, keys])
  end
end

#update_metadata_files(keys, collection_name) ⇒ Object

update metadata files after minting



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vendi/minter.rb', line 41

def (keys, collection_name)
   = (collection_name)
   = (collection_name)
  # metadata sent
  m = metadatas(keys, collection_name)
  if File.exist? 
    sent = from_json()
    sent.merge!(m)
    to_json(, sent)
  else
    to_json(, m)
  end
  # metadata available
  nfts = (collection_name)
  keys.each do |key|
    nfts.delete(key)
  end
  to_json(, nfts)
end

#wait_for_tx_in_ledger(wid, tx_id) ⇒ Object

wait for NFT to be minted



126
127
128
129
130
131
132
# File 'lib/vendi/minter.rb', line 126

def wait_for_tx_in_ledger(wid, tx_id)
  eventually "Tx #{tx_id} is in ledger" do
    @logger.info "Waiting for #{tx_id} to get in_ledger"
    tx = @cw.shelley.transactions.get(wid, tx_id)
    tx.code == 200 && tx['status'] == 'in_ledger'
  end
end