Skip to content

Commit

Permalink
use permits to get snip20 balance
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Oct 17, 2023
1 parent 30c01a3 commit fd2e933
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ import com.ionspin.kotlin.bignum.integer.BigInteger
import io.eqoty.cosmwasm.std.types.Coin
import io.eqoty.cosmwasm.std.types.ContractInfo
import io.eqoty.secret.std.contract.msg.Snip20Msgs
import io.eqoty.secret.std.contract.msg.SnipMsgs
import io.eqoty.secret.std.types.Permission
import io.eqoty.secret.std.types.Permit
import io.eqoty.secretk.client.Json
import io.eqoty.secretk.client.SigningCosmWasmClient
import io.eqoty.secretk.types.MsgExecuteContract
import io.eqoty.secretk.types.TxOptions
import io.eqoty.secretk.extensions.accesscontrol.PermitFactory
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.serialization.encodeToString
import kotlin.random.Random

object BalanceUtils {

private val snip20ToAddressViewingKey =
mutableMapOf<ContractInfo, MutableMap<String, SnipMsgs.ExecuteAnswer.ViewingKey>>()
private val snip20ToAddressPermit = mutableMapOf<ContractInfo, MutableMap<String, Permit>>()

private val httpClient = HttpClient {
expectSuccess = true
Expand Down Expand Up @@ -73,21 +70,27 @@ object BalanceUtils {
}

suspend fun getSnip20Balance(
client: SigningCosmWasmClient,
senderAddress: String,
contract: ContractInfo
client: SigningCosmWasmClient, senderAddress: String, contract: ContractInfo
): BigInteger? {
val viewingKey =
snip20ToAddressViewingKey[contract]?.get(senderAddress)
?: createViewingKey(client, senderAddress, contract).apply {
snip20ToAddressViewingKey[contract]?.set(senderAddress, this)
}
val query =
Json.encodeToString(Snip20Msgs.Query(balance = Snip20Msgs.Query.Balance(senderAddress, viewingKey.key)))
val permit = snip20ToAddressPermit.getOrPut(contract) { mutableMapOf() }.getOrPut(senderAddress) {
PermitFactory.newPermit(
client.wallet!!,
senderAddress,
client.chainId!!,
"balanceUtilsPermit",
listOf(contract.address),
listOf(Permission.Balance)
)
}
val query = Json.encodeToString(
Snip20Msgs.Query(
withPermit = Snip20Msgs.Query.WithPermit(
permit = permit, query = Snip20Msgs.QueryWithPermit(balance = Snip20Msgs.QueryWithPermit.Balance())
)
)
)
val response = client.queryContractSmart(
contract.address,
query,
contract.codeHash
contract.address, query, contract.codeHash
)
return Json.decodeFromString<Snip20Msgs.QueryAnswer>(response).balance!!.amount
}
Expand Down Expand Up @@ -116,31 +119,4 @@ object BalanceUtils {
}
return response.bodyAsText()
}

private suspend fun createViewingKey(
client: SigningCosmWasmClient,
senderAddress: String,
contract: ContractInfo
): SnipMsgs.ExecuteAnswer.ViewingKey {
val entropy = Random.nextBytes(40).encodeBase64()
val msg = Json.encodeToString(SnipMsgs.Execute(createViewingKey = SnipMsgs.Execute.CreateViewingKey(entropy)))
val msgs = listOf(
MsgExecuteContract(
sender = senderAddress,
contractAddress = contract.address,
codeHash = contract.codeHash,
msg = msg,
)
)
val simulate = client.simulate(msgs)
val gasLimit = (simulate.gasUsed.toDouble() * 1.1).toInt()

val response = client.execute(
msgs,
txOptions = TxOptions(gasLimit = gasLimit)
)
return Json.decodeFromString<SnipMsgs.ExecuteAnswer>(response.data[0]).viewingKey!!
}


}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx4g

# Publishing : Required
GROUP=io.eqoty.secretk
VERSION_NAME=6.0.2
VERSION_NAME=6.0.3

# Publishing : Optional
POM_NAME=secretk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.eqoty.secret.std.contract.msg

import com.ionspin.kotlin.bignum.integer.BigInteger
import io.eqoty.secret.std.contract.msg.SnipMsgs.ExecuteAnswer.ResponseStatus
import io.eqoty.secret.std.types.Permit
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -27,14 +28,31 @@ object Snip20Msgs {
@Serializable
data class Query(
val balance: Balance? = null,
@SerialName("with_permit")
val withPermit: WithPermit? = null,
) {
@Serializable
data class Balance(
val address: String,
val key: String,
)

@Serializable
data class WithPermit(
val permit: Permit,
val query: QueryWithPermit,
)
}

@Serializable
data class QueryWithPermit(
val balance: Balance? = null,
) {
@Serializable
class Balance
}


@Serializable
data class QueryAnswer(
val balance: Balance? = null,
Expand All @@ -60,8 +78,7 @@ object Snip20Msgs {
@Serializable
data class Send(
val recipient: String,
@SerialName("recipient_code_hash")
val recipientCodeHash: String? = null,
@SerialName("recipient_code_hash") val recipientCodeHash: String? = null,
@Contextual val amount: BigInteger,
val msg: String? = null,
val memo: String? = null,
Expand Down Expand Up @@ -105,16 +122,11 @@ object Snip20Msgs {

@Serializable
data class InitConfig(
@SerialName("public_total_supply")
val publicTotalSupply: Boolean? = null,
@SerialName("enable_deposit")
val enableDeposit: Boolean? = null,
@SerialName("enable_redeem")
val enableRedeem: Boolean? = null,
@SerialName("enable_mint")
val enableMint: Boolean? = null,
@SerialName("enable_burn")
val enableBurn: Boolean? = null,
@SerialName("public_total_supply") val publicTotalSupply: Boolean? = null,
@SerialName("enable_deposit") val enableDeposit: Boolean? = null,
@SerialName("enable_redeem") val enableRedeem: Boolean? = null,
@SerialName("enable_mint") val enableMint: Boolean? = null,
@SerialName("enable_burn") val enableBurn: Boolean? = null,
)

}

0 comments on commit fd2e933

Please sign in to comment.