From 65a87b86715fc1a90ecbd64930a5d59bfecc4b36 Mon Sep 17 00:00:00 2001 From: lazynina Date: Sun, 13 Feb 2022 12:32:50 -0500 Subject: [PATCH] update deso.ros to include coin change in transfer operations --- rosetta-cli-conf/testnet/deso.ros | 68 +++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/rosetta-cli-conf/testnet/deso.ros b/rosetta-cli-conf/testnet/deso.ros index a4bc3cd7..7213ad91 100644 --- a/rosetta-cli-conf/testnet/deso.ros +++ b/rosetta-cli-conf/testnet/deso.ros @@ -39,26 +39,46 @@ transfer(10){ max_fee_amount = "1200"; send_buffer = {{dust_amount}} + {{max_fee_amount}}; - sender = { - "account_identifier": { - "address": "tBCKWCLyYKGa4Lb4buEsMGYePEWcaVAqcunvDVD4zVDcH8NoB5EgPF" - } - }; + // We look for a coin of value >= the reserved_amount to create + // a transfer with change (reserved_amount is max_fee_amount + dust_amount x 2). + reserved_amount = "2400"; + sender = find_balance({ + "minimum_balance":{ + "value": {{reserved_amount}}, + "currency": {{currency}} + }, + "require_coin": true + }); - // Send between dust_amount and 10 DESO + // The amount we send to the recipient is a random value + // between the dust_amount and the value of the entire coin (minus + // the amount reserved for fee payment and covering the dust minimum + // of the change UTXO). + receivable_amount = {{sender.balance.value}} - {{send_buffer}}; recipient_amount = random_number({ "minimum": {{dust_amount}}, - "maximum": "10000000000" + "maximum": {{receivable_amount}} }); print_message({ "recipient_amount":{{recipient_amount}} }); + // The change amount is what we aren't sending to the recipient + // minus the maximum fee. Don't worry, we will adjust this + // amount to avoid overpaying the fee after the dry run + // completes. + raw_change_amount = {{sender.balance.value}} - {{recipient_amount}}; + change_amount = {{raw_change_amount}} - {{max_fee_amount}}; + print_message({ + "change_amount":{{change_amount}} + }); + // The last thing we need to do before creating the transaction // is to find a recipient with a *types.AccountIdentifier that // is not equal to the sender. recipient = find_balance({ "not_account_identifier":[{{sender.account_identifier}}], + "not_coins":[{{sender.coin}}], "minimum_balance":{ "value": "0", "currency": {{currency}} @@ -67,8 +87,7 @@ transfer(10){ "create_probability": 50 }); - sender_amount = 0 - {{recipient_amount}}; - sender_amount -= {{send_buffer}}; + sender_amount = 0 - {{sender.balance.value}}; transfer_dry_run.confirmation_depth = "1"; transfer_dry_run.dry_run = true; transfer_dry_run.operations = [ @@ -76,13 +95,20 @@ transfer(10){ "operation_identifier":{"index":0}, "type":"INPUT", "account":{{sender.account_identifier}}, - "amount":{"value":{{recipient_amount}},"currency":{{currency}}} + "amount":{"value":{{sender_amount}},"currency":{{currency}}}, + "coin_change":{"coin_action":"coin_spent", "coin_identifier":{{sender.coin}}} }, { "operation_identifier":{"index":1}, "type":"OUTPUT", "account":{{recipient.account_identifier}}, "amount":{"value":{{recipient_amount}},"currency":{{currency}}} + }, + { + "operation_identifier":{"index":2}, + "type":"OUTPUT", + "account":{{sender.account_identifier}}, + "amount":{"value":{{change_amount}},"currency":{{currency}}} } ]; }, @@ -97,7 +123,7 @@ transfer(10){ "suggested_fee":{{suggested_fee}} }); - output_amount = {{recipient_amount}} - {{suggested_fee.value}}; + change_amount = {{raw_change_amount}} - {{suggested_fee.value}}; transfer.network = {{transfer_dry_run.network}}; transfer.confirmation_depth = {{transfer_dry_run.confirmation_depth}}; transfer.operations = [ @@ -105,13 +131,20 @@ transfer(10){ "operation_identifier":{"index":0}, "type":"INPUT", "account":{{sender.account_identifier}}, - "amount":{"value":{{sender_amount}},"currency":{{currency}}} + "amount":{"value":{{sender_amount}},"currency":{{currency}}}, + "coin_change":{"coin_action":"coin_spent", "coin_identifier":{{sender.coin}}} }, { "operation_identifier":{"index":1}, "type":"OUTPUT", "account":{{recipient.account_identifier}}, - "amount":{"value":{{output_amount}},"currency":{{currency}}} + "amount":{"value":{{recipient_amount}},"currency":{{currency}}} + }, + { + "operation_identifier":{"index":2}, + "type":"OUTPUT", + "account":{{sender.account_identifier}}, + "amount":{"value":{{change_amount}},"currency":{{currency}}} } ]; } @@ -130,7 +163,8 @@ return_funds(10){ "minimum_balance":{ "value": {{reserved_amount}}, "currency": {{currency}} - } + }, + "require_coin": true }); // We send the maximum amount available to the recipient. Don't worry @@ -150,7 +184,8 @@ return_funds(10){ "operation_identifier":{"index":0}, "type":"INPUT", "account":{{sender.account_identifier}}, - "amount":{"value":{{sender_amount}},"currency":{{currency}}} + "amount":{"value":{{sender_amount}},"currency":{{currency}}}, + "coin_change":{"coin_action":"coin_spent", "coin_identifier":{{sender.coin}}} }, { "operation_identifier":{"index":1}, @@ -182,7 +217,8 @@ return_funds(10){ "operation_identifier":{"index":0}, "type":"INPUT", "account":{{sender.account_identifier}}, - "amount":{"value":{{sender_amount}},"currency":{{currency}}} + "amount":{"value":{{sender_amount}},"currency":{{currency}}}, + "coin_change":{"coin_action":"coin_spent", "coin_identifier":{{sender.coin}}} }, { "operation_identifier":{"index":1},