Skip to content

Commit

Permalink
refactor: get default network from advanced logic (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrolland committed Apr 28, 2021
1 parent bbdd0b6 commit f83996b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Utils from '@requestnetwork/utils';
import ReferenceBased from './reference-based';

const CURRENT_VERSION = '0.1.0';
// Default network if the storage data does not give any
const DEFAULT_NETWORK = 'mainnet';

import * as walletAddressValidator from 'wallet-address-validator';

Expand Down Expand Up @@ -100,7 +102,7 @@ function createCreationAction(
throw Error('acceptedTokens must contains only valid ethereum addresses');
}

const network = creationParameters.network || 'mainnet';
const network = creationParameters.network || DEFAULT_NETWORK;
if (!supportedCurrencies[network]) {
throw Error(`network ${network} not supported`);
}
Expand Down Expand Up @@ -216,7 +218,10 @@ function applyActionToExtension(
actionSigner: IdentityTypes.IIdentity,
timestamp: number,
): RequestLogicTypes.IExtensionStates {
checkSupportedCurrency(requestState.currency, extensionAction.parameters.network || 'rinkeby');
checkSupportedCurrency(
requestState.currency,
extensionAction.parameters.network || DEFAULT_NETWORK,
);

const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState);

Expand Down Expand Up @@ -343,7 +348,7 @@ function applyCreation(
paymentAddress: extensionAction.parameters.paymentAddress,
refundAddress: extensionAction.parameters.refundAddress,
salt: extensionAction.parameters.salt,
network: extensionAction.parameters.network,
network: extensionAction.parameters.network || DEFAULT_NETWORK,
acceptedTokens: extensionAction.parameters.acceptedTokens,
maxRateTimespan: extensionAction.parameters.maxRateTimespan,
},
Expand All @@ -358,7 +363,7 @@ function applyCreation(
paymentAddress: extensionAction.parameters.paymentAddress,
refundAddress: extensionAction.parameters.refundAddress,
salt: extensionAction.parameters.salt,
network: extensionAction.parameters.network,
network: extensionAction.parameters.network || DEFAULT_NETWORK,
acceptedTokens: extensionAction.parameters.acceptedTokens,
maxRateTimespan: extensionAction.parameters.maxRateTimespan,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const extensionFullState = {
parameters: {
feeAddress,
feeAmount,
network: 'mainnet',
paymentAddress,
refundAddress,
salt,
Expand All @@ -86,6 +87,7 @@ export const extensionFullState = {
values: {
feeAddress,
feeAmount,
network: 'mainnet',
paymentAddress,
refundAddress,
salt,
Expand Down
8 changes: 2 additions & 6 deletions packages/payment-processor/src/payment/any-to-erc20-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ export interface IPaymentSettings {
* Processes a transaction to pay a request with an ERC20 currency that is different from the request currency (eg. fiat).
* The payment is made by the ERC20 fee proxy contract.
* @param request the request to pay
* @param paymentTokenAddress the token address to pay the request
* @param maxToSpend maximum of token the user is willing to spend
* @param signerOrProvider the Web3 provider, or signer. Defaults to window.ethereum.
* @param paymentSettings payment settings
* @param amount optionally, the amount to pay. Defaults to remaining amount of the request.
* @param feeAmount optionally, the fee amount to pay. Defaults to the fee amount.
* @param network optionally, network of the payment. Defaults to 'mainnet'
* @param overrides optionally, override default transaction values, like gas.
*/
export async function payAnyToErc20ProxyRequest(
Expand Down Expand Up @@ -70,12 +68,10 @@ export async function payAnyToErc20ProxyRequest(
/**
* Encodes the call to pay a request with an ERC20 currency that is different from the request currency (eg. fiat). The payment is made by the ERC20 fee proxy contract.
* @param request request to pay
* @param paymentTokenAddress token address to pay with
* @param maxToSpend maximum of token the user is willing to spend
* @param signerOrProvider the Web3 provider, or signer. Defaults to window.ethereum.
* @param paymentSettings payment settings
* @param amount optionally, the amount to pay. Defaults to remaining amount of the request.
* @param feeAmountOverride optionally, the fee amount to pay. Defaults to the fee amount of the request.
* @param network optionally, network of the payment. Defaults to 'mainnet'
*/
export async function encodePayAnyToErc20ProxyRequest(
request: ClientTypes.IRequestData,
Expand Down
6 changes: 4 additions & 2 deletions packages/payment-processor/src/payment/conversion-erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export async function approveErc20ForProxyConversionIfNeeded(
overrides?: ITransactionOverrides,
): Promise<ContractTransaction | void> {
const network =
request.extensions[ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_ERC20_PROXY].values.network ||
'mainnet';
request.extensions[ExtensionTypes.ID.PAYMENT_NETWORK_ANY_TO_ERC20_PROXY].values.network;
if (!network) {
throw new Error(`Payment network currency must have a network`);
}

if (
!(await checkErc20Allowance(
Expand Down
13 changes: 9 additions & 4 deletions packages/payment-processor/src/payment/swap-any-to-erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export { ISwapSettings } from './swap-erc20-fee-proxy';
* Processes a transaction to swap tokens and pay an ERC20 Request through a proxy with fees.
* @param request
* @param signerOrProvider the Web3 provider, or signer. Defaults to window.ethereum.
* @param paymentSettings settings for the swap: swap path, max amount to swap, deadline
* @param options to override amount, feeAmount and transaction parameters
*/
export async function swapToPayAnyToErc20Request(
Expand All @@ -31,9 +30,12 @@ export async function swapToPayAnyToErc20Request(
if (!request.extensions[PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY]) {
throw new Error(`The request must have the payment network any-to-erc20-proxy`);
}

const network =
request.extensions[PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY].values.network ||
'mainnet';
request.extensions[PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY].values.network;
if (!network) {
throw new Error(`Payment network currency must have a network`);
}

const encodedTx = encodeSwapToPayAnyToErc20Request(request, signerOrProvider, options);
const proxyAddress = erc20SwapConversionArtifact.getAddress(network);
Expand Down Expand Up @@ -68,7 +70,10 @@ export function encodeSwapToPayAnyToErc20Request(
if (!swapSettings) {
throw new Error(`Swap Settings are required`);
}
const network = conversionSettings.currency?.network || 'mainnet';
const network = conversionSettings.currency?.network;
if (!network) {
throw new Error(`Currency in conversion settings must have a network`);
}

/** On Chain conversion preparation */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { checkErc20Allowance, encodeApproveAnyErc20 } from './erc20';
* if the current approval is missing or not sufficient.
* @param request request to pay, used to know the network
* @param ownerAddress address of the payer
* @param paymentCurrency ERC20 currency used for the swap
* @param paymentTokenAddress ERC20 currency used for the swap
* @param signerOrProvider the web3 provider. Defaults to Etherscan.
* @param minAmount ensures the approved amount is sufficient to pay this amount
* @param overrides optionally, override default transaction values, like gas.
Expand Down Expand Up @@ -62,8 +62,10 @@ export async function approveErc20ForSwapWithConversionToPay(
overrides?: ITransactionOverrides,
): Promise<ContractTransaction> {
const network =
request.extensions[PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY].values.network ||
'mainnet';
request.extensions[PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY].values.network;
if (!network) {
throw new Error(`Payment network currency must have a network`);
}

const encodedTx = encodeApproveAnyErc20(
paymentTokenAddress,
Expand Down

0 comments on commit f83996b

Please sign in to comment.