This contract implements a secure, modular delegated spending system for Cosmos blockchains using CosmWasm. It allows users to deposit credits, authorize spenders, and delegate spending rights, following best practices for security, modularity, and extensibility.
instantiate
: Initializes the contract with an admin address and token denominationexecute
: Processes deposit, authorization, revocation, and spending operationsquery
: Provides information about balances and authorization statusInstantiateMsg
: Sets contract admin and the native token denomination usedDeposit
: Adds deposited funds to user's balanceAuthorizeSpender
: Grants spending permission to another addressRevokeSpender
: Removes spending permission from an addressSpendFrom
: Allows spending tokens from an owner's account (if authorized)Balance
: Returns the token balance of a specified addressIsAuthorized
: Checks if a spender is authorized by an owner// Create a new contract instance with an admin and the accepted token denomination let instantiate_msg = InstantiateMsg { admin: "cosmos1...".to_string(), denom: "uatom".to_string(), };
// User deposits tokens by sending them with the Deposit message let execute_msg = ExecuteMsg::Deposit {};
// Owner authorizes another address to spend on their behalf let execute_msg = ExecuteMsg::AuthorizeSpender { spender: "cosmos2...".to_string(), };
// Authorized spender spends tokens from owner's account let execute_msg = ExecuteMsg::SpendFrom { owner: "cosmos1...".to_string(), amount: 100u128, };
contract/
exec.rs # Execution logic (deposit, authorize, revoke, spend)
init.rs # Initialization logic
mod.rs # Public contract interface
query.rs # Query logic
error.rs # Custom error type
lib.rs # Crate entrypoint
msg/
exec.rs # ExecuteMsg definitions
init.rs # InstantiateMsg definition
query.rs # QueryMsg definitions
schema.rs # JSON schema generator
state.rs # State storage (admin, denom, balances, authorizations)
tests_integration.rs # Integration tests (cw-multi-test)
Cargo.toml # Dependencies
Security & Best Practices
cargo wasm && wasm-opt -Oz -o contract.wasm target/wasm32-unknown-unknown/release/credits_delegation.wasm
See tests_integration.rs
for a full deposit, authorization, and spend flow.
MIT
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4