This repository was archived by the owner on Mar 18, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstx_transfer_example.py
More file actions
160 lines (135 loc) · 5 KB
/
Copy pathstx_transfer_example.py
File metadata and controls
160 lines (135 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Example script demonstrating how to use the STX Transfer Task.
This script shows how to create queue messages that will be processed
by the STX Transfer Task to send STX tokens to recipients.
"""
from uuid import UUID
from app.backend.factory import backend
from app.backend.models import QueueMessageCreate, QueueMessageType
def create_stx_transfer_message(
wallet_id: UUID = None,
recipient: str = None,
amount: int = None,
fee: int = 400,
memo: str = "",
dao_id: UUID = None,
) -> str:
"""Create a queue message for STX transfer.
Args:
wallet_id: UUID of the wallet to send STX from (None = use backend wallet)
recipient: STX address to send tokens to (e.g., "SP1ABC...DEF")
amount: Amount of STX to send (in STX, not microSTX)
fee: Transaction fee in microSTX (default: 400)
memo: Optional memo for the transaction
dao_id: Optional DAO ID if this transfer is DAO-related
Returns:
str: ID of the created queue message
Examples:
# Send 5 STX from a specific wallet
message_id = create_stx_transfer_message(
wallet_id=UUID("12345678-1234-1234-1234-123456789abc"),
recipient="SP1ABC123DEF456GHI789JKL012MNO345PQR678",
amount=5,
fee=250,
memo="Payment for services"
)
# Send 1 STX from backend wallet (for system funding)
message_id = create_stx_transfer_message(
wallet_id=None, # Use backend wallet
recipient="SP1ABC123DEF456GHI789JKL012MNO345PQR678",
amount=1,
memo="Agent account funding"
)
"""
# Create the queue message
queue_message = QueueMessageCreate(
type=QueueMessageType.get_or_create("stx_transfer"),
wallet_id=wallet_id,
dao_id=dao_id,
message={
"recipient": recipient,
"amount": amount,
"fee": fee,
"memo": memo,
},
)
# Save to the queue
created_message = backend.create_queue_message(queue_message)
return str(created_message.id)
def create_multiple_stx_transfers(transfers: list) -> list:
"""Create multiple STX transfer messages at once.
Args:
transfers: List of dictionaries with transfer parameters
Returns:
list: List of created message IDs
Example:
transfers = [
{
"wallet_id": UUID("12345678-1234-1234-1234-123456789abc"),
"recipient": "SP1ABC123DEF456GHI789JKL012MNO345PQR678",
"amount": 10,
"memo": "Payment 1"
},
{
"wallet_id": UUID("87654321-4321-4321-4321-cba987654321"),
"recipient": "SP2DEF456GHI789JKL012MNO345PQR678STU",
"amount": 5,
"memo": "Payment 2"
}
]
message_ids = create_multiple_stx_transfers(transfers)
"""
message_ids = []
for transfer in transfers:
message_id = create_stx_transfer_message(
wallet_id=transfer["wallet_id"],
recipient=transfer["recipient"],
amount=transfer["amount"],
fee=transfer.get("fee", 400),
memo=transfer.get("memo", ""),
dao_id=transfer.get("dao_id"),
)
message_ids.append(message_id)
return message_ids
# Example usage
if __name__ == "__main__":
# Example wallet and recipient (replace with real values)
example_wallet_id = UUID("12345678-1234-1234-1234-123456789abc")
example_recipient = "SP1ABC123DEF456GHI789JKL012MNO345PQR678STU"
print("Creating STX transfer queue message...")
try:
# Create a single transfer message
message_id = create_stx_transfer_message(
wallet_id=example_wallet_id,
recipient=example_recipient,
amount=1, # Send 1 STX
fee=400, # 200 microSTX fee
memo="Test transfer from example script",
)
print(f"Successfully created STX transfer message: {message_id}")
print(
f"The STX Transfer Task will process this message and send 1 STX "
f"from wallet {example_wallet_id} to {example_recipient}"
)
except Exception as e:
print(f"Error creating STX transfer message: {e}")
# Example of multiple transfers
print("\nExample of multiple transfers:")
example_transfers = [
{
"wallet_id": example_wallet_id,
"recipient": "SP1ABC123DEF456GHI789JKL012MNO345PQR678STU",
"amount": 2,
"memo": "Payment to Alice",
},
{
"wallet_id": example_wallet_id,
"recipient": "SP2DEF456GHI789JKL012MNO345PQR678STUVW",
"amount": 3,
"memo": "Payment to Bob",
},
]
try:
message_ids = create_multiple_stx_transfers(example_transfers)
print(f"Created {len(message_ids)} transfer messages: {message_ids}")
except Exception as e:
print(f"Error creating multiple transfers: {e}")