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

Sc 16316 feature sdk update serialise helpers to use eip 1559 #180

Merged
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
12 changes: 6 additions & 6 deletions src/helpers/serialiseExampleUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {

dotenv.config()

const poolAddress = '0x4ff5637548e48abaf07baf4a5769b8abbe03a322'
const amount = BigInt('1000000')
const poolAddress = '0xa5dfe2bae4cb3b34e1a8df2e85350e5d5ba7bc44'
const amount = BigInt(1e6) // 1 USDC / micro eth amount
const walletAddress = '0xaA5aA072369A3F34fcA3926DDf31977fAD95022D'

async function main() {
Expand Down Expand Up @@ -51,15 +51,15 @@ async function main() {

// 🚨 3) Sign the transaction 🚨
const deserializeTx = parseTransaction(txBytes)
const { nonce, gasPrice, gasLimit, to, value, data, chainId, accessList, type } = deserializeTx
const { nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, chainId, accessList, type } =
deserializeTx

if (!type) return

const transactionRequest = {
nonce,
// maxPriorityFeePerGas,
// maxFeePerGas,
gasPrice,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value: value.toHexString(),
Expand Down
41 changes: 10 additions & 31 deletions src/helpers/serialiseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,13 @@ import { JsonRpcProvider } from '@ethersproject/providers'

import PoolV2PoolAbi from '../abis/PoolV2Pool.abi.json'

const ZERO = BigNumber.from(0)

export interface UnsignedTransactionBundle {
txInstance: UnsignedTransaction
txBytes: string
}

async function estimateGasForFunction(
contract: Contract,
functionName: string,
args: any[],
from: string
): Promise<BigNumber> {
// Ensure the contract has the function
if (typeof contract.functions[functionName] !== 'function') {
throw new Error('Function not found in contract')
}

// Estimate gas for the function call
const estimateGas = await contract.estimateGas[functionName](...args, { from })

return estimateGas
}

// Creates unsigned transaction object for arbitrary function call
const createUnsignedTransactionBundle = async (
provider: Provider,
Expand All @@ -41,14 +26,11 @@ const createUnsignedTransactionBundle = async (
try {
const contract = new Contract(contractAddress, abi, provider)

// Estimate gas price and limit
const gasPrice = await provider.getGasPrice()
const gasLimit = await estimateGasForFunction(contract, functionName, functionArgs, wallet)
const feeData = await provider.getFeeData()
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas || ZERO
const maxFeePerGas = feeData.maxFeePerGas || ZERO

// todo: get fee data so can use type 2: eip-1559
// const feeData = await provider.getFeeData()
// const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas || ZERO
// const maxFeePerGas = feeData.maxFeePerGas || ZERO
const gasLimit = await contract.estimateGas[functionName](...functionArgs, { from: wallet })

// Get current nonce
const nonce = await provider.getTransactionCount(wallet)
Expand All @@ -59,16 +41,13 @@ const createUnsignedTransactionBundle = async (
const unsignedTx: UnsignedTransaction = {
to: contractAddress,
data: contract.interface.encodeFunctionData(functionName, functionArgs),
gasLimit: gasLimit.toHexString(),
gasPrice: gasPrice.toHexString(),
// maxPriorityFeePerGas: maxPriorityFeePerGas.toHexString(),
// maxFeePerGas: maxFeePerGas.toHexString(),
maxPriorityFeePerGas: maxPriorityFeePerGas.toHexString(),
maxFeePerGas: maxFeePerGas.toHexString(),
nonce,
value: parseEther('0').toHexString(),
chainId,
type: 1,

accessList: []
type: 2,
gasLimit
}

const txBytes = serialize(unsignedTx)
Expand Down