Skip to content

Commit

Permalink
simple agent based token payments
Browse files Browse the repository at this point in the history
  • Loading branch information
ejfitzgerald committed Feb 27, 2024
1 parent a02502a commit b8efcd5
Show file tree
Hide file tree
Showing 4 changed files with 1,448 additions and 0 deletions.
27 changes: 27 additions & 0 deletions integrations/agent-token-payments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Agent based Token Payments

This example is two locally running agents Alice and Bob.

Periodically, Bob will request funds from Alice. Alice will then send tokens to Bob via the Fetch.ai test network.

This example serves as a simple use case for the basics of interacting with the Fetch.ai network.

## Running the agents

Install dependencies

```bash
poetry install
```

Enter the poetry shell

```bash
poetry shell
```

Start the agents

```bash
python3 main.py
```
67 changes: 67 additions & 0 deletions integrations/agent-token-payments/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from uagents import Agent, Bureau, Context, Model
from uagents.setup import fund_agent_if_low


class PaymentRequest(Model):
address: str
amount: int


alice = Agent(name='alice', seed='alice token payment seed phrase')


@alice.on_interval(2)
async def alice_interval(ctx: Context):
current_balance = ctx.ledger.query_bank_all_balances(ctx.wallet.address())[0].amount
ctx.logger.info(f'Current balance is {current_balance}')


@alice.on_message(PaymentRequest)
async def alice_payment_request(ctx: Context, sender: str, msg: PaymentRequest):
ctx.logger.info(f'Payment request received from {sender} to transfer {msg.amount} to {msg.address}')

tx = ctx.ledger.send_tokens(
msg.address,
msg.amount,
ctx.ledger.network_config.fee_denomination,
ctx.wallet,
)

ctx.logger.info(f'Sent transaction: {tx.tx_hash} to network... waiting for confirmation')
tx.wait_to_complete()
ctx.logger.info(f'Transaction complete!')


bob = Agent(name='bob', seed='bob token payment seed phrase')


@bob.on_interval(2)
async def bob_interval(ctx: Context):
current_balance = ctx.ledger.query_bank_all_balances(ctx.wallet.address())[0].amount
ctx.logger.info(f'Current balance is {current_balance}')


@bob.on_interval(30)
async def bob_interval(ctx: Context):
ctx.logger.info(f'Requesting payment from alice...')

# request funds from alice
await ctx.send(alice.address, PaymentRequest(address=str(bob.wallet.address()), amount=15))


print('Alice Address:', alice.address)
print('Alice Wallet Address:', alice.wallet.address())
print('Bob Address:', bob.address)
print('Bob Wallet Address:', bob.wallet.address())

bureau = Bureau()
bureau.add(alice)
bureau.add(bob)

if __name__ == '__main__':
print('Funding agents (if necessary)...')
# fund_agent_if_low(alice.wallet.address())
# fund_agent_if_low(bob.wallet.address())

print('Starting agents...')
bureau.run()
Loading

0 comments on commit b8efcd5

Please sign in to comment.