forked from RequestNetwork/requestNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpay-erc20-request.ts
32 lines (27 loc) · 972 Bytes
/
pay-erc20-request.ts
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
import { Wallet } from 'ethers';
import {
approveErc20,
hasErc20Approval,
hasSufficientFunds,
payRequest,
} from '@requestnetwork/payment-processor';
import { RequestNetwork } from '@requestnetwork/request-client.js';
/* eslint-disable @typescript-eslint/no-floating-promises */
// for demo purpose
const wallet = Wallet.createRandom();
const requestNetwork = new RequestNetwork();
// eslint-disable-next-line
(async () => {
const account = wallet.address;
const request = await requestNetwork.fromRequestId('REQUEST_ID');
const requestData = request.getData();
if (!(await hasSufficientFunds({ request: requestData, address: account }))) {
throw new Error('You do not have enough funds to pay this request');
}
if (!(await hasErc20Approval(requestData, account))) {
const approvalTx = await approveErc20(requestData, wallet);
await approvalTx.wait(1);
}
const tx = await payRequest(requestData, wallet);
await tx.wait(1);
})();