diff --git a/src/Conversions.ts b/src/Conversions.ts index ba1cb96..2afad12 100644 --- a/src/Conversions.ts +++ b/src/Conversions.ts @@ -79,7 +79,7 @@ export const toNativeWeiFrom = ({ const bigxRate = BigNumberJs(xRate ?? '0'); if (bigAmount.isZero() || bigxRate.isZero()) { - return BigNumberJs(constants.Zero.toString()); + return BigNumberJs(0); } const amountAsFraction = toPrecision({ diff --git a/src/RegistrationManager.ts b/src/RegistrationManager.ts index 67d492d..1215d9b 100644 --- a/src/RegistrationManager.ts +++ b/src/RegistrationManager.ts @@ -30,6 +30,7 @@ import { import type { ManagerEvent, PastEventOptions } from './definitions/event.type'; import { getServerConfig } from './ServerConfigParams'; import { getPastEventsForHub } from './getPastEventsForHub'; +import type { RelayManagerData } from '@rsksmart/rif-relay-client'; export type RelayServerRegistryInfo = { url: string; @@ -68,7 +69,7 @@ export class RegistrationManager { private readonly _txStoreManager: TxStoreManager; - private _relayData: IRelayHub.RelayManagerDataStruct | undefined; + private _relayData: RelayManagerData | undefined; private _lastWorkerAddedTransaction: TypedEvent | undefined; @@ -186,7 +187,14 @@ export class RegistrationManager { } } - this._relayData = await this.getRelayData(); + const relayData = await this.getRelayData(); + + this._relayData = { + currentlyStaked: await relayData.currentlyStaked, + manager: await relayData.manager, + registered: await relayData.registered, + url: await relayData.url, + }; for (const eventData of hubEventsSinceLastScan) { switch (eventData.event) { @@ -423,7 +431,7 @@ export class RegistrationManager { const provider = getProvider(); const gasPrice = await provider.getGasPrice(); - const gasLimit = BigNumber.from(minTxGasCost ? minTxGasCost : 21000); + const gasLimit = BigNumber.from(minTxGasCost ?? 21000); const txCost = gasLimit.mul(gasPrice); const managerBalance = await provider.getBalance(this._managerAddress); @@ -470,7 +478,7 @@ export class RegistrationManager { const provider = getProvider(); const gasPrice = await provider.getGasPrice(); - const gasLimit = BigNumber.from(minTxGasCost ? minTxGasCost : 21000); + const gasLimit = BigNumber.from(minTxGasCost ?? 21000); const txCost = gasPrice.mul(gasLimit); const workerBalance = await provider.getBalance(this._workerAddress); diff --git a/src/RelayServer.ts b/src/RelayServer.ts index 5866cd1..11d4e7f 100644 --- a/src/RelayServer.ts +++ b/src/RelayServer.ts @@ -266,29 +266,26 @@ export class RelayServer extends EventEmitter { contracts: { relayHubAddress }, } = this.config; + const relayHubAddressValue = await metadata.relayHubAddress; + // Check that the relayHub is the correct one - if ( - metadata.relayHubAddress.toString().toLowerCase() !== - relayHubAddress.toLowerCase() - ) { + if (relayHubAddressValue.toLowerCase() !== relayHubAddress.toLowerCase()) { throw new Error( - `Wrong hub address.\nRelay server's hub address: ${ - this.config.contracts.relayHubAddress - }, request's hub address: ${metadata.relayHubAddress.toString()}\n` + `Wrong hub address.\nRelay server's hub address: ${this.config.contracts.relayHubAddress}, request's hub address: ${relayHubAddressValue}\n` ); } - const feesReceiver = relayRequest.relayData.feesReceiver.toString(); + const feesReceiver = await relayRequest.relayData.feesReceiver; // Check the relayWorker (todo: once migrated to multiple relays, check if exists) if (feesReceiver.toLowerCase() !== this.feesReceiver.toLowerCase()) { throw new Error(`Wrong fees receiver address: ${feesReceiver}\n`); } - const gasPrice = relayRequest.relayData.gasPrice.toString(); + const gasPrice = await relayRequest.relayData.gasPrice; // Check that the gasPrice is initialized & acceptable if (this.gasPrice.gt(gasPrice)) { throw new Error( - `Unacceptable gasPrice: relayServer's gasPrice:${this.gasPrice.toString()} request's gasPrice: ${gasPrice}` + `Unacceptable gasPrice: relayServer's gasPrice:${this.gasPrice.toString()} request's gasPrice: ${gasPrice.toString()}` ); } @@ -299,9 +296,11 @@ export class RelayServer extends EventEmitter { ); } - validateVerifier(envelopingRequest: EnvelopingTxRequest): void { - const callVerifier = - envelopingRequest.relayRequest.relayData.callVerifier.toString(); + async validateVerifier( + envelopingRequest: EnvelopingTxRequest + ): Promise { + const callVerifier = await envelopingRequest.relayRequest.relayData + .callVerifier; if (!this.isTrustedVerifier(callVerifier)) { throw new Error(`Invalid verifier: ${callVerifier}`); } @@ -320,8 +319,8 @@ export class RelayServer extends EventEmitter { async validateRequestWithVerifier( envelopingTransaction: EnvelopingTxRequest ): Promise { - const verifier = - envelopingTransaction.relayRequest.relayData.callVerifier.toString(); + const verifier = await envelopingTransaction.relayRequest.relayData + .callVerifier; if (!this.isTrustedVerifier(verifier)) { throw new Error('Invalid verifier'); @@ -410,9 +409,7 @@ export class RelayServer extends EventEmitter { return { maxPossibleGas: initialGasEstimation, - maxPossibleGasWithFee: initialGasEstimation.add( - fee.toFixed(0).toString() - ), + maxPossibleGasWithFee: initialGasEstimation.add(fee.toFixed(0)), }; } @@ -557,17 +554,20 @@ export class RelayServer extends EventEmitter { const provider = getProvider(); + const relayHubAddress = await envelopingTransaction.metadata + .relayHubAddress; + const gasPrice = await envelopingTransaction.relayRequest.relayData + .gasPrice; + const currentBlock = await provider.getBlockNumber(); const details: SendTransactionDetails = { signer: this.workerAddress, serverAction: ServerAction.RELAY_CALL, method, - destination: envelopingTransaction.metadata.relayHubAddress.toString(), + destination: relayHubAddress, gasLimit: maxPossibleGasWithViewCall, creationBlockNumber: currentBlock, - gasPrice: BigNumber.from( - envelopingTransaction.relayRequest.relayData.gasPrice - ), + gasPrice: BigNumber.from(gasPrice.toString()), }; const txDetails = await this.transactionManager.sendTransaction(details); // after sending a transaction is a good time to check the worker's balance, and replenish it. diff --git a/src/TransactionManager.ts b/src/TransactionManager.ts index 640852d..b2a6079 100644 --- a/src/TransactionManager.ts +++ b/src/TransactionManager.ts @@ -291,10 +291,10 @@ data | 0x${transaction.data ?? ''} const bigRetryGasPriceFactor = BigNumberJs( this.config.blockchain.retryGasPriceFactor ); - const oldPrice = BigNumber.from(oldGasPrice); - const bigOldGasPrice = BigNumberJs(oldPrice.toString()); - const bigNewGasPrice = bigRetryGasPriceFactor.multipliedBy(bigOldGasPrice); + const bigNewGasPrice = bigRetryGasPriceFactor.multipliedBy( + oldGasPrice ? oldGasPrice.toString() : 0 + ); let newGasPrice = BigNumber.from(bigNewGasPrice.toFixed(0)); diff --git a/src/Utils.ts b/src/Utils.ts index fa0298d..efbd28d 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -10,6 +10,7 @@ import { getDefaultProvider, providers, utils } from 'ethers'; import { getAddress } from 'ethers/lib/utils'; import ow from 'ow'; import { getServerConfig } from './ServerConfigParams'; +import type { RelayManagerData } from '@rsksmart/rif-relay-client'; const CONFIG_CONTRACTS = 'contracts'; const CONFIG_BLOCKCHAIN = 'blockchain'; @@ -87,18 +88,17 @@ export function isSecondEventLater(a: TypedEvent, b: TypedEvent): boolean { } export function isRegistrationValid( - relayData: IRelayHub.RelayManagerDataStruct | undefined, + relayData: RelayManagerData | undefined, managerAddress: string ): boolean { if (relayData) { - const manager = relayData.manager.toString(); const { app: { url: serverUrl }, } = getServerConfig(); return ( - isSameAddress(manager, managerAddress) && - relayData.url.toString() === serverUrl + isSameAddress(relayData.manager, managerAddress) && + relayData.url === serverUrl ); } diff --git a/src/relayServerUtils.ts b/src/relayServerUtils.ts index 78f071f..9a0751d 100644 --- a/src/relayServerUtils.ts +++ b/src/relayServerUtils.ts @@ -57,14 +57,11 @@ async function calculateFee( } const { transferFeePercentage } = appConfig; - + const data = await relayRequest.request.data; //Even if transferFeePercentage = 0, it has priority over gas fee - if ( - transferFeePercentage >= 0 && - isTransferOrTransferFrom(relayRequest.request.data.toString()) - ) { + if (transferFeePercentage >= 0 && isTransferOrTransferFrom(data.toString())) { const transferFee = await calculateFeeFromTransfer( - relayRequest.request.data.toString(), + data.toString(), transferFeePercentage, relayRequest ); @@ -97,12 +94,12 @@ async function calculateFixedUsdFee( envelopingRequest: EnvelopingRequest, fixedUsdFee: number ) { - const tokenAddress = envelopingRequest.request.tokenContract.toString(); - const gasPrice = envelopingRequest.relayData.gasPrice.toString(); + const tokenContractAddress = await envelopingRequest.request.tokenContract; + const gasPrice = await envelopingRequest.relayData.gasPrice; const provider = getProvider(); - const tokenInstance = ERC20__factory.connect(tokenAddress, provider); + const tokenInstance = ERC20__factory.connect(tokenContractAddress, provider); const tokenSymbol = await tokenInstance.symbol(); const exchangeRate = await getExchangeRate(US_DOLLAR_SYMBOL, tokenSymbol); @@ -112,8 +109,8 @@ async function calculateFixedUsdFee( return await convertTokenToGas( fixedFeeInToken.toString(), - tokenAddress, - gasPrice + tokenContractAddress, + gasPrice.toString() ); } @@ -125,7 +122,7 @@ async function calculateFixedUsdFee( async function calculateFeeFromTransfer( data: string, transferFeePercentage: number, - relayRequest: EnvelopingRequest + envelopingRequest: EnvelopingRequest ): Promise { if (!isTransferOrTransferFrom(data)) { return BigNumberJs(0); @@ -140,11 +137,13 @@ async function calculateFeeFromTransfer( const valueInDecimal = BigNumberJs('0x' + valueHex); const feeInToken = valueInDecimal.multipliedBy(transferFeePercentage); + const tokenContractAddress = await envelopingRequest.request.tokenContract; + const gasPrice = await envelopingRequest.relayData.gasPrice; return await convertTokenToGas( feeInToken.toString(), - relayRequest.request.tokenContract.toString(), - relayRequest.relayData.gasPrice.toString() + tokenContractAddress, + gasPrice.toString() ); } @@ -196,8 +195,8 @@ async function validateIfGasAmountIsAcceptable( const relayRequest = envelopingTransaction.relayRequest; const estimatedDestinationGasCost = await estimateInternalCallGas({ - from: relayRequest.relayData.callForwarder.toString(), - to: relayRequest.request.to.toString(), + from: relayRequest.relayData.callForwarder, + to: relayRequest.request.to, gasPrice: relayRequest.relayData.gasPrice, data: relayRequest.request.data, }); @@ -207,8 +206,9 @@ async function validateIfGasAmountIsAcceptable( ); const { gas } = relayRequest.request as RelayRequestBody; + const gasValue = await gas; const bigGasFromRequestMaxAgreed = bigMaxEstimatedGasDeviation.multipliedBy( - gas.toString() + gasValue.toString() ); if (estimatedDestinationGasCost.gt(bigGasFromRequestMaxAgreed.toFixed(0))) { @@ -227,14 +227,19 @@ async function validateIfTokenAmountIsAcceptable( return; } - const { tokenAmount, tokenContract } = - envelopingTransaction.relayRequest.request; - const { gasPrice } = envelopingTransaction.relayRequest.relayData; + const { + request: { tokenAmount, tokenContract }, + relayData: { gasPrice }, + } = envelopingTransaction.relayRequest; + + const tokenContractAddress = await tokenContract; + const tokenAmountValue = await tokenAmount; + const gasPriceValue = await gasPrice; const tokenAmountInGas = await convertTokenToGas( - tokenAmount.toString(), - tokenContract.toString(), - gasPrice.toString() + tokenAmountValue.toString(), + tokenContractAddress, + gasPriceValue.toString() ); const isTokenAmountAcceptable = tokenAmountInGas.isGreaterThanOrEqualTo( @@ -294,14 +299,12 @@ async function convertGasToTokenAndNative( relayRequest: EnvelopingRequest, initialEstimation: BigNumber ) { - const gasPrice = relayRequest.relayData.gasPrice.toString(); + const gasPrice = await relayRequest.relayData.gasPrice; + const tokenContractAddress = await relayRequest.request.tokenContract; const provider = getProvider(); - const tokenInstance = ERC20__factory.connect( - relayRequest.request.tokenContract.toString(), - provider - ); + const tokenInstance = ERC20__factory.connect(tokenContractAddress, provider); const token: ExchangeToken = { instance: tokenInstance, @@ -328,7 +331,7 @@ async function convertGasToTokenAndNative( valueInToken: initialEstimationInToken.toString(), valueInNative: initialEstimationInNative.toString(), exchangeRate: xRate, - gasPrice, + gasPrice: gasPrice.toString(), }; }