diff --git a/app/src/main/java/com/example/android_sdk/account.kt b/app/src/main/java/com/example/android_sdk/account.kt index 47825c9..2fd054a 100644 --- a/app/src/main/java/com/example/android_sdk/account.kt +++ b/app/src/main/java/com/example/android_sdk/account.kt @@ -4,7 +4,19 @@ import kotlinx.coroutines.* import org.apache.commons.lang3.RandomUtils import org.json.JSONArray import org.json.JSONObject +import org.web3j.abi.FunctionEncoder +import org.web3j.abi.FunctionReturnDecoder +import org.web3j.abi.TypeReference +import org.web3j.abi.datatypes.Address +import org.web3j.abi.datatypes.Function +import org.web3j.abi.datatypes.Utf8String +import org.web3j.abi.datatypes.generated.Uint8 import org.web3j.crypto.* +import org.web3j.protocol.Web3j +import org.web3j.protocol.core.DefaultBlockParameterName +import org.web3j.protocol.core.methods.request.Transaction +import org.web3j.protocol.http.HttpService +import org.web3j.utils.Convert import org.web3j.utils.Numeric import java.math.BigDecimal import java.math.BigInteger @@ -217,82 +229,116 @@ suspend fun getBalanceAsync( owner_account: String, token_address: String? = "0x0000000000000000000000000000000000000000" ): JSONObject = withContext(Dispatchers.IO) { + networkSettings(network) val jsonData = JSONObject() - val dbConnector = DBConnector() - dbConnector.connect() - val connection = dbConnector.getConnection() - // return array & object val resultArray = JSONArray() val resultData = JSONObject().apply { put("result", "FAIL") put("value", resultArray) } - - val query = - "SELECT balance, (SELECT decimals FROM token_table WHERE t.network ='$network' AND t.token_address ='$token_address' LIMIT 1) FROM token_owner_table t WHERE network = '$network' AND owner_account = '$owner_account' AND token_address = '$token_address'" - - connection?.use { - val dbQueryExecutor = DBQueryExector(it) - val resultSet = dbQueryExecutor.executeQuery(query) - resultSet?.use { - while (it.next()) { - val jsonData = JSONObject().apply { - var balance = it.getString("balance") - var decimals = it.getString("decimals") - var newBalance = - BigDecimal(balance.toDouble()).divide(BigDecimal.TEN.pow(decimals.toInt())) - put("balance", newBalance.toString()) - - } - resultArray.put(jsonData) - } + try { + val web3j = Web3j.build(HttpService(rpcUrl)) + + if(token_address == "0x0000000000000000000000000000000000000000") { + val ethGetBalance = web3j.ethGetBalance(owner_account, DefaultBlockParameterName.LATEST).send() + val balance = ethGetBalance.balance + val balanceEther = Convert.fromWei(balance.toString(), Convert.Unit.ETHER) + jsonData.put("balance", balanceEther) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + val balanceFunction = Function("balanceOf", listOf(Address(owner_account)), listOf(object : TypeReference() {})) + val encodedbalanceFunction = FunctionEncoder.encode(balanceFunction) + val balanceResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, token_address, encodedbalanceFunction), + DefaultBlockParameterName.LATEST + ).send() + val tokenBalance = BigInteger(balanceResponse.result.replace("0x", ""), 16) + val decimalsFunction = Function("decimals", emptyList(), listOf(object : TypeReference() {})) + val encodedDecimalsFunction = FunctionEncoder.encode(decimalsFunction) + val decimalsResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, token_address, encodedDecimalsFunction), + DefaultBlockParameterName.LATEST + ).send() + val decimalsOutput = + FunctionReturnDecoder.decode(decimalsResponse.result, decimalsFunction.outputParameters) + val decimals = (decimalsOutput[0].value as BigInteger).toInt() + var newBalance = + BigDecimal(tokenBalance.toDouble()).divide(BigDecimal.TEN.pow(decimals.toInt())) + jsonData.put("balance", newBalance) + resultArray.put(jsonData) resultData.put("result", "OK") resultData.put("value", resultArray) } + } catch (e: Exception) { + jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } - dbConnector.disconnect() - resultData } suspend fun getTokenInfoAsync( network: String, token_address: String, ) : JSONObject = withContext(Dispatchers.IO) { - val dbConnector = DBConnector() - dbConnector.connect() - val connection = dbConnector.getConnection() - + networkSettings(network) + val jsonData = JSONObject() + // return array & object val resultArray = JSONArray() val resultData = JSONObject().apply { put("result", "FAIL") put("value", resultArray) } - - val query = - "SELECT network, token_address, token_name, token_symbol, decimals, total_supply FROM token_table WHERE network = '$network' AND token_address = '$token_address'" - - connection?.use { - val dbQueryExecutor = DBQueryExector(it) - val resultSet = dbQueryExecutor.executeQuery(query) - resultSet?.use { - while (it.next()) { - val jsonData = JSONObject().apply { - put("network", it.getString("network")) - put("token_id", it.getString("token_address")) - put("name", it.getString("token_name")) - put("symbol", it.getString("token_symbol")) - put("decimals", it.getString("decimals")) - put("total_supply", it.getString("total_supply")) - } - resultArray.put(jsonData) - } - resultData.put("result", "OK") - resultData.put("value", resultArray) - } + try { + val web3j = Web3j.build(HttpService(rpcUrl)) + + val nameFunction = Function("name", emptyList(), listOf(object : TypeReference() {})) + val encodedNameFunction = FunctionEncoder.encode(nameFunction) + val nameResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, token_address, encodedNameFunction), + DefaultBlockParameterName.LATEST + ).send() + + val nameOutput = FunctionReturnDecoder.decode(nameResponse.result, nameFunction.outputParameters) + + val tokenName: String = nameOutput[0].value as String + + + val symbolFunction = Function("symbol", emptyList(), listOf(object : TypeReference() {})) + val encodedSymbolFunction = FunctionEncoder.encode(symbolFunction) + val symbolResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, token_address, encodedSymbolFunction), + DefaultBlockParameterName.LATEST + ).send() + + val symbolOutput = FunctionReturnDecoder.decode(symbolResponse.result, symbolFunction.outputParameters) + + val tokenSymbol: String = symbolOutput[0].value as String + + val decimalsFunction = Function("decimals", emptyList(), listOf(object : TypeReference() {})) + val encodedDecimalsFunction = FunctionEncoder.encode(decimalsFunction) + val decimalsResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, token_address, encodedDecimalsFunction), + DefaultBlockParameterName.LATEST + ).send() + val decimalsOutput = + FunctionReturnDecoder.decode(decimalsResponse.result, decimalsFunction.outputParameters) + val decimals = (decimalsOutput[0].value as BigInteger).toInt() + jsonData.put("name", tokenName) + jsonData.put("symbol", tokenSymbol) + jsonData.put("decimals", decimals) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } catch (e: Exception) { + jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } - dbConnector.disconnect() - resultData } suspend fun getTokenHistoryAsync( @@ -542,4 +588,3 @@ suspend fun getSignerAddressFromSignature( - diff --git a/app/src/main/java/com/example/android_sdk/global.kt b/app/src/main/java/com/example/android_sdk/global.kt index 1fa000d..5251fed 100644 --- a/app/src/main/java/com/example/android_sdk/global.kt +++ b/app/src/main/java/com/example/android_sdk/global.kt @@ -3,6 +3,8 @@ package com.example.android_sdk import android.content.Context import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import org.json.JSONArray +import org.json.JSONObject import org.web3j.abi.FunctionEncoder import org.web3j.abi.FunctionReturnDecoder import org.web3j.abi.TypeReference @@ -33,7 +35,7 @@ import java.util.Base64 import javax.crypto.Cipher fun kthuluSdkVersion(){ - println("SDK version:0.0.77, Connect OK") + println("SDK version:0.0.80, Connect OK") } var rpcUrl =""; var bridgeConfigContractAddress = ""; @@ -202,480 +204,504 @@ fun removeData(key: String) { suspend fun getEstimateGasAsync( network: String, - txType: String, - tokenAddress: String? = null, - fromAddress: String? = null, - toAddress: String? = null, - tokenAmount: String? = null, - tokenId: String? = null, - toTokenAddress: String? = null, - toNetwork: String? = null, - decimals: Int? = null, + tx_type: String, + token_id: String? = null, + to_network: String? = null, + to_token_id: String? = null, + from: String? = null, + to: String? = null, + nft_token_id: String? = null, + amount: String? = null, batchTokenId: Array? = null, batchTokenAmount: Array? = null, name: String? = null, symbol: String? = null, - owner: String? = null, baseURI: String? = null, uriType: String? = null, tokenURI: String? = null, batchTokenURI: Array? = null -): BigInteger = withContext(Dispatchers.IO) { - networkSettings(network) - var result = BigInteger.ZERO; - val web3 = Web3j.build(HttpService(rpcUrl)) - val gasPrice = web3.ethGasPrice().sendAsync().get().gasPrice - - when(txType) { - "baseFee" -> result = gasPrice - "transferCoin" -> - try { - result = web3.ethEstimateGas( - Transaction.createEtherTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - toAddress, - Convert.toWei(tokenAmount, Convert.Unit.ETHER).toBigInteger() // value - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO - } - "transferERC20" -> - if (tokenAddress != null && toAddress != null && fromAddress != null && tokenAmount != null) { - // Ensure amount is a valid number - if (BigDecimal(tokenAmount) <= BigDecimal.ZERO) BigInteger.ZERO - - val decimalsFunction = Function("decimals", emptyList(), listOf(object : TypeReference() {})) - val encodedDecimalsFunction = FunctionEncoder.encode(decimalsFunction) - val decimalsResponse = web3.ethCall( - Transaction.createEthCallTransaction(null, tokenAddress, encodedDecimalsFunction), - DefaultBlockParameterName.LATEST - ).send() - val decimalsOutput = - FunctionReturnDecoder.decode(decimalsResponse.result, decimalsFunction.outputParameters) - val decimals = (decimalsOutput[0].value as BigInteger).toInt() - val decimalMultiplier = BigDecimal.TEN.pow(decimals.toInt()) - val tokenAmount = BigDecimal(tokenAmount).multiply(decimalMultiplier).toBigInteger() - - val function = Function( - "transfer", - listOf(Address(toAddress), Uint256(tokenAmount)), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) +): JSONObject = withContext(Dispatchers.IO) { + networkSettings(network) + val jsonData = JSONObject() + + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + + try{ + val web3 = Web3j.build(HttpService(rpcUrl)) + val gasPrice = web3.ethGasPrice().sendAsync().get().gasPrice + var result = BigInteger.ZERO; + when(tx_type) { + "baseFee" -> result = gasPrice + "transferCoin" -> try { result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, + Transaction.createEtherTransaction( + from, BigInteger.ONE, gasPrice, BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data + to, + Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger() // value ) ).send().amountUsed } catch (ex: Exception) { // Handle the exception appropriately result = BigInteger.ZERO } - } - "deployERC20" -> - if (name != null && symbol != null && fromAddress != null && tokenAmount != null) { - val function = Function( - "deployWrapped20", - listOf(Utf8String(name), Utf8String(symbol), Uint8(BigInteger("18")), Uint256(BigInteger(tokenAmount))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - bridgeContractAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "transferERC20" -> + if (token_id != null && to != null && from != null && amount != null) { + // Ensure amount is a valid number + if (BigDecimal(amount) <= BigDecimal.ZERO) BigInteger.ZERO + + val decimalsFunction = Function("decimals", emptyList(), listOf(object : TypeReference() {})) + val encodedDecimalsFunction = FunctionEncoder.encode(decimalsFunction) + val decimalsResponse = web3.ethCall( + Transaction.createEthCallTransaction(null, token_id, encodedDecimalsFunction), + DefaultBlockParameterName.LATEST + ).send() + val decimalsOutput = + FunctionReturnDecoder.decode(decimalsResponse.result, decimalsFunction.outputParameters) + val decimals = (decimalsOutput[0].value as BigInteger).toInt() + val decimalMultiplier = BigDecimal.TEN.pow(decimals.toInt()) + val tokenAmount = BigDecimal(amount).multiply(decimalMultiplier).toBigInteger() + + val function = Function( + "transfer", + listOf(Address(to), Uint256(tokenAmount)), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "bridgeToken" -> - if (fromAddress != null && tokenAmount != null) { - val function = Function( - "moveFromETHER", - listOf(Uint256("KLAYTNs".toBigInteger())), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - bridgeContractAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "deployERC20" -> + if (name != null && symbol != null && from != null && amount != null) { + val function = Function( + "deployWrapped20", + listOf(Utf8String(name), Utf8String(symbol), Uint8(BigInteger("18")), Uint256(BigInteger(amount))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + bridgeContractAddress, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "swapToken" -> - if (fromAddress != null && tokenAddress != null && tokenAmount != null && toTokenAddress != null) { - val path = DynamicArray(Address::class.java, listOf(Address(tokenAddress), Address(toTokenAddress))) - // Deadline is the current time + 10 minutes in seconds - val deadline = Instant.now().epochSecond + 600 - val function = Function("swapExactTokensForTokens", listOf(Uint256(BigInteger(tokenAmount)), Uint256(BigInteger.ZERO), path, Address(fromAddress), Uint256(deadline)), emptyList()) - - val encodedFunction = FunctionEncoder.encode(function) - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - uniswapV2RouterAddress, - BigInteger.ZERO, // value - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "bridgeToken" -> + if (from != null && amount != null) { + val function = Function( + "moveFromETHER", + listOf(Uint256("KLAYTNs".toBigInteger())), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + bridgeContractAddress, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "transferERC721" -> - if (tokenAddress != null && toAddress != null && fromAddress != null && tokenId != null) { - val function = Function( - "safeTransferFrom", - listOf(Address(fromAddress), Address(toAddress), Uint256(BigInteger(tokenId))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "swapToken" -> + if (from != null && token_id != null && amount != null && to_token_id != null) { + val path = DynamicArray(Address::class.java, listOf(Address(token_id), Address(to_token_id))) + // Deadline is the current time + 10 minutes in seconds + val deadline = Instant.now().epochSecond + 600 + val function = Function("swapExactTokensForTokens", listOf(Uint256(BigInteger(amount)), Uint256(BigInteger.ZERO), path, Address(from), Uint256(deadline)), emptyList()) + + val encodedFunction = FunctionEncoder.encode(function) + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + uniswapV2RouterAddress, + BigInteger.ZERO, // value + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "transferERC1155" -> - if (tokenAddress != null && toAddress != null && fromAddress != null && tokenId != null && tokenAmount != null) { - val function = Function( - "safeTransferFrom", - listOf( - Address(fromAddress), Address(toAddress), Uint256(BigInteger(tokenId)), - Uint256(BigInteger(tokenAmount)), DynamicBytes(byteArrayOf(0)) - ), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "transferERC721" -> + if (token_id != null && to != null && from != null && nft_token_id != null) { + val function = Function( + "safeTransferFrom", + listOf(Address(from), Address(to), Uint256(BigInteger(nft_token_id))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "batchTransferERC721" -> - if (tokenAddress != null && toAddress != null && fromAddress != null && batchTokenId != null) { - val batchTokenId = batchTokenId.map { Uint256(BigInteger(it)) } - val function = Function( - "safeBatchTransferFrom", - listOf( - Address(fromAddress), Address(toAddress), DynamicArray(batchTokenId) - ), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "transferERC1155" -> + if (token_id != null && to != null && from != null && nft_token_id != null && amount != null) { + val function = Function( + "safeTransferFrom", + listOf( + Address(from), Address(to), Uint256(BigInteger(nft_token_id)), + Uint256(BigInteger(amount)), DynamicBytes(byteArrayOf(0)) + ), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "batchTransferERC1155" -> - if (tokenAddress != null && toAddress != null && fromAddress != null && batchTokenId != null && batchTokenAmount != null) { - val batchTokenId = batchTokenId.map { Uint256(BigInteger(it)) } - val batchAmount = batchTokenAmount.map { Uint256(BigInteger(it)) } - val function = Function( - "safeBatchTransferFrom", - listOf( - Address(fromAddress), Address(toAddress), DynamicArray(batchTokenId), DynamicArray(batchAmount), DynamicBytes(byteArrayOf(0)) - ), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "batchTransferERC721" -> + if (token_id != null && to != null && from != null && batchTokenId != null) { + val batchTokenId = batchTokenId.map { Uint256(BigInteger(it)) } + val function = Function( + "safeBatchTransferFrom", + listOf( + Address(from), Address(to), DynamicArray(batchTokenId) + ), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "deployERC721" -> - if (name != null && symbol != null && fromAddress != null && baseURI != null && uriType != null) { - val function = Function( - "deployWrapped721", - listOf(Utf8String(name), Utf8String(symbol), Utf8String(baseURI), Uint8(BigInteger(uriType))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - nftTransferContractAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "batchTransferERC1155" -> + if (token_id != null && to != null && from != null && batchTokenId != null && batchTokenAmount != null) { + val batchTokenId = batchTokenId.map { Uint256(BigInteger(it)) } + val batchAmount = batchTokenAmount.map { Uint256(BigInteger(it)) } + val function = Function( + "safeBatchTransferFrom", + listOf( + Address(from), Address(to), DynamicArray(batchTokenId), DynamicArray(batchAmount), DynamicBytes(byteArrayOf(0)) + ), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "deployERC1155" -> - if (name != null && symbol != null && fromAddress != null && baseURI != null && uriType != null) { - val function = Function( - "deployWrapped1155", - listOf(Utf8String(name), Utf8String(symbol), Utf8String(baseURI), Uint8(BigInteger(uriType))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - nftTransferContractAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "deployERC721" -> + if (name != null && symbol != null && from != null && baseURI != null && uriType != null) { + val function = Function( + "deployWrapped721", + listOf(Utf8String(name), Utf8String(symbol), Utf8String(baseURI), Uint8(BigInteger(uriType))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + nftTransferContractAddress, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "mintERC721" -> - if (fromAddress != null && toAddress != null && tokenURI != null && tokenId != null && tokenAddress != null) { - val function = Function( - "mint", - listOf(Address(toAddress), Uint256(BigInteger(tokenId)), Utf8String(tokenURI)), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "deployERC1155" -> + if (name != null && symbol != null && from != null && baseURI != null && uriType != null) { + val function = Function( + "deployWrapped1155", + listOf(Utf8String(name), Utf8String(symbol), Utf8String(baseURI), Uint8(BigInteger(uriType))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + nftTransferContractAddress, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "mintERC1155" -> - if (fromAddress != null && toAddress != null && tokenURI != null && tokenId != null && tokenAddress != null && tokenAmount!= null) { - val function = Function( - "mint", - listOf(Address(toAddress), Uint256(BigInteger(tokenId)), Uint256(BigInteger(tokenAmount)), Utf8String(tokenURI), DynamicBytes(byteArrayOf(0))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "mintERC721" -> + if (from != null && to != null && tokenURI != null && nft_token_id != null && token_id != null) { + val function = Function( + "mint", + listOf(Address(to), Uint256(BigInteger(nft_token_id)), Utf8String(tokenURI)), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "batchMintERC721" -> - if (fromAddress != null && toAddress != null && batchTokenURI != null && batchTokenId != null && tokenAddress != null) { - val a = batchTokenId.map { Uint256(BigInteger(it)) } - val b = batchTokenURI.map { Utf8String(it) } - - val function = Function( - "mintBatch", - listOf(Address(toAddress), DynamicArray(a), DynamicArray(b)), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "mintERC1155" -> + if (from != null && to != null && tokenURI != null && nft_token_id != null && token_id != null && amount!= null) { + val function = Function( + "mint", + listOf(Address(to), Uint256(BigInteger(nft_token_id)), Uint256(BigInteger(amount)), Utf8String(tokenURI), DynamicBytes(byteArrayOf(0))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "batchMintERC1155" -> - if (fromAddress != null && toAddress != null && batchTokenURI != null && batchTokenId != null && tokenAddress != null && batchTokenAmount!= null) { - val a = batchTokenId.map { Uint256(BigInteger(it)) } - val b = batchTokenAmount.map { Uint256(BigInteger(it)) } - val c = batchTokenURI.map { Utf8String(it) } - - val function = Function( - "mintBatch", - listOf(Address(toAddress), DynamicArray(a), DynamicArray(b), DynamicArray(c), DynamicBytes(byteArrayOf(0))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "batchMintERC721" -> + if (from != null && to != null && batchTokenURI != null && batchTokenId != null && token_id != null) { + val a = batchTokenId.map { Uint256(BigInteger(it)) } + val b = batchTokenURI.map { Utf8String(it) } + + val function = Function( + "mintBatch", + listOf(Address(to), DynamicArray(a), DynamicArray(b)), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "burnERC721" -> - if (fromAddress != null && tokenId != null && tokenAddress != null) { - val function = Function( - "burn", - listOf(Uint256(BigInteger(tokenId))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "batchMintERC1155" -> + if (from != null && to != null && batchTokenURI != null && batchTokenId != null && token_id != null && batchTokenAmount!= null) { + val a = batchTokenId.map { Uint256(BigInteger(it)) } + val b = batchTokenAmount.map { Uint256(BigInteger(it)) } + val c = batchTokenURI.map { Utf8String(it) } + + val function = Function( + "mintBatch", + listOf(Address(to), DynamicArray(a), DynamicArray(b), DynamicArray(c), DynamicBytes(byteArrayOf(0))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } - "burnERC1155" -> - if (fromAddress != null && tokenId != null && tokenAddress != null && tokenAmount != null) { - val function = Function( - "burn", - listOf(Address(fromAddress), Uint256(BigInteger(tokenId)), Uint256(BigInteger(tokenAmount))), - emptyList() - ) - val encodedFunction = FunctionEncoder.encode(function) - - try { - result = web3.ethEstimateGas( - Transaction.createFunctionCallTransaction( - fromAddress, - BigInteger.ONE, - gasPrice, - BigInteger.ZERO, // temporary gasLimit - tokenAddress, - encodedFunction // data - ) - ).send().amountUsed - } catch (ex: Exception) { - // Handle the exception appropriately - result = BigInteger.ZERO + "burnERC721" -> + if (from != null && nft_token_id != null && token_id != null) { + val function = Function( + "burn", + listOf(Uint256(BigInteger(nft_token_id))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } + } + "burnERC1155" -> + if (from != null && nft_token_id != null && token_id != null && amount != null) { + val function = Function( + "burn", + listOf(Address(from), Uint256(BigInteger(nft_token_id)), Uint256(BigInteger(amount))), + emptyList() + ) + val encodedFunction = FunctionEncoder.encode(function) + + try { + result = web3.ethEstimateGas( + Transaction.createFunctionCallTransaction( + from, + BigInteger.ONE, + gasPrice, + BigInteger.ZERO, // temporary gasLimit + token_id, + encodedFunction // data + ) + ).send().amountUsed + } catch (ex: Exception) { + // Handle the exception appropriately + result = BigInteger.ZERO + } } - } + } + BigDecimal(result).multiply(BigDecimal(1.2)).setScale(0, RoundingMode.DOWN).toBigInteger() + if (result == BigInteger.ZERO) { + jsonData.put("error", "execution revert") + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } else { + jsonData.put("gas", result) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } + } catch (e: Exception) { + jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } - BigDecimal(result).multiply(BigDecimal(1.2)).setScale(0, RoundingMode.DOWN).toBigInteger() } fun textToHex(text: String): String { diff --git a/app/src/main/java/com/example/android_sdk/nft.kt b/app/src/main/java/com/example/android_sdk/nft.kt index 4beb79a..2302a1d 100644 --- a/app/src/main/java/com/example/android_sdk/nft.kt +++ b/app/src/main/java/com/example/android_sdk/nft.kt @@ -1050,10 +1050,16 @@ suspend fun sendNFT721TransactionAsync( to: String, token_id: String, collection_id: String -): JSONObject = withContext(Dispatchers.IO){ +): JSONObject = withContext(Dispatchers.IO) { networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1083,19 +1089,31 @@ suspend fun sendNFT721TransactionAsync( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "transferERC721", + collection_id, + from, + to, + null, + token_id + ) + + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "transferERC721", - collection_id, - from, - to, - null, - token_id - ), // Add 20% to the gas limit + BigInteger(gasPrice), + BigInteger(gasLimit),// Add 20% to the gas limit collection_id, encodedFunction ) @@ -1103,33 +1121,38 @@ suspend fun sendNFT721TransactionAsync( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "transferERC721", - collection_id, - from, - to, - null, - token_id - ), + BigInteger(gasLimit), collection_id, BigInteger.ZERO, encodedFunction, //1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = + web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1141,8 +1164,14 @@ suspend fun sendNFT1155TransactionAsync( collection_id: String, amount: String, ): JSONObject = withContext(Dispatchers.IO) { - val jsonData = JSONObject() networkSettings(network) + val jsonData = JSONObject() + + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1182,22 +1211,31 @@ suspend fun sendNFT1155TransactionAsync( .get() .transactionCount - val gasPrice = ethGasPrice.gasPrice - val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate =getEstimateGasAsync( + network, + "transferERC1155", + collection_id, + from, + to, + amount, + token_id + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "transferERC1155", - collection_id, - from, - to, - amount, - token_id - ), // Add 20% to the gas limit + BigInteger(gasPrice), + BigInteger(gasLimit),// Add 20% to the gas limit collection_id, encodedFunction ) @@ -1205,25 +1243,37 @@ suspend fun sendNFT1155TransactionAsync( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync(network, "transferERC1155",collection_id, from, to, amount, token_id), + BigInteger(gasLimit), collection_id, BigInteger.ZERO, encodedFunction, //1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1234,8 +1284,14 @@ suspend fun sendNFT721BatchTransactionAsync( token_id: Array, collection_id: String ): JSONObject = withContext(Dispatchers.IO) { - val jsonData = JSONObject() networkSettings(network) + val jsonData = JSONObject() + + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1269,26 +1325,35 @@ suspend fun sendNFT721BatchTransactionAsync( .get() .transactionCount - val gasPrice = ethGasPrice.gasPrice - val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "batchTransferERC721", + collection_id, + from, + to, + null, + null, + null, + null, + null, + token_id + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "batchTransferERC721", - collection_id, - from, - to, - null, - null, - null, - null, - null, - token_id - ), // Add 20% to the gas limit + BigInteger(gasPrice), + BigInteger(gasLimit),// Add 20% to the gas limit collection_id, encodedFunction ) @@ -1296,37 +1361,37 @@ suspend fun sendNFT721BatchTransactionAsync( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "batchTransferERC721", - collection_id, - from, - to, - null, - null, - null, - null, - null, - token_id - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1342,6 +1407,12 @@ suspend fun sendNFT1155BatchTransactionAsync( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1384,27 +1455,35 @@ suspend fun sendNFT1155BatchTransactionAsync( .get() .transactionCount - val gasPrice = ethGasPrice.gasPrice - val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "batchTransferERC1155", + collection_id, + from, + to, + null, + null, + null, + null, + token_id, + amount + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "batchTransferERC1155", - collection_id, - from, - to, - null, - null, - null, - null, - null, - token_id, - amount - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -1412,38 +1491,37 @@ suspend fun sendNFT1155BatchTransactionAsync( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "batchTransferERC1155", - collection_id, - from, - to, - null, - null, - null, - null, - null, - token_id, - amount - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1458,6 +1536,12 @@ suspend fun deployErc721Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1491,25 +1575,35 @@ suspend fun deployErc721Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "deployERC721", + null, + from, + null, + null, + null, + null, + null, + null, + null, + name, symbol, token_base_uri, uri_type + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "deployERC721", - null, - from, - null, - null, - null, - null, - null, - null, - null, - null, - name, symbol, null, token_base_uri, uri_type - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit nftTransferContractAddress, encodedFunction ) @@ -1517,39 +1611,37 @@ suspend fun deployErc721Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "deployERC721", - null, - from, - null, - null, - null, - null, - null, - null, - null, - null, - name, symbol, null, token_base_uri, uri_type - ), + BigInteger(gasLimit), nftTransferContractAddress, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result", "OK") - jsonData.put("transactionHash", txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1564,6 +1656,12 @@ suspend fun deployErc1155Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1597,25 +1695,35 @@ suspend fun deployErc1155Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate =getEstimateGasAsync( + network, + "deployERC1155", + null, + from, + null, + null, + null, + null, + null, + null, + null, + name, symbol, token_base_uri, uri_type + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "deployERC1155", - null, - from, - null, - null, - null, - null, - null, - null, - null, - null, - name, symbol, null, token_base_uri, uri_type - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit nftTransferContractAddress, encodedFunction ) @@ -1623,40 +1731,37 @@ suspend fun deployErc1155Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "deployERC1155", - null, - from, - null, - null, - null, - null, - null, - null, - null, - null, - name, symbol, null, token_base_uri, uri_type - ), + BigInteger(gasLimit), nftTransferContractAddress, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result", "OK") - jsonData.put("transactionHash", txHash) - jsonData.put("maxPriorityFeePerGas", maxPriorityFeePerGas) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1672,6 +1777,12 @@ suspend fun mintErc721Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1700,21 +1811,32 @@ suspend fun mintErc721Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "mintERC721", + collection_id, + from, + to, + null, + token_id, + null, null, null, null, null, null, null, null, + token_uri + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "mintERC721", - collection_id, - from, - to, - null, - token_id, - null, null, null, null, null, null, null, null, null, null, - token_uri - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -1722,35 +1844,37 @@ suspend fun mintErc721Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "mintERC721", - collection_id, - from, - to, - null, - token_id, - null, null, null, null, null, null, null, null, null, null, - token_uri - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1766,6 +1890,12 @@ suspend fun mintErc1155Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1794,21 +1924,32 @@ suspend fun mintErc1155Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "mintERC1155", + collection_id, + from, + to, + amount, + token_id, + null, null, null, null, null, null, null, null, + token_uri + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "mintERC1155", - collection_id, - from, - to, - amount, - token_id, - null, null, null, null, null, null, null, null, null, null, - token_uri - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -1816,35 +1957,37 @@ suspend fun mintErc1155Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "mintERC1155", - collection_id, - from, - to, - amount, - token_id, - null, null, null, null, null, null, null, null, null, null, - token_uri - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -1859,6 +2002,12 @@ suspend fun batchMintErc721Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1890,21 +2039,32 @@ suspend fun batchMintErc721Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "batchMintERC721", + collection_id, + from, + to, + null, + null, + null, null, null, token_id, null, null, null, null, + null, token_uri + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "batchMintERC721", - collection_id, - from, - to, - null, - null, - null, null, null, token_id, null, null, null, null, null, null, - null, token_uri - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -1912,36 +2072,37 @@ suspend fun batchMintErc721Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "batchMintERC721", - collection_id, - from, - to, - null, - null, - null, null, null, token_id, null, null, null, null, null, null, - null, token_uri - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) - jsonData.put("maxPriorityFeePerGas",maxPriorityFeePerGas) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } suspend fun batchMintErc1155Async( @@ -1956,6 +2117,12 @@ suspend fun batchMintErc1155Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(from) val privateKey = runCatching { @@ -1988,21 +2155,32 @@ suspend fun batchMintErc1155Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "batchMintERC1155", + collection_id, + from, + to, + null, + null, + null, null, token_id, amount, null, null, null, null, null, + token_uri + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "batchMintERC1155", - collection_id, - from, - to, - null, - null, - null, null, null, token_id, amount, null, null, null, null, null, - null, token_uri - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -2010,35 +2188,37 @@ suspend fun batchMintErc1155Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "batchMintERC1155", - collection_id, - from, - to, - null, - null, - null, null, null, token_id, amount, null, null, null, null, null, - null, token_uri - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -2051,6 +2231,12 @@ suspend fun burnErc721Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(owner) val privateKey = runCatching { @@ -2079,21 +2265,32 @@ suspend fun burnErc721Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "burnERC721", + collection_id, + owner, + null, + null, + token_id, + null, null, null, null, null, null, null, null, null, null, + + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "burnERC721", - collection_id, - owner, - null, - null, - token_id, - null, null, null, null, null, null, null, null, null, null, - null, null - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -2101,35 +2298,37 @@ suspend fun burnErc721Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "burnERC721", - collection_id, - owner, - null, - null, - token_id, - null, null, null, null, null, null, null, null, null, null, - null, null - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -2143,6 +2342,12 @@ suspend fun burnErc1155Async( networkSettings(network) val jsonData = JSONObject() + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + try { val getAddressInfo = getAccountInfoAsync(owner) val privateKey = runCatching { @@ -2171,21 +2376,31 @@ suspend fun burnErc1155Async( .transactionCount val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val gasLimitEstimate = getEstimateGasAsync( + network, + "burnERC1155", + collection_id, + owner, + null, + amount, + token_id, + null, null, null, null, null, null, null, null, null, null + ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - getEstimateGasAsync( - network, - "burnERC1155", - collection_id, - owner, - null, - amount, - token_id, - null, null, null, null, null, null, null, null, null, null, - null, null - ), // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, encodedFunction ) @@ -2193,35 +2408,37 @@ suspend fun burnErc1155Async( RawTransaction.createTransaction( chainId, nonce, - getEstimateGasAsync( - network, - "burnERC1155", - collection_id, - owner, - null, - amount, - token_id, - null, null, null, null, null, null, null, null, null, null, - null, null - ), // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit collection_id, BigInteger.ZERO, encodedFunction, //0.1gwei BigInteger(maxPriorityFeePerGas), - getEstimateGasAsync(network, "baseFee") + BigInteger(gasPrice) ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - jsonData.put("result","OK") - jsonData.put("transactionHash",txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) + } else { + jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } @@ -2232,8 +2449,15 @@ suspend fun bridgeErc721Async( token_id: String, token_address: String ): JSONObject = withContext(Dispatchers.IO) { - val jsonData = JSONObject() networkSettings(network) + val jsonData = JSONObject() + + // return array & object + val resultArray = JSONArray() + var resultData = JSONObject() + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + val getAddressInfo = getAccountInfoAsync(fromAddress) val privateKey = runCatching { getAddressInfo.getJSONArray("value") @@ -2297,11 +2521,16 @@ suspend fun bridgeErc721Async( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger(gasPrice), // Add 20% to the gas price BigInteger.valueOf(200000), // Add 20% to the gas limit nftTransferContractAddress, encodedFunction @@ -2315,23 +2544,30 @@ suspend fun bridgeErc721Async( nftFee, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) val signedTx = Numeric.toHexString(signedMessage) - val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - if (txHash != null) { - jsonData.put("result", "OK") - jsonData.put("transactionHash", txHash) + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (transactionHash != "") { + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "OK") + resultData.put("value", resultArray) } else { - jsonData.put("result", "FAIL") jsonData.put("error", "insufficient funds") + jsonData.put("transaction_hash", transactionHash) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } catch (e: Exception) { - jsonData.put("result", "FAIL") jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) } } diff --git a/app/src/main/java/com/example/android_sdk/transaction.kt b/app/src/main/java/com/example/android_sdk/transaction.kt index 1406e0c..2df1ed0 100644 --- a/app/src/main/java/com/example/android_sdk/transaction.kt +++ b/app/src/main/java/com/example/android_sdk/transaction.kt @@ -117,8 +117,7 @@ suspend fun sendTransactionAsync( .transactionCount val chainId = web3.ethChainId().sendAsync().get().chainId.toLong() - - val gasLimit = getEstimateGasAsync( + val gasLimitEstimate = getEstimateGasAsync( network, "transferCoin", null, @@ -126,11 +125,20 @@ suspend fun sendTransactionAsync( toAddress, amount ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val transaction = if (network == "bnb") { RawTransaction.createEtherTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price , - gasLimit, // Add 20% to the gas price , + BigInteger(gasPrice), // Add 20% to the gas price , + BigInteger(gasLimit), // Add 20% to the gas price , toAddress, weiAmount as BigInteger? // value ) @@ -139,12 +147,12 @@ suspend fun sendTransactionAsync( RawTransaction.createTransaction( chainId, nonce, - gasLimit, // gasLimit Add 20% to the gas limit, + BigInteger(gasLimit), // gasLimit Add 20% to the gas limit, toAddress, // to weiAmount, // value "0x", // data BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // maxFeePerGas Add 20% to the gas price + BigInteger(gasPrice) // maxFeePerGas Add 20% to the gas price ) } @@ -282,7 +290,7 @@ suspend fun sendTokenTransactionAsync( val chainId = web3.ethChainId().sendAsync().get().chainId.toLong() - val gasLimit = getEstimateGasAsync( + val gasLimitEstimate = getEstimateGasAsync( network, "transferERC20", token_address, @@ -290,11 +298,20 @@ suspend fun sendTokenTransactionAsync( toAddress, amount ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val transaction = if (network == "bnb") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price , - gasLimit, // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price , + BigInteger(gasLimit), // Add 20% to the gas limit token_address, // to tokenAmount, // value encodedFunction // data @@ -303,12 +320,12 @@ suspend fun sendTokenTransactionAsync( RawTransaction.createTransaction( chainId, nonce, - gasLimit, // gasLimit Add 20% to the gas limit, + BigInteger(gasLimit), // gasLimit Add 20% to the gas limit, token_address, // to BigInteger.ZERO, // value encodedFunction, // data BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // maxFeePerGas Add 20% to the gas price + BigInteger(gasPrice) // maxFeePerGas Add 20% to the gas price ) } @@ -439,7 +456,7 @@ suspend fun deployErc20Async( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() - val gasLimit = getEstimateGasAsync( + val gasLimitEstimate = getEstimateGasAsync( network, "deployERC20", null, @@ -454,12 +471,20 @@ suspend fun deployErc20Async( null, name, symbol ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - gasLimit, // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit bridgeContractAddress, encodedFunction ) @@ -467,12 +492,12 @@ suspend fun deployErc20Async( RawTransaction.createTransaction( chainId, nonce, - gasLimit, // Add 20% to the gas limit + BigInteger(gasLimit), // Add 20% to the gas limit bridgeContractAddress, BigInteger.ZERO, encodedFunction, BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -548,11 +573,16 @@ suspend fun bridgeTokenAsync( val amountInWei = Convert.toWei(amount, Convert.Unit.ETHER).toBigIntegerExact() + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger(gasPrice), // Add 20% to the gas price BigInteger.valueOf(200000), // Add 20% to the gas limit bridgeContractAddress, encodedFunction @@ -566,7 +596,7 @@ suspend fun bridgeTokenAsync( amountInWei, encodedFunction, BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -658,11 +688,16 @@ suspend fun bridgeTokenAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger(gasPrice), // Add 20% to the gas price BigInteger.valueOf(200000), // Add 20% to the gas limit bridgeContractAddress, encodedFunction @@ -676,7 +711,7 @@ suspend fun bridgeTokenAsync( tokenFee, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -775,11 +810,16 @@ suspend fun tokenSwapAppoveAsync( .get() .transactionCount + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger(gasPrice), // Add 20% to the gas price BigInteger("200000"), // Add 20% to the gas limit fromTokenId, arroveTokenEncodedFunction @@ -793,7 +833,7 @@ suspend fun tokenSwapAppoveAsync( BigInteger.ZERO, // value arroveTokenEncodedFunction, BigInteger(maxPriorityFeePerGas), // maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -901,11 +941,16 @@ suspend fun coinForTokenswapAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger(gasPrice), // Add 20% to the gas price BigInteger("200000"), // Add 20% to the gas limit uniswapV2RouterAddress, encodedFunction @@ -919,7 +964,7 @@ suspend fun coinForTokenswapAsync( amountInWei, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -1029,7 +1074,7 @@ suspend fun tokenForTokenswapAsync( .get() .transactionCount - val gasLimit = getEstimateGasAsync( + val gasLimitEstimate = getEstimateGasAsync( network, "swapToken", fromTokenId, @@ -1039,6 +1084,14 @@ suspend fun tokenForTokenswapAsync( "", toTokenId ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() @@ -1046,8 +1099,8 @@ suspend fun tokenForTokenswapAsync( if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - gasLimit, // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit uniswapV2RouterAddress, encodedFunction ) @@ -1055,12 +1108,12 @@ suspend fun tokenForTokenswapAsync( RawTransaction.createTransaction( chainId, nonce, - gasLimit, // Add 20% to the gas limit, + BigInteger(gasLimit), // Add 20% to the gas limit, uniswapV2RouterAddress, BigInteger.ZERO, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials) @@ -1074,7 +1127,6 @@ suspend fun tokenForTokenswapAsync( resultData.put("value", resultArray) } else { jsonData.put("error", "insufficient funds") - jsonData.put("transaction_hash", transactionHash) resultArray.put(jsonData) resultData.put("result", "FAIL") resultData.put("value", resultArray) @@ -1180,7 +1232,7 @@ suspend fun tokenForCoinswapAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() - val gasLimit = getEstimateGasAsync( + val gasLimitEstimate =getEstimateGasAsync( network, "swapToken", fromTokenId, @@ -1190,13 +1242,21 @@ suspend fun tokenForCoinswapAsync( "", toTokenId ) + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + + val gasLimit = gasLimitEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + val gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") val tx = if (network == "bnb" || network == "bnbTest") { RawTransaction.createTransaction( nonce, - getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price - gasLimit, // Add 20% to the gas limit + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger(gasLimit), // Add 20% to the gas limit uniswapV2RouterAddress, encodedFunction ) @@ -1209,7 +1269,7 @@ suspend fun tokenForCoinswapAsync( BigInteger.ZERO, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // maxPriorityFeePerGas - getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price + BigInteger(gasPrice) // Add 20% to the gas price ) } val signedMessage = TransactionEncoder.signMessage(tx, credentials)