Skip to content

Commit

Permalink
import list update
Browse files Browse the repository at this point in the history
  • Loading branch information
SionNoh committed Aug 23, 2023
1 parent aeecb94 commit 4fb0109
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 38 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/com/example/android_sdk/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/example/android_sdk/account.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/example/android_sdk/dbserver.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.android_sdk

import java.sql.*
import java.util.*

object ConfigHolder {
var databaseUrl: String? = null
Expand Down
46 changes: 36 additions & 10 deletions app/src/main/java/com/example/android_sdk/global.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -35,7 +32,7 @@ import java.util.Base64
import javax.crypto.Cipher

var rpcUrl ="";
var erc20DeployContractAddress = "";
var erc20BridgeContractAddress = "";
var erc721DeployContractAddress = "";
var erc1155DeployContractAddress = "";

Expand All @@ -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"
Expand All @@ -76,7 +73,7 @@ fun networkSettings(network: String) {
erc1155DeployContractAddress = when (network) {
"ethereum" -> ""
"cypress" -> ""
"polygon" -> "0x7E055Cb85FBE64da619865Df8a392d12f009aD81"
"polygon" -> "0xf643a4fb01cbbfb561cc906c1f37d5718ef3bba3"
"bnb" -> ""
"goerli" -> "0xFEA394a312369b7772513cF856ce4424C1756F2C"
"baobab" -> "0x96856126a6bb4870cdd3e179004cd18cef569044"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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("")
}
9 changes: 0 additions & 9 deletions app/src/main/java/com/example/android_sdk/nft.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
128 changes: 112 additions & 16 deletions app/src/main/java/com/example/android_sdk/transaction.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -75,12 +71,26 @@ suspend fun transaction() = runBlocking<Unit> {
// 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..
*/
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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
)
}
Expand All @@ -288,7 +298,6 @@ suspend fun sendTokenTransactionAsync(
}
}


suspend fun deployErc20Async(
network: String,
ownerAddress: String,
Expand All @@ -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()
)

Expand Down Expand Up @@ -348,7 +362,7 @@ suspend fun deployErc20Async(
null,
name, symbol
), // Add 20% to the gas limit
erc20DeployContractAddress,
erc20BridgeContractAddress,
encodedFunction
)
} else {
Expand All @@ -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
)
}
Expand All @@ -392,4 +406,86 @@ suspend fun deployErc20Async(
jsonData.put("result", "FAIL")
jsonData.put("error", e.message)
}
}
}

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)
}
}

0 comments on commit 4fb0109

Please sign in to comment.