Skip to content

Commit d0df5b9

Browse files
committed
feat: add delegate declarative payment example
1 parent b552378 commit d0df5b9

File tree

3 files changed

+252
-1
lines changed

3 files changed

+252
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ npm run retrieve
4040
npm run create
4141
npm run pay
4242
npm run declare
43+
npm run delegate
4344
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"retrieve": "node src/retrieveRequest.js",
1010
"create": "node src/createRequest.js",
1111
"pay": "node src/payRequest.js",
12-
"declare": "node src/declarePaymentSentAndReceived.js"
12+
"declare": "node src/declarePaymentSentAndReceived.js",
13+
"delegate": "node src/delegateDeclarePaymentSentAndReceived.js"
1314
},
1415
"author": "",
1516
"license": "ISC",
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
const waitForConfirmation = async (dataOrPromise) => {
2+
const data = await dataOrPromise;
3+
return new Promise((resolve, reject) => {
4+
data.on("confirmed", resolve);
5+
data.on("error", reject);
6+
});
7+
};
8+
9+
(async () => {
10+
const {
11+
RequestNetwork,
12+
Types,
13+
Utils,
14+
} = require("@requestnetwork/request-client.js");
15+
const {
16+
EthereumPrivateKeySignatureProvider,
17+
} = require("@requestnetwork/epk-signature");
18+
const { config } = require("dotenv");
19+
const { Wallet } = require("ethers");
20+
21+
// Load environment variables from .env file
22+
config();
23+
24+
const payeeEpkSignatureProvider = new EthereumPrivateKeySignatureProvider({
25+
method: Types.Signature.METHOD.ECDSA,
26+
privateKey: process.env.PAYEE_PRIVATE_KEY, // Must include 0x prefix
27+
});
28+
29+
const payerEpkSignatureProvider = new EthereumPrivateKeySignatureProvider({
30+
method: Types.Signature.METHOD.ECDSA,
31+
privateKey: process.env.PAYER_PRIVATE_KEY, // Must include 0x prefix
32+
});
33+
34+
const payeeDelegateEpkSignatureProvider =
35+
new EthereumPrivateKeySignatureProvider({
36+
method: Types.Signature.METHOD.ECDSA,
37+
privateKey: process.env.PAYEE_DELEGATE_PRIVATE_KEY, // Must include 0x prefix
38+
});
39+
40+
const payerDelegateEpkSignatureProvider =
41+
new EthereumPrivateKeySignatureProvider({
42+
method: Types.Signature.METHOD.ECDSA,
43+
privateKey: process.env.PAYER_DELEGATE_PRIVATE_KEY, // Must include 0x prefix
44+
});
45+
46+
const payeeRequestClient = new RequestNetwork({
47+
nodeConnectionConfig: {
48+
baseURL: "https://sepolia.gateway.request.network/",
49+
},
50+
signatureProvider: payeeEpkSignatureProvider,
51+
});
52+
53+
const payerRequestClient = new RequestNetwork({
54+
nodeConnectionConfig: {
55+
baseURL: "https://sepolia.gateway.request.network/",
56+
},
57+
signatureProvider: payerEpkSignatureProvider,
58+
});
59+
60+
const payeeDelegateRequestClient = new RequestNetwork({
61+
nodeConnectionConfig: {
62+
baseURL: "https://sepolia.gateway.request.network/",
63+
},
64+
signatureProvider: payeeDelegateEpkSignatureProvider,
65+
});
66+
67+
const payerDelegateRequestClient = new RequestNetwork({
68+
nodeConnectionConfig: {
69+
baseURL: "https://sepolia.gateway.request.network/",
70+
},
71+
signatureProvider: payerDelegateEpkSignatureProvider,
72+
});
73+
74+
const payeeIdentityAddress = new Wallet(process.env.PAYEE_PRIVATE_KEY)
75+
.address;
76+
const payerIdentityAddress = new Wallet(process.env.PAYER_PRIVATE_KEY)
77+
.address;
78+
const payeeDelegateIdentityAddress = new Wallet(
79+
process.env.PAYEE_DELEGATE_PRIVATE_KEY,
80+
).address;
81+
const payerDelegateIdentityAddress = new Wallet(
82+
process.env.PAYER_DELEGATE_PRIVATE_KEY,
83+
).address;
84+
85+
const payeeIdentity = {
86+
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
87+
value: payeeIdentityAddress,
88+
};
89+
90+
const payerIdentity = {
91+
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
92+
value: payerIdentityAddress,
93+
};
94+
95+
const payeeDelegateIdentity = {
96+
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
97+
value: payeeDelegateIdentityAddress,
98+
};
99+
100+
const payerDelegateIdentity = {
101+
type: Types.Identity.TYPE.ETHEREUM_ADDRESS,
102+
value: payerDelegateIdentityAddress,
103+
};
104+
105+
// In this example, the payee is also the payment recipient.
106+
const paymentRecipient = payeeIdentityAddress;
107+
const feeRecipient = "0x0000000000000000000000000000000000000000";
108+
109+
const requestCreateParameters = {
110+
requestInfo: {
111+
currency: {
112+
type: Types.RequestLogic.CURRENCY.ERC20,
113+
value: "0x370DE27fdb7D1Ff1e1BaA7D11c5820a324Cf623C", // FAU token address
114+
network: "sepolia",
115+
},
116+
expectedAmount: "1000000000000000000", // 1.0
117+
payee: payeeIdentity,
118+
payer: payerIdentity,
119+
timestamp: Utils.getCurrentTimestampInSecond(),
120+
},
121+
paymentNetwork: {
122+
// We can declare payments because ERC20 fee proxy payment network inherits from declarative payment network
123+
id: Types.Extension.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT,
124+
parameters: {
125+
paymentNetworkName: "sepolia",
126+
paymentAddress: paymentRecipient,
127+
feeAddress: feeRecipient,
128+
feeAmount: "0",
129+
},
130+
},
131+
contentData: {
132+
reason: "🍕",
133+
dueDate: "2023.06.16",
134+
builderId: "request-network",
135+
createdWith: "quickstart",
136+
},
137+
signer: payeeIdentity,
138+
};
139+
140+
const payeeRequest = await payeeRequestClient.createRequest(
141+
requestCreateParameters,
142+
);
143+
const payeeRequestData = await payeeRequest.waitForConfirmation();
144+
145+
const payeeRequestDataAfterDelegate =
146+
await payeeRequest.addDeclarativeDelegate(
147+
payeeDelegateIdentity,
148+
payeeIdentity,
149+
);
150+
console.log(
151+
"payeeRequestDataAfterDelegate: " +
152+
JSON.stringify(payeeRequestDataAfterDelegate, null, 2),
153+
);
154+
155+
const payeeRequestDataAfterDelegateConfirmed = await waitForConfirmation(
156+
payeeRequestDataAfterDelegate,
157+
);
158+
console.log(
159+
"payeeRequestDataAfterDelegateConfirmed: " +
160+
JSON.stringify(payeeRequestDataAfterDelegateConfirmed, null, 2),
161+
);
162+
163+
const payerRequest = await payerRequestClient.fromRequestId(
164+
payeeRequestData.requestId,
165+
);
166+
const payerRequestData = payerRequest.getData();
167+
168+
const payerRequestDataAfterDelegate =
169+
await payerRequest.addDeclarativeDelegate(
170+
payerDelegateIdentity,
171+
payerIdentity,
172+
);
173+
console.log(
174+
"payerRequestDataAfterDelegate: " +
175+
JSON.stringify(payerRequestDataAfterDelegate, null, 2),
176+
);
177+
178+
const payerRequestDataAfterDelegateConfirmed = await waitForConfirmation(
179+
payerRequestDataAfterDelegate,
180+
);
181+
console.log(
182+
"payerRequestDataAfterDelegateConfirmed: " +
183+
JSON.stringify(payerRequestDataAfterDelegateConfirmed, null, 2),
184+
);
185+
186+
const payerDelegateRequest = await payerDelegateRequestClient.fromRequestId(
187+
payeeRequestData.requestId,
188+
);
189+
190+
const payerDelegateRequestData = payerDelegateRequest.getData();
191+
192+
const payerDelegateRequestDataAfterSent =
193+
await payerDelegateRequest.declareSentPayment(
194+
payerDelegateRequestData.expectedAmount,
195+
"payment initiated from the bank",
196+
payerDelegateIdentity,
197+
);
198+
console.log(
199+
"payerDelegateRequestDataAfterSent: " +
200+
JSON.stringify(payerDelegateRequestDataAfterSent, null, 2),
201+
);
202+
203+
const payerDelegateRequestDataAfterSentConfirmed = await waitForConfirmation(
204+
payerDelegateRequestDataAfterSent,
205+
);
206+
console.log(
207+
"payerDelegateRequestDataAfterSentConfirmed: " +
208+
JSON.stringify(payerDelegateRequestDataAfterSentConfirmed, null, 2),
209+
);
210+
console.log(
211+
"Observe extensionsData contains 5 events: paymentNetwork 'create', contentData 'create', paymentNetwork 'delegate' x2, and paymentNetwork 'declareSentPayment'",
212+
);
213+
214+
const payeeDelegateRequest = await payeeDelegateRequestClient.fromRequestId(
215+
payeeRequestData.requestId,
216+
);
217+
218+
const payeeDelegateRequestData = payeeDelegateRequest.getData();
219+
220+
const payeeDelegateRequestDataAfterReceived =
221+
await payeeDelegateRequest.declareReceivedPayment(
222+
payeeDelegateRequestData.expectedAmount,
223+
"payment received from the bank",
224+
payeeDelegateIdentity,
225+
);
226+
227+
const payeeDelegateRequestDataAfterReceivedConfirmed =
228+
await waitForConfirmation(payeeDelegateRequestDataAfterReceived);
229+
console.log(
230+
"payeeDelegateRequestDataAfterReceivedConfirmed: " +
231+
JSON.stringify(payeeDelegateRequestDataAfterReceivedConfirmed, null, 2),
232+
);
233+
console.log(
234+
"Observe extensionsData contains 6 events: paymentNetwork 'create', contentData 'create', paymentNetwork 'delegate' x2, paymentNetwork 'declareSentPayment', and paymentNetwork 'declareReceivedPayment'",
235+
);
236+
237+
console.log(
238+
"Request balance: " +
239+
payeeDelegateRequestDataAfterReceivedConfirmed.balance.balance,
240+
);
241+
console.log(
242+
"Request balance events: " +
243+
JSON.stringify(
244+
payeeDelegateRequestDataAfterReceivedConfirmed.balance.events,
245+
null,
246+
2,
247+
),
248+
);
249+
})();

0 commit comments

Comments
 (0)