From 4fb0109f1af013d678d9acbde99281b511c2cc93 Mon Sep 17 00:00:00 2001 From: SionNoh Date: Wed, 23 Aug 2023 17:17:05 +0900 Subject: [PATCH] import list update --- .../com/example/android_sdk/MainActivity.kt | 1 - .../java/com/example/android_sdk/account.kt | 3 +- .../java/com/example/android_sdk/dbserver.kt | 1 - .../java/com/example/android_sdk/global.kt | 46 +++++-- .../main/java/com/example/android_sdk/nft.kt | 9 -- .../com/example/android_sdk/transaction.kt | 128 +++++++++++++++--- 6 files changed, 150 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/example/android_sdk/MainActivity.kt b/app/src/main/java/com/example/android_sdk/MainActivity.kt index 3a6fed5..a191471 100644 --- a/app/src/main/java/com/example/android_sdk/MainActivity.kt +++ b/app/src/main/java/com/example/android_sdk/MainActivity.kt @@ -5,7 +5,6 @@ import android.content.Context import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.coroutines.* -import signMessage // Application에서 Context를 가져올 수 있도록 구현 class MyContext : Application() { 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 f60409c..678ff78 100644 --- a/app/src/main/java/com/example/android_sdk/account.kt +++ b/app/src/main/java/com/example/android_sdk/account.kt @@ -1,4 +1,5 @@ -import com.example.android_sdk.* +package com.example.android_sdk + import kotlinx.coroutines.* import org.apache.commons.lang3.RandomUtils import org.json.JSONArray diff --git a/app/src/main/java/com/example/android_sdk/dbserver.kt b/app/src/main/java/com/example/android_sdk/dbserver.kt index dbef11d..8ebf882 100644 --- a/app/src/main/java/com/example/android_sdk/dbserver.kt +++ b/app/src/main/java/com/example/android_sdk/dbserver.kt @@ -1,7 +1,6 @@ package com.example.android_sdk import java.sql.* -import java.util.* object ConfigHolder { var databaseUrl: String? = null 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 ccd8d69..d5392c4 100644 --- a/app/src/main/java/com/example/android_sdk/global.kt +++ b/app/src/main/java/com/example/android_sdk/global.kt @@ -1,9 +1,7 @@ package com.example.android_sdk import android.content.Context -import getTokenInfoAsync import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.withContext import org.web3j.abi.FunctionEncoder import org.web3j.abi.FunctionReturnDecoder @@ -12,7 +10,6 @@ import org.web3j.abi.datatypes.Address import org.web3j.abi.datatypes.DynamicArray import org.web3j.abi.datatypes.DynamicBytes import org.web3j.abi.datatypes.Function -import org.web3j.abi.datatypes.Type import org.web3j.abi.datatypes.Utf8String import org.web3j.abi.datatypes.generated.Uint256 import org.web3j.abi.datatypes.generated.Uint8 @@ -35,7 +32,7 @@ import java.util.Base64 import javax.crypto.Cipher var rpcUrl =""; -var erc20DeployContractAddress = ""; +var erc20BridgeContractAddress = ""; var erc721DeployContractAddress = ""; var erc1155DeployContractAddress = ""; @@ -51,10 +48,10 @@ fun networkSettings(network: String) { "tbnb" -> "https://data-seed-prebsc-1-s1.binance.org:8545" else -> throw IllegalArgumentException("Invalid main network type") } - erc20DeployContractAddress = when (network) { + erc20BridgeContractAddress = when (network) { "ethereum" -> "" "cypress" -> "" - "polygon" -> "0x96856126a6bb4870cDD3e179004CD18cEf569044" + "polygon" -> "0x7362fa30ada8ccf2130017f2a8f0b6be78aa38de" "bnb" -> "" "goerli" -> "0xc11735Ce3c155E755bC9839A5B5d06dEa0482306" "baobab" -> "0x808ee7147d91eae0f658164248402ac380eb5f17" @@ -76,7 +73,7 @@ fun networkSettings(network: String) { erc1155DeployContractAddress = when (network) { "ethereum" -> "" "cypress" -> "" - "polygon" -> "0x7E055Cb85FBE64da619865Df8a392d12f009aD81" + "polygon" -> "0xf643a4fb01cbbfb561cc906c1f37d5718ef3bba3" "bnb" -> "" "goerli" -> "0xFEA394a312369b7772513cF856ce4424C1756F2C" "baobab" -> "0x96856126a6bb4870cdd3e179004cd18cef569044" @@ -250,8 +247,8 @@ suspend fun getEstimateGasAsync( "deployERC20" -> if (name != null && symbol != null && fromAddress != null && tokenAmount != null) { val function = Function( - "deployedERC20", - listOf(Utf8String(name), Utf8String(symbol), Uint256(BigInteger(tokenAmount)), Address(fromAddress)), + "deployWrapped20", + listOf(Utf8String(name), Utf8String(symbol), Uint8(BigInteger("18")), Uint256(BigInteger(tokenAmount))), emptyList() ) val encodedFunction = FunctionEncoder.encode(function) @@ -262,7 +259,31 @@ suspend fun getEstimateGasAsync( BigInteger.ONE, gasPrice, BigInteger.ZERO, // temporary gasLimit - erc20DeployContractAddress, + erc20BridgeContractAddress, + 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 + erc20BridgeContractAddress, encodedFunction // data ) ).send().amountUsed @@ -591,3 +612,8 @@ suspend fun getEstimateGasAsync( } BigDecimal(result).multiply(BigDecimal(1.2)).setScale(0, RoundingMode.DOWN).toBigInteger() } + +fun textToHex(text: String): String { + if (text.isEmpty()) return "0x00" + return "0x" + text.map { it.toInt().toString(16).padStart(2, '0') }.joinToString("") +} 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 841b9e6..021b84b 100644 --- a/app/src/main/java/com/example/android_sdk/nft.kt +++ b/app/src/main/java/com/example/android_sdk/nft.kt @@ -1,15 +1,6 @@ package com.example.android_sdk -//import com.google.gson.Gson -//import com.google.gson.JsonArray -//import com.google.gson.JsonObject -//import com.google.gson.JsonParser -//import com.google.gson.reflect.TypeToken -//import okhttp3.OkHttpClient -//import okhttp3.Request -//import okhttp3.RequestBody import android.annotation.SuppressLint -import getAccountInfoAsync import kotlinx.coroutines.* import org.json.JSONArray import org.json.JSONObject 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 dc248d4..d9b0ad9 100644 --- a/app/src/main/java/com/example/android_sdk/transaction.kt +++ b/app/src/main/java/com/example/android_sdk/transaction.kt @@ -1,9 +1,6 @@ package com.example.android_sdk -import getAccountInfoAsync -import getTokenInfoAsync import kotlinx.coroutines.* -import kotlinx.serialization.json.Json import org.json.JSONObject import org.web3j.abi.FunctionEncoder import org.web3j.abi.FunctionReturnDecoder @@ -20,7 +17,6 @@ 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.tx.Transfer import org.web3j.utils.Convert import org.web3j.utils.Numeric import java.math.BigDecimal @@ -75,12 +71,26 @@ suspend fun transaction() = runBlocking { // deployErc20Async( // "polygon", // fromAddress, -// "AbcSeonghunToken", -// "AST", -// "5000000000" +// "AbcUseToken", +// "AUT", +// "50000" // ) // }.await() // println("Transaction hash: ${deployErc20}") +// /** +// * Transaction hash: 0x.. +// */ + + val bridgeToken = + async { + bridgeTokenAsync( + "polygon", + "ETHEREUM", + "0x0000000000000000000000000000000000000001", + "0.00000001" + ) + }.await() + println("Transaction hash: ${bridgeToken}") /** * Transaction hash: 0x.. */ @@ -154,7 +164,7 @@ suspend fun sendTransactionAsync( toAddress, // to weiAmount, // value "0x", // data - BigInteger("35000000000"), // maxPriorityFeePerGas + BigInteger("35000000000"), // 35 Gwei maxPriorityFeePerGas getEstimateGasAsync(network, "baseFee") // maxFeePerGas Add 20% to the gas price ) } @@ -262,7 +272,7 @@ suspend fun sendTokenTransactionAsync( token_address, // to BigInteger.ZERO, // value encodedFunction, // data - BigInteger("35000000000"), // 30 Gwei maxPriorityFeePerGas + BigInteger("35000000000"), // 35 Gwei maxPriorityFeePerGas getEstimateGasAsync(network, "baseFee") // maxFeePerGas Add 20% to the gas price ) } @@ -288,7 +298,6 @@ suspend fun sendTokenTransactionAsync( } } - suspend fun deployErc20Async( network: String, ownerAddress: String, @@ -309,15 +318,20 @@ suspend fun deployErc20Async( null } + val decimals = "18" val jsonData = JSONObject() + val decimalMultiplier = BigDecimal.TEN.pow(decimals.toInt()) + val tokenAmount = BigDecimal(totalSupply).multiply(decimalMultiplier).toString() try { val web3j = Web3j.build(HttpService(rpcUrl)) val credentials = Credentials.create(privateKey) + + val function = Function( - "deployedERC20", - listOf(Utf8String(name), Utf8String(symbol), Uint256(BigInteger(totalSupply)), Address(ownerAddress)), + "deployWrapped20", + listOf(Utf8String(name), Utf8String(symbol), Uint8(BigInteger(decimals)), Uint256(BigInteger(tokenAmount))), emptyList() ) @@ -348,7 +362,7 @@ suspend fun deployErc20Async( null, name, symbol ), // Add 20% to the gas limit - erc20DeployContractAddress, + erc20BridgeContractAddress, encodedFunction ) } else { @@ -370,10 +384,10 @@ suspend fun deployErc20Async( null, name, symbol ), // Add 20% to the gas limit - erc20DeployContractAddress, + erc20BridgeContractAddress, BigInteger.ZERO, encodedFunction, - BigInteger("35000000000"), // 33 Gwei maxPriorityFeePerGas + BigInteger("35000000000"), // 35 Gwei maxPriorityFeePerGas getEstimateGasAsync(network, "baseFee") // Add 20% to the gas price ) } @@ -392,4 +406,86 @@ suspend fun deployErc20Async( jsonData.put("result", "FAIL") jsonData.put("error", e.message) } -} \ No newline at end of file +} + +suspend fun bridgeTokenAsync( + network: String, + fromAddress: String, + toNetwork: String, + amount: String, +): JSONObject = withContext(Dispatchers.IO){ + networkSettings(network) + val jsonData = JSONObject() + networkSettings(network) + 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 bigIntValue = BigInteger(hex, 16) + + val function = Function( + "moveFromETHER", + listOf(Uint256(bigIntValue)), + 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, + getEstimateGasAsync(network, "baseFee"), // Add 20% to the gas price + BigInteger.valueOf(200000), // Add 20% to the gas limit + erc20BridgeContractAddress, + encodedFunction + ) + } else { + RawTransaction.createTransaction( + chainId, + nonce, + BigInteger.valueOf(200000), // Add 20% to the gas limit + erc20BridgeContractAddress, + BigInteger("45000000000000000000"), + encodedFunction, + BigInteger("35000000000"), // 35 Gwei maxPriorityFeePerGas + getEstimateGasAsync(network, "baseFee") // 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) + } else { + jsonData.put("result", "FAIL") + jsonData.put("error", "insufficient funds") + } + } catch (e: Exception) { + jsonData.put("result", "FAIL") + jsonData.put("error", e.message) + } +}