- owner
- manager
- payer
In order to deploy an instance of the PaymentManager we use smart contract tools.
Deployment command:
npx tools --network <network> cmd=deploy name=PaymentManager from=<owner_address> initMethod=initialize initArgs=<manager_address>,<uniswap_router_address>,<stablecoin_address>,<wallet_address>,<orgid_address>
Three smart contracts are deployed as a result:
- implementation
- proxy
- proxyadmin
Successful deployment creates a configuration file in .openzeppelin
directory.
{
"version": "0.3.2",
"contract": {
"name": "PaymentManager",
"implementation": "0x54ffec25694a0A17F5722e1fD74530d0f88bB43b",
"proxy": "0xFb5F0b76329fc6cf09b4E5ecF7B23227320689dF"
},
"owner": "0xA0B74BFE28223c9e08d6DBFa74B5bf4Da763f959",
"proxyAdmin": "0xf47c896d6Ab5141b74BDa49cbF3913C9c92A395f",
"blockNumber": 9090296
}
Existence of the configuration file is required for the contract upgrade using smart contract tools
.
PaymentManager uses OpenZeppelin's proxy upgrade pattern.
Upgrade command (smart contract tools):
npx tools --network <network> cmd=upgrade name=PaymentManager from=<owner_address> initMethod=<initializer_name> initArgs=<arg1>,<arg2>,<arg-n>
As a result of the successful upgrade, the existing configuration file will be overridden by the new configuration.
- function:
changeOrgId(address)
- arguments:
address
: An address of the OrgId instance
The function call is restricted to be used by the manager only.
- function:
changeManager(address)
- arguments:
address
: An address of the new manager
The function call is restricted to be used by the manager only.
- function:
changeUniswap(address)
- arguments:
address
: An address of the Uniswap router
The function call is restricted to be used by the manager only.
- function:
changeWallet(address)
- arguments:
address
: An address of the new wallet
The function call is restricted to be used by the manager only.
When payment is made the Paid
event is emitted. Listening to this event allow to catch new payments by the manager.
- event:
Paid
- arguments:
uint256
: Index of the payment
- function:
payments(uint256)
- arguments:
uint256
: Index of the payment
The data returned with the function call represents the payment structure:
status
(uint256
): The payment status.0
- paid,1
- refundedtokenIn
(address
): Address of the token paid by payer (WETH address in case of native ETH)amountIn
(uint256
): Amount paid by the payer in tokenstokenOut
(address
): Address of the target token (actually it is a stableCoin)amountOut
(uint256
): Amount paid by the payer in target tokenspayer
(address
): Address of the payermerchant
(bytes32
): The merchant organization IdisEther
(bool
): Is payer has paid with ETHattachment
(string
): Textual attachment (eq: offerId, orderId, or URI)
- function:
refund(uint256,bool)
- arguments:
uint256
: The index of the paymentbool
: The flag that allows making a refund in stablecoin instead of the token has used by the payer for payment
To be able to make refund manager have to transfer stablecoinn tokens in an amount of the funds dedicated to refund to the PaymentManager contract address.
When stablecoin tokens sent the manager should call refund
function to make a refund.
If bool
argument of the refund
function has been set to the true
value then stablecoin tokens will be sent to the payer directly without conversion to the initial asset had used by the payer.
Otherwise, stablecoin tokens will be converted to the tokens (or native ETH) had used by the payer for payment and sent to him.
The payment cannot be refunded twice.
The function call is restricted to be used by the manager only.
- function:
getAmountIn(uint256,address)
- arguments:
uint256
: The amount of stablecoin to be paidaddress
: An address of the payers token
This function is using a token conversion feature of the Uniswap router contract. The Uniswap contract calculates tokens conversions quotes on the base of the state of tokens liquidity pools. Each time getAmountIn
function called the amount can change due to liquidity pools mutability.
- function:
pay(uint256,uint256,address,uint256,string,bytes32)
- arguments:
uint256
: The amount of stablecoin to be paiduint256
: The amount of payers tokens that should be used for paymentaddress
: An address of the payers tokenuint256
: The deadline. The time after which a transaction will be revertedstring
: Textual attachment to the paymentbytes32
: The merchant organization address
Before the call of this function, the payer has to approve spending of his tokens for the PaymentManage contract.
The function call will be reverted in the following cases:
- If payers tokens not approved
- If the estimation of the amount of payer tokens that should be used for payment is changed and became greater than sent in the second parameter
- If the transfer of payer tokens has failed
During the payment, tokens conversion quote can be changed in a negative direction. In that case, the rest of the tokens after conversion will be sent to the payer.
If payment made with stablecoin tokens then these tokens will be sent without the Uniswap usage.
- function:
payETH(uint256,uint256,string,bytes32)
- arguments:
uint256
: The amount of stablecoin to be paiduint256
: The deadline. The time after which a transaction will be revertedstring
: Textual attachment to the paymentbytes32
: The merchant organization address
This function behaviour is the same as at a pay
function.
See the same case for manager
role.