Skip to content

Commit

Permalink
CU-86dud526g - AbstractWalletConnectEIP155Adapter - Support type 2 tr…
Browse files Browse the repository at this point in the history
…ansactions and add eth_estimateGas method
  • Loading branch information
raulduartep committed Aug 15, 2024
1 parent 573235c commit 61e9a22
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/wallet-connect-sdk-wallet-core",
"comment": "Support type 2 transactions and add eth_estimateGas method",
"type": "minor"
}
],
"packageName": "@cityofzion/wallet-connect-sdk-wallet-core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ethers } from 'ethers'

export type TCustomSigner = ethers.Signer & TypedDataSigner

const DEFAULT_GAS_LIMIT = 0x5208

/* Some methods are named in snakecase, as Ethereum methods are in snakecase and the SDK does not make any transformer for the method called by the dapp. */
export abstract class AbstractWalletConnectEIP155Adapter {
protected async getServices(args: TAdapterMethodParam) {
Expand All @@ -27,7 +29,6 @@ export abstract class AbstractWalletConnectEIP155Adapter {

protected async resolveParams(param: any, args: TAdapterMethodParam) {
const { provider, wallet } = await this.getServices(args)
const gasPrice = await provider.getGasPrice()

if (typeof param !== 'object') {
throw new Error('Invalid Params')
Expand All @@ -38,7 +39,29 @@ export abstract class AbstractWalletConnectEIP155Adapter {
delete param.gas
}

param.gasPrice = gasPrice
if (param.type && typeof param.type !== 'number') {
param.type = parseInt(param.type)
}

const gasPrice = await provider.getGasPrice()

if (param.type === 2) {
param.maxPriorityFeePerGas = param.maxPriorityFeePerGas ?? gasPrice
param.maxFeePerGas = param.maxPriorityFeePerGas ?? gasPrice
} else {
param.gasPrice = param.gasPrice ?? gasPrice
}

let gasLimit = param.gasLimit

if (!gasLimit) {
const connectedWallet = wallet.connect(provider)
try {
gasLimit = await connectedWallet.estimateGas({ ...param, gasLimit: DEFAULT_GAS_LIMIT })
} catch (error) {
gasLimit = DEFAULT_GAS_LIMIT
}
}

return {
param,
Expand Down Expand Up @@ -126,6 +149,13 @@ export abstract class AbstractWalletConnectEIP155Adapter {
return hash
}

async eth_estimateGas(args: TAdapterMethodParam) {
const { param, provider, wallet } = await this.resolveParams(args.request.params.request.params[0], args)
const connectedWallet = wallet.connect(provider)

return await connectedWallet.estimateGas(param)
}

// TODO: It'll be implemented in this issue: #86du71hh4 WC - Ghostmarket via WC asks for wallet methods we don't support
wallet_switchEthereumChain(): Promise<boolean> {
throw new Error('It is impossible to switch the Ethereum chain')
Expand Down

0 comments on commit 61e9a22

Please sign in to comment.