@@ -4,24 +4,24 @@ import {
4
4
getPaymentNetworkExtension ,
5
5
} from '@requestnetwork/payment-detection' ;
6
6
import { ClientTypes , ExtensionTypes , CurrencyTypes } from '@requestnetwork/types' ;
7
- import { singleRequestProxyFactoryArtifact } from '@requestnetwork/smart-contracts' ;
7
+ import { singleRequestForwarderFactoryArtifact } from '@requestnetwork/smart-contracts' ;
8
8
import { IERC20__factory } from '@requestnetwork/smart-contracts/types' ;
9
9
10
10
/**
11
- * Deploys a Single Request Proxy contract for a given request.
11
+ * Deploys a Single Request Forwarder contract for a given request.
12
12
*
13
13
* @param request - The request data object containing payment network and currency information.
14
14
* @param signer - The Ethereum signer used to deploy the contract.
15
- * @returns A Promise that resolves to the address of the deployed Single Request Proxy contract.
15
+ * @returns A Promise that resolves to the address of the deployed Single Request Forwarder contract.
16
16
* @throws {Error } If the payment network is unsupported, payment chain is not found, payee is not found, or if there are invalid payment network values.
17
17
*
18
18
* @remarks
19
- * This function supports deploying proxies for ERC20_FEE_PROXY_CONTRACT and ETH_FEE_PROXY_CONTRACT payment networks.
20
- * It uses the SingleRequestProxyFactory contract to create either an ERC20 or Ethereum Single Request Proxy .
19
+ * This function supports deploying forwarders for ERC20_FEE_PROXY_CONTRACT and ETH_FEE_PROXY_CONTRACT payment networks.
20
+ * It uses the SingleRequestForwarderFactory contract to create either an ERC20 or Ethereum Single Request Forwarder .
21
21
* The function calculates the payment reference and handles the deployment transaction, including waiting for confirmation.
22
- * The factory address is automatically determined based on the payment chain using the singleRequestProxyFactoryArtifact .
22
+ * The factory address is automatically determined based on the payment chain using the singleRequestForwarderFactoryArtifact .
23
23
*/
24
- export async function deploySingleRequestProxy (
24
+ export async function deploySingleRequestForwarder (
25
25
request : ClientTypes . IRequestData ,
26
26
signer : Signer ,
27
27
) : Promise < string > {
@@ -42,13 +42,13 @@ export async function deploySingleRequestProxy(
42
42
}
43
43
44
44
// Use artifact's default address for the payment chain
45
- const singleRequestProxyFactory = singleRequestProxyFactoryArtifact . connect (
45
+ const singleRequestForwarderFactory = singleRequestForwarderFactoryArtifact . connect (
46
46
paymentChain as CurrencyTypes . EvmChainName ,
47
47
signer ,
48
48
) ;
49
49
50
- if ( ! singleRequestProxyFactory . address ) {
51
- throw new Error ( `SingleRequestProxyFactory not found on chain ${ paymentChain } ` ) ;
50
+ if ( ! singleRequestForwarderFactory . address ) {
51
+ throw new Error ( `SingleRequestForwarderFactory not found on chain ${ paymentChain } ` ) ;
52
52
}
53
53
54
54
const salt = requestPaymentNetwork ?. values ?. salt ;
@@ -73,15 +73,15 @@ export async function deploySingleRequestProxy(
73
73
74
74
if ( isERC20 ) {
75
75
const tokenAddress = request . currencyInfo . value ;
76
- tx = await singleRequestProxyFactory . createERC20SingleRequestProxy (
76
+ tx = await singleRequestForwarderFactory . createERC20SingleRequestProxy (
77
77
paymentRecipient ,
78
78
tokenAddress ,
79
79
paymentReference ,
80
80
feeAddress ,
81
81
feeAmount ,
82
82
) ;
83
83
} else {
84
- tx = await singleRequestProxyFactory . createEthereumSingleRequestProxy (
84
+ tx = await singleRequestForwarderFactory . createEthereumSingleRequestProxy (
85
85
paymentRecipient ,
86
86
paymentReference ,
87
87
feeAddress ,
@@ -92,134 +92,137 @@ export async function deploySingleRequestProxy(
92
92
const receipt = await tx . wait ( ) ;
93
93
94
94
const event = receipt . events ?. find (
95
- ( e ) =>
95
+ ( e : ethers . Event ) =>
96
96
e . event ===
97
97
( isERC20 ? 'ERC20SingleRequestProxyCreated' : 'EthereumSingleRequestProxyCreated' ) ,
98
98
) ;
99
99
100
100
if ( ! event ) {
101
- throw new Error ( 'Single request proxy creation event not found' ) ;
101
+ throw new Error ( 'Single request forwarder creation event not found' ) ;
102
102
}
103
103
104
- const proxyAddress = event . args ?. proxyAddress || event . args ?. [ 0 ] ;
104
+ const forwarderAddress = event . args ?. proxyAddress || event . args ?. [ 0 ] ;
105
105
106
- if ( ! proxyAddress ) {
107
- throw new Error ( 'Proxy address not found in event data' ) ;
106
+ if ( ! forwarderAddress ) {
107
+ throw new Error ( 'Forwarder address not found in event data' ) ;
108
108
}
109
109
110
- return proxyAddress ;
110
+ return forwarderAddress ;
111
111
}
112
112
113
113
/**
114
- * Validates that a contract is a SingleRequestProxy by checking required methods
114
+ * Validates that a contract is a SingleRequestForwarder by checking required methods
115
115
* @param proxyAddress - The address of the contract to validate
116
116
* @param signer - The Ethereum signer used to interact with the contract
117
- * @throws {Error } If the contract is not a valid SingleRequestProxy
117
+ * @throws {Error } If the contract is not a valid SingleRequestForwarder
118
118
*/
119
- async function validateSingleRequestProxy ( proxyAddress : string , signer : Signer ) : Promise < void > {
120
- const proxyInterface = new ethers . utils . Interface ( [
119
+ async function validateSingleRequestForwarder (
120
+ forwarderAddress : string ,
121
+ signer : Signer ,
122
+ ) : Promise < void > {
123
+ const forwarderInterface = new ethers . utils . Interface ( [
121
124
'function payee() view returns (address)' ,
122
125
'function paymentReference() view returns (bytes)' ,
123
126
'function feeAddress() view returns (address)' ,
124
127
'function feeAmount() view returns (uint256)' ,
125
128
] ) ;
126
129
127
- const proxyContract = new Contract ( proxyAddress , proxyInterface , signer ) ;
130
+ const forwarderContract = new Contract ( forwarderAddress , forwarderInterface , signer ) ;
128
131
129
132
try {
130
133
await Promise . all ( [
131
- proxyContract . payee ( ) ,
132
- proxyContract . paymentReference ( ) ,
133
- proxyContract . feeAddress ( ) ,
134
- proxyContract . feeAmount ( ) ,
134
+ forwarderContract . payee ( ) ,
135
+ forwarderContract . paymentReference ( ) ,
136
+ forwarderContract . feeAddress ( ) ,
137
+ forwarderContract . feeAmount ( ) ,
135
138
] ) ;
136
139
} catch ( error ) {
137
- throw new Error ( 'Invalid SingleRequestProxy contract' ) ;
140
+ throw new Error ( 'Invalid SingleRequestForwarder contract' ) ;
138
141
}
139
142
}
140
143
141
144
/**
142
- * Executes a payment through an ERC20SingleRequestProxy contract
143
- * @param proxyAddress - The address of the SingleRequestProxy contract
145
+ * Executes a payment through an ERC20SingleRequestForwarder contract
146
+ * @param forwarderAddress - The address of the SingleRequestForwarder contract
144
147
* @param signer - The Ethereum signer used to execute the payment transaction
145
148
* @param amount - The amount to be paid
146
- * @throws {Error } If the contract is not an ERC20SingleRequestProxy
149
+ * @throws {Error } If the contract is not an ERC20SingleRequestForwarder
147
150
*/
148
- export async function payWithERC20SingleRequestProxy (
149
- proxyAddress : string ,
151
+ export async function payWithERC20SingleRequestForwarder (
152
+ forwarderAddress : string ,
150
153
signer : Signer ,
151
154
amount : string ,
152
155
) : Promise < void > {
153
156
if ( ! amount || ethers . BigNumber . from ( amount ) . lte ( 0 ) ) {
154
157
throw new Error ( 'Amount must be a positive number' ) ;
155
158
}
156
159
157
- const proxyInterface = new ethers . utils . Interface ( [
160
+ const forwarderInterface = new ethers . utils . Interface ( [
158
161
'function tokenAddress() view returns (address)' ,
159
162
] ) ;
160
163
161
- const proxyContract = new Contract ( proxyAddress , proxyInterface , signer ) ;
164
+ const forwarderContract = new Contract ( forwarderAddress , forwarderInterface , signer ) ;
162
165
163
166
let tokenAddress : string ;
164
167
try {
165
- // Attempt to fetch the token address from the proxy contract, to determine if it's an ERC20 SingleRequestProxy .
166
- tokenAddress = await proxyContract . tokenAddress ( ) ;
168
+ // Attempt to fetch the token address from the forwarder contract, to determine if it's an ERC20 SingleRequestForwarder .
169
+ tokenAddress = await forwarderContract . tokenAddress ( ) ;
167
170
} catch {
168
- throw new Error ( 'Contract is not an ERC20SingleRequestProxy ' ) ;
171
+ throw new Error ( 'Contract is not an ERC20SingleRequestForwarder ' ) ;
169
172
}
170
173
171
174
const erc20Contract = IERC20__factory . connect ( tokenAddress , signer ) ;
172
175
173
- // Transfer tokens to the proxy
174
- const transferTx = await erc20Contract . transfer ( proxyAddress , amount ) ;
176
+ // Transfer tokens to the forwarder
177
+ const transferTx = await erc20Contract . transfer ( forwarderAddress , amount ) ;
175
178
await transferTx . wait ( ) ;
176
179
177
180
// Trigger the proxy's receive function to finalize payment
178
181
const triggerTx = await signer . sendTransaction ( {
179
- to : proxyAddress ,
182
+ to : forwarderAddress ,
180
183
value : ethers . constants . Zero ,
181
184
} ) ;
182
185
await triggerTx . wait ( ) ;
183
186
}
184
187
185
188
/**
186
- * Executes a payment through an EthereumSingleRequestProxy contract
187
- * @param proxyAddress - The address of the SingleRequestProxy contract
189
+ * Executes a payment through an EthereumSingleRequestForwarder contract
190
+ * @param forwarderAddress - The address of the SingleRequestForwarder contract
188
191
* @param signer - The Ethereum signer used to execute the payment transaction
189
192
* @param amount - The amount to be paid
190
- * @throws {Error } If the contract is an ERC20SingleRequestProxy
193
+ * @throws {Error } If the contract is not an EthereumSingleRequestForwarder
191
194
*/
192
- export async function payWithEthereumSingleRequestProxy (
193
- proxyAddress : string ,
195
+ export async function payWithEthereumSingleRequestForwarder (
196
+ forwarderAddress : string ,
194
197
signer : Signer ,
195
198
amount : string ,
196
199
) : Promise < void > {
197
200
if ( ! amount || ethers . BigNumber . from ( amount ) . lte ( 0 ) ) {
198
201
throw new Error ( 'Amount must be a positive number' ) ;
199
202
}
200
203
201
- const proxyInterface = new ethers . utils . Interface ( [
204
+ const forwarderInterface = new ethers . utils . Interface ( [
202
205
'function tokenAddress() view returns (address)' ,
203
206
] ) ;
204
207
205
- const proxyContract = new Contract ( proxyAddress , proxyInterface , signer ) ;
208
+ const forwarderContract = new Contract ( forwarderAddress , forwarderInterface , signer ) ;
206
209
207
210
try {
208
- // Attempt to fetch the token address from the proxy contract, to determine if it's an Ethereum SingleRequestProxy .
209
- await proxyContract . tokenAddress ( ) ;
211
+ // Attempt to fetch the token address from the forwarder contract, to determine if it's an Ethereum SingleRequestForwarder .
212
+ await forwarderContract . tokenAddress ( ) ;
210
213
211
- // If the token address is fetched, it means the contract is an ERC20SingleRequestProxy .
212
- throw new Error ( 'Contract is not an EthereumSingleRequestProxy ' ) ;
214
+ // If the token address is fetched, it means the contract is an ERC20SingleRequestForwarder .
215
+ throw new Error ( 'Contract is not an EthereumSingleRequestForwarder ' ) ;
213
216
} catch ( error ) {
214
- // If the token address is not fetched, it means the contract is an EthereumSingleRequestProxy .
215
- if ( error . message === 'Contract is not an EthereumSingleRequestProxy ' ) {
216
- // If the error message is 'Contract is not an EthereumSingleRequestProxy ', throw the error.
217
+ // If the token address is not fetched, it means the contract is an EthereumSingleRequestForwarder .
218
+ if ( error . message === 'Contract is not an EthereumSingleRequestForwarder ' ) {
219
+ // If the error message is 'Contract is not an EthereumSingleRequestForwarder ', throw the error.
217
220
throw error ;
218
221
}
219
222
}
220
223
221
224
const tx = await signer . sendTransaction ( {
222
- to : proxyAddress ,
225
+ to : forwarderAddress ,
223
226
value : amount ,
224
227
} ) ;
225
228
await tx . wait ( ) ;
@@ -228,48 +231,48 @@ export async function payWithEthereumSingleRequestProxy(
228
231
/**
229
232
* Executes a payment through a Single Request Proxy contract.
230
233
*
231
- * @param singleRequestProxyAddress - The address of the deployed Single Request Proxy contract.
234
+ * @param singleRequestForwarderAddress - The address of the deployed Single Request Forwarder contract.
232
235
* @param signer - The Ethereum signer used to execute the payment transaction.
233
236
* @param amount - The amount to be paid, as a string representation of the value.
234
237
* @returns A Promise that resolves when the payment transaction is confirmed.
235
- * @throws {Error } If the SingleRequestProxy contract is invalid.
236
- * @throws {Error } If the proxy contract type cannot be determined, or if any transaction fails.
238
+ * @throws {Error } If the SingleRequestForwarder contract is invalid.
239
+ * @throws {Error } If the forwarder contract type cannot be determined, or if any transaction fails.
237
240
*
238
241
* @remarks
239
242
* This function supports both ERC20 and Ethereum payments.
240
- * For ERC20 payments, it first transfers the tokens to the proxy contract and then triggers the payment.
241
- * For Ethereum payments, it directly sends the Ether to the proxy contract.
243
+ * For ERC20 payments, it first transfers the tokens to the forwarder contract and then triggers the payment.
244
+ * For Ethereum payments, it directly sends the Ether to the forwarder contract.
242
245
* The function automatically detects whether the proxy is for ERC20 or Ethereum based on the contract interface.
243
246
*/
244
- export async function payRequestWithSingleRequestProxy (
245
- singleRequestProxyAddress : string ,
247
+ export async function payRequestWithSingleRequestForwarder (
248
+ singleRequestForwarderAddress : string ,
246
249
signer : Signer ,
247
250
amount : string ,
248
251
) : Promise < void > {
249
252
if ( ! amount || ethers . BigNumber . from ( amount ) . lte ( 0 ) ) {
250
253
throw new Error ( 'Amount must be a positive number' ) ;
251
254
}
252
255
253
- // Validate the SingleRequestProxy contract
254
- await validateSingleRequestProxy ( singleRequestProxyAddress , signer ) ;
256
+ // Validate the SingleRequestForwarder contract
257
+ await validateSingleRequestForwarder ( singleRequestForwarderAddress , signer ) ;
255
258
256
- const proxyInterface = new ethers . utils . Interface ( [
259
+ const forwarderInterface = new ethers . utils . Interface ( [
257
260
'function tokenAddress() view returns (address)' ,
258
261
] ) ;
259
262
260
- const proxyContract = new Contract ( singleRequestProxyAddress , proxyInterface , signer ) ;
263
+ const forwarderContract = new Contract ( singleRequestForwarderAddress , forwarderInterface , signer ) ;
261
264
262
265
let isERC20 : boolean ;
263
266
try {
264
- await proxyContract . tokenAddress ( ) ;
267
+ await forwarderContract . tokenAddress ( ) ;
265
268
isERC20 = true ;
266
269
} catch {
267
270
isERC20 = false ;
268
271
}
269
272
270
273
if ( isERC20 ) {
271
- await payWithERC20SingleRequestProxy ( singleRequestProxyAddress , signer , amount ) ;
274
+ await payWithERC20SingleRequestForwarder ( singleRequestForwarderAddress , signer , amount ) ;
272
275
} else {
273
- await payWithEthereumSingleRequestProxy ( singleRequestProxyAddress , signer , amount ) ;
276
+ await payWithEthereumSingleRequestForwarder ( singleRequestForwarderAddress , signer , amount ) ;
274
277
}
275
278
}
0 commit comments