A tool for handling mass payments for Cardano (ADA)
Project description
Cardano Mass Payments
Cardano Mass Payments is a Cardano python package that automates and optimizes the sending of large numbers of transactions using a single command.
Table of contents
- Requirements
- Installation
- Features
- Package Usage
- Code for Transaction Plan Generation
- Code for Mass Payments Script Generation
- Algorithm
- Possible Errors
Requirements
The package can only be run on python >= 3.7
.
These are the minimum required versions of the required tools:
pycardano >= 0.7.0
Currently, all Script methods requires the following cardano tools:
- Cardano Node:
>=1.35.5
Installation
Install the library using pip:
pip install cardano-mass-payments
Features
- Can be used as a Python script - Mass Payments Script can be generated just by calling the
cardano-mass-payments-generate
command. - Can be used as a Python library - Mass Payments Script can be generated by incorporating the
cardano-mass-payments
library in your python code. - Fee optimization - The library groups the payments into transaction groups, making fees significantly less than the total fees when sending these payments individually.
- Support for both Cardano CLI and PyCardano - Currently,
cardano-mass-payments
supports communication to Cardano viacardano-cli
andpycardano
- Transaction plan generation - Alongside the scripts,
cardano-mass-payments
also includes generation of a file which contains the final and adjusted transaction plan which will be the basis of the mass payments script. The script also includes commands that update the transaction plan file. - Can recover and continue in case of an error during execution - Whenever an error is encountered in the script, the library can create an updated script using the updated transaction plan file (which skips already submitted transactions) and can go straight to pending transactions.
- Support for custom metadata and messages to be included in the transactions - Cardano Metadata is supported and Transaction Messages will be automatically fixed by the script to satisfy Cardano's policy regarding transaction messages.
- Structured JSON output - With JSON Output Type, script output will be on a structured JSON format, from the final output, to the error messages as well.
- Dust collection - Allows for handling Input UTxOs with less than a specified amount (dust) and combining them into one Input UTxO.
- Immediate execution - Allows for automatically executing the newly generated script from the
cardano-mass-payments-generate
command.
Package Usage
Parameter Details
Parameter | Description |
---|---|
--cardano-network |
Dictates which Cardano network the package will connect to. Possible values: - MAINNET - TESTNET |
--script-method |
Dictates which method to use for handling the internal logic (getting protocol parameters, getting user utxos). Possible values: - HOST_CLI - connects package to the Cardano CLI installed in the machine- DOCKER_CLI - connects package to the Cardano CLI Docker Image- PYCARDANO - uses Pycardano in the package |
--output-type |
Dictates the type of the generated output. Possible values: - BASH_SCRIPT - Creates a bash file and displays the file name in the console- CONSOLE - Displays the script contents in console- JSON - Display a JSON object in console containing the script file name- TRANSACTION_PLAN - Only create the transaction plan and display the file name in the console |
--sources-csv |
CSV File containing the source address details Format per line: <source_address>,<signing_key_file_of_address_1>,<signing_key_file_of_address_2>,...,<signing_key_file_of_address_n> |
--payments-csv |
CSV File containing the payment details that will be sent to Cardano. Format per line: <destination_address>,<amount_in_lovelace> |
--allowed-ttl-slots |
Number of slots allowed before the transaction to be deemed invalid. Default: 1000 |
--dust-collection-method |
Method used for dust collection. Possible values: - COLLECT_TO_SOURCE - dust will all be collected and sent to the primary source address- COLLECT_PER_ADDRESS - dust will be collected and sent per address |
--dust-collection-threshold |
Allowable amount in lovelace that will be used for checking if the transaction is deemed as a dust. Default: 10000000 |
--source-address |
Primary Source Address (should be in the --sources-csv file). If not provided, will use the first address in the --sources-csv file. |
--source-signing-key-file |
Signing Key File of Primary Source Address. If not provided, will use the corresponding signing key file of the primary source address in the --sources-csv file. |
--metadata-json-file |
JSON File containing the metadata template that will be included in the cardano transaction. |
--metadata-message-file |
TXT File containing the message that will be added in the transaction metadata. |
--transaction-plan-file |
TXT Transaction Plan File which can be used to generate the script. |
--magic-number |
Cardano Network Magic Number where the python script will connect to. |
--cardano-node-docker-image |
Docker Image name of the Cardano node. |
--reward-withdrawal-amount |
Amount of lovelace that will be withdrawn from the source's rewards |
Flag Details
Flag | Description |
---|---|
--add-comments |
Add comments in the generated bash script |
--use-docker-cli-for-pycardano |
By default, Pycardano connects to Cardano CLI. This flag is used if the connected cli is the host one or the docker one |
--enable-dust-collection |
Enable Dust Collection (Additional Step of combining small utxos into 1 utxo) |
--execute-script-now |
Execute the script right after it's generated |
--include-rewards |
Withdraw rewards of source address and include it in the transaction |
Example Command
cardano-mass-payments-generate --sources-csv source.csv --payments-csv payments.csv --cardano-network TESTNET
--add-comments --output-type BASH_SCRIPT --metadata-message-file metadata.txt --metadata-json-file metadata.json
--execute-script-now --enable-dust-collection --script-method PYCARDANO --use-docker-cli-for-pycardano
Code for Transaction Plan Generation
Using the python package, we can generate the transaction plan object and save it in as a json file
from cardano_mass_payments.masspayments import generate_transaction_plan
from cardano_mass_payments.constants.common import (
ScriptOutputFormats,
CardanoNetwork,
ScriptMethod,
DustCollectionMethod,
)
transaction_plan = generate_transaction_plan(
output_format=ScriptOutputFormats.BASH_SCRIPT,
source_address="test_source_address",
source_details={
"test_source_address": "test_signing_key_file"
},
payments_csv_file="payments.csv",
cardano_network=CardanoNetwork.TESTNET,
script_method=ScriptMethod.METHOD_DOCKER_CLI,
allowed_ttl_slots=1000,
dust_collection_method=DustCollectionMethod.COLLECT_TO_SOURCE,
dust_collection_threshold=10000000,
enable_dust_collection=True,
)
with open("transaction_plan_filename", "w+") as transaction_plan_file:
transaction_plan_file.write(transaction_plan.json())
Code for Mass Payments Script Generation
Using the python package, we can also generate the mass payments script
from cardano_mass_payments.masspayments import generate_bash_script
from cardano_mass_payments.constants.common import (
CardanoNetwork,
ScriptMethod,
)
bash_script_result = generate_bash_script(
transaction_plan=transaction_plan, # Generated transaction plan from generate_transaction_plan
source_details={
"test_source_address": "test_signing_key_file"
},
source_address="test_source_address",
metadata_file="metadata.json",
allow_ttl_slots=1000,
add_comments=True,
store_in_file=False,
network=CardanoNetwork.TESTNET,
method=ScriptMethod.METHOD_DOCKER_CLI,
)
Algorithm
- Parse the source and payment CSV files.
- If
--transaction-plan-file
is provided, parse the transaction plan file. - Else, generate a transaction plan based on the parsed source and payment details.
- Create the initial transaction plan setup.
- Check the initial transaction byte size of the possible payment group UTxO (1 input, n payments).
- If the transaction size is greater than the maximum, adjust the number of payments until the transaction size satisfies the size check.
- Divide the payments into k groups with n (obtained from step
3.1.2
) payments. - Select Input UTxOs that can satisfy the total transaction amounts and fees.
- Create a mock transaction with the selected input utxos and payments based on the payment groups + 1 payment utxo for the change.
- Get the size of the mock transaction. This will determine if dust collection is required or not.
- If it's greater than the maximum, dust collection is required.
- Dust Collection Step
- While all selected input utxos are not yet used,
- Select Input UTxOs that can satisfy the maximum transaction byte size
- n inputs, 1 payment for 1st group
- n+1 inputs, 1 payment for the remaining groups (the additional input is the input utxo from the previous dust group)
- Create a Dust Group with the selected inputs
- Select Input UTxOs that can satisfy the maximum transaction byte size
- While all selected input utxos are not yet used,
- Adjust UTxO Step
- Classify the payment groups on whether their transaction size is above/below the maximum transaction size.
- For those above the maximum,
- Remove payments until transaction satisfy the maximum size.
- Move the removed payments in a temporary list.
- For those below the maximum,
- Get payments from the temporary list and add them until transaction doesn't satisfy the maximum size.
- If there are payments in the temporary list,
- If the preparation transaction size is less than the maximum, add payments until transaction doesn't satisfy the maximum size.
- If there are remaining payments in the temporary list, treat them as a new payment group.
- Generate the transaction plan using the adjusted details obtained in step
3.3
.
- Create the initial transaction plan setup.
- Create a transaction plan file from the generated transaction plan.
- If
--output-type
isTRANSACTION_PLAN
, return the transaction plan filename. - Else, create a bash script from the transaction plan and print the corresponding
--output-type
.BASH_SCRIPT
- Display the file name in the console.CONSOLE
- Displays the script contents in console.JSON
- Display a JSON object in console containing the script file name.
- If
--execute-script-now
,- Display transaction plan details.
- Transaction Plan Filename
- Number of Dust Transactions to be Generated
- Number of Payment Transaction Groups Generated
- Total Amount Required
- Expected Maximum Change Returned
- A prompt will be displayed asking the user to review the plan and asks to continue with the execution.
- Repeat asking for input if the user inputs any value other than yes/no.
- If yes, execute the script.
- If no, a "Thank You" message will be displayed.
- Display transaction plan details.
Possible Errors
Error | JSON Output | Description |
---|---|---|
ScriptError |
{ "code": 500001, "message": STR, "context": DICT } |
Unexpected Errors encountered during any part of the mass payments script generation |
InsufficientBalance |
{ "code": 400001, "message": STR, "context": { "required_amount": INT, "current_amount": INT } } |
Current Balance doesn't satisfy the total transaction amount and fee required for submission |
InvalidMethod |
{ "code": 400002, "message": "Invalid Method", "context": { "allowed_methods": STR[], "method": STR } } |
Method is not included in the allowed methods. This will show on invalid command argument value. |
InvalidNetwork |
{ "code": 400003, "message": "Invalid Network", "context": { "allowed_networks": STR[], "network": STR } } |
Network is not included in the allowed networks. This will show on invalid command argument value. |
InvalidFileError |
{ "code": 400004, "message": STR, "context": { "file":STR } } |
Unexpected Errors encountered while doing file related function (Source and Payment CSV Files, Signing Key Files, Metadata Files, Transaction Plan Files) |
InvalidType |
{ "code": 400005, "message": STR, "context": { "type":STR } } |
Value Type doesn't match with the required field type. This will show on invalid command argument values. |
EmptyList |
{ "code": 400006, "message": STR } |
List is empty. |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cardano-mass-payments-0.1.5.tar.gz
.
File metadata
- Download URL: cardano-mass-payments-0.1.5.tar.gz
- Upload date:
- Size: 74.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1272a19ff1926ca21b0659efea636c2115cdad6e476a6b63744e1801ce75aa8f |
|
MD5 | 6a70395fb8036d4f86ba90e53ce58638 |
|
BLAKE2b-256 | af021d6800fc43d28d18b593f0698213856585b159afa11d093b73eea69b7547 |
File details
Details for the file cardano_mass_payments-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: cardano_mass_payments-0.1.5-py3-none-any.whl
- Upload date:
- Size: 91.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f102354d73079989e76724b828e22c2c17d3b595cd5b86e567dcde83ac4e56b |
|
MD5 | 928cc830922563aa3d4e59ff5ae7c2e7 |
|
BLAKE2b-256 | be7c8b063f05416714dc30708c7ad04f9cc96104a34a19abcae2f989ecb49a9a |