💰AgentVault

The AgentVault contract manages funds for AI agents, providing secure storage, daily spending limits, and multi-token support.

🎯 Overview

AgentVault provides:

  • Secure Fund Storage - Safe storage for agent funds

  • Daily Spending Limits - Prevent excessive spending

  • Multi-Token Support - Native tokens + ERC20

  • Withdrawal Controls - Owner-controlled withdrawals

  • Token Whitelist - Control which tokens can be deposited

📊 Contract Architecture

contract AgentVault is Ownable, ReentrancyGuard {
    struct Vault {
        uint256 nativeBalance;
        mapping(address => uint256) tokenBalances;
        address[] allowedTokens;
        uint256 dailyLimit;
        uint256 dailySpent;
        uint256 lastResetTime;
        bool isActive;
    }
    
    mapping(address => Vault) private vaults;
    mapping(address => bool) public registeredAgents;
    
    uint256 public constant MIN_DAILY_LIMIT = 0.01 ether;
    uint256 public constant MAX_DAILY_LIMIT = 100 ether;
}
circle-exclamation

🔧 Core Functions

1. Create Vault

Create a new vault for an agent (contract owner only).

Parameters:

  • agent - Address of the agent (owner address)

  • dailyLimit - Daily spending limit in wei (must be between 0.01 and 100 ether)

Requirements:

  • Caller must be contract owner

  • Vault must not already exist

  • Daily limit must be within MIN_DAILY_LIMIT and MAX_DAILY_LIMIT

Events:

Example:

2. Deposit Native Tokens

Deposit native blockchain tokens (STT) into the vault.

Parameters:

  • agent - Address of the agent

Requirements:

  • Vault must exist

  • msg.value must be > 0

Events:

Example:

3. Withdraw Native Tokens

Withdraw native tokens from the vault (agent or contract owner only).

Parameters:

  • agent - Address of the agent

  • recipient - Address to receive the funds

  • amount - Amount to withdraw in wei

Requirements:

  • Caller must be agent or contract owner

  • Vault must be active

  • Sufficient balance

  • Amount must not exceed daily limit

Example:

4. Deposit ERC20 Tokens

Deposit ERC20 tokens into the vault.

Requirements:

  • Token must be allowed for this vault

  • Caller must have approved the vault

  • Amount must be > 0

Example:

5. Withdraw ERC20 Tokens

Withdraw ERC20 tokens from the vault.

Requirements:

  • Caller must be agent or contract owner

  • Vault must be active

  • Sufficient token balance

Example:

6. Allow Token

Enable an ERC20 token for the vault (contract owner only).

Example:

7. Disallow Token

Disable an ERC20 token for the vault (contract owner only).

Example:

8. Update Daily Limit

Update the daily spending limit (contract owner only).

Example:

9. Activate/Deactivate Vault

Control vault status (contract owner only).

Example:

🔍 Query Functions

Get Native Balance

Example:

Get Token Balance

Example:

Get Daily Limit Info

Example:

Get Allowed Tokens

Example:

Check if Vault is Active

Example:

📡 Events

VaultCreated

NativeDeposit

Listen for deposits:

NativeWithdraw

TokenDeposit

TokenWithdraw

DailyLimitUpdated

TokenAllowed / TokenDisallowed

VaultActivated / VaultDeactivated

💡 Usage Patterns

Pattern 1: Basic Vault Setup

Pattern 2: Multi-Token Vault

Pattern 3: Automated Refill

Pattern 4: Daily Limit Management

🔒 Security Features

1. Daily Spending Limits

Prevents agents from spending all funds at once:

  • Daily limit resets every 24 hours

  • Tracks spending per day

  • Prevents withdrawal if limit exceeded

2. Access Control

  • Contract Owner: Can create vaults, update limits, allow/disallow tokens, activate/deactivate vaults

  • Agent Address: Can withdraw from their own vault

  • Anyone: Can deposit to any vault

3. Token Whitelist

Only approved tokens can be deposited:

4. Reentrancy Protection

All state-changing functions use nonReentrant modifier to prevent reentrancy attacks.

🔗 Contract Addresses

Somnia Testnet

Somnia Mainnet

⚠️ Important Notes

  1. Agent Addresses - Vault uses agent addresses (not IDs). Always get the agent's owner address from the registry first.

  2. Daily Limit Resets - Automatically resets every 24 hours from last reset

  3. Gas Costs - Keep enough native tokens for gas fees

  4. Token Approvals - Remember to approve vault before depositing ERC20

  5. Owner vs Agent - Contract owner manages vault settings, agent address can withdraw


Next: Learn about AgentExecutor for task execution.

Last updated