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

Retrieve Account Balance final #74

Merged
merged 6 commits into from
Sep 15, 2024
Merged

Conversation

sajalbnl
Copy link
Contributor

@sajalbnl sajalbnl commented Sep 7, 2024

retrieve account balance #35

// Convert hex string to BigInteger
val balanceBigInt = cleanedHex.toBigInteger(16)

// If StarkNet uses a base unit similar to Ethereum's wei, you might need to adjust the balance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI comments left in

import com.example.walletapp.data.model.StarknetResponse
import retrofit2.Call

class StarknetRepository() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this name to StarknetUtils

interface StarknetApiService {


@POST("rFAP8fkTAz9TmYw8_V5Fyzxi-WSoQdhk") // rpc end point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API key should not be in codebase. Load from local.properties or similar. Here is a guide: https://medium.com/make-android/how-to-safeguard-your-api-keys-in-android-projects-with-github-secrets-5679e0e89a77

you can ignore the part about adding it to github secrets

import retrofit2.Call

class StarknetRepository() {
fun getAccountBalance(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why arent you using the starknet SDK library? It will be a lot easier to work with.

Follow the example here: https://github.com/software-mansion/starknet-jvm/blob/main/androiddemo/src/main/java/com/example/androiddemo/MainActivity.kt#L148

I have also pasted the getBalance() function from the example here:

    private suspend fun getBalance(accountAddress: Felt): Uint256 {
        // Create a call to Starknet ERC-20 ETH contract
        val call = Call(
            contractAddress = erc20ContractAddress,
            entrypoint = "balanceOf", // entrypoint can be passed both as a string name and Felt value
            calldata = listOf(accountAddress), // calldata is List<Felt>, so we wrap accountAddress in listOf()
        )

        // Create a Request object which has to be executed in synchronous or asynchronous way
        val request = provider.callContract(call)

        // Execute a Request. This operation returns JVM CompletableFuture
        val future = request.sendAsync()

        // Await the completion of the future without blocking the main thread
        // this comes from kotlinx-coroutines-jdk8
        // The result of the future is a List<Felt> which represents the output values of the balanceOf function
        val response = future.await()

        // Output value's type is UInt256 and is represented by two Felt values
        return Uint256(
            low = response[0],
            high = response[1],
        )
    }

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitInstance {
Copy link
Collaborator

@trbutler4 trbutler4 Sep 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont need this, we just need a provider:

  // Create a provider for interacting with Starknet
    private val provider = JsonRpcProvider(
        url = BuildConfig.DEMO_RPC_URL,
    )

the above is from the starknet sdk jvm example at: https://github.com/software-mansion/starknet-jvm/blob/main/androiddemo/src/main/java/com/example/androiddemo/MainActivity.kt#L148

please follow their example closely

}


fun ConvertHexToBalance(hex: String): String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Balance conversion can be handled like this:

 // Output value's type is UInt256 and is represented by two Felt values
        return Uint256(
            low = response[0],
            high = response[1],
        )

which is from the starknet jvm sdk example. This is a better approach to handle Felt type conversion

import retrofit2.http.Body
import retrofit2.http.POST

interface StarknetApiService {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use the provider, and a utility class with a function to get the balance, we dont need this.

@trbutler4 trbutler4 merged commit 18d235a into keep-starknet-strange:main Sep 15, 2024
1 check failed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants