Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency contract for e2e tests #3040

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/e2e/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ gem 'cardano_wallet', '~> 0.3.17'
gem 'bip_mnemonic', '0.0.4'
gem 'rake', '12.3.3'
gem 'rspec', '3.10.0'
gem "mustache", '1.1.1'
gem "blake2b", '0.10.0'
gem 'mustache', '1.1.1'
gem 'blake2b', '0.10.0'
gem 'cbor', '0.5.9.6'
2 changes: 2 additions & 0 deletions test/e2e/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ GEM
blake2b (0.10.0)
cardano_wallet (0.3.17)
httparty (~> 0.18.0)
cbor (0.5.9.6)
diff-lcs (1.4.4)
httparty (0.18.1)
mime-types (~> 3.0)
Expand Down Expand Up @@ -36,6 +37,7 @@ DEPENDENCIES
bip_mnemonic (= 0.0.4)
blake2b (= 0.10.0)
cardano_wallet (~> 0.3.17)
cbor (= 0.5.9.6)
mustache (= 1.1.1)
rake (= 12.3.3)
rspec (= 3.10.0)
Expand Down
Binary file modified test/e2e/fixtures/fixture_wallets.json.gpg
Binary file not shown.
22 changes: 22 additions & 0 deletions test/e2e/fixtures/plutus/currency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"transaction": "84a50081825820{{transactionId}}{{transactionIdxHex}}018002000d8009a1581c{{policyId}}a245617066656c1903e84662616e616e6101a20381590cd0{{policy}}0480f5f6",
"inputs": [
{
"id": "{{transactionId}}",
"index": {{transactionIdx}},
"address": "{{address}}",
"amount": {
"quantity": {{amount}},
"unit": "lovelace"
},
"assets": []
}
],
"redeemers": [
{
"purpose": "minting",
"policy_id": "{{policyId}}",
"data": "D87980"
}
]
}
1 change: 1 addition & 0 deletions test/e2e/fixtures/plutus/currency_policy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
590ccd0100003323233322232332233322233223322332232323232323233223232323233332222323232323322323332223233322232333333332222222232323333222232323322323332223232323322332232323333322222332233223322332233223322222323253353061332235305b00722353550620022235300c005223305b33058491024330003322333553055120013505e505c235355069001223335530581200135061505f23535506c0012233353550550012330714800000488cc1c80080048cc1c400520000013304c002001332235306300122333223355002001200130571200133506222335530581200123535506d0012233550700023355305b1200123535507000122335507300233353550590012330794800000488cc1e80080048cc1e400520000013305000200150693355305d1200123535506b0012233506b335506e00733506b335506e002001506c506c00135300e00722533335301c001213504c35304a335738921024c680004b4988400484d4130d4c128cd5ce249024c680004b49884d4130d4c128cd5ce2481024c680004b498034d4c040008888888888801ccc161240102433100353010002222222222233355305d120013505b505e253353074333573466e3c03cd4c07400488d4c0e4008880081d81d44ccd5cd19b8700e35301d0012235303900222001076075107500a3333573466e1cd55ce9baa0044800081048d4108d4c100cd5ce2490350543100041499263333573466e1cd55cea8012400046601064646464646464646464646666ae68cdc39aab9d500a480008cccccccccc058cd40908c8c8cccd5cd19b8735573aa004900011980e181a1aba150023029357426ae8940088d4148d4c140cd5ce249035054310005149926135573ca00226ea8004d5d0a80519a8120129aba150093335502b75ca0546ae854020ccd540add728151aba1500733502403a35742a00c66a04866aa096086eb4d5d0a8029919191999ab9a3370e6aae7540092000233501e3232323333573466e1cd55cea80124000466a04c66a078eb4d5d0a80118209aba135744a00446a0ac6a60a866ae712401035054310005549926135573ca00226ea8004d5d0a8011919191999ab9a3370e6aae7540092000233502433503c75a6ae854008c104d5d09aba250022350563530543357389201035054310005549926135573ca00226ea8004d5d09aba250022350523530503357389201035054310005149926135573ca00226ea8004d5d0a80219a8123ae35742a00666a04866aa096eb88004d5d0a80118199aba135744a00446a09c6a609866ae71241035054310004d49926135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135573ca00226ea8004d5d0a8011919191999ab9a3370ea00290031180d98191aba135573ca00646666ae68cdc3a801240084603460786ae84d55cf280211999ab9a3370ea00690011180d18151aba135573ca00a46666ae68cdc3a802240004603a6eb8d5d09aab9e50062350493530473357389201035054310004849926499264984d55cea80089baa001357426ae8940088d4108d4c100cd5ce249035054310004149926104013504135303f335738920103505435000404984d55cf280089baa0012212330010030022001222222222212333333333300100b00a00900800700600500400300220012212330010030022001122123300100300212001122123300100300212001122123300100300212001212222300400521222230030052122223002005212222300100520011232230023758002640026aa09e446666aae7c004941288cd4124c010d5d080118019aba200202c23232323333573466e1cd55cea801a4000466600e6464646666ae68cdc39aab9d5002480008cc034c0acd5d0a80119a8080139aba135744a00446a0646a606066ae712401035054310003149926135573ca00226ea8004d5d0a801999aa805bae500a35742a00466a018eb8d5d09aba2500223502e35302c335738921035054310002d49926135744a00226aae7940044dd50009110919980080200180110009109198008018011000899aa800bae75a224464460046eac004c8004d5412488c8cccd55cf80112822919a82219aa82398031aab9d5002300535573ca00460086ae8800c09c4d5d080089119191999ab9a3370ea002900011a80418029aba135573ca00646666ae68cdc3a801240044a01046a0506a604c66ae712410350543100027499264984d55cea80089baa001121223002003112200112001232323333573466e1cd55cea8012400046600c600e6ae854008dd69aba135744a00446a0446a604066ae71241035054310002149926135573ca00226ea80048848cc00400c00880048c8cccd5cd19b8735573aa002900011bae357426aae7940088d4078d4c070cd5ce2481035054310001d499261375400224464646666ae68cdc3a800a40084a00e46666ae68cdc3a8012400446a014600c6ae84d55cf280211999ab9a3370ea00690001280511a8109a980f99ab9c490103505431000204992649926135573aa00226ea8004484888c00c0104488800844888004480048c8cccd5cd19b8750014800880e88cccd5cd19b8750024800080e88d4064d4c05ccd5ce2490350543100018499264984d55ce9baa001232323232323333573466e1d4005200c200b23333573466e1d4009200a200d23333573466e1d400d200823300b375c6ae854014dd69aba135744a00a46666ae68cdc3a8022400c46601a6eb8d5d0a8039bae357426ae89401c8cccd5cd19b875005480108cc048c050d5d0a8049bae357426ae8940248cccd5cd19b875006480088c050c054d5d09aab9e500b23333573466e1d401d2000230133016357426aae7940308d4084d4c07ccd5ce2481035054310002049926499264992649926135573aa00826aae79400c4d55cf280109aab9e500113754002424444444600e01044244444446600c012010424444444600a010244444440082444444400644244444446600401201044244444446600201201040024646464646666ae68cdc3a800a400446660106eb4d5d0a8021bad35742a0066eb4d5d09aba2500323333573466e1d400920002300a300b357426aae7940188d4048d4c040cd5ce2490350543100011499264984d55cea80189aba25001135573ca00226ea80048488c00800c888488ccc00401401000c80048c8c8cccd5cd19b875001480088c018dd71aba135573ca00646666ae68cdc3a80124000460106eb8d5d09aab9e500423500c35300a3357389201035054310000b499264984d55cea80089baa001212230020032122300100320011122232323333573466e1cd55cea80124000466aa04e600c6ae854008c014d5d09aba25002235009353007335738921035054310000849926135573ca00226ea800448004800449888cd54c028480048d4d5407c00488cd54088008ccd4d540200048cd54c038480048d4d5408c00488cd54098008d5403c00400488ccd5540200500080048cd54c038480048d4d5408c00488cd54098008d54034004004ccd55400c03c008004444888ccd54c010480054070cd54c028480048d4d5407c00488cd54088008d5402c004ccd54c0104800488d4d54080008894cd4c094ccd54c03c48004d403540408d4d5408c00488cc028008014018400c4cd408001000d4074004cd54c028480048d4d5407c00488c8cd5408c00cc004014c8004d540a0894cd4d40800044d5402c00c884d4d54094008894cd4c0a8cc0300080204cd5404001c0044c01800c008c8004d5408488448894cd4d40700044008884cc014008ccd54c01c480040140100044484888c00c01044884888cc0080140104484888c00401044800448cd403888ccd4d401c00c88008008004d4d401400488004c8004d5406c8844894cd4d40540045405c884cd4060c010008cd54c01848004010004c8004d5406888448894cd4d40540044d4d403c00c88004884ccd4d404401488008c010008ccd54c01c480040140100044cd4008894cd4c05c0084064400405848848cc00400c0084800488ccd5cd19b8f002001016015225335301300110151335738004028640026aa028442244a66a6a01c0022a02044266a022600800466aa600c24002008002266a00c00202244a66a6020004200220222466a00644666a6a00c006440040040026a6a008002440022442466002006004240022442466002006004240024424660020060044002224400424424466002008006240022244246600200600422400244666ae68cdc380100080280209100109100090009119b800020011123230010012233003300200200133223332223322330023355007488120{{transactionId}}0048{{encodedTransactionIdx}}d4010cd5401d220105617066656c00483403ccd4010cd5401d22010662616e616e61004800940148848cc00400c00880044488008488488cc00401000c48004448848cc00400c008448005
18 changes: 18 additions & 0 deletions test/e2e/helpers/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def hex_to_bytes(s)
s.scan(/../).map { |x| x.hex.chr }.join
end

def binary_to_hex(binary_as_string)
"%02x" % binary_as_string.to_i(2)
end

##
# encode string asset_name to hex representation
def asset_name(asset_name)
asset_name.unpack("H*").first
end

def absolute_path(path)
if path.start_with? "."
File.join(Dir.pwd, path[1..-1])
Expand Down Expand Up @@ -141,3 +151,11 @@ def get_latest_configs_base_url
end
end
end

##
# extend String class with hexdump methods
class String
def cbor_to_hex
bytes.map{|x| "%02x" % x}.join
end
end
74 changes: 72 additions & 2 deletions test/e2e/spec/e2e_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def run_contract(contract_setup, scripts)
policy_id = get_policy_id(policy)
mint_script = "mintBurn_1.json"
burn_script = "mintBurn_2.json"
assets = [{"asset_name" => "6d696e742d6275726e",
assets = [{"asset_name" => asset_name("mint-burn"),
"quantity" => 1,
"policy_id" => policy_id}]

payload_mint = get_templated_plutus_tx(mint_script,{vkHash: vkHash,
policyId: policy_id,
policy: policy})
Expand Down Expand Up @@ -210,6 +210,76 @@ def run_contract(contract_setup, scripts)
expect(src_after['total']).to eq (init_src['total'] - fee)
end

it "currency" do
currency_script = "currency.json"
currency_policy = "currency_policy"

# Perform coin selection to select input to be used in minting contract
address = SHELLEY.addresses.list(@wid)[0]['id']
payload_cs = [{ :address => address,
:amount => { :quantity => 1000000000, :unit => "lovelace" }}
]
coin_selection = SHELLEY.coin_selections.random(@wid, payload_cs)
input = coin_selection['inputs'].select{|i| i['assets'] == []}.first
tx_id = input['id']
tx_idx = input['index'].to_i
amount = input['amount']['quantity'].to_i
address = input['address']

# encode input indexes for contract payload
tx_idx_hex = tx_idx.to_cbor.cbor_to_hex # cbor as hex
encoded_tx_idx = plutus_encode_idx(tx_idx) # special Plutus bit-wise encoding

# feed payload for contract with data from coin selection
policy = read_mustached_file(currency_policy, {transactionId: tx_id,
encodedTransactionIdx: encoded_tx_idx})
policy_id = get_policy_id(policy)
payload = get_templated_plutus_tx(currency_script, {policy: policy,
policyId: policy_id,
transactionId: tx_id,
transactionIdx: tx_idx,
transactionIdxHex: tx_idx_hex,
amount: amount,
address: address})

# run contract
r = run_script(currency_script, payload)

# expected minted currency
apfel = {"policy_id" => policy_id,
"asset_name" => asset_name("apfel"),
"quantity" => 1000}
banana = {"policy_id" => policy_id,
"asset_name" => asset_name("banana"),
"quantity" => 1}

# verify decoded transactions show that currency will be minted
expect(r[:tx_unbalanced]['assets_minted']).to eq [apfel, banana]
expect(r[:tx_balanced]['assets_minted']).to eq [apfel, banana]

# make sure currency is minted as expected
src_balance = get_shelley_balances(@wid)
expect(src_balance['assets_total']).to include(apfel)
expect(src_balance['assets_total']).to include(banana)

# send out minted currency to special address not to litter fixture wallet
payment = [{ :address => "addr_test1qqkgrywfhejgd67twkzqmx84rsr3v374pzujd5rlm0e8exnlxjupjgrqwk5dk9tard6zfwwjq4lc89szs2w599js35tqmaykuj",
:amount => { :quantity => 0, :unit => 'lovelace' },
:assets => [apfel, banana]
}]
tx_constructed = SHELLEY.transactions.construct(@wid, payment)
tx_signed = SHELLEY.transactions.sign(@wid, PASS, tx_constructed['transaction'])
tx_submitted = PROXY.submit_external_transaction(Base64.decode64(tx_signed['transaction']))
tx_id = tx_submitted['id']

wait_for_tx_in_ledger(@wid, tx_id)

src_balance_after = get_shelley_balances(@wid)
expect(src_balance_after['assets_total']).not_to include(apfel)
expect(src_balance_after['assets_total']).not_to include(banana)

end

end

describe "E2E Construct -> Sign -> Submit" do
Expand Down
35 changes: 34 additions & 1 deletion test/e2e/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "base64"
require "blake2b"
require "mustache"
require "cbor"
require_relative "../env"
require_relative "../helpers/utils"
require_relative "../helpers/matchers"
Expand Down Expand Up @@ -69,7 +70,7 @@
}
},
{ "policy_id" => "919e8a1922aaa764b1d66407c6f62244e77081215f385b60a6209149",
"asset_name" => "4861707079436f696e",
"asset_name" => asset_name("HappyCoin"),
"fingerprint" => "asset19mwamgpre24at3z34v2e5achszlhhqght9djqp",
"metadata" => { "name" => "HappyCoin",
"description" => "Coin with asset name - and everyone is happy!!!",
Expand Down Expand Up @@ -371,6 +372,38 @@ def wait_for_tx_in_ledger(wid, tx_id)
## Plutus helpers
PLUTUS_DIR = "fixtures/plutus"

##
# Encode input index the way Plutus does in hex-encoded CBOR script
# _probably_ should work fine for 0-127 index range
# @param [Int] input idx
# @return [Hex] hex encoded input idx imitating Plutus bit-wise, non-standard encoding
#
# @example
# > plutus_encode_idx(43)
# => "158c"
#
# @see lib/core-integration/aux/Plutus/FlatInteger.hs
# reference Haskell impl of encoding Int into sequence of bits
# @see lib/core-integration/src/Test/Integration/Plutus.hs
# the way it's done in the integration tests
# ```
# idxEncoded = toHex $ BS.pack $ Bits.asBytes
# $ toBits "00" <> Bits.bits (fromIntegral idx :: Integer) <> toBits "001100"
# ```
def plutus_encode_idx(int)
raise "Not supported index. (0-127) are supported." if int > 127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"7 bits are enough for everyone" — Bill Gates
😂

# convert int to binary and add trailing bit
b = int.to_s(2) + "0"
# add additional leading bits so it is 8-bit long
b = "0" * (8 - b.length) + b
# add additional leading and trailing bits
b = "00" + b + "001100"
# convert to hex and add leading 0's if needed (so it is 4 digit long)
h = binary_to_hex(b)
h = "0" * (4 - h.length) + h
h
end

##
# Balance -> Sign -> Submit
def balance_sign_submit(wid, payload)
Expand Down