|
| 1 | +import pathlib |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import pytest |
| 5 | +from retry import retry |
| 6 | + |
| 7 | +from pycardano import * |
| 8 | + |
| 9 | +from .base import TEST_RETRIES, TestBase |
| 10 | + |
| 11 | + |
| 12 | +class TestZeroEmptyAsset(TestBase): |
| 13 | + @retry(tries=TEST_RETRIES, backoff=1.5, delay=6, jitter=(0, 4)) |
| 14 | + @pytest.mark.post_chang |
| 15 | + def test_submit_zero_and_empty(self): |
| 16 | + address = Address(self.payment_vkey.hash(), network=self.NETWORK) |
| 17 | + |
| 18 | + # Load payment keys or create them if they don't exist |
| 19 | + def load_or_create_key_pair(base_dir, base_name): |
| 20 | + skey_path = base_dir / f"{base_name}.skey" |
| 21 | + vkey_path = base_dir / f"{base_name}.vkey" |
| 22 | + |
| 23 | + if skey_path.exists(): |
| 24 | + skey = PaymentSigningKey.load(str(skey_path)) |
| 25 | + vkey = PaymentVerificationKey.from_signing_key(skey) |
| 26 | + else: |
| 27 | + key_pair = PaymentKeyPair.generate() |
| 28 | + key_pair.signing_key.save(str(skey_path)) |
| 29 | + key_pair.verification_key.save(str(vkey_path)) |
| 30 | + skey = key_pair.signing_key |
| 31 | + vkey = key_pair.verification_key |
| 32 | + return skey, vkey |
| 33 | + |
| 34 | + tempdir = tempfile.TemporaryDirectory() |
| 35 | + PROJECT_ROOT = tempdir.name |
| 36 | + |
| 37 | + root = pathlib.Path(PROJECT_ROOT) |
| 38 | + # Create the directory if it doesn't exist |
| 39 | + root.mkdir(parents=True, exist_ok=True) |
| 40 | + """Generate keys""" |
| 41 | + key_dir = root / "keys" |
| 42 | + key_dir.mkdir(exist_ok=True) |
| 43 | + |
| 44 | + # Generate policy keys, which will be used when minting NFT |
| 45 | + policy_skey, policy_vkey = load_or_create_key_pair(key_dir, "policy") |
| 46 | + |
| 47 | + """Create policy""" |
| 48 | + # A policy that requires a signature from the policy key we generated above |
| 49 | + pub_key_policy_1 = ScriptPubkey(policy_vkey.hash()) |
| 50 | + |
| 51 | + # A policy that requires a signature from the extended payment key |
| 52 | + pub_key_policy_2 = ScriptPubkey(self.extended_payment_vkey.hash()) |
| 53 | + |
| 54 | + # Combine two policies using ScriptAll policy |
| 55 | + policy = ScriptAll([pub_key_policy_1, pub_key_policy_2]) |
| 56 | + |
| 57 | + # Calculate policy ID, which is the hash of the policy |
| 58 | + policy_id = policy.hash() |
| 59 | + |
| 60 | + """Define NFT""" |
| 61 | + my_nft = MultiAsset.from_primitive( |
| 62 | + { |
| 63 | + policy_id.payload: { |
| 64 | + b"MY_NFT_1": 1, # Name of our NFT1 # Quantity of this NFT |
| 65 | + b"MY_NFT_2": 1, # Name of our NFT2 # Quantity of this NFT |
| 66 | + } |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + native_scripts = [policy] |
| 71 | + |
| 72 | + """Create metadata""" |
| 73 | + # We need to create a metadata for our NFTs, so they could be displayed correctly by blockchain explorer |
| 74 | + metadata = { |
| 75 | + 721: { # 721 refers to the metadata label registered for NFT standard here: |
| 76 | + # https://github.com/cardano-foundation/CIPs/blob/master/CIP-0010/registry.json#L14-L17 |
| 77 | + policy_id.payload.hex(): { |
| 78 | + "MY_NFT_1": { |
| 79 | + "description": "This is my first NFT thanks to PyCardano", |
| 80 | + "name": "PyCardano NFT example token 1", |
| 81 | + "id": 1, |
| 82 | + "image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw", |
| 83 | + }, |
| 84 | + "MY_NFT_2": { |
| 85 | + "description": "This is my second NFT thanks to PyCardano", |
| 86 | + "name": "PyCardano NFT example token 2", |
| 87 | + "id": 2, |
| 88 | + "image": "ipfs://QmRhTTbUrPYEw3mJGGhQqQST9k86v1DPBiTTWJGKDJsVFw", |
| 89 | + }, |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + # Place metadata in AuxiliaryData, the format acceptable by a transaction. |
| 95 | + auxiliary_data = AuxiliaryData(AlonzoMetadata(metadata=Metadata(metadata))) |
| 96 | + |
| 97 | + """Build mint transaction""" |
| 98 | + |
| 99 | + # Create a transaction builder |
| 100 | + builder = TransactionBuilder(self.chain_context) |
| 101 | + |
| 102 | + # Add our own address as the input address |
| 103 | + builder.add_input_address(address) |
| 104 | + |
| 105 | + # Set nft we want to mint |
| 106 | + builder.mint = my_nft |
| 107 | + |
| 108 | + # Set native script |
| 109 | + builder.native_scripts = native_scripts |
| 110 | + |
| 111 | + # Set transaction metadata |
| 112 | + builder.auxiliary_data = auxiliary_data |
| 113 | + |
| 114 | + # Calculate the minimum amount of lovelace that need to hold the NFT we are going to mint |
| 115 | + min_val = min_lovelace_pre_alonzo(Value(0, my_nft), self.chain_context) |
| 116 | + |
| 117 | + # Send the NFT to our own address |
| 118 | + nft_output = TransactionOutput(address, Value(min_val, my_nft)) |
| 119 | + builder.add_output(nft_output) |
| 120 | + |
| 121 | + # Build and sign transaction |
| 122 | + signed_tx = builder.build_and_sign( |
| 123 | + [self.payment_skey, self.extended_payment_skey, policy_skey], address |
| 124 | + ) |
| 125 | + |
| 126 | + print("############### Transaction created ###############") |
| 127 | + print(signed_tx) |
| 128 | + print(signed_tx.to_cbor_hex()) |
| 129 | + |
| 130 | + # Submit signed transaction to the network |
| 131 | + print("############### Submitting transaction ###############") |
| 132 | + self.chain_context.submit_tx(signed_tx) |
| 133 | + |
| 134 | + self.assert_output(address, nft_output) |
| 135 | + |
| 136 | + """Build transaction with 0 nft""" |
| 137 | + |
| 138 | + # Create a transaction builder |
| 139 | + builder = TransactionBuilder(self.chain_context) |
| 140 | + |
| 141 | + # Add our own address as the input address |
| 142 | + builder.add_input_address(address) |
| 143 | + |
| 144 | + # Calculate the minimum amount of lovelace that need to hold the NFT we are going to mint |
| 145 | + min_val = min_lovelace_pre_alonzo(Value(0), self.chain_context) |
| 146 | + |
| 147 | + # Send the NFT to our own address |
| 148 | + nft_output = TransactionOutput( |
| 149 | + address, |
| 150 | + Value( |
| 151 | + min_val, |
| 152 | + MultiAsset.from_primitive( |
| 153 | + {policy_vkey.hash().payload: {b"MY_NFT_1": 0}} |
| 154 | + ), |
| 155 | + ), |
| 156 | + ) |
| 157 | + builder.add_output(nft_output) |
| 158 | + |
| 159 | + # Build and sign transaction |
| 160 | + signed_tx = builder.build_and_sign( |
| 161 | + [self.payment_skey, self.extended_payment_skey], address |
| 162 | + ) |
| 163 | + |
| 164 | + print("############### Transaction created ###############") |
| 165 | + print(signed_tx) |
| 166 | + print(signed_tx.to_cbor_hex()) |
| 167 | + |
| 168 | + # Submit signed transaction to the network |
| 169 | + print("############### Submitting transaction ###############") |
| 170 | + self.chain_context.submit_tx(signed_tx) |
| 171 | + |
| 172 | + self.assert_output(address, nft_output) |
| 173 | + |
| 174 | + """Build transaction with empty multi-asset""" |
| 175 | + |
| 176 | + # Create a transaction builder |
| 177 | + builder = TransactionBuilder(self.chain_context) |
| 178 | + |
| 179 | + # Add our own address as the input address |
| 180 | + builder.add_input_address(address) |
| 181 | + |
| 182 | + # Calculate the minimum amount of lovelace that need to hold the NFT we are going to mint |
| 183 | + min_val = min_lovelace_pre_alonzo(Value(0), self.chain_context) |
| 184 | + |
| 185 | + # Send the NFT to our own address |
| 186 | + nft_output = TransactionOutput( |
| 187 | + address, |
| 188 | + Value(min_val, MultiAsset.from_primitive({policy_vkey.hash().payload: {}})), |
| 189 | + ) |
| 190 | + builder.add_output(nft_output) |
| 191 | + |
| 192 | + # Build and sign transaction |
| 193 | + signed_tx = builder.build_and_sign( |
| 194 | + [self.payment_skey, self.extended_payment_skey], address |
| 195 | + ) |
| 196 | + |
| 197 | + print("############### Transaction created ###############") |
| 198 | + print(signed_tx) |
| 199 | + print(signed_tx.to_cbor_hex()) |
| 200 | + |
| 201 | + # Submit signed transaction to the network |
| 202 | + print("############### Submitting transaction ###############") |
| 203 | + self.chain_context.submit_tx(signed_tx) |
| 204 | + |
| 205 | + self.assert_output(address, nft_output) |
0 commit comments