Skip to content

Commit

Permalink
Use SmartWallet's existing estimate method to estimate the gas of a t…
Browse files Browse the repository at this point in the history
…ransaction. (#127)

* Bump version.

* Use the estimate function inside the package rather than going to the provider.

* Remove correction.

* remove beta as it causes lerna to fail.

* remove unused variables.
  • Loading branch information
jessgusclark authored Jan 30, 2024
1 parent fada470 commit 63ff07c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/rif-relay-sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 12 additions & 9 deletions packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 = {
Expand All @@ -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(),
Expand Down
15 changes: 10 additions & 5 deletions packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 63ff07c

Please sign in to comment.