diff --git a/packages/rif-relay-sdk/package.json b/packages/rif-relay-sdk/package.json index 3ec9c2b..4c35d29 100644 --- a/packages/rif-relay-sdk/package.json +++ b/packages/rif-relay-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@rsksmart/rif-relay-light-sdk", - "version": "1.0.17", + "version": "1.1.0", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts b/packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts index 3a9f09d..986f903 100644 --- a/packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts +++ b/packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts @@ -18,11 +18,12 @@ import { } from './types' import { dataTypeFields, + filterTxOptions, getDomainSeparator, - INTERNAL_TRANSACTION_ESTIMATE_CORRECTION, MAX_RELAY_NONCE_GAP, validUntilTime, - ZERO_ADDRESS + ZERO_ADDRESS, + ZERO_HASH } from './helpers' import ERC20Abi from './erc20abi.json' @@ -122,12 +123,14 @@ export class RIFRelaySDK { ? estTokenGas.toNumber() * tokenGasIncrease : estTokenGas - const estimated = await this.provider.estimateGas({ ...tx, gasPrice }) - const correction = - estimated.toNumber() > INTERNAL_TRANSACTION_ESTIMATE_CORRECTION - ? estimated.sub(INTERNAL_TRANSACTION_ESTIMATE_CORRECTION) - : estimated - const internalCallCost = Math.round(correction.toNumber() * 1.01) + // estimate the gas of the transaction: + const estimated = await this.smartWallet + .estimateDirectExecute( + tx.to || ZERO_ADDRESS, + tx.data || ZERO_HASH, + filterTxOptions(tx), + ) + const updatedNonceWithPendingTxs = nonce.add(pendingTxsCount) const relayRequest: RelayRequest = { @@ -137,7 +140,7 @@ export class RIFRelaySDK { to: tx.to || ZERO_ADDRESS, data: tx.data?.toString() || '0x', value: tx.value?.toString() || '0', - gas: internalCallCost.toString(), + gas: estimated.toString(), nonce: updatedNonceWithPendingTxs.toString(), tokenContract: tokenContract.toLowerCase(), tokenAmount: tokenAmount.toString(), diff --git a/packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts b/packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts index 39a8fd6..66640d3 100644 --- a/packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts +++ b/packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts @@ -1,10 +1,10 @@ -import { BigNumber } from 'ethers' import { DeployRequestType, DomainSeparatorType, RelayDataType, RelayRequestType } from './types' +import { TransactionRequest } from '@ethersproject/abstract-provider' export interface EIP712Domain { name?: string | undefined @@ -31,12 +31,17 @@ export function getDomainSeparator ( } } +export const filterTxOptions = (transactionRequest: TransactionRequest) => + Object.keys(transactionRequest) + .filter(key => !['from', 'to', 'data'].includes(key)) + .reduce((obj: any, key: any) => { + obj[key] = (transactionRequest as any)[key] + return obj + }, {}) + export const validUntilTime = () => Math.floor(Date.now() / 1000) + TWO_DAYS export const MAX_RELAY_NONCE_GAP = 3 export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' -export const INTERNAL_TRANSACTION_ESTIMATE_CORRECTION = 20000 -export const RIF_TOKEN_ADDRESS_TESTNET = - '0x19F64674D8A5B4E652319F5e239eFd3bc969A1fE' -export const TWO_RIF = BigNumber.from('2000000000000000000') +export const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000' export const TWO_DAYS = 172800