Skip to content

Commit

Permalink
Use the estimate function inside the package rather than going to the…
Browse files Browse the repository at this point in the history
… provider.
  • Loading branch information
jessgusclark committed Jan 25, 2024
1 parent b518a79 commit 167164f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/rif-relay-sdk/src/RifRelaySDK/RifRelaySDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ 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 @@ -121,8 +124,18 @@ export class RIFRelaySDK {
? estTokenGas.toNumber() * tokenGasIncrease
: estTokenGas

const estimated = await this.provider.estimateGas({ ...tx, gasPrice })
const internalCallCost = Math.round(estimated.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 internalCallCost = estimated.gt(INTERNAL_TRANSACTION_ESTIMATE_CORRECTION)
? estimated.sub(INTERNAL_TRANSACTION_ESTIMATE_CORRECTION)
: estimated

const updatedNonceWithPendingTxs = nonce.add(pendingTxsCount)

const relayRequest: RelayRequest = {
Expand Down
11 changes: 11 additions & 0 deletions packages/rif-relay-sdk/src/RifRelaySDK/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RelayDataType,
RelayRequestType
} from './types'
import { TransactionRequest } from '@ethersproject/abstract-provider'

export interface EIP712Domain {
name?: string | undefined
Expand All @@ -31,10 +32,20 @@ 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 ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000'
export const INTERNAL_TRANSACTION_ESTIMATE_CORRECTION = BigNumber.from(20000)
export const RIF_TOKEN_ADDRESS_TESTNET =
'0x19F64674D8A5B4E652319F5e239eFd3bc969A1fE'
export const TWO_RIF = BigNumber.from('2000000000000000000')
Expand Down

0 comments on commit 167164f

Please sign in to comment.