-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adding transfer funds * removing local.properties * adding local.properties to .gitignore * minor fix * rollback local.properties file * minor fix * adding dropdown for eth and strk * sending eth
- Loading branch information
Showing
8 changed files
with
380 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ lightclientservice/.idea | |
**/*.so | ||
|
||
**/.idea/* | ||
|
||
wallet_app/local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
wallet_app/app/src/main/java/com/example/walletapp/ui/account/WalletViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.example.walletapp.ui.account | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateListOf | ||
import com.example.walletapp.BuildConfig | ||
import com.example.walletapp.model.Token | ||
import com.example.walletapp.utils.StarknetClient | ||
import com.example.walletapp.utils.weiToEther | ||
import com.example.walletapp.utils.toDoubleWithTwoDecimal | ||
import com.swmansion.starknet.account.Account | ||
import com.swmansion.starknet.data.types.Felt | ||
import com.swmansion.starknet.data.types.Uint256 | ||
import com.swmansion.starknet.provider.exceptions.RpcRequestFailedException | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.withContext | ||
|
||
|
||
class WalletViewModel : ViewModel() { | ||
private val _balances = MutableStateFlow(emptyList<HashMap<String, Double>>()) | ||
val balances: StateFlow<List<HashMap<String, Double>>> get() = _balances | ||
val tokenIds = mutableStateListOf<String>() | ||
|
||
private val starknetClient = StarknetClient(BuildConfig.RPC_URL) | ||
|
||
private val _errorMessage = mutableStateOf("") | ||
val errorMessage: State<String> get() = _errorMessage | ||
|
||
fun fetchBalance(accountAddress: Felt, tokens: List<Token>, coinViewModel: CoinViewModel) { | ||
viewModelScope.launch { | ||
if (tokens.isNotEmpty()) { | ||
tokenIds.addAll(tokens.map { it.tokenId }) | ||
coinViewModel.fetchTokenImages(tokenIds) | ||
try { | ||
coinViewModel.getTokenPrices( | ||
ids = tokenIds.joinToString(",") { it }, | ||
vsCurrencies = "usd" | ||
) | ||
val balanceDeferred: List<Deferred<HashMap<String, Double>>> = tokens.map { token -> | ||
async(Dispatchers.IO) { | ||
try { | ||
val balanceInWei = | ||
starknetClient.getBalance(accountAddress, token.contactAddress) | ||
val balanceInEther = weiToEther(balanceInWei).toDoubleWithTwoDecimal() | ||
hashMapOf(token.name to balanceInEther) | ||
} catch (e: RpcRequestFailedException) { | ||
withContext(Dispatchers.Main) { | ||
_errorMessage.value = "${e.code}: ${e.message}" | ||
} | ||
hashMapOf(token.name to 0.0) | ||
} | ||
} | ||
} | ||
// Wait for all balance fetching to complete | ||
_balances.value = balanceDeferred.awaitAll() | ||
} catch (e: Exception) { | ||
withContext(Dispatchers.Main) { | ||
_errorMessage.value = e.message.toString() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun transferFunds(account: Account, toAddress: Felt, amount: Uint256) { | ||
viewModelScope.launch { | ||
try { | ||
//TODO Handle the transaction hash if transaction was successfully | ||
starknetClient.transferFunds(account, toAddress, amount) | ||
} catch (e: Exception) { | ||
println(e) | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.