Skip to content

Commit

Permalink
Merge pull request #259 from Adamant-im/dev/trello.com/c/dt01fPcy
Browse files Browse the repository at this point in the history
[trello.com/c/dt01fPcy] Fixed gas limit for eth & erc20
  • Loading branch information
StanislavDevIOS authored Jun 5, 2023
2 parents 1360ff5 + 3ca5fed commit 6ec4234
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Adamant/Wallets/ERC20/ERC20WalletService+Send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extension ERC20WalletService: WalletServiceTwoStepSend {
amount: "\(amount)"
).transaction

await calculateFee(for: ethRecipient)

let policies: Policies = Policies(
gasLimitPolicy: .manual(gasLimit),
gasPricePolicy: .manual(gasPrice)
Expand Down
8 changes: 4 additions & 4 deletions Adamant/Wallets/ERC20/ERC20WalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ class ERC20WalletService: WalletService {
await calculateFee()
}

private func calculateFee() async {
func calculateFee(for address: EthereumAddress? = nil) async {
guard let token = token else { return }

let priceRaw = try? await getGasPrices()
let gasLimitRaw = try? await getGasLimit()
let gasLimitRaw = try? await getGasLimit(to: address)

var price = priceRaw ?? BigUInt(token.defaultGasPriceGwei).toWei()
var gasLimit = gasLimitRaw ?? BigUInt(token.defaultGasLimit)
Expand Down Expand Up @@ -349,7 +349,7 @@ class ERC20WalletService: WalletService {
}
}

func getGasLimit() async throws -> BigUInt {
func getGasLimit(to address: EthereumAddress?) async throws -> BigUInt {
guard let web3 = await self.web3,
let ethWallet = ethWallet,
let erc20 = erc20
Expand All @@ -360,7 +360,7 @@ class ERC20WalletService: WalletService {
do {
let transaction = try await erc20.transfer(
from: ethWallet.ethAddress,
to: ethWallet.ethAddress,
to: address ?? ethWallet.ethAddress,
amount: "\(ethWallet.balance)"
).transaction

Expand Down
2 changes: 2 additions & 0 deletions Adamant/Wallets/Ethereum/EthWalletService+Send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extension EthWalletService: WalletServiceTwoStepSend {
tx.to = ethRecipient
tx.value = bigUIntAmount

await calculateFee(for: ethRecipient)

let resolver = PolicyResolver(provider: provider)
let policies: Policies = Policies(
gasLimitPolicy: .manual(gasLimit),
Expand Down
8 changes: 4 additions & 4 deletions Adamant/Wallets/Ethereum/EthWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ class EthWalletService: WalletService {
await calculateFee()
}

private func calculateFee() async {
func calculateFee(for address: EthereumAddress? = nil) async {
let priceRaw = try? await getGasPrices()
let gasLimitRaw = try? await getGasLimit()
let gasLimitRaw = try? await getGasLimit(to: address)

var price = priceRaw ?? defaultGasPriceGwei.toWei()
var gasLimit = gasLimitRaw ?? defaultGasLimit
Expand Down Expand Up @@ -370,7 +370,7 @@ class EthWalletService: WalletService {
}
}

func getGasLimit() async throws -> BigUInt {
func getGasLimit(to address: EthereumAddress?) async throws -> BigUInt {
guard let web3 = await self.web3,
let ethWallet = ethWallet
else {
Expand All @@ -380,7 +380,7 @@ class EthWalletService: WalletService {
do {
var transaction: CodableTransaction = .emptyTransaction
transaction.from = ethWallet.ethAddress
transaction.to = ethWallet.ethAddress
transaction.to = address ?? ethWallet.ethAddress

let price = try await web3.eth.estimateGas(for: transaction)
return price
Expand Down

0 comments on commit 6ec4234

Please sign in to comment.