Module 10 - Building a PayStream DApp on Router Chain
Cloning the Repository
For Understanding how to Write a Smart Contract for a PayStream DApp on Router Chain, Clone the Below Repository in your Local Machine -
This will Clone the PayStream DApp in the Current Directory.
File Structure
The cosmwasm
Directory consists of Smart Contracts written on Router Chain and evm
Directory consists of the Smart Contracts written on Polygon and Arbitrum.
Directorycosmwasm/
Directory.cargo/
- config
Directorycontracts/
Directoryrouterpay/
Directoryexamples/
- schema.rs
Directorysrc/
- contract.rs
- execution.rs
- lib.rs
- modifiers.rs
- query.rs
- reply.rs
- state.rs
- sudo.rs
- tests.rs
- Cargo.toml
Directorypackages/
Directoryrouter-pay-stream/
Directorysrc/
- lib.rs
- routerpay.rs
- Cargo.toml
Directoryscripts/
- deploy.ts
Directoryshell/
- build.sh
Directoryutils/
- execute.ts
- init_wasm.ts
- upload_wasm.ts
- utils.ts
- .editorconfig
- .env.example
- .gitignore
- Cargo.lock
- Cargo.toml
- package.json
- README.md
- rustfmt.toml
- yarn-error.log
- yarn.log
Directoryevm/
Directorycontracts/
- RouterPay.sol
Directoryscript/
- deploy_on_chains.ts
- enroll_added_chains.ts
- enroll_on_chains.ts
- verify_on_chains.ts
Directoryshell/
- setup.sh
Directoryutils/
- chain.ts
- Deploy.ts
- onEachChain.ts
- types.ts
- utils.ts
- .env.example
- .eslintignore
- .eslintrc
- .gitignore
- .prettierrc
- config.ts
- hardhat.config.ts
- package.json
- README.md
- tsconfig.json
- yarn.lock
CosmWasm-side
The Router PayStream Contract is a Smart Contract that facilitates Salary Streaming between a Payer and a Payee. It enables the Creation, Management, and Withdrawal of Salary Streams across Different Chains.
This Documentation Provides a Guide for Developers to Understand and Interact with the Router PayStream Contract. It Covers the Contract’s Message Signatures, Deployment Process, and Usage Instructions.
Execute Messages
The Router PayStream Contract Supports various Messages for Different Operations. Each Message has a Specific Structure that Developers need to follow when Interacting with the Contract.
CreateStream
The CreateStream
function allows the Payer to Create a Salary Stream for a Payee with or without Specifying a reason. The Parameters for this function are -
- whitelisted_addresses:
Option<Vec<(String,String)>>
Addresses of Chains where the Payee can Withdraw funds. - start_time:
u64
The Start time of the Stream. - pay_per_month:
Uint128
The Payment amount per month, internally we Convert it inpay_per_sec
. - recipient:
String
Owner of Stream who can do Whitelist Address or Blacklist. - remarks:
Option<String>
Creator can set remarks if any for Stream, e.g for what reason Stream is created.
CreateStream Message Structure:
CancelStream
The CancelStream
function allows the Payer to Cancel a Salary Stream at anytime. When a Stream is Canceled, any remaining ROUTE Tokens are transferred to the Payee’s Owner Address, and the Stream is Closed. The Parameters for this function are -
- stream_id:
u64
The ID of the Stream to be Canceled. - remarks:
Option<String>
Payer can Pass reason for Cancelling.
CancelStream Message Structure:
DepositRoute
The DepositRoute function
enables any User to Deposit ROUTE Tokens into the Smart Contract. This function is used to add funds to the Contract for Salary Payments.
DepositRoute Message Structure:
WithdrawSalary
The `WithdrawSalary` function allows the payee to initiate a withdrawal from the salary stream on the router chain or on other chain. The parameters for this function are:
- stream_id:
u64
The ID of the stream from which to withdraw. - recipient:
String
The address of the recipient who will receive the withdrawn route tokens. - dst_chain_id:
Option<String>
The chain ID to which the payee wants to withdraw the route, if ‘None’ or ‘Router Chain ID’ is passed, the route will be transferred to the router chain; otherwise, it will be transferred to the destination chain if it is enrolled, or the call will be reverted. - max_amount:
Option<Uint128>
Max amount to withdraw from salary, if passed zero or None then it will withdraw all accumulated amount
WithdrawSalary Message Structure :
EnrollRemoteContract
The `EnrollRemoteContract` function allows the payee to initiate a withdrawal from the salary stream on the router chain or on other chain. The parameters for this function are:
- chain_id:
String
dst chain id - remote_contract:
String
contract address on dst chain
EnrollRemoteContract Message Structure :
MapChainType
The `MapChainType` function allows the contract owner to map chain type. The parameters for this function are:
- chain_id:
String
dst chain id - chain_type: u64 chain type of dst chain
MapChainType Message Structure :
WithdrawFunds
The `WithdrawFunds` function allows the owner to withdraw funds from the contract. The parameters for this function are:
- recipient:
String
router address of recipient to which amount will be transferred - amount:
Uint128
amount to be withdrawn
WithdrawFunds Message Structure :
UpdateWhiteListAddress
The `UpdateWhiteListAddress` function allows the stream owner to whitelist or blacklist a address for their stream. The parameters for this function are:
- stream_id:
u64
stream id for which this operation to be applied - address:
String
address to be whitelisted - chain_id:
String
chain id of provided address - to:
bool
true means whitelist and false means blacklist
UpdateWhiteListAddress Message Structure :
UpdateCrossChainMetadata
The `UpdateCrossChainMetadata` function allows the ownwer of contract to update metadata such as ack_gas_limit or dst_gas_limit or relayer_fee. The parameters for this function are:
- dst_gas_limit:
Option<u64>
dst gas limit for IReceive, it’s optional if passed Some(_) then only it will update the dst_gas_limit - ack_gas_limit:
Option<u64>
ack gas limit for sudo msg IAck on rotuer chain - relayer_fee:
Option<Uint128>
realyer fee for relaying the ISend message to dst chain
UpdateCrossChainMetadata Message Structure :
Query Messages
GetContractVersion
The `GetContractVersion` function Fetches the contract version.
GetContractVersion Message Structure :
GetRemoteContract
The `GetRemoteContract` function Fetches the contract address of provided chainid. The parameters for this function is:
- chain_id:
String
dst chain id for which the query is being made.
GetRemoteContract Message Structure :
GetRouterPayMetadata
The `GetRouterPayMetadata` function Fetches the metadat mapped to provided stream id. The parameters for this function is:
- stream_id:
u64
stream id for which the query is being made.
GetRouterPayMetadata Message Structure :
GetOwner
The `GetOwner` function Fetches the contract owner.
GetOwner Message Structure :
GetCrossChainMetadata
The `GetCrossChainMetadata` function fetches the crosschain metadata contains dst_gas_limit,ack_gas_limit and relayer_fee.
GetCrossChainMetadata Message Structure :
GetStreams
The `GetStreams` function fetches stream metadata for the range provided. The parameters for this function are:
- from:
u64
stream id from where to fetch - to:
Option<u64>
end stream id to where to fetch if not provided then it will fetches next 10 streams
GetStreams Message Structure :
GetStreamWhiteListAddress
The `GetStreamWhiteListAddress` function fetches all whitelist address provided stream id, returns Vec<(String,String)>
. The parameters for this function is:
- stream_id:
u64
stream id for which the query is being made.
GetStreamWhiteListAddress Message Structure :
GetUserStreamIds
The `GetUserStreamIds` function fetches all stream ids associated with a specific router address. The parameters for this function is:
- address:
String
user router address to get all stream ids
GetUserStreamIds Message Structure :
GetUserStreamsInfo
The `GetUserStreamsInfo` function fetches detailed information about the streams associated with a specific address. The parameters for this function is:
- address:
String
user router address to get all stream ids
GetUserStreamsInfo Message Structure :
IsWhiteListed
The `IsWhiteListed` function checks if a specific address is whitelisted for a particular stream on a specific chain. The parameters for this function are:
- stream_id:
u64
stream if for which this checks applies - chain_id:
String
chain id where address belongs to - address:
String
address for provided chain id
IsWhiteListed Message Structure :
GetAccumulatedAmount
The `GetAccumulatedAmount` function fetches the accumulated amount for a specific stream. The parameters for this function is:
- stream_id:
u64
stream if for which this checks applies
GetAccumulatedAmount Message Structure :
These functions and their functionalities form the core of Router Pay Streaming, allowing for the creation, management, and withdrawal of salary streams on different chains.
EVM-side
RouterPay.sol
Router PayStream is a DApp to provide a Convenient Way for Employees to Receive their Salaries. The Contract follows the UUPS (Universal Upgradeable Proxy Standard) Pattern and Implements Secure cross-chain Payment Logic.
Constants and Imports
The Contract uses -
- Solidity Version 0.8.16 or higher for Enhanced Security features.
- ABI Coder v2 for Improved encoding/decoding Capabilities.
- OpenZeppelin’s Upgradeable Contracts for Secure Contract Upgradeability.
- Router Protocol’s Gateway Interface for cross-chain functionality.
Events
These Events Track -
-
RouterPayRequest
- Emitted when Initiating a cross-chain Payment.requestId
- Unique Identifier for Tracking.dstChainId
- Target Dhain Identifier.recipient
- Payment recipient Address.maxAmount
- Maximum Transfer Amount.
-
RouterPayReceive
- Emitted when receiving a cross-chain Payment.srcChainId
- Origin Chain Identifier.amount
- Transferred Amount.recipient
- Recipient Address.
State Variables
gatewayAddress
- Router Protocol Gateway Contract Address.routerChainId
- Chain Identifier for Router Chain.gateway
- Interface Instance for Gateway Interactions.routerPayOnRouterChainAddress
- Corresponding Contract Address on Router Chain.
Initialization
This function -
- Initializes the Upgradeable Contracts.
- Sets the Router Chain Configuration.
- Configures the Gateway Contract Interface.
Initiating Payment Withdrawal
Key Components -
-
Validation Checks -
- Destination Chain Configuration.
- Request Metadata format.
-
Payload Creation with -
- Destination Chain ID.
- Sender Address.
- Recipient address.
- Stream ID.
- Maximum amount.
-
Cross-chain message sending via gateway.
-
Event emission for tracking.
Receiving Cross-Chain Payments
Process -
- Verifies sender Authenticity.
- Decodes Payment Information.
- Emits receipt Event.
Setting DApp Metadata
Allows owner to -
- Set fee payer address.
- Configure Gateway Metadata.
Updating Router Pay Contract
Enables -
- Updating Router Chain Contract Address.
- Maintaining cross-chain Configuration.
Gateway Modifier
Ensures -
- Only Gateway can Call cross-chain functions.
- Protected cross-chain Message handling.
Upgrade Authorization
Controls -
- Contract Upgrade Process.
- Owner-only Upgrade authorization.
Address to Bytes Conversion
Provides -
- Efficient address Conversion.
- Assembly-optimized Processing.