|
35 | 35 | VERIFICATION_KEY_HASH_SIZE,
|
36 | 36 | PoolKeyHash,
|
37 | 37 | TransactionId,
|
| 38 | + ScriptHash, |
38 | 39 | VerificationKeyHash,
|
39 | 40 | )
|
40 | 41 | from pycardano.key import VerificationKey
|
@@ -2264,3 +2265,88 @@ def test_burning_all_assets_under_single_policy(chain_context):
|
2264 | 2265 |
|
2265 | 2266 | assert AssetName(b"AssetName3") not in multi_asset.get(policy_id_1, {})
|
2266 | 2267 | assert AssetName(b"AseetName4") not in multi_asset.get(policy_id_1, {})
|
| 2268 | + |
| 2269 | + |
| 2270 | +def test_token_transfer_with_change(chain_context): |
| 2271 | + """Test token transfer with change address handling. |
| 2272 | + |
| 2273 | + Replicates issue where transaction fails with 'Input UTxOs depleted' when: |
| 2274 | + - Input 1: 4 ADA |
| 2275 | + - Input 2: ~1.03 ADA + 1,876,083 tokens |
| 2276 | + - Output: ~1.32 ADA + 382 tokens |
| 2277 | + - Expected change should handle remaining ADA and tokens |
| 2278 | + """ |
| 2279 | + # Create the vault address that holds tokens |
| 2280 | + vault_address = Address.from_primitive( |
| 2281 | + "addr_test1vrs324jltsc0ssuptpa5ngpfk89cps92xa99a2t6vlg6kdqtm5qnv" |
| 2282 | + ) |
| 2283 | + |
| 2284 | + # Create receiver address |
| 2285 | + receiver_address = Address.from_primitive( |
| 2286 | + "addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x" |
| 2287 | + ) |
| 2288 | + |
| 2289 | + # Create token details |
| 2290 | + token_policy_id = ScriptHash(bytes.fromhex("1f847bb9ac60e869780037c0510dbd89f745316db7ec4fee81ff1e97")) |
| 2291 | + token_name = AssetName(b"dux_1") |
| 2292 | + |
| 2293 | + # Create the two input UTXOs and patch chain_context.utxos |
| 2294 | + with patch.object(chain_context, "utxos") as mock_utxos: |
| 2295 | + mock_utxos.return_value = [ |
| 2296 | + UTxO( |
| 2297 | + TransactionInput.from_primitive([ |
| 2298 | + "e11efc26f94a3cbf724dc052c43abf36f7a631a831acc6d783f1c9c8c52725c5", |
| 2299 | + 0 |
| 2300 | + ]), |
| 2301 | + TransactionOutput( |
| 2302 | + vault_address, |
| 2303 | + Value( |
| 2304 | + 1038710, # ~1.03 ADA |
| 2305 | + MultiAsset.from_primitive({ |
| 2306 | + token_policy_id.payload: { |
| 2307 | + b"dux_1": 1876083 # 1,876,083 tokens |
| 2308 | + } |
| 2309 | + }) |
| 2310 | + ) |
| 2311 | + ) |
| 2312 | + ) |
| 2313 | + ] |
| 2314 | + |
| 2315 | + # Create transaction builder |
| 2316 | + tx_builder = TransactionBuilder(chain_context) |
| 2317 | + |
| 2318 | + # Add inputs - using add_input_address for the vault input |
| 2319 | + tx_builder.add_input_address(vault_address) |
| 2320 | + tx_builder.add_input(UTxO( |
| 2321 | + TransactionInput.from_primitive([b"1" * 32, 0]), |
| 2322 | + TransactionOutput(receiver_address, Value(4000000)) # 4 ADA input |
| 2323 | + )) |
| 2324 | + |
| 2325 | + # Add output for receiver |
| 2326 | + output_value = Value( |
| 2327 | + 1326255, # ~1.32 ADA |
| 2328 | + MultiAsset.from_primitive({ |
| 2329 | + token_policy_id.payload: { |
| 2330 | + b"dux_1": 382 # 382 tokens |
| 2331 | + } |
| 2332 | + }) |
| 2333 | + ) |
| 2334 | + tx_builder.add_output(TransactionOutput(receiver_address, output_value)) |
| 2335 | + |
| 2336 | + # Build transaction with change going back to vault |
| 2337 | + tx = tx_builder.build(change_address=vault_address, merge_change=True) |
| 2338 | + |
| 2339 | + # Verify the transaction outputs |
| 2340 | + assert len(tx.outputs) == 2 # One for receiver, one for change |
| 2341 | + |
| 2342 | + # Verify receiver output |
| 2343 | + receiver_output = tx.outputs[0] |
| 2344 | + assert receiver_output.address == receiver_address |
| 2345 | + assert receiver_output.amount.coin == 1326255 |
| 2346 | + assert receiver_output.amount.multi_asset[token_policy_id][token_name] == 382 |
| 2347 | + |
| 2348 | + # Verify change output |
| 2349 | + change_output = tx.outputs[1] |
| 2350 | + assert change_output.address == vault_address |
| 2351 | + assert change_output.amount.coin == 4000000 + 1038710 - 1326255 - tx.fee |
| 2352 | + assert change_output.amount.multi_asset[token_policy_id][token_name] == 1876083 - 382 |
0 commit comments