Send tokens with Agents
Introduction
This file can be run on any platform supporting Python, with the necessary install permissions. This example shows how to use Agents to make them send tokens to each other.
Please check out the example code in our examples repo (opens in a new tab) to run this locally.
Supporting documentation
- Creating an agent
- Creating an interval task
- Communicating with other agents
- Register in Almanac
- Almanac Contract
- Protocols
- How to use agents to send tokens
The agent
Self hosted
local-sending_tokens.pyfrom uagents import Agent, Bureau, Context, Model from uagents.network import wait_for_tx_to_complete from uagents.setup import fund_agent_if_low class PaymentRequest(Model): wallet_address: str amount: int denom: str class TransactionInfo(Model): tx_hash: str AMOUNT = 100 DENOM = "atestfet" alice = Agent(name="alice", seed="alice secret phrase") bob = Agent(name="bob", seed="bob secret phrase") fund_agent_if_low(alice.wallet.address()) fund_agent_if_low(bob.wallet.address(), min_balance=AMOUNT) @alice.on_interval(period=10.0) async def request_funds(ctx: Context): await ctx.send( bob.address, PaymentRequest( wallet_address=str(alice.wallet.address()), amount=AMOUNT, denom=DENOM ), ) @alice.on_message(model=TransactionInfo) async def confirm_transaction(ctx: Context, sender: str, msg: TransactionInfo): ctx.logger.info(f"Received transaction info from {sender}: {msg}") tx_resp = await wait_for_tx_to_complete(msg.tx_hash, ctx.ledger) coin_received = tx_resp.events["coin_received"] if ( coin_received["receiver"] == str(alice.wallet.address()) and coin_received["amount"] == f"{AMOUNT}{DENOM}" ): ctx.logger.info(f"Transaction was successful: {coin_received}") @bob.on_message(model=PaymentRequest, replies=TransactionInfo) async def send_payment(ctx: Context, sender: str, msg: PaymentRequest): ctx.logger.info(f"Received payment request from {sender}: {msg}") transaction = ctx.ledger.send_tokens( msg.wallet_address, msg.amount, msg.denom, bob.wallet ) await ctx.send(alice.address, TransactionInfo(tx_hash=transaction.tx_hash)) bureau = Bureau() bureau.add(alice) bureau.add(bob) if __name__ == "__main__": bureau.run()
Expected output
[ bob]: Received payment request from agent1qdp9j2ev86k3h5acaayjm8tpx36zv4mjxn05pa2kwesspstzj697xy5vk2a: wallet_address='fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy' amount=100 denom='atestfet' [alice]: Received transaction info from agent1q2kxet3vh0scsf0sm7y2erzz33cve6tv5uk63x64upw5g68kr0chkv7hw50: tx_hash='DB662CCF415C7B0C9A02928967BE1817506D02A041AA05CA48DCE5CF87D5A638' [alice]: Transaction was successful: {'receiver': 'fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy', 'amount': '100atestfet'}