Skip to content

Commit 9af0ebd

Browse files
authored
chore: rename single request proxy (#1488)
1 parent 0d77e49 commit 9af0ebd

File tree

6 files changed

+189
-179
lines changed

6 files changed

+189
-179
lines changed

packages/payment-processor/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export * from './payment/encoder-approval';
2727
export * as Escrow from './payment/erc20-escrow-payment';
2828
export * from './payment/prepared-transaction';
2929
export * from './payment/utils-near';
30-
export * from './payment/single-request-proxy';
30+
export * from './payment/single-request-forwarder';
3131

3232
import * as utils from './payment/utils';
3333

packages/payment-processor/src/payment/single-request-proxy.ts renamed to packages/payment-processor/src/payment/single-request-forwarder.ts

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import {
44
getPaymentNetworkExtension,
55
} from '@requestnetwork/payment-detection';
66
import { ClientTypes, ExtensionTypes, CurrencyTypes } from '@requestnetwork/types';
7-
import { singleRequestProxyFactoryArtifact } from '@requestnetwork/smart-contracts';
7+
import { singleRequestForwarderFactoryArtifact } from '@requestnetwork/smart-contracts';
88
import { IERC20__factory } from '@requestnetwork/smart-contracts/types';
99

1010
/**
11-
* Deploys a Single Request Proxy contract for a given request.
11+
* Deploys a Single Request Forwarder contract for a given request.
1212
*
1313
* @param request - The request data object containing payment network and currency information.
1414
* @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.
1616
* @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.
1717
*
1818
* @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.
2121
* 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.
2323
*/
24-
export async function deploySingleRequestProxy(
24+
export async function deploySingleRequestForwarder(
2525
request: ClientTypes.IRequestData,
2626
signer: Signer,
2727
): Promise<string> {
@@ -42,13 +42,13 @@ export async function deploySingleRequestProxy(
4242
}
4343

4444
// Use artifact's default address for the payment chain
45-
const singleRequestProxyFactory = singleRequestProxyFactoryArtifact.connect(
45+
const singleRequestForwarderFactory = singleRequestForwarderFactoryArtifact.connect(
4646
paymentChain as CurrencyTypes.EvmChainName,
4747
signer,
4848
);
4949

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}`);
5252
}
5353

5454
const salt = requestPaymentNetwork?.values?.salt;
@@ -73,15 +73,15 @@ export async function deploySingleRequestProxy(
7373

7474
if (isERC20) {
7575
const tokenAddress = request.currencyInfo.value;
76-
tx = await singleRequestProxyFactory.createERC20SingleRequestProxy(
76+
tx = await singleRequestForwarderFactory.createERC20SingleRequestProxy(
7777
paymentRecipient,
7878
tokenAddress,
7979
paymentReference,
8080
feeAddress,
8181
feeAmount,
8282
);
8383
} else {
84-
tx = await singleRequestProxyFactory.createEthereumSingleRequestProxy(
84+
tx = await singleRequestForwarderFactory.createEthereumSingleRequestProxy(
8585
paymentRecipient,
8686
paymentReference,
8787
feeAddress,
@@ -92,134 +92,137 @@ export async function deploySingleRequestProxy(
9292
const receipt = await tx.wait();
9393

9494
const event = receipt.events?.find(
95-
(e) =>
95+
(e: ethers.Event) =>
9696
e.event ===
9797
(isERC20 ? 'ERC20SingleRequestProxyCreated' : 'EthereumSingleRequestProxyCreated'),
9898
);
9999

100100
if (!event) {
101-
throw new Error('Single request proxy creation event not found');
101+
throw new Error('Single request forwarder creation event not found');
102102
}
103103

104-
const proxyAddress = event.args?.proxyAddress || event.args?.[0];
104+
const forwarderAddress = event.args?.proxyAddress || event.args?.[0];
105105

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');
108108
}
109109

110-
return proxyAddress;
110+
return forwarderAddress;
111111
}
112112

113113
/**
114-
* Validates that a contract is a SingleRequestProxy by checking required methods
114+
* Validates that a contract is a SingleRequestForwarder by checking required methods
115115
* @param proxyAddress - The address of the contract to validate
116116
* @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
118118
*/
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([
121124
'function payee() view returns (address)',
122125
'function paymentReference() view returns (bytes)',
123126
'function feeAddress() view returns (address)',
124127
'function feeAmount() view returns (uint256)',
125128
]);
126129

127-
const proxyContract = new Contract(proxyAddress, proxyInterface, signer);
130+
const forwarderContract = new Contract(forwarderAddress, forwarderInterface, signer);
128131

129132
try {
130133
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(),
135138
]);
136139
} catch (error) {
137-
throw new Error('Invalid SingleRequestProxy contract');
140+
throw new Error('Invalid SingleRequestForwarder contract');
138141
}
139142
}
140143

141144
/**
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
144147
* @param signer - The Ethereum signer used to execute the payment transaction
145148
* @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
147150
*/
148-
export async function payWithERC20SingleRequestProxy(
149-
proxyAddress: string,
151+
export async function payWithERC20SingleRequestForwarder(
152+
forwarderAddress: string,
150153
signer: Signer,
151154
amount: string,
152155
): Promise<void> {
153156
if (!amount || ethers.BigNumber.from(amount).lte(0)) {
154157
throw new Error('Amount must be a positive number');
155158
}
156159

157-
const proxyInterface = new ethers.utils.Interface([
160+
const forwarderInterface = new ethers.utils.Interface([
158161
'function tokenAddress() view returns (address)',
159162
]);
160163

161-
const proxyContract = new Contract(proxyAddress, proxyInterface, signer);
164+
const forwarderContract = new Contract(forwarderAddress, forwarderInterface, signer);
162165

163166
let tokenAddress: string;
164167
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();
167170
} catch {
168-
throw new Error('Contract is not an ERC20SingleRequestProxy');
171+
throw new Error('Contract is not an ERC20SingleRequestForwarder');
169172
}
170173

171174
const erc20Contract = IERC20__factory.connect(tokenAddress, signer);
172175

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);
175178
await transferTx.wait();
176179

177180
// Trigger the proxy's receive function to finalize payment
178181
const triggerTx = await signer.sendTransaction({
179-
to: proxyAddress,
182+
to: forwarderAddress,
180183
value: ethers.constants.Zero,
181184
});
182185
await triggerTx.wait();
183186
}
184187

185188
/**
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
188191
* @param signer - The Ethereum signer used to execute the payment transaction
189192
* @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
191194
*/
192-
export async function payWithEthereumSingleRequestProxy(
193-
proxyAddress: string,
195+
export async function payWithEthereumSingleRequestForwarder(
196+
forwarderAddress: string,
194197
signer: Signer,
195198
amount: string,
196199
): Promise<void> {
197200
if (!amount || ethers.BigNumber.from(amount).lte(0)) {
198201
throw new Error('Amount must be a positive number');
199202
}
200203

201-
const proxyInterface = new ethers.utils.Interface([
204+
const forwarderInterface = new ethers.utils.Interface([
202205
'function tokenAddress() view returns (address)',
203206
]);
204207

205-
const proxyContract = new Contract(proxyAddress, proxyInterface, signer);
208+
const forwarderContract = new Contract(forwarderAddress, forwarderInterface, signer);
206209

207210
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();
210213

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');
213216
} 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.
217220
throw error;
218221
}
219222
}
220223

221224
const tx = await signer.sendTransaction({
222-
to: proxyAddress,
225+
to: forwarderAddress,
223226
value: amount,
224227
});
225228
await tx.wait();
@@ -228,48 +231,48 @@ export async function payWithEthereumSingleRequestProxy(
228231
/**
229232
* Executes a payment through a Single Request Proxy contract.
230233
*
231-
* @param singleRequestProxyAddress - The address of the deployed Single Request Proxy contract.
234+
* @param singleRequestForwarderAddress - The address of the deployed Single Request Forwarder contract.
232235
* @param signer - The Ethereum signer used to execute the payment transaction.
233236
* @param amount - The amount to be paid, as a string representation of the value.
234237
* @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.
237240
*
238241
* @remarks
239242
* 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.
242245
* The function automatically detects whether the proxy is for ERC20 or Ethereum based on the contract interface.
243246
*/
244-
export async function payRequestWithSingleRequestProxy(
245-
singleRequestProxyAddress: string,
247+
export async function payRequestWithSingleRequestForwarder(
248+
singleRequestForwarderAddress: string,
246249
signer: Signer,
247250
amount: string,
248251
): Promise<void> {
249252
if (!amount || ethers.BigNumber.from(amount).lte(0)) {
250253
throw new Error('Amount must be a positive number');
251254
}
252255

253-
// Validate the SingleRequestProxy contract
254-
await validateSingleRequestProxy(singleRequestProxyAddress, signer);
256+
// Validate the SingleRequestForwarder contract
257+
await validateSingleRequestForwarder(singleRequestForwarderAddress, signer);
255258

256-
const proxyInterface = new ethers.utils.Interface([
259+
const forwarderInterface = new ethers.utils.Interface([
257260
'function tokenAddress() view returns (address)',
258261
]);
259262

260-
const proxyContract = new Contract(singleRequestProxyAddress, proxyInterface, signer);
263+
const forwarderContract = new Contract(singleRequestForwarderAddress, forwarderInterface, signer);
261264

262265
let isERC20: boolean;
263266
try {
264-
await proxyContract.tokenAddress();
267+
await forwarderContract.tokenAddress();
265268
isERC20 = true;
266269
} catch {
267270
isERC20 = false;
268271
}
269272

270273
if (isERC20) {
271-
await payWithERC20SingleRequestProxy(singleRequestProxyAddress, signer, amount);
274+
await payWithERC20SingleRequestForwarder(singleRequestForwarderAddress, signer, amount);
272275
} else {
273-
await payWithEthereumSingleRequestProxy(singleRequestProxyAddress, signer, amount);
276+
await payWithEthereumSingleRequestForwarder(singleRequestForwarderAddress, signer, amount);
274277
}
275278
}

0 commit comments

Comments
 (0)