|
| 1 | +# In-Memory Requests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +In-memory requests allow for creating and managing requests without immediately persisting them to storage. This enables faster payment workflows and deferred persistence. |
| 6 | + |
| 7 | +## Key benefits: |
| 8 | + |
| 9 | +* **Faster payment flow:** In-memory requests are helpful when payment is the priority, such as in e-commerce cases. In this scenario, the request is a receipt rather than an invoice. |
| 10 | +* **Deferred Persistence:** With in-memory requests, a request can be created on the front end with a user's signature and passed on to the backend for persistence. |
| 11 | + |
| 12 | +## How it works: |
| 13 | + |
| 14 | +The flow of creating and paying an in-memory request is similar to a regular request with the following key differences: |
| 15 | + |
| 16 | +* Create an in-memory request by passing the argument `skipPeristence: true` when instantiating the `RequestNetwork` instance. |
| 17 | +* An in-memory request is _not_ persisted immediately like normal requests. Instead, it is stored in memory on the device where it was created. It can be persisted at a later time using the `persistTransaction()`function. |
| 18 | +* An in-memory request has the `inMemoryInfo` property. |
| 19 | +* Avoid calling `getData()` on an in-memory request because it will fail silently by returning an empty `EventEmitter` object. |
| 20 | +* Retrieving an in-memory request with `requestClient.fromRequestId()` will fail because the request has not been persisted yet so it is not possible to read it from the Request Node. |
| 21 | + |
| 22 | +{% stepper %} |
| 23 | +{% step %} |
| 24 | +### Install necessary dependencies |
| 25 | + |
| 26 | +To create in-memory requests, it is necessary to install the following package: |
| 27 | + |
| 28 | +```bash |
| 29 | +npm install @requestnetwork/request-client.js |
| 30 | +``` |
| 31 | + |
| 32 | +Along with the following package for payments: |
| 33 | + |
| 34 | +```bash |
| 35 | +npm install @requestnetwork/payment-processor |
| 36 | +``` |
| 37 | +{% endstep %} |
| 38 | + |
| 39 | +{% step %} |
| 40 | +### Create an in-memory request |
| 41 | + |
| 42 | +Create an in-memory request by passing the argument `skipPeristence: true` when instantiating the `RequestNetwork` instance. |
| 43 | + |
| 44 | +<pre class="language-typescript"><code class="lang-typescript">// Request parameters |
| 45 | +const requestParameters = {...} |
| 46 | + |
| 47 | + |
| 48 | +const web3SignatureProvider = new Web3SignatureProvider( |
| 49 | + ethersProvider!.provider |
| 50 | + ); |
| 51 | + |
| 52 | + const inMemoryRequestNetwork = new RequestNetwork({ |
| 53 | + nodeConnectionConfig: { |
| 54 | + baseURL: "https://gnosis.gateway.request.network", |
| 55 | + }, |
| 56 | + signatureProvider: web3SignatureProvider, |
| 57 | + <a data-footnote-ref href="#user-content-fn-1"> skipPersistence: true,</a> |
| 58 | + }); |
| 59 | + |
| 60 | + let inMemoryRequest = |
| 61 | + await inMemoryRequestNetwork.createRequest(requestParameters); |
| 62 | + |
| 63 | +</code></pre> |
| 64 | +{% endstep %} |
| 65 | + |
| 66 | +{% step %} |
| 67 | +### Pay an in-memory request |
| 68 | + |
| 69 | +To pay an in-memory request, pass the `inMemoryInfo.requestData` property to the payment function. |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { |
| 73 | + payRequest |
| 74 | +} from "@requestnetwork/payment-processor"; |
| 75 | + |
| 76 | +const paymentTx = await payRequest( |
| 77 | + inMemoryRequest.inMemoryInfo.requestData, |
| 78 | + signer |
| 79 | + ); |
| 80 | + |
| 81 | +await paymentTx.wait(confirmationBlocks); |
| 82 | + |
| 83 | +``` |
| 84 | +{% endstep %} |
| 85 | + |
| 86 | +{% step %} |
| 87 | +### Persist in-memory request |
| 88 | + |
| 89 | +In-memory requests need to be persisted using a new `RequestNetwork` client that does not use the `skipPersistence` property. |
| 90 | + |
| 91 | +```typescript |
| 92 | +const persistingRequestNetwork = new RequestNetwork({ |
| 93 | + nodeConnectionConfig: { |
| 94 | + baseURL: "https://gnosis.gateway.request.network", |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | +await persistingRequestNetwork.persistRequest(inMemoryRequest); |
| 99 | +``` |
| 100 | +{% endstep %} |
| 101 | +{% endstepper %} |
| 102 | + |
| 103 | +[^1]: Configure the RequestNetwork instance to produce in-memory requests |
| 104 | + |
0 commit comments