Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default wallet implementation to env vars #80 #85

Merged
merged 9 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions wallet_app/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ android {
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"



buildConfigField("String", "DEMO_RPC_URL", "\"https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${properties.getProperty("KEY_NAME")}\"")
buildConfigField("String", "DEMO_ACCOUNT_ADDRESS", "\"0x02dc260794e4c2eeae87b1403a88385a72c18a5844d220b88117b2965a8cf3a5\"")
buildConfigField("String", "DEMO_PRIVATE_KEY", "\"rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk\"")
buildConfigField("String", "DEMO_RECIPIENT_ACCOUNT_ADDRESS", "\"0xc1c7db92d22ef773de96f8bde8e56c85\"")
buildConfigField("String", "RPC_URL", "\"${properties.getProperty("RPC_URL")}\"")
buildConfigField("String", "ACCOUNT_ADDRESS", "\"${properties.getProperty("ACCOUNT_ADDRESS")}\"")
buildConfigField("String", "PRIVATE_KEY", "\"${properties.getProperty("KEY_NAME")}\"")
}

buildTypes {
Expand Down Expand Up @@ -98,6 +96,7 @@ dependencies {
implementation(libs.androidx.hilt.navigation.fragment)
implementation (libs.androidx.hilt.navigation.compose.v100alpha03)


implementation(libs.androidx.ui.tooling.preview)
debugImplementation(libs.androidx.ui.tooling)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun StarknetLogo (modifier: Modifier = Modifier) {
fun CreateAccount( modifier: Modifier) {
val context = (LocalContext.current as Activity)
val scope = rememberCoroutineScope()
val starknetClient = StarknetClient(BuildConfig.DEMO_RPC_URL)
val starknetClient = StarknetClient(BuildConfig.RPC_URL)

Column(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.example.walletapp.BuildConfig
import com.swmansion.starknet.account.StandardAccount
import com.swmansion.starknet.data.types.Call
import com.swmansion.starknet.data.types.Felt
Expand All @@ -12,12 +13,15 @@ const val ETH_ERC20_ADDRESS = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741
class StarknetClient(private val rpcUrl: String) {

private val provider = JsonRpcProvider(rpcUrl)
private val privateKey=BuildConfig.PRIVATE_KEY
private val accountAddress=BuildConfig.ACCOUNT_ADDRESS

suspend fun deployAccount() {


// Predefined values for account creation
val privateKey = Felt.fromHex("0x2bbf4f9fd0bbb2e60b0316c1fe0b76cf7a4d0198bd493ced9b8df2a3a24d68a") // TODO(#80): Load from local.properties
val accountAddress = Felt.fromHex("0xb3ff441a68610b30fd5e2abbf3a1548eb6ba6f3559f2862bf2dc757e5828ca") // TODO(#80): Load from local.properties
val privateKey = Felt.fromHex(privateKey)
val accountAddress = Felt.fromHex(accountAddress)

val signer = StarkCurveSigner(privateKey)
val chainId = provider.getChainId().sendAsync().await()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.walletapp

import StarknetClient
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
Expand Down Expand Up @@ -54,6 +56,12 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import com.swmansion.starknet.data.types.Felt
import com.swmansion.starknet.provider.exceptions.RpcRequestFailedException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.math.BigDecimal
import java.text.DecimalFormat


class WalletActivity : ComponentActivity() {
Expand All @@ -80,6 +88,24 @@ class WalletActivity : ComponentActivity() {
val networkList = listOf("Starknet Mainnet", "Test Networks")
var selectedNetworkIndex by remember { mutableStateOf(0) }
val context = (LocalContext.current as Activity)
val address=BuildConfig.ACCOUNT_ADDRESS
val accountAddress = Felt.fromHex(address)
val starknetClient = StarknetClient(BuildConfig.RPC_URL)
var balance by remember { mutableStateOf("") }

LaunchedEffect (Unit){
try {
// Get the balance of the account
val getBalance = starknetClient.getEthBalance(accountAddress)
withContext(Dispatchers.Main) {
balance = starknetClient.weiToEther(getBalance).toDoubleWithTwoDecimal()
}
} catch (e: RpcRequestFailedException) {
withContext(Dispatchers.Main) { Toast.makeText(context, "${e.code}: ${e.message}", Toast.LENGTH_LONG).show() }
} catch (e: Exception) {
withContext(Dispatchers.Main) { Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() }
}
}

Column(
modifier = Modifier
Expand All @@ -104,37 +130,37 @@ class WalletActivity : ComponentActivity() {


Text(
text = "$11,625.48", // TODO(#82): load actual balance
fontFamily = FontFamily(Font(R.font.inter_regular)),
text ="$11,625.48",
fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
fontSize = 28.sp,
fontSize = 24.sp,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(top = 70.dp)
)
Text(
text = "0xfoo...123", // TODO(#82): load actual address
text = address.take(10) + ".....",
fontFamily = FontFamily(Font(R.font.inter_regular)),
color = Color.White,
fontSize = 16.sp,
fontSize = 14.sp,
modifier = Modifier.align(Alignment.CenterHorizontally)
)

Spacer(modifier = Modifier.height(32.dp))

// TODO(#82): load actual balance

WalletCard(
icon = painterResource(id = R.drawable.ic_ethereum),
amount = "$11,625.7",
exchange = 4.44,
exchange = balance,
type = "ETH"
)

// TOOD(#82): load actual balance
WalletCard(
icon = painterResource(id = R.drawable.token2),
amount = "$1.78",
exchange = 4.44,
exchange ="4.44",
type = "STRK"
)

Expand Down Expand Up @@ -194,9 +220,14 @@ class WalletActivity : ComponentActivity() {
Spacer(modifier = Modifier.height(15.dp))
}
}
// Function to format BigDecimal to Double with 2 decimal places
fun BigDecimal.toDoubleWithTwoDecimal(): String {
val decimalFormat = DecimalFormat("#.00")
return decimalFormat.format(this.toDouble())
}

@Composable
fun WalletCard(icon: Painter, amount: String, exchange: Double, type: String) {
fun WalletCard(icon: Painter, amount: String, exchange: String, type: String) {
Card(
backgroundColor = Color(0xFF1E1E96),
modifier = Modifier
Expand All @@ -223,16 +254,17 @@ class WalletActivity : ComponentActivity() {
)
Row {
Text(
text = exchange.toString(),
fontFamily = FontFamily(Font(R.font.inter_regular)),
text = exchange,
fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
fontSize = 10.sp
fontSize = 14.sp
)
Spacer(modifier=Modifier.width(2.dp))
Text(
text = type,
fontFamily = FontFamily(Font(R.font.publicsans_bold)),
color = Color.White,
fontSize = 10.sp
fontSize = 12.sp
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class AccountBalanceActivity : ComponentActivity() {

val scope = CoroutineScope(Dispatchers.IO)

val starknetClient = StarknetClient(BuildConfig.DEMO_RPC_URL)
val starknetClient = StarknetClient(BuildConfig.RPC_URL)


Column(modifier = Modifier
Expand Down
Loading