0

My contract is already deployed on BSC testnet.

val web3j = Web3j.build(HttpService("https://data-seed-prebsc-1-s1.binance.org:8545/"))
val keyPair = ECKeyPair.create(BigInteger("1234"))
val credential = Credentials.create(keyPair)
val contractGasProvider: ContractGasProvider = DefaultGasProvider()

val greeter = Greeter.load(BuildConfig.CONTRACT_ADDRESS_10COLOR, web3j, credential, contractGasProvider)
val greeterErc20 = GreeterErc20.load(BuildConfig.CONTRACT_ADDRESS_ERC20, web3j, credential, contractGasProvider)

    // setGasProvider is added because of getting below error
    // java.lang.RuntimeException: Error processing transaction request: transaction underpriced
    greeter.setGasProvider(object : DefaultGasProvider() {
        override fun getGasPrice(contractFunc: String): BigInteger {
            return BigInteger.valueOf(10000000000)
        }

        override fun getGasLimit(contractFunc: String): BigInteger {
            return BigInteger.valueOf(4300000)
        }
    })

    greeterErc20.setGasProvider(object : DefaultGasProvider() {
        override fun getGasPrice(contractFunc: String): BigInteger {
            return BigInteger.valueOf(10000000000)
        }

        override fun getGasLimit(contractFunc: String): BigInteger {
            return BigInteger.valueOf(4300000)
        }
    })

    val result = web3j.ethGetBalance(Pref.user!!.mkey, DefaultBlockParameterName.LATEST).sendAsync().get();
    Log.e("Balance", result.balance.toString())
    // result.balance.toString() = 1400000000000000000

    val balance = Convert.fromWei(result.balance.toString(), Convert.Unit.ETHER)
    Log.e("Balance", balance.toString())
    // balance Converted = 1.4

    val block: EthBlock.Block = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().block
    val gasLimit = block.gasLimit
    Log.e("gasLimit", gasLimit.toString())
    // gasLimit = 50000000

    // After this I am calling ownerOf method and its working as expected
    try {
        val greeting: Future<String> = greeter.ownerOf(BigInteger.valueOf(nftUrl)).sendAsync()
        val ownerIdStr: String? = greeting.get()
        Log.e("ownerId", "" + ownerIdStr)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    //Then I am calling allowance and its also working as expected
    try {
        val greeting: Future<BigInteger> = greeterErc20?.allowance(Pref.user?.mkey, BuildConfig.CONTRACT_ADDRESS_10COLOR)!!.sendAsync()
        val allowance: BigInteger? = greeting.get()
        Log.e("allowance", "" + allowance)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    // allowance = 0

    // If allowance == 0 then I am trying to call approve method
    try {
        val approveAmount = BigInteger.valueOf(400000000000000000)
        val greeting: Future<TransactionReceipt> = greeterErc20?.approve(BuildConfig.CONTRACT_ADDRESS_10COLOR, approveAmount)!!.sendAsync()
        val transactionReceipt: TransactionReceipt? = greeting.get()
        Log.e("approve transactionReceipt", "" + transactionReceipt?.transactionHash)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    // Here I am getting below error
    // Error processing transaction request : insufficient funds for gas * price + value
Pavya
  • 6,015
  • 4
  • 29
  • 42

0 Answers0