generated from CAJUNGOTSHOP/Paytr-33
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpayInvoiceERC20Escrow
61 lines (51 loc) · 2.35 KB
/
payInvoiceERC20Escrow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function payInvoiceERC20Escrow(
address _payee,
address _feeAddress,
uint256 _amount,
uint256 _feeAmount,
bytes calldata _paymentReference,
uint8 _shouldPayoutViaRequestNetwork
) external nonReentrant whenNotPaused {
PaymentERC20 storage paymentERC20 = paymentMapping[_paymentReference];
uint256 totalAmount = _amount + _feeAmount;
if(_amount == 0) revert ZeroAmount();
if(_payee == address(0)) revert ZeroPayeeAddress();
if(_feeAddress == address(0)) revert ZeroFeeAddress();
if(paymentERC20.amount != 0) revert PaymentReferenceInUse();
if(totalAmount < minTotalAmountParameter) revert InvalidTotalAmount();
IERC20(baseAsset).safeTransferFrom(msg.sender, address(this), totalAmount);
uint256 cUsdcbalanceBeforeSupply = getContractCometBalance();
IComet(cometAddress).supply(baseAsset, totalAmount);
uint256 cUsdcbalanceAfterSupply = getContractCometBalance();
uint256 cUsdcAmountToWrap = cUsdcbalanceAfterSupply - cUsdcbalanceBeforeSupply;
uint256 wrappedShares = IWrapper(wrapperAddress).deposit(cUsdcAmountToWrap, address(this));
paymentMapping[_paymentReference] = PaymentERC20({
amount: _amount,
feeAmount: _feeAmount,
wrapperSharesReceived: wrappedShares,
dueDate: 0,
payer: msg.sender,
payee: _payee,
feeAddress: _feeAddress,
shouldPayoutViaRequestNetwork: _shouldPayoutViaRequestNetwork
});
emit PaymentERC20Event(baseAsset, _payee, _feeAddress, _amount, 0, _feeAmount, _paymentReference);
}
const paytrContract = new ethers.Contract(paytrContractAddress, PaytrABI, signer);
await paytrContract.payInvoiceERC20Escrow(
payeeAddress,
feeAddress,
amount,
feeAmount,
paymentReference, //bytes
shouldPayOutViaRequestNetwork
);
//example:
await paytrContract.payInvoiceERC20(
"0x67B94473D81D0cd00849D563C94d0432Ac988B49",
"0xbF7Dc06Bd27BA2C4013cE02380a85aa7fe860f0A",
100_000_000, //100 USDC
5_000_000, //5 USDC fee
"0x494e56332d32343034",
0 //the payment will not be routed through Request Network
);