Skip to content

Commit

Permalink
Merge pull request #191 from consensusnetworks/bug/eip-1559-ethers
Browse files Browse the repository at this point in the history
Fix sending EIP1559 transaction with ethers
  • Loading branch information
shanejearley authored Nov 8, 2022
2 parents 052e979 + 60ff6e1 commit 167cd23
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 37 deletions.
5 changes: 2 additions & 3 deletions apps/web/src/composables/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export default function useEthers() {
const etherAmount = ethers.utils.parseEther(value)
const tx = {
to,
value: etherAmount,
value: etherAmount
}
const { hash } = await signer.sendTransaction(tx)
return hash
return await signer.sendTransaction(tx)
}

async function signEthersMessage(messageInit: MessageInit): Promise<string> {
Expand Down
16 changes: 5 additions & 11 deletions apps/web/src/composables/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MessageInit } from '@/interfaces/MessageInit'
import useEnvironment from '@/composables/environment'
import useEthers from '@/composables/ethers'

const ledgerPath = '44\'/60\'/0\'/0/0'
const ledgerPath = 'm/44\'/60\'/0\'/0/0'

export default function useLedger() {
const { ethereumURL, ledgerType, speculosURL } = useEnvironment()
Expand All @@ -29,20 +29,14 @@ export default function useLedger() {
async function sendLedgerTransaction({ from, to, value }: TransactionInit) {
const signer = getEthersLedgerSigner()
const provider = signer.provider as ethers.providers.Provider
const { chainId } = await provider.getNetwork()
const nonce = await provider.getTransactionCount(from)
const unsignedTransaction = {
to,
data: '0x00',
nonce,
chainId,
value: ethers.utils.parseUnits(value)
value: ethers.utils.parseUnits(value),
type: 0
} as ethers.UnsignedTransaction
const { gasPrice, gasLimit } = await getGasPriceAndLimit(ethereumURL, unsignedTransaction as ethers.utils.Deferrable<ethers.providers.TransactionRequest>)
unsignedTransaction.gasPrice = gasPrice
unsignedTransaction.gasLimit = gasLimit


// Todo check before click (user can +/- gas limit accordingly)
const { gasPrice, gasLimit } = await getGasPriceAndLimit(ethereumURL, unsignedTransaction as ethers.utils.Deferrable<ethers.providers.TransactionRequest>)
const balance = await provider.getBalance(from)
const required = gasPrice.mul(gasLimit).add(ethers.utils.parseEther(value))
console.log('Balance', ethers.utils.formatEther(balance))
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/composables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default function useWallet() {
let signer = ethersSignerCreator[signerKey](selectedProvider.value)
if (isWalletConnectSigner(signer)) signer = await signer
const value = ethers.utils.parseEther(amountToStake.value)
await ssv.connect(signer as ethers.Signer).deposit({ value })
await ssv.connect(signer as ethers.Signer).deposit({ value, type: 0 })
} else {
// Todo @ccali11 this should happen sooner - ideally we'll this disable method if missing ssv provider
console.log('Please connect to one of the following providers:', ethersProviderList)
Expand Down
34 changes: 21 additions & 13 deletions common/ethers-ledger-signer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ethers } from 'ethers'
import Eth from '@ledgerhq/hw-app-eth'
import Eth, { ledgerService } from '@ledgerhq/hw-app-eth'
import useTransports from './providers/transports'
import { EthersLedgerSignerOptions } from './interfaces/EthersLedgerSignerOptions'

const defaultPath = '44\'/60\'/0\'/0/0'
const defaultPath = 'm/44\'/60\'/0\'/0/0'
const defaultType = 'usb'
const { createUSBTransport, createSpeculosTransport } = useTransports()
const transportCreators = {
Expand Down Expand Up @@ -76,7 +76,7 @@ export default class EthersLedgerSigner extends ethers.Signer {
}

async signMessage(message: ethers.utils.Bytes | string): Promise<string> {
if (typeof(message) === 'string') {
if (typeof (message) === 'string') {
message = ethers.utils.toUtf8Bytes(message)
}
const messageHex = ethers.utils.hexlify(message).substring(2)
Expand All @@ -90,19 +90,19 @@ export default class EthersLedgerSigner extends ethers.Signer {
async signTransaction(transaction: ethers.providers.TransactionRequest): Promise<string> {
const tx = await ethers.utils.resolveProperties(transaction)
const baseTx: ethers.utils.UnsignedTransaction = {
chainId: tx.chainId || undefined,
data: tx.data || undefined,
gasLimit: tx.gasLimit || undefined,
gasPrice: tx.gasPrice || undefined,
nonce: tx.nonce ? ethers.BigNumber.from(tx.nonce).toNumber() : undefined,
to: tx.to || undefined,
value: tx.value || undefined,
chainId: (tx.chainId || undefined),
data: (tx.data || undefined),
gasLimit: (tx.gasLimit || undefined),
gasPrice: (tx.gasPrice || undefined),
nonce: (tx.nonce ? ethers.BigNumber.from(tx.nonce).toNumber(): undefined),
to: (tx.to || undefined),
value: (tx.value || undefined),
type: (tx.type || undefined)
}

const unsignedTx = ethers.utils.serializeTransaction(baseTx).substring(2)
const signature = await this._retry((eth) => eth.signTransaction(this.path, unsignedTx))

console.log('Signed tx', tx)
const resolution = await ledgerService.resolveTransaction(unsignedTx, {}, {})
const signature = await this._retry((eth) => eth.signTransaction(this.path, unsignedTx, resolution))

return ethers.utils.serializeTransaction(baseTx, {
v: ethers.BigNumber.from('0x' + signature.v).toNumber(),
Expand All @@ -111,6 +111,14 @@ export default class EthersLedgerSigner extends ethers.Signer {
})
}

// Populates all fields in a transaction, signs it and sends it to the network
async sendTransaction(transaction: ethers.utils.Deferrable<ethers.providers.TransactionRequest>): Promise<ethers.providers.TransactionResponse> {
this._checkProvider('sendTransaction')
const tx = await this.populateTransaction(transaction)
const signedTx = await this.signTransaction(tx)
return await (this.provider as ethers.providers.JsonRpcProvider).sendTransaction(signedTx)
}

connect(provider: ethers.providers.Provider): ethers.Signer {
const options = {
provider,
Expand Down
10 changes: 1 addition & 9 deletions scripts/local/dev
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ seed=$(aws secretsmanager get-secret-value \
export BIP39_SEED="$seed"

# Get args
while getopts :a:f:l:m:n:t: flag
while getopts :a:f:l:m:n:t:x: flag
do
case "${flag}" in
a) app=${OPTARG};;
Expand All @@ -46,14 +46,6 @@ do
esac
done

echo "APP set to $app"
echo "EXPOSE set to $external"
echo "FORK set to $fork"
echo "LEDGER set to $ledger"
echo "MOCK set to $mock"
echo "NETWORK set to $network"
echo "TREZOR set to $trezor"

# Default to mainnet if fork is set vaguely
if [ "$fork" = true ]; then
fork=mainnet
Expand Down

0 comments on commit 167cd23

Please sign in to comment.