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 ce2f5e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 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"
}
4 changes: 2 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/wallet-connect-sdk-wallet-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"@cityofzion/neon-dappkit": "0.5.1",
"ethers": "5.7.2",
"@ethersproject/abstract-signer": "^5.7.0",
"@walletconnect/web3wallet": "^1.12.3",
"@walletconnect/core": "^2.13.3"
"@walletconnect/web3wallet": "1.12.3",
"@walletconnect/core": "2.13.3"
},
"devDependencies": {
"@types/node": "^20.1.3",
Expand Down
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 ce2f5e9

Please sign in to comment.