-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add delegate declarative payment example #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,4 +40,5 @@ npm run retrieve | |
| npm run create | ||
| npm run pay | ||
| npm run declare | ||
| npm run delegate | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| const waitForConfirmation = async (dataOrPromise) => { | ||
| const data = await dataOrPromise; | ||
| return new Promise((resolve, reject) => { | ||
| data.on("confirmed", resolve); | ||
| data.on("error", reject); | ||
| }); | ||
| }; | ||
|
|
||
| (async () => { | ||
| const { | ||
| RequestNetwork, | ||
| Types, | ||
| Utils, | ||
| } = require("@requestnetwork/request-client.js"); | ||
| const { | ||
| EthereumPrivateKeySignatureProvider, | ||
| } = require("@requestnetwork/epk-signature"); | ||
| const { config } = require("dotenv"); | ||
| const { Wallet } = require("ethers"); | ||
|
|
||
| // Load environment variables from .env file | ||
| config(); | ||
|
|
||
| const payeeEpkSignatureProvider = new EthereumPrivateKeySignatureProvider({ | ||
| method: Types.Signature.METHOD.ECDSA, | ||
| privateKey: process.env.PAYEE_PRIVATE_KEY, // Must include 0x prefix | ||
| }); | ||
|
|
||
| const payerEpkSignatureProvider = new EthereumPrivateKeySignatureProvider({ | ||
| method: Types.Signature.METHOD.ECDSA, | ||
| privateKey: process.env.PAYER_PRIVATE_KEY, // Must include 0x prefix | ||
| }); | ||
|
|
||
| const payeeDelegateEpkSignatureProvider = | ||
| new EthereumPrivateKeySignatureProvider({ | ||
| method: Types.Signature.METHOD.ECDSA, | ||
| privateKey: process.env.PAYEE_DELEGATE_PRIVATE_KEY, // Must include 0x prefix | ||
| }); | ||
|
|
||
| const payerDelegateEpkSignatureProvider = | ||
| new EthereumPrivateKeySignatureProvider({ | ||
| method: Types.Signature.METHOD.ECDSA, | ||
| privateKey: process.env.PAYER_DELEGATE_PRIVATE_KEY, // Must include 0x prefix | ||
| }); | ||
|
|
||
| const payeeRequestClient = new RequestNetwork({ | ||
| nodeConnectionConfig: { | ||
| baseURL: "https://sepolia.gateway.request.network/", | ||
| }, | ||
| signatureProvider: payeeEpkSignatureProvider, | ||
| }); | ||
|
|
||
| const payerRequestClient = new RequestNetwork({ | ||
| nodeConnectionConfig: { | ||
| baseURL: "https://sepolia.gateway.request.network/", | ||
| }, | ||
| signatureProvider: payerEpkSignatureProvider, | ||
| }); | ||
|
|
||
| const payeeDelegateRequestClient = new RequestNetwork({ | ||
| nodeConnectionConfig: { | ||
| baseURL: "https://sepolia.gateway.request.network/", | ||
| }, | ||
| signatureProvider: payeeDelegateEpkSignatureProvider, | ||
| }); | ||
|
|
||
| const payerDelegateRequestClient = new RequestNetwork({ | ||
| nodeConnectionConfig: { | ||
| baseURL: "https://sepolia.gateway.request.network/", | ||
| }, | ||
| signatureProvider: payerDelegateEpkSignatureProvider, | ||
| }); | ||
|
|
||
| const payeeIdentityAddress = new Wallet(process.env.PAYEE_PRIVATE_KEY) | ||
| .address; | ||
| const payerIdentityAddress = new Wallet(process.env.PAYER_PRIVATE_KEY) | ||
| .address; | ||
| const payeeDelegateIdentityAddress = new Wallet( | ||
| process.env.PAYEE_DELEGATE_PRIVATE_KEY, | ||
| ).address; | ||
| const payerDelegateIdentityAddress = new Wallet( | ||
| process.env.PAYER_DELEGATE_PRIVATE_KEY, | ||
| ).address; | ||
|
|
||
| const payeeIdentity = { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: payeeIdentityAddress, | ||
| }; | ||
|
|
||
| const payerIdentity = { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: payerIdentityAddress, | ||
| }; | ||
|
|
||
| const payeeDelegateIdentity = { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: payeeDelegateIdentityAddress, | ||
| }; | ||
|
|
||
| const payerDelegateIdentity = { | ||
| type: Types.Identity.TYPE.ETHEREUM_ADDRESS, | ||
| value: payerDelegateIdentityAddress, | ||
| }; | ||
|
|
||
| // In this example, the payee is also the payment recipient. | ||
| const paymentRecipient = payeeIdentityAddress; | ||
| const feeRecipient = "0x0000000000000000000000000000000000000000"; | ||
|
|
||
| const requestCreateParameters = { | ||
| requestInfo: { | ||
| currency: { | ||
| type: Types.RequestLogic.CURRENCY.ERC20, | ||
| value: "0x370DE27fdb7D1Ff1e1BaA7D11c5820a324Cf623C", // FAU token address | ||
| network: "sepolia", | ||
| }, | ||
| expectedAmount: "1000000000000000000", // 1.0 | ||
| payee: payeeIdentity, | ||
| payer: payerIdentity, | ||
| timestamp: Utils.getCurrentTimestampInSecond(), | ||
| }, | ||
| paymentNetwork: { | ||
| // We can declare payments because ERC20 fee proxy payment network inherits from declarative payment network | ||
| 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", | ||
| builderId: "request-network", | ||
| createdWith: "quickstart", | ||
| }, | ||
| signer: payeeIdentity, | ||
| }; | ||
|
|
||
| const payeeRequest = await payeeRequestClient.createRequest( | ||
| requestCreateParameters, | ||
| ); | ||
| const payeeRequestData = await payeeRequest.waitForConfirmation(); | ||
|
|
||
| const payeeRequestDataAfterDelegate = | ||
| await payeeRequest.addDeclarativeDelegate( | ||
| payeeDelegateIdentity, | ||
| payeeIdentity, | ||
| ); | ||
| console.log( | ||
| "payeeRequestDataAfterDelegate: " + | ||
| JSON.stringify(payeeRequestDataAfterDelegate, null, 2), | ||
| ); | ||
|
|
||
| const payeeRequestDataAfterDelegateConfirmed = await waitForConfirmation( | ||
| payeeRequestDataAfterDelegate, | ||
| ); | ||
| console.log( | ||
| "payeeRequestDataAfterDelegateConfirmed: " + | ||
| JSON.stringify(payeeRequestDataAfterDelegateConfirmed, null, 2), | ||
| ); | ||
|
|
||
| const payerRequest = await payerRequestClient.fromRequestId( | ||
| payeeRequestData.requestId, | ||
| ); | ||
| const payerRequestData = payerRequest.getData(); | ||
|
|
||
| const payerRequestDataAfterDelegate = | ||
| await payerRequest.addDeclarativeDelegate( | ||
| payerDelegateIdentity, | ||
| payerIdentity, | ||
| ); | ||
| console.log( | ||
| "payerRequestDataAfterDelegate: " + | ||
| JSON.stringify(payerRequestDataAfterDelegate, null, 2), | ||
| ); | ||
|
|
||
| const payerRequestDataAfterDelegateConfirmed = await waitForConfirmation( | ||
| payerRequestDataAfterDelegate, | ||
| ); | ||
| console.log( | ||
| "payerRequestDataAfterDelegateConfirmed: " + | ||
| JSON.stringify(payerRequestDataAfterDelegateConfirmed, null, 2), | ||
| ); | ||
|
|
||
| const payerDelegateRequest = await payerDelegateRequestClient.fromRequestId( | ||
| payeeRequestData.requestId, | ||
| ); | ||
|
|
||
| const payerDelegateRequestData = payerDelegateRequest.getData(); | ||
|
|
||
| const payerDelegateRequestDataAfterSent = | ||
| await payerDelegateRequest.declareSentPayment( | ||
| payerDelegateRequestData.expectedAmount, | ||
| "payment initiated from the bank", | ||
| payerDelegateIdentity, | ||
| ); | ||
| console.log( | ||
| "payerDelegateRequestDataAfterSent: " + | ||
| JSON.stringify(payerDelegateRequestDataAfterSent, null, 2), | ||
| ); | ||
|
|
||
| const payerDelegateRequestDataAfterSentConfirmed = await waitForConfirmation( | ||
| payerDelegateRequestDataAfterSent, | ||
| ); | ||
| console.log( | ||
| "payerDelegateRequestDataAfterSentConfirmed: " + | ||
| JSON.stringify(payerDelegateRequestDataAfterSentConfirmed, null, 2), | ||
| ); | ||
| console.log( | ||
| "Observe extensionsData contains 5 events: paymentNetwork 'create', contentData 'create', paymentNetwork 'delegate' x2, and paymentNetwork 'declareSentPayment'", | ||
| ); | ||
|
|
||
| const payeeDelegateRequest = await payeeDelegateRequestClient.fromRequestId( | ||
| payeeRequestData.requestId, | ||
| ); | ||
|
|
||
| const payeeDelegateRequestData = payeeDelegateRequest.getData(); | ||
|
|
||
| const payeeDelegateRequestDataAfterReceived = | ||
| await payeeDelegateRequest.declareReceivedPayment( | ||
| payeeDelegateRequestData.expectedAmount, | ||
| "payment received from the bank", | ||
| payeeDelegateIdentity, | ||
| ); | ||
|
|
||
| const payeeDelegateRequestDataAfterReceivedConfirmed = | ||
| await waitForConfirmation(payeeDelegateRequestDataAfterReceived); | ||
| console.log( | ||
| "payeeDelegateRequestDataAfterReceivedConfirmed: " + | ||
| JSON.stringify(payeeDelegateRequestDataAfterReceivedConfirmed, null, 2), | ||
| ); | ||
| console.log( | ||
| "Observe extensionsData contains 6 events: paymentNetwork 'create', contentData 'create', paymentNetwork 'delegate' x2, paymentNetwork 'declareSentPayment', and paymentNetwork 'declareReceivedPayment'", | ||
| ); | ||
|
|
||
| console.log( | ||
| "Request balance: " + | ||
| payeeDelegateRequestDataAfterReceivedConfirmed.balance.balance, | ||
| ); | ||
| console.log( | ||
| "Request balance events: " + | ||
| JSON.stringify( | ||
| payeeDelegateRequestDataAfterReceivedConfirmed.balance.events, | ||
| null, | ||
| 2, | ||
| ), | ||
| ); | ||
| })(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.