From 327f8ee69cbe6ea068516ad6862f45f6922b75ff Mon Sep 17 00:00:00 2001 From: Seonghun Date: Tue, 29 Aug 2023 11:09:51 +0900 Subject: [PATCH] update --- .../main/java/io/kthulu/sdk/MainActivity.kt | 2 + app/src/main/java/io/kthulu/sdk/account.kt | 42 +- app/src/main/java/io/kthulu/sdk/global.kt | 16 +- app/src/main/java/io/kthulu/sdk/nft.kt | 550 +++++++++++++----- .../main/java/io/kthulu/sdk/transaction.kt | 40 +- 5 files changed, 475 insertions(+), 175 deletions(-) diff --git a/app/src/main/java/io/kthulu/sdk/MainActivity.kt b/app/src/main/java/io/kthulu/sdk/MainActivity.kt index 52d1cf3..bb0e620 100644 --- a/app/src/main/java/io/kthulu/sdk/MainActivity.kt +++ b/app/src/main/java/io/kthulu/sdk/MainActivity.kt @@ -23,8 +23,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + launch { withContext(Dispatchers.IO) { + } } } diff --git a/app/src/main/java/io/kthulu/sdk/account.kt b/app/src/main/java/io/kthulu/sdk/account.kt index 976ebd5..0b37082 100644 --- a/app/src/main/java/io/kthulu/sdk/account.kt +++ b/app/src/main/java/io/kthulu/sdk/account.kt @@ -388,7 +388,10 @@ suspend fun getTokenInfoAsync( suspend fun getTokenHistoryAsync( network: String, owner_account: String, - token_address: String = "0x0000000000000000000000000000000000000000" + token_address: String? = "0x0000000000000000000000000000000000000000", + sort: String? = "DESC", + limit: Int? = 1000, + page_number: Int? = 0 ) : JSONObject = withContext(Dispatchers.IO) { val resultArray = JSONArray() var jsonData = JSONObject() @@ -401,7 +404,7 @@ suspend fun getTokenHistoryAsync( val dbConnector = DBConnector() dbConnector.connect() val connection = dbConnector.getConnection() - + var total_count = 0 val query = "SELECT " + " network," + @@ -420,7 +423,10 @@ suspend fun getTokenHistoryAsync( "WHERE " + " network = '$network' AND token_address = '$token_address' AND (`from` ='$owner_account' OR `to` ='$owner_account')" + "ORDER BY " + - " block_number DESC" + " block_number $sort " + + "LIMIT $limit" + + " OFFSET ${(page_number!!) * limit!!}" + connection?.use { val dbQueryExecutor = DBQueryExector(it) val resultSet = dbQueryExecutor.executeQuery(query) @@ -438,11 +444,15 @@ suspend fun getTokenHistoryAsync( put("gas_used", it.getString("gas_used")) put("symbol", it.getString("symbol")) put("decimals", it.getString("decimals")) + total_count++ } resultArray.put(jsonData) } resultData.put("result", "OK") + resultData.put("sum", total_count) + resultData.put("sort", sort) + resultData.put("page_count", page_number) resultData.put("value", resultArray) } } @@ -515,8 +525,8 @@ suspend fun getTokenListAsync( network: String, ownerAddress: String, sort: String? = "DESC", - limit: Int? = null, - page_number: Int? = null): JSONObject = withContext(Dispatchers.IO) { + limit: Int? = 1000, + page_number: Int? = 0): JSONObject = withContext(Dispatchers.IO) { val resultArray = JSONArray() var jsonData = JSONObject() val resultData = JSONObject().apply { @@ -528,7 +538,7 @@ suspend fun getTokenListAsync( val dbConnector = DBConnector() dbConnector.connect() val connection = dbConnector.getConnection() - + var total_count = 0 val resultArray = JSONArray() val resultData = JSONObject().apply { put("result", "FAIL") @@ -547,18 +557,15 @@ suspend fun getTokenListAsync( " balance AS balance," + " (SELECT decimals FROM token_table WHERE network = t.network AND token_address = t.token_address LIMIT 1) AS decimals," + " (SELECT token_symbol FROM token_table WHERE network = t.network AND token_address = t.token_address LIMIT 1) AS symbol," + - " (SELECT token_name FROM token_table WHERE network = t.network AND token_address = t.token_address LIMIT 1) AS name," + - " (SELECT COUNT(*) FROM token_owner_table WHERE network = '$network' AND owner_account = '$ownerAddress') AS sum " + + " (SELECT token_name FROM token_table WHERE network = t.network AND token_address = t.token_address LIMIT 1) AS name " + " FROM" + " token_owner_table t" + " WHERE" + " network = '$network' AND owner_account = '$ownerAddress'" + " ORDER BY" + " idx $sort"; - - if(offset != 0) { - query += " LIMIT $limit OFFSET $offset"; - } + "LIMIT $limit" + + " OFFSET ${(page_number!!) * limit!!}" connection?.use { val dbQueryExecutor = DBQueryExector(it) @@ -573,12 +580,14 @@ suspend fun getTokenListAsync( put("decimals", it.getString("decimals")) put("symbol", it.getString("symbol")) put("name", it.getString("name")) + total_count ++ } resultArray.put(jsonData) - sum = it.getInt("sum") } resultData.put("result", "OK") - resultData.put("sum", sum) + resultData.put("sum", total_count) + resultData.put("sort", sort) + resultData.put("page_count", page_number) resultData.put("value", resultArray) } } @@ -593,9 +602,9 @@ suspend fun getTokenListAsync( } suspend fun signMessage( + network: String, fromAddress: String, collection_id: String, - network: String, token_id: String, prefix: String ): String { @@ -634,10 +643,10 @@ suspend fun signMessage( } suspend fun getSignerAddressFromSignature( + network: String, signature: String, fromAddress: String, collection_id: String, - network: String, token_id: String, prefix: String ): String { @@ -668,3 +677,4 @@ suspend fun getSignerAddressFromSignature( + diff --git a/app/src/main/java/io/kthulu/sdk/global.kt b/app/src/main/java/io/kthulu/sdk/global.kt index beaf5d4..774c74c 100644 --- a/app/src/main/java/io/kthulu/sdk/global.kt +++ b/app/src/main/java/io/kthulu/sdk/global.kt @@ -35,11 +35,11 @@ import java.util.Base64 import javax.crypto.Cipher suspend fun kthuluSdkVersion(){ - println("SDK version:0.0.87, Connect OK") + println("SDK version:1.0.0, Connect OK") val resultArray = JSONArray() var resultData = JSONObject() val jsonData = JSONObject() - jsonData.put("version", "SDK version:0.0.87, Connect OK") + jsonData.put("version", "SDK version:1.0.0, Connect OK") resultArray.put(jsonData) resultData.put("result", "OK") resultData.put("value", resultArray) @@ -49,6 +49,7 @@ var rpcUrl =""; var bridgeConfigContractAddress = ""; var bridgeContractAddress = ""; var nftTransferContractAddress = ""; +var bridgeSetupContractAddress = ""; var uniswapV2RouterAddress = ""; var uniswapV2FactoryAddress = ""; var maxPriorityFeePerGas = ""; @@ -131,6 +132,17 @@ fun networkSettings(network: String) { "tbnb" -> "" else -> throw IllegalArgumentException("Invalid main network type") } + bridgeSetupContractAddress = when (network) { + "ethereum" -> "0x3cf93d43251324c527346abf3e0559f4c7a713d1" + "cypress" -> "0x41ec118425e4d13b509382e97cdc3f09dbba8fd9" + "polygon" -> "0x4f5d095ccda117e168ea58bcccffafb9c3617491" + "bnb" -> "" + "goerli" -> "" + "baobab" -> "" + "mumbai" -> "" + "tbnb" -> "" + else -> throw IllegalArgumentException("Invalid main network type") + } } // Create RSA key diff --git a/app/src/main/java/io/kthulu/sdk/nft.kt b/app/src/main/java/io/kthulu/sdk/nft.kt index 614a8e1..dbb7958 100644 --- a/app/src/main/java/io/kthulu/sdk/nft.kt +++ b/app/src/main/java/io/kthulu/sdk/nft.kt @@ -27,7 +27,9 @@ import org.web3j.protocol.http.HttpService import org.web3j.utils.Numeric import java.io.IOException import java.io.InputStreamReader +import java.math.BigDecimal import java.math.BigInteger +import java.math.RoundingMode import java.net.HttpURLConnection import java.net.URL import java.sql.ResultSet @@ -750,27 +752,27 @@ suspend fun getNFTsTransferHistory( var transferQuery = " SELECT" + - " transaction.network AS network," + - " transaction.`from` AS from_address," + - " transaction.`to` AS to_address," + - " transaction.collection_id AS collection_id," + - " transaction.block_number AS block_number," + - " transaction.`timestamp` AS timestamp," + - " transaction.transaction_hash AS transaction_hash," + - " transaction.log_id AS log_id," + - " transaction.token_id AS token_id," + - " transaction.amount AS amount," + - " transaction.currency AS currency," + - " transaction.currency_symbol AS currency_symbol," + - " transaction.decimals AS decimals," + - " transaction.price AS price," + - " transaction.market AS market," + - " transaction.sales_info AS sales_info," + - " transaction.transaction_type AS type" + - " FROM " + - " nft_transaction_table AS transaction" + - " WHERE" + - " transaction.network = '${network}'" + " transaction.network AS network," + + " transaction.`from` AS from_address," + + " transaction.`to` AS to_address," + + " transaction.collection_id AS collection_id," + + " transaction.block_number AS block_number," + + " transaction.`timestamp` AS timestamp," + + " transaction.transaction_hash AS transaction_hash," + + " transaction.log_id AS log_id," + + " transaction.token_id AS token_id," + + " transaction.amount AS amount," + + " transaction.currency AS currency," + + " transaction.currency_symbol AS currency_symbol," + + " transaction.decimals AS decimals," + + " transaction.price AS price," + + " transaction.market AS market," + + " transaction.sales_info AS sales_info," + + " transaction.transaction_type AS type" + + " FROM " + + " nft_transaction_table AS transaction" + + " WHERE" + + " transaction.network = '${network}'" if(token_id != null){ transferQuery += " AND transaction.token_id = '${token_id}' " } @@ -799,11 +801,11 @@ suspend fun getNFTsTransferHistory( var sumQuery = "SELECT" + - " count(*) AS sum" + - " FROM " + - " nft_transaction_table AS transaction" + - " WHERE " + - " transaction.network = '$network'" + " count(*) AS sum" + + " FROM " + + " nft_transaction_table AS transaction" + + " WHERE " + + " transaction.network = '$network'" if (token_id != null) { sumQuery += " AND transaction.token_id = '$token_id' " } @@ -1091,13 +1093,13 @@ suspend fun sendNFT721TransactionAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() val gasLimitEstimate = getEstimateGasAsync( - network, - "transferERC721", - collection_id, - from, - to, - null, - token_id + network, + "transferERC721", + collection_id, + from, + to, + null, + token_id ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -1213,7 +1215,7 @@ suspend fun sendNFT1155TransactionAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() - val gasLimitEstimate = getEstimateGasAsync( + val gasLimitEstimate =getEstimateGasAsync( network, "transferERC1155", collection_id, @@ -1457,17 +1459,17 @@ suspend fun sendNFT1155BatchTransactionAsync( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() val gasLimitEstimate = getEstimateGasAsync( - network, - "batchTransferERC1155", - collection_id, - from, - to, - null, - null, - null, - null, - token_id, - amount + network, + "batchTransferERC1155", + collection_id, + from, + to, + null, + null, + null, + null, + token_id, + amount ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -1576,18 +1578,18 @@ suspend fun deployErc721Async( 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 + network, + "deployERC721", + null, + from, + null, + null, + null, + null, + null, + null, + null, + name, symbol, token_base_uri, uri_type ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -1695,19 +1697,19 @@ suspend fun deployErc1155Async( 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 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") @@ -1925,15 +1927,15 @@ suspend fun mintErc1155Async( 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 + network, + "mintERC1155", + collection_id, + from, + to, + amount, + token_id, + null, null, null, null, null, null, null, null, + token_uri ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -2040,15 +2042,15 @@ suspend fun batchMintErc721Async( val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() val gasLimitEstimate = getEstimateGasAsync( - network, - "batchMintERC721", - collection_id, - from, - to, - null, - null, - null, null, null, null, null, null, null, - null, null, token_uri, start_id, end_id + network, + "batchMintERC721", + collection_id, + from, + to, + null, + null, + null, null, null, null, null, null, null, + null, null, token_uri, start_id, end_id ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -2156,15 +2158,15 @@ suspend fun batchMintErc1155Async( 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 + 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") @@ -2266,14 +2268,14 @@ suspend fun burnErc721Async( 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, + network, + "burnERC721", + collection_id, + owner, + null, + null, + token_id, + null, null, null, null, null, null, null, null, null, null, ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -2331,6 +2333,111 @@ suspend fun burnErc721Async( } } +suspend fun approveSetupNftAsync( + network: String, + fromAddress: String, + token_address: String +): 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(fromAddress) + val privateKey = runCatching { + getAddressInfo.getJSONArray("value") + .getJSONObject(0) + .getString("private") + }.getOrElse { + // handle error here + println("Error while fetching the private key: ${it.message}") + null + } + var transactionHash = ""; + val web3j = Web3j.build(HttpService(rpcUrl)) + val credentials = + Credentials.create(privateKey) + + var gasPrice = "" + try { + val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") + gasPrice = gasPriceEstimate.getJSONArray("value") + .getJSONObject(0) + .getString("gas") + + } catch (e: Exception){ + jsonData.put("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + return@withContext resultData + } + + val function = + Function("setApprovalForAll", listOf(Address(bridgeSetupContractAddress), Bool(true)), + emptyList()) + + val encodedFunction = FunctionEncoder.encode(function) + + val nonce = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING) + .sendAsync() + .get() + .transactionCount + + val chainId = web3j.ethChainId().sendAsync().get().chainId.toLong() + + val tx = + if (network == "bnb" || network == "bnbTest") { + RawTransaction.createTransaction( + nonce, + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger("100000"), // Add 20% to the gas limit + token_address, + encodedFunction + ) + } else { + RawTransaction.createTransaction( + chainId, + nonce, + BigInteger("100000"), // Add 20% to the gas limit + token_address, + BigInteger.ZERO, // value + encodedFunction, + BigInteger(maxPriorityFeePerGas), // maxPriorityFeePerGas + BigInteger(gasPrice) // Add 20% to the gas price + ) + } + val signedMessage = TransactionEncoder.signMessage(tx, credentials) + val signedTx = Numeric.toHexString(signedMessage) + + 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("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } + +} + suspend fun burnErc1155Async( network: String, owner: String, @@ -2377,14 +2484,14 @@ suspend fun burnErc1155Async( 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 + network, + "burnERC1155", + collection_id, + owner, + null, + amount, + token_id, + null, null, null, null, null, null, null, null, null, null ) val gasPriceEstimate = getEstimateGasAsync(network, "baseFee") @@ -2446,7 +2553,10 @@ suspend fun bridgeErc721Async( fromAddress: String, toNetwork: String, token_id: String, - token_address: String + token_address: String, + name: String, + symbol: String, + ownership: String ): JSONObject = withContext(Dispatchers.IO) { networkSettings(network) val jsonData = JSONObject() @@ -2479,8 +2589,9 @@ suspend fun bridgeErc721Async( val toNetworkHex = BigInteger(hex, 16) val function = Function( - "moveFromERC721", - listOf(Uint256(toNetworkHex), Address(token_address), Uint256(BigInteger(token_id))), + "setupFromERC721", + listOf(Uint256(toNetworkHex), Utf8String(name), + Utf8String(symbol), Address(ownership), Address(token_address), Uint256(BigInteger(token_id))), emptyList() ) @@ -2503,13 +2614,13 @@ suspend fun bridgeErc721Async( // Assuming each value is of length 64 characters (32 bytes, which is standard for Ethereum) // val networkHex = networkFeeResponse.result.substring(2, 66) // val tokenFeeHex = networkFeeResponse.result.substring(66, 130) - val nftFeeHex = networkFeeResponse.result.substring(130, 194) -// val regFeeHex = networkFeeResponse.result.substring(194, 258) +// val nftFeeHex = networkFeeResponse.result.substring(130, 194) + val regFeeHex = networkFeeResponse.result.substring(194, 258) // val network = String(BigInteger(networkHex, 16).toByteArray()) // val tokenFee = BigInteger(tokenFeeHex, 16) - val nftFee = BigInteger(nftFeeHex, 16) -// val regFee = BigInteger(regFeeHex, 16) +// val nftFee = BigInteger(nftFeeHex, 16) + val regFee = BigInteger(regFeeHex, 16) val encodedFunction = FunctionEncoder.encode(function) @@ -2530,17 +2641,151 @@ suspend fun bridgeErc721Async( RawTransaction.createTransaction( nonce, BigInteger(gasPrice), // Add 20% to the gas price - BigInteger.valueOf(200000), // Add 20% to the gas limit - nftTransferContractAddress, + BigInteger.valueOf(1000000), // Add 20% to the gas limit + bridgeSetupContractAddress, encodedFunction ) } else { RawTransaction.createTransaction( chainId, nonce, - BigInteger.valueOf(200000), // Add 20% to the gas limit - nftTransferContractAddress, - nftFee, // value + BigInteger.valueOf(1000000), // Add 20% to the gas limit + bridgeSetupContractAddress, + regFee, // value + encodedFunction, + BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas + BigInteger(gasPrice) // Add 20% to the gas price + ) + } + val signedMessage = TransactionEncoder.signMessage(tx, credentials) + val signedTx = Numeric.toHexString(signedMessage) + + val transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash + if (!transactionHash.isNullOrEmpty()) { + 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("error", e.message) + resultArray.put(jsonData) + resultData.put("result", "FAIL") + resultData.put("value", resultArray) + } +} + +suspend fun bridgeErc1155Async( + network: String, + fromAddress: String, + toNetwork: String, + token_id: String, + token_address: String, + name: String, + symbol: String, + ownership: String, + amount: String +): 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) + + val getAddressInfo = getAccountInfoAsync(fromAddress) + val privateKey = runCatching { + getAddressInfo.getJSONArray("value") + .getJSONObject(0) + .getString("private") + }.getOrElse { + // handle error here + println("Error while fetching the private key: ${it.message}") + null + } + + try { + val web3j = Web3j.build(HttpService(rpcUrl)) + val credentials = + Credentials.create(privateKey) + + val hex = textToHex(toNetwork) + + // Convert hex string to BigInteger + val toNetworkHex = BigInteger(hex, 16) + + val function = Function( + "setupFromERC1155", + listOf(Uint256(toNetworkHex), Utf8String(name), + Utf8String(symbol), Address(ownership), Address(token_address), Uint256(BigInteger(token_id)), Uint256(BigInteger(amount))), + emptyList() + ) + + val networkFeeIdxFunction = Function("getNetworkFeeIdxByName", listOf(Uint256(toNetworkHex)), emptyList()) + val encodedNetworkFeeIdxFunction = FunctionEncoder.encode(networkFeeIdxFunction) + val networkFeeIdxResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, bridgeConfigContractAddress, encodedNetworkFeeIdxFunction), + DefaultBlockParameterName.LATEST + ).send() + + val networkFeeIdx = BigInteger(networkFeeIdxResponse.result.replace("0x", ""), 16) + + val networkFeeFunction = Function("getNetworkFeeByIdx", listOf(Uint32(networkFeeIdx)), emptyList()) + val encodedNetworkFeeFunction = FunctionEncoder.encode(networkFeeFunction) + val networkFeeResponse = web3j.ethCall( + Transaction.createEthCallTransaction(null, bridgeConfigContractAddress, encodedNetworkFeeFunction), + DefaultBlockParameterName.LATEST + ).send() + + // Assuming each value is of length 64 characters (32 bytes, which is standard for Ethereum) +// val networkHex = networkFeeResponse.result.substring(2, 66) +// val tokenFeeHex = networkFeeResponse.result.substring(66, 130) +// val nftFeeHex = networkFeeResponse.result.substring(130, 194) + val regFeeHex = networkFeeResponse.result.substring(194, 258) + +// val network = String(BigInteger(networkHex, 16).toByteArray()) +// val tokenFee = BigInteger(tokenFeeHex, 16) +// val nftFee = BigInteger(nftFeeHex, 16) + val regFee = BigInteger(regFeeHex, 16) + + val encodedFunction = FunctionEncoder.encode(function) + + val nonce = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING) + .sendAsync() + .get() + .transactionCount + + 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, + BigInteger(gasPrice), // Add 20% to the gas price + BigInteger.valueOf(1000000), // Add 20% to the gas limit + bridgeSetupContractAddress, + encodedFunction + ) + } else { + RawTransaction.createTransaction( + chainId, + nonce, + BigInteger.valueOf(1000000), // Add 20% to the gas limit + bridgeSetupContractAddress, + regFee, // value encodedFunction, BigInteger(maxPriorityFeePerGas), // 35 Gwei maxPriorityFeePerGas BigInteger(gasPrice) // Add 20% to the gas price @@ -2572,14 +2817,17 @@ suspend fun bridgeErc721Async( suspend fun verifyNFT( network: String, - tokenId: String, - contractAddress: String, - apiKey: String + token_id: String, + collection_id: String, + api_key: String? = null ): JSONObject = withContext(Dispatchers.IO) { + val result = JSONObject() + var resultData = JSONObject() + resultData.put("result", "FAIL") + val dbConnector = DBConnector() dbConnector.connect() val connection = dbConnector.getConnection() - val result = JSONObject() result.put("ContractVerify", false) result.put("TokenURIAvailable", false) result.put("TokenURIResponseOnTime", false) @@ -2595,13 +2843,13 @@ suspend fun verifyNFT( var imageURL: String? = null var query = "SELECT nft_type, token_uri, token_info, image_url FROM " + - "nft_token_table " + - "WHERE " + - "network = '${network}' " + - "AND " + - "collection_id = '${contractAddress}' " + - "AND " + - "token_id = '${tokenId}' " + "nft_token_table " + + "WHERE " + + "network = '${network}' " + + "AND " + + "collection_id = '${collection_id}' " + + "AND " + + "token_id = '${token_id}' " try { if (connection != null) { val dbQueryExector = DBQueryExector(connection) @@ -2614,30 +2862,38 @@ suspend fun verifyNFT( tokenInfo = getTransaction1.getString("token_info") imageURL = getTransaction1.getString("image_url") } - } - catch (ex: SQLException) { + } catch (ex: SQLException) { ex.printStackTrace() } finally { getTransaction1.close() } } } + if(nftType.isNullOrEmpty()) { + resultData.put("error", "DB info NULL") + resultData.put("result", "FAIL") + return@withContext resultData + } val hostUrl: String when (network) { "ethereum" -> { - hostUrl = "https://api.etherscan.com/api?module=contract&action=getabi&address=$contractAddress&apikey=$apiKey" + hostUrl = "https://api.etherscan.com/api?module=contract&action=getabi&address=$collection_id&apikey=$api_key" } "cypress" -> { hostUrl = "" } "polygon" -> { - hostUrl = "https://api.polygonscan.com/api?module=contract&action=getabi&address=$contractAddress&apikey=$apiKey" + hostUrl = "https://api.polygonscan.com/api?module=contract&action=getabi&address=$collection_id&apikey=$api_key" } "bnb" -> { - hostUrl = "https://api.bscscan.com/api?module=contract&action=getabi&address=$contractAddress&apikey=$apiKey" + hostUrl = "https://api.bscscan.com/api?module=contract&action=getabi&address=$collection_id&apikey=$api_key" } else -> { - hostUrl = "" + result.keys().forEach { + result.remove(it) + } + result.put("error", "DB info NULL") + return@withContext resultData } } @@ -2770,7 +3026,7 @@ suspend fun verifyNFT( ) val encodedsupportsInterface = FunctionEncoder.encode(supportsInterface) val supportsInterfaceResponse = web3.ethCall( - Transaction.createEthCallTransaction(null, contractAddress, encodedsupportsInterface), + Transaction.createEthCallTransaction(null, collection_id, encodedsupportsInterface), DefaultBlockParameterName.LATEST ).send() val supportsInterfaceOutput = @@ -2875,11 +3131,12 @@ suspend fun verifyNFT( println("JSON parsing error: ${e.message}") } } - - result + resultData.put("result", "OK") + resultData.put("value", result) + resultData } catch (e: Exception) { println(e) - result + resultData } } @@ -2989,3 +3246,4 @@ suspend fun chkNFTHolder( result.put("error", e.message) } } + diff --git a/app/src/main/java/io/kthulu/sdk/transaction.kt b/app/src/main/java/io/kthulu/sdk/transaction.kt index 8d11d9f..4922a1e 100644 --- a/app/src/main/java/io/kthulu/sdk/transaction.kt +++ b/app/src/main/java/io/kthulu/sdk/transaction.kt @@ -656,16 +656,23 @@ suspend fun bridgeCoinAsync( val signedTx = Numeric.toHexString(signedMessage) val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - if (txHash != null) { - jsonData.put("result", "OK") - jsonData.put("transactionHash", txHash) + if(txHash != "") { + jsonData.put("transaction_hash", txHash) + 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", txHash) + 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) } } @@ -802,16 +809,23 @@ suspend fun bridgeTokenAsync( val signedTx = Numeric.toHexString(signedMessage) val txHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - if (txHash != null) { - jsonData.put("result", "OK") - jsonData.put("transactionHash", txHash) + if(txHash != "") { + jsonData.put("transaction_hash", txHash) + 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", txHash) + 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) } } @@ -1076,17 +1090,19 @@ suspend fun coinForTokenswapAsync( val signedTx = Numeric.toHexString(signedMessage) transactionHash = web3j.ethSendRawTransaction(signedTx).sendAsync().get().transactionHash - if(transactionHash != "") { + if(!transactionHash.isNullOrEmpty()) { jsonData.put("transaction_hash", transactionHash) resultArray.put(jsonData) resultData.put("result", "OK") resultData.put("value", resultArray) + return@withContext resultData } else { jsonData.put("error", "insufficient funds") jsonData.put("transaction_hash", transactionHash) resultArray.put(jsonData) resultData.put("result", "FAIL") resultData.put("value", resultArray) + return@withContext resultData } } else { @@ -1094,12 +1110,14 @@ suspend fun coinForTokenswapAsync( resultArray.put(jsonData) resultData.put("result", "FAIL") resultData.put("value", resultArray) + return@withContext resultData } } catch (e: Exception) { jsonData.put("error", e.message) resultArray.put(jsonData) resultData.put("result", "FAIL") resultData.put("value", resultArray) + return@withContext resultData } }