This PR implements path payment support for the Utility Drip Contracts, allowing utility providers to accept payment in USDC but receive the final payout in XLM via Stellar's path payment functionality.
withdraw_earnings_path_payment(): New function that enables providers to withdraw earnings in a different token than the payment token- Token Conversion Support: Automatic conversion from USD cents to destination tokens using the price oracle
- Supported Token Management: Functions to add/remove supported withdrawal tokens
withdraw_earnings_path_payment(env, meter_id, amount_usd_cents, destination_token)- Main path payment functionadd_supported_withdrawal_token(env, token)- Add token to supported withdrawal listremove_supported_withdrawal_token(env, token)- Remove token from supported withdrawal listget_supported_withdrawal_tokens(env)- Get list of supported withdrawal tokensis_withdrawal_token_supported(env, token)- Check if token is supported for withdrawalconvert_usd_to_token_if_needed(env, usd_cents, destination_token)- Helper for USD to token conversion
- Added
SupportedWithdrawalToken(Address)toDataKeyenum for tracking supported withdrawal tokens
- User pays: Customer tops up their meter using USDC
- Provider withdraws: Utility provider can choose to receive earnings in XLM instead of USDC
- Automatic conversion: Contract handles the conversion using the price oracle
- Seamless experience: Provider receives XLM even though original payment was in USDC
USDC Payment → Contract (USD cents) → Oracle Conversion → XLM Withdrawal
- Oracle Integration: Uses existing price oracle for accurate conversions
- Security: Validates destination tokens are supported before processing
- Fallback: If destination token equals meter token, uses regular withdrawal
- Event Emission: Emits
PathPaymentevents for tracking and monitoring - Balance Management: Properly updates meter balances and provider pools
- Validates withdrawal amounts are positive and available
- Checks destination token is supported
- Ensures contract has sufficient destination token balance
- Proper error codes for all failure scenarios
Added comprehensive test coverage including:
test_path_payment_usdc_to_xlm()- Tests USDC to XLM conversiontest_path_payment_same_token()- Tests fallback to regular withdrawaltest_supported_withdrawal_tokens()- Tests token support functionstest_add_remove_withdrawal_tokens()- Tests token management
- All conversions use the trusted price oracle
- Providers must authenticate to withdraw
- Supported tokens must be explicitly whitelisted
- Contract balance checks prevent insufficient funds errors
- Proper authorization checks on all functions
- All existing functionality remains unchanged
- New functions are additive only
- Existing withdrawal methods continue to work as before
- No breaking changes to existing contracts
contracts/utility_contracts/src/lib.rs- Core implementationcontracts/utility_contracts/src/test.rs- Test coverage
- None (implementation integrated into existing files)
cargo test --lib test_path_payment
cargo test --lib test_supported_withdrawal_tokens
cargo test --lib test_add_remove_withdrawal_tokens- Deploy updated contract to testnet first
- Ensure price oracle is configured and functional
- Fund contract with XLM reserves for path payments
- Add supported withdrawal tokens as needed
- Monitor path payment events for debugging
- Support for multiple destination tokens beyond XLM
- Dynamic path optimization for best conversion rates
- Liquidity pool integration for better token availability
- Historical path payment tracking and analytics
This PR fully addresses issue #46: "Support for Multi-Asset Payouts" by implementing the requested path payment functionality that allows utility providers to accept payment in USDC but receive the final payout in XLM.