diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TokenBalanceERC20TokenMethod.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TokenBalanceERC20TokenMethod.kt index a0bee5aab..c424db487 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TokenBalanceERC20TokenMethod.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TokenBalanceERC20TokenMethod.kt @@ -13,7 +13,7 @@ data class TokenBalanceERC20TokenMethod( override val data: ByteArray get() { val prefixData = prefix.hexToBytes() - val addressData = address.hexToBytes().leftPadToFixedSize(32) + val addressData = address.hexToBytes().leftPadToFixedSize(fixedSize = 32) return prefixData + addressData } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TransferERC20TokenMethod.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TransferERC20TokenMethod.kt index 1ddc75093..33922733a 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TransferERC20TokenMethod.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/ethereum/tokenmethods/TransferERC20TokenMethod.kt @@ -13,8 +13,8 @@ data class TransferERC20TokenMethod( override val data: ByteArray get() { val prefixData = prefix.hexToBytes() - val addressData = destination.hexToBytes().leftPadToFixedSize(32) - val amountData = amount.toByteArray().leftPadToFixedSize(32) + val addressData = destination.hexToBytes().leftPadToFixedSize(fixedSize = 32) + val amountData = amount.toByteArray().leftPadToFixedSize(fixedSize = 32) return prefixData + addressData + amountData } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/VechainTransactionBuilder.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/VechainTransactionBuilder.kt index 18c6ff41e..3e700e951 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/VechainTransactionBuilder.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/VechainTransactionBuilder.kt @@ -37,21 +37,21 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W minimum = Fee.Vechain( amount = Amount( token = VechainWalletManager.VTHO_TOKEN, - value = gas.toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL) + value = gas.toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL), ), gasPriceCoef = 0, ), normal = Fee.Vechain( amount = Amount( token = VechainWalletManager.VTHO_TOKEN, - value = (gas * 1.5).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL) + value = (gas * NORMAL_FEE_COEFFICIENT).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL), ), gasPriceCoef = 127, ), priority = Fee.Vechain( amount = Amount( token = VechainWalletManager.VTHO_TOKEN, - value = (gas * 2).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL) + value = (gas * PRIORITY_FEE_COEFFICIENT).toBigDecimal().movePointLeft(GAS_TO_VET_DECIMAL), ), gasPriceCoef = 255, ), @@ -85,7 +85,7 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W fee, transactionData.destinationAddress, blockInfo, - nonce + nonce, ) val publicKeys = DataVector() @@ -95,12 +95,15 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W signatures.add(unmarshalSignature(signature, hash, publicKey)) val compileWithSignatures = TransactionCompiler.compileWithSignatures( - coinType, inputData.toByteArray(), signatures, publicKeys + coinType, + inputData.toByteArray(), + signatures, + publicKeys, ) val output = VeChain.SigningOutput.parseFrom(compileWithSignatures) if (output.error != Common.SigningError.OK) { - throw IllegalStateException("something went wrong") + error("something went wrong") } return output.encoded.toByteArray() @@ -119,7 +122,7 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W .setChainTag(chainTag) .setNonce(nonce) .setBlockRef(blockInfo.blockRef) - .setExpiration(180) + .setExpiration(EXPIRATION_BLOCKS) .setGas(intrinsicGas(clause)) .setGasPriceCoef(fee.gasPriceCoef) .addClauses(clause) @@ -166,14 +169,17 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W } private fun unmarshalSignature(signature: ByteArray, hash: ByteArray, publicKey: Wallet.PublicKey): ByteArray { - val r = BigInteger(1, signature.copyOfRange(0, 32)) - val s = BigInteger(1, signature.copyOfRange(32, 64)) + val r = BigInteger(1, signature.copyOfRange(fromIndex = 0, toIndex = 32)) + val s = BigInteger(1, signature.copyOfRange(fromIndex = 32, toIndex = 64)) val ecdsaSignature = ECDSASignature(r, s).canonicalise() val recId = ecdsaSignature.determineRecId( hash, - PublicKey(publicKey.blockchainKey.toDecompressedPublicKey().sliceArray(1..64)), + PublicKey( + publicKey = publicKey.blockchainKey.toDecompressedPublicKey() + .sliceArray(1..PUBLIC_KEY_LENGTH), + ), ) val signatureData = SignatureData(ecdsaSignature.r, ecdsaSignature.s, recId.toBigInteger()) @@ -183,13 +189,20 @@ class VechainTransactionBuilder(blockchain: Blockchain, private val publicKey: W } private companion object { - private const val TX_GAS = 5_000L - private const val CLAUSE_GAS = 16_000L - private const val VM_INVOCATION_COST = 15_000L + const val TX_GAS = 5_000L + const val CLAUSE_GAS = 16_000L + const val VM_INVOCATION_COST = 15_000L + + const val Z_GAS = 4L + const val NZ_GAS = 68L + + const val GAS_TO_VET_DECIMAL = 5 + + const val NORMAL_FEE_COEFFICIENT = 1.5 + const val PRIORITY_FEE_COEFFICIENT = 2 - private const val Z_GAS = 4L - private const val NZ_GAS = 68L + const val EXPIRATION_BLOCKS = 180 - private const val GAS_TO_VET_DECIMAL = 5 + const val PUBLIC_KEY_LENGTH = 64 } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkProvider.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkProvider.kt index 821c05718..41db1ba28 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkProvider.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkProvider.kt @@ -63,7 +63,7 @@ internal class VechainNetworkProvider( private fun mapBlockInfo(response: VechainLatestBlockResponse): VechainBlockInfo { val blockRef = response.blockId .removePrefix("0x") - .substring(0..15) + .substring(range = 0..BLOCK_REFERENCE_LENGTH) .toLongOrNull(radix = 16) return VechainBlockInfo( blockId = response.blockId, @@ -71,4 +71,9 @@ internal class VechainNetworkProvider( blockNumber = response.number, ) } + + private companion object { + + const val BLOCK_REFERENCE_LENGTH = 15 + } } diff --git a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkService.kt b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkService.kt index ac47f3c12..dfd27de39 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkService.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/blockchains/vechain/network/VechainNetworkService.kt @@ -47,7 +47,7 @@ internal class VechainNetworkService( accountResponse = accountInfoDeferred.await().extractResult(), pendingTxsInfo = pendingTxsDeferred.awaitAll().map { it.extractResult() }, tokenBalances = tokenBalances, - ) + ), ) } } catch (e: Exception) { diff --git a/blockchain/src/main/java/com/tangem/blockchain/common/assembly/impl/VechainWalletManagerAssembly.kt b/blockchain/src/main/java/com/tangem/blockchain/common/assembly/impl/VechainWalletManagerAssembly.kt index 2e8125247..18e609925 100644 --- a/blockchain/src/main/java/com/tangem/blockchain/common/assembly/impl/VechainWalletManagerAssembly.kt +++ b/blockchain/src/main/java/com/tangem/blockchain/common/assembly/impl/VechainWalletManagerAssembly.kt @@ -9,7 +9,10 @@ internal object VechainWalletManagerAssembly : WalletManagerAssembly