-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpayRequest.js
134 lines (123 loc) · 3.95 KB
/
payRequest.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(async () => {
const {
RequestNetwork,
Types,
Utils,
} = require("@requestnetwork/request-client.js");
const {
EthereumPrivateKeySignatureProvider,
} = require("@requestnetwork/epk-signature");
const {
approveErc20,
hasSufficientFunds,
hasErc20Approval,
payRequest,
} = require("@requestnetwork/payment-processor");
const { providers, Wallet } = require("ethers");
const { config } = require("dotenv");
// Load environment variables from .env file
config();
const epkSignatureProvider = new EthereumPrivateKeySignatureProvider({
method: Types.Signature.METHOD.ECDSA,
privateKey: process.env.PAYEE_PRIVATE_KEY, // Must include 0x prefix
});
const requestClient = new RequestNetwork({
nodeConnectionConfig: {
baseURL: "https://sepolia.gateway.request.network/",
},
signatureProvider: epkSignatureProvider,
});
const payeeIdentity = new Wallet(process.env.PAYEE_PRIVATE_KEY).address;
const payerIdentity = payeeIdentity;
const paymentRecipient = payeeIdentity;
const feeRecipient = "0x0000000000000000000000000000000000000000";
const requestCreateParameters = {
requestInfo: {
currency: {
type: Types.RequestLogic.CURRENCY.ERC20,
value: "0x370DE27fdb7D1Ff1e1BaA7D11c5820a324Cf623C",
network: "sepolia",
},
expectedAmount: "1000000000000000000",
payee: {
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
value: payeeIdentity,
},
payer: {
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
value: payerIdentity,
},
timestamp: Utils.getCurrentTimestampInSecond(),
},
paymentNetwork: {
id: Types.Extension.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT,
parameters: {
paymentNetworkName: "sepolia",
paymentAddress: paymentRecipient,
feeAddress: feeRecipient,
feeAmount: "0",
},
},
contentData: {
reason: "🍕",
dueDate: "2023.06.16",
},
signer: {
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
value: payeeIdentity,
},
};
const request = await requestClient.createRequest(requestCreateParameters);
let requestData = await request.waitForConfirmation();
console.log(`Created Request: ${JSON.stringify(requestData)}`);
const provider = new providers.JsonRpcProvider(
process.env.JSON_RPC_PROVIDER_URL,
);
const payerWallet = new Wallet(
process.env.PAYER_PRIVATE_KEY, // Must have 0x prefix
provider,
);
console.log(
`Checking if payer ${payerWallet.address} has sufficient funds...`,
);
const _hasSufficientFunds = await hasSufficientFunds(
requestData,
payerWallet.address,
{
provider: provider,
},
);
console.log(`_hasSufficientFunds = ${_hasSufficientFunds}`);
if (!_hasSufficientFunds) {
throw new Error(`Insufficient Funds: ${payerWallet.address}`);
}
console.log(
`Checking if payer ${payerWallet.address} has sufficient approval...`,
);
const _hasErc20Approval = await hasErc20Approval(
requestData,
payerWallet.address,
provider,
);
console.log(`_hasErc20Approval = ${_hasErc20Approval}`);
if (!_hasErc20Approval) {
console.log(`Requesting approval...`);
const approvalTx = await approveErc20(requestData, payerWallet);
await approvalTx.wait(2);
console.log(`Approval granted. ${approvalTx.hash}`);
}
const paymentTx = await payRequest(requestData, payerWallet);
await paymentTx.wait(2);
console.log(`Payment complete. ${paymentTx.hash}`);
let startTime = Date.now();
while (requestData.balance?.balance < requestData.expectedAmount) {
requestData = await request.refresh();
console.log(`current balance = ${requestData.balance?.balance}`);
await new Promise((resolve) => setTimeout(resolve, 1000));
// Check if 5 seconds have passed, and if so, break out of the loop
if (Date.now() - startTime >= 5000) {
console.log("Timeout: Exiting loop after 5 seconds.");
break;
}
}
})();