-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IOS-7628 Move staking-related logic from txbuilder into helper
- Loading branch information
Showing
4 changed files
with
130 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
BlockchainSdk/Blockchains/Ethereum/EthereumStakeKitTransactionHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// EthereumStakeKitTransactionHelper.swift | ||
// BlockchainSdk | ||
// | ||
// Created by Dmitry Fedorov on 16.08.2024. | ||
// Copyright © 2024 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import WalletCore | ||
import BigInt | ||
|
||
struct EthereumStakeKitTransactionHelper { | ||
func prepareForSign( | ||
_ stakingTransaction: StakeKitTransaction, | ||
transactionBuilder: EthereumTransactionBuilder | ||
) throws -> Data { | ||
let input = try buildSigningInput( | ||
stakingTransaction: stakingTransaction, | ||
transactionBuilder: transactionBuilder | ||
) | ||
let preSigningOutput = try transactionBuilder.buildTxCompilerPreSigningOutput(input: input) | ||
return preSigningOutput.dataHash | ||
} | ||
|
||
func prepareForSend( | ||
stakingTransaction: StakeKitTransaction, | ||
transactionBuilder: EthereumTransactionBuilder, | ||
signatureInfo: SignatureInfo | ||
) throws -> Data { | ||
let input = try buildSigningInput( | ||
stakingTransaction: stakingTransaction, | ||
transactionBuilder: transactionBuilder | ||
) | ||
let output = try transactionBuilder.buildSigningOutput(input: input, signatureInfo: signatureInfo) | ||
return output.encoded | ||
} | ||
|
||
private func buildSigningInput( | ||
stakingTransaction: StakeKitTransaction, | ||
transactionBuilder: EthereumTransactionBuilder | ||
) throws -> EthereumSigningInput { | ||
let compiledTransactionData = Data(hex: stakingTransaction.unsignedData) | ||
let compiledTransaction = try JSONDecoder().decode( | ||
EthereumCompiledTransaction.self, | ||
from: compiledTransactionData | ||
) | ||
|
||
guard let amountValue = stakingTransaction.amount.bigUIntValue else { | ||
throw EthereumTransactionBuilderError.invalidAmount | ||
} | ||
|
||
guard let gasLimit = BigUInt(compiledTransaction.gasLimit, radix: 16), | ||
let baseFee = BigUInt(compiledTransaction.maxFeePerGas, radix: 16), | ||
let priorityFee = BigUInt(compiledTransaction.maxPriorityFeePerGas, radix: 16) else { | ||
throw EthereumTransactionBuilderError.feeParametersNotFound | ||
} | ||
|
||
return try transactionBuilder.buildSigningInput( | ||
// TODO: refactor, 'user' field is used only in erc20Transfer, consider moving elsewhere? | ||
destination: .contract(user: "", contract: compiledTransaction.to, value: amountValue), | ||
fee: Fee( | ||
stakingTransaction.fee.amount, | ||
parameters: EthereumEIP1559FeeParameters(gasLimit: gasLimit, baseFee: baseFee, priorityFee: priorityFee) | ||
), | ||
parameters: EthereumTransactionParams( | ||
data: Data(hex: compiledTransaction.data), | ||
nonce: compiledTransaction.nonce | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters