Skip to content

Commit

Permalink
Merge pull request #114 from peanutprotocol/setFeeOptions-update
Browse files Browse the repository at this point in the history
feat: update setFeeOptions to take optional provider & chainId
  • Loading branch information
borcherd authored Feb 27, 2024
2 parents fd222f2 + e12cf74 commit 6c215e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@squirrel-labs/peanut-sdk",
"version": "0.4.7",
"version": "0.4.8",
"description": "The Peanut Protocol SDK! Check out the documentation at https://docs.peanut.to",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/consts/interfaces.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ export enum ESignAndSubmitTx {
ERROR_INSUFFICIENT_NATIVE_TOKEN,
}

export enum ESetFeeOptionsStatusCodes {
ERROR_PROVIDER_OR_CHAINID_REQUIRED,
ERROR_PROVIDER_CHAINID_MISMATCH,
}

export enum EGetLinkFromTxStatusCodes {
ERROR_GETTING_TX_RECEIPT_FROM_HASH,
}
Expand Down Expand Up @@ -622,6 +627,7 @@ export type allErrorEnums =
| EXChainStatusCodes
| ERaffleErrorCodes
| EGenericErrorCodes // New enum added here
| ESetFeeOptionsStatusCodes

export class SDKStatus extends Error {
code: allErrorEnums
Expand Down
35 changes: 26 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ async function getEIP1559Tip(chainId: string): Promise<ethers.BigNumber | null>
async function setFeeOptions({
txOptions,
unsignedTx,
provider,
provider = undefined,
chainId = undefined,
eip1559 = true,
maxFeePerGas = null,
maxFeePerGasMultiplier = 1.2,
Expand All @@ -498,7 +499,8 @@ async function setFeeOptions({
}: {
txOptions?: any
unsignedTx?: interfaces.IPeanutUnsignedTransaction
provider: Provider
provider?: Provider | undefined
chainId?: string | undefined
eip1559?: boolean
maxFeePerGas?: ethers.BigNumber | null
maxFeePerGasMultiplier?: number
Expand All @@ -508,8 +510,25 @@ async function setFeeOptions({
maxPriorityFeePerGasMultiplier?: number
gasLimitMultiplier?: number
}) {
// eip1559 = true
//TODO: Technically, if the provided tx Options have all the data filled out, we wouldn't have to check chainid or provider, because there's nth to do. Maybe Implememt an entry check for that
config.verbose && console.log('Setting tx options...')

let _chainId: string = chainId || ''

if (!provider && !chainId) {
throw new interfaces.SDKStatus(
interfaces.ESetFeeOptionsStatusCodes.ERROR_PROVIDER_OR_CHAINID_REQUIRED,
'Either provider or chainId must be provided'
)
} else if (chainId && !provider) {
_chainId = chainId
provider = await getDefaultProvider(chainId)
} else if (!chainId && provider) {
// if chainId and provider are both provided, check if they match
const network = await provider.getNetwork()
_chainId = network.chainId.toString()
}

let feeData
// if not txOptions, create it (oneliner)
txOptions = txOptions || {}
Expand All @@ -522,9 +541,7 @@ async function setFeeOptions({
console.error('Failed to fetch gas price from provider:', error)
throw error
}

const chainId = Number(await provider.getNetwork().then((network: any) => network.chainId))
const chainDetails = CHAIN_DETAILS[chainId]
const chainDetails = CHAIN_DETAILS[_chainId]

if (gasLimit) {
txOptions.gasLimit = gasLimit
Expand All @@ -550,7 +567,7 @@ async function setFeeOptions({
// Check if EIP-1559 is supported
// if on milkomeda or bnb or linea, set eip1559 to false
// Even though linea is eip1559 compatible, it is more reliable to use the good old gasPrice
if ([2001, 200101, 56, 59144, 59140, 534352, 5000, 5001].includes(chainId)) {
if (['2001', '200101', '56', '59144', '59140', '534352', '5000', '5001'].includes(_chainId)) {
eip1559 = false
config.verbose && console.log('Setting eip1559 to false as an exception')
} else if (chainDetails && chainDetails.features) {
Expand Down Expand Up @@ -588,14 +605,14 @@ async function setFeeOptions({
// for some chains, like arbitrum or base, providers tend to return an incorrect maxPriorityFeePerGas
// Sanity check so that it's never more than the base fee.
// exception: linea, where baseFee is hardcoded to 7 gwei (minimum allowed)
if (![59144, 59140].includes(chainId)) {
if (!['59144', '59140'].includes(_chainId)) {
if (BigInt(txOptions.maxPriorityFeePerGas) > lastBaseFeePerGas) {
txOptions.maxPriorityFeePerGas = lastBaseFeePerGas.toString()
}
}

// for polygon (137), set priority fee to min 40 gwei (they have a minimum of 30 for spam prevention)
if (chainId == 137) {
if (_chainId == '137') {
const minPriorityFee = ethers.utils.parseUnits('40', 'gwei')
if (ethers.BigNumber.from(txOptions.maxPriorityFeePerGas).lt(minPriorityFee)) {
txOptions.maxPriorityFeePerGas = minPriorityFee.toString()
Expand Down
12 changes: 12 additions & 0 deletions test/basic/setFeeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ describe('setFeeOptions function', () => {
expect(txOptions).toHaveProperty('gasPrice')
}, 30000)

it('should correctly set fee options for non-EIP-1559 chains with only chainId', async () => {
const txOptions = await setFeeOptions({
chainId: '5000',
})

expect(txOptions).toHaveProperty('gasPrice')
}, 30000)

it('should throw error if no chainId & provider are passed in', async () => {
await expect(setFeeOptions({})).rejects.toThrow('Either provider or chainId must be provided')
}, 30000)

it('should correctly set gas limit for specific chains', async () => {
const provider = await getDefaultProvider('137')

Expand Down

0 comments on commit 6c215e9

Please sign in to comment.