Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SmartWallet's existing estimate method to estimate the gas of a transaction. #127

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading