This page will introduce the primary operations provided by Request Network’s SDK while using the EthereumPrivateKeySignatureProvider to sign requests with a private key that is managed outside of a wallet.
This approach works well for Node.js environments without access to a Web3 wallet.
const { Types,Utils } =require("@requestnetwork/request-client.js");constpayeeIdentity='0x7eB023BFbAeE228de6DC5B92D0BeEB1eDb1Fd567';constpayerIdentity='0x519145B771a6e450461af89980e5C17Ff6Fd8A92';constpaymentRecipient= payeeIdentity;constfeeRecipient='0x0000000000000000000000000000000000000000';constrequestCreateParameters= { requestInfo: {// The currency in which the request is denominated currency: { type:Types.RequestLogic.CURRENCY.ERC20, value:'0x370DE27fdb7D1Ff1e1BaA7D11c5820a324Cf623C', network:'sepolia', },// The expected amount as a string, in parsed units, respecting `decimals`// Consider using `parseUnits()` from ethers or viem expectedAmount:'1000000000000000000',// The payee identity. Not necessarily the same as the payment recipient. payee: { type:Types.Identity.TYPE.ETHEREUM_ADDRESS, value: payeeIdentity, },// The payer identity. If omitted, any identity can pay the request. payer: { type:Types.Identity.TYPE.ETHEREUM_ADDRESS, value: payerIdentity, },// The request creation timestamp. timestamp:Utils.getCurrentTimestampInSecond(), },// The paymentNetwork is the method of payment and related details. paymentNetwork: { id:Types.Extension.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT, parameters: { paymentNetworkName:'sepolia', paymentAddress: payeeIdentity, feeAddress: feeRecipient, feeAmount:'0', }, },// The contentData can contain anything.// Consider using rnf_invoice format from @requestnetwork/data-format contentData: { reason:'🍕', dueDate:'2023.06.16', },// The identity that signs the request, either payee or payer identity. signer: { type:Types.Identity.TYPE.ETHEREUM_ADDRESS, value: payeeIdentity, },};
Then, call createRequest() to create the request and waitForConfirmation() to wait until the request is persisted in IPFS and the CID hash is stored on-chain.
Then, construct an ethers v5 Provider and Wallet using a private key. These allow you to read and write to the chain, respectively.
Unfortunately, the Request Network SDK does not yet support ethers v6.
const { providers,Wallet } =require("ethers");constprovider=newproviders.JsonRpcProvider(process.env.JSON_RPC_PROVIDER_URL,);constpayerWallet=newWallet(process.env.PAYER_PRIVATE_KEY,// Must include 0x prefix provider,);
Coming soon. Probably involves publicClientToProvider() and walletClientToSigner().
Then, check that the payer has sufficient funds using hasSufficientFunds()
Then, in the case of an ERC-20 request, check that the payer has granted sufficient approval using hasErc20Approval(). If not, submit an approval transaction using approveErc20. Wait for an appropriate number of block confirmations. On Sepolia or Ethereum, 2 block confirmations should suffice. Other chains may require more.
Then, call fromIdentity() to get an array of Request objects or fromRequestId() to get a single Request object. This function retrieves the Requests stored in IPFS and queries on-chain events to determine the balances paid so far. Finally, call getData() on each Request to get the request contents.