Created
August 20, 2024 16:00
-
-
Save kehiy/c54b48f16aa39cb12b813be9d6a9d398 to your computer and use it in GitHub Desktop.
A python script which sends bulk transaction to a list of addresses in Pactus network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import grpc | |
import json | |
from pactus.crypto import CryptoConfig | |
from pactus.crypto.address import Address | |
from pactus.crypto.bls.private_key import PrivateKey | |
from pactus.transaction.transaction import Transaction | |
from pactus.types.amount import Amount | |
from pactus.rpc.transaction_pb2_grpc import TransactionStub | |
from pactus.rpc.blockchain_pb2_grpc import BlockchainStub | |
from pactus.rpc.blockchain_pb2 import ( | |
GetBlockchainInfoRequest, | |
GetBlockchainInfoResponse, | |
) | |
from pactus.rpc.transaction_pb2 import BroadcastTransactionRequest | |
# Get last block height from network | |
def height(stub: BlockchainStub): | |
res: GetBlockchainInfoResponse = stub.GetBlockchainInfo(GetBlockchainInfoRequest()) | |
return res.last_block_height | |
def main() -> None: | |
# Initialize crypto config and gRPC connections | |
CryptoConfig.use_testnet() | |
channel = grpc.insecure_channel( | |
"testnet1.pactus.org:50052" | |
) # Replace with your preferred address | |
t_stub = TransactionStub(channel) | |
b_stub = BlockchainStub(channel) | |
# Addresses and transactions list | |
transaction_ids = [] | |
addresses = [] | |
with open("address.json", "r") as file: | |
addresses = json.load(file) | |
# Fixed memo and fee | |
memo = "This is a test transaction" | |
fee = Amount.from_string("0.01") | |
# Sender address info | |
sender = Address.from_string("Your address here") | |
sec = PrivateKey.from_string( | |
"Your private key here" | |
) | |
# Last blockchain height | |
lbh = height(b_stub) | |
# Loop over all address | |
for addr in addresses: | |
# Generate random amounts | |
random_float = random.uniform(0, 1.5) # Replace with your preferred numbers | |
amount = Amount.from_nano_pac(random_float) | |
# Drive receiver address from string | |
receiver = Address.from_string(addr) | |
# Creating and signing transaction | |
tx = Transaction.create_transfer_tx(lbh, sender, receiver, amount, fee, memo) | |
signed_tx = tx.sign(sec) | |
# Broadcasting transaction | |
req = BroadcastTransactionRequest(signed_raw_transaction=signed_tx) | |
tx_id = t_stub.BroadcastTransaction(req) | |
# Adding transaction ID to list | |
transaction_ids.append(tx_id) | |
# Printing transaction ID | |
print(f"Transaction sent: {tx_id}") | |
# Save sent transaction IDs into a file | |
with open("transactions.json", "w") as file: | |
json.dump(transaction_ids, file) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment