Skip to content

Commit

Permalink
v4.0.0
Browse files Browse the repository at this point in the history
fix deploy utils. make simulate optional if gaslimit specified.
revert MsgInstantiateContract.codeId to being mutable
  • Loading branch information
luca992 committed Jul 19, 2023
1 parent 6c723df commit c0321fa
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 96 deletions.
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,7 @@ A Kotlin multiplatform Encryption and REST client for Secret Network utilizing g

### Gradle

```gradle
repositories {
mavenCentral()
// And the following repository to workaround this issue:
// https://youtrack.jetbrains.com/issue/KT-52240/Dependencies-of-a-dependency-are-not-resolved
maven {
name = "komputing/KHash GitHub Packages"
url = uri("https://maven.pkg.github.com/komputing/KHash")
credentials {
username = "token"
// see: https://github.community/t/download-from-github-package-registry-without-authentication/14407/44
password =
"\u0039\u0032\u0037\u0034\u0031\u0064\u0038\u0033\u0064\u0036\u0039\u0061\u0063\u0061\u0066\u0031\u0062\u0034\u0061\u0030\u0034\u0035\u0033\u0061\u0063\u0032\u0036\u0038\u0036\u0062\u0036\u0032\u0035\u0065\u0034\u0061\u0065\u0034\u0032\u0062"
}
}
}
```kotlin
dependencies {
implementation("io.eqoty.secretk:client:$latest")
}
Expand Down Expand Up @@ -75,10 +59,8 @@ you need to declare your dependency in your `Package.swift`:
val grpcGatewayEndpoint = "http://secretnetworkendpoint.com:1337"
val mnemonic = ""
val wallet = DirectSigningWallet(mnemonic)
val accAddress = wallet.getAccounts()[0].address
val client = SigningCosmWasmClient.init(
grpcGatewayEndpoint,
accAddress,
wallet
)
```
Expand Down Expand Up @@ -167,11 +149,9 @@ println("recipientBalance: $recipientBalance")

```swift
let wallet = DirectSigningWallet(mnemonic: mnemonic)
let accAddress = (try! await wallet.getAccounts())[0].address
let SigningCosmWasmClientCompanion = SigningCosmWasmClient.Companion()
let client = try! await SigningCosmWasmClient.Companion.doInit(SigningCosmWasmClientCompanion)(
apiUrl: grpcGatewayEndpoint,
senderAddress: accAddress,
wallet: wallet,
seed: nil,
broadcastMode: BroadcastMode.block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.serialization.json.jsonObject
class MsgInstantiateContract(
override val sender: String,
/** The id of the contract's WASM code */
val codeId: Int?,
var codeId: Int?,
/** A unique label across all contracts */
val label: String,
/** The input message to the contract's constructor */
Expand Down Expand Up @@ -65,7 +65,7 @@ class MsgInstantiateContract(

val msgContent = MsgInstantiateContractProto(
sender = addressToBytes(sender),
codeId = codeId,
codeId = codeId!!,
label = label,
initMsg = initMsgEncrypted!!.toByteArray(),
initFunds = initFunds.map { it.toProto() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlin.random.Random

Expand All @@ -33,7 +32,7 @@ object BalanceUtils {
nodeInfo: NodeInfo,
client: SigningCosmWasmClient,
targetBalance: Int,
address: String = client.senderAddress,
address: String,
) {
var balance = try {
getScrtBalance(client, address)
Expand All @@ -44,7 +43,7 @@ object BalanceUtils {
}
while (balance.amount.toInt() < targetBalance) {
try {
getFromFaucet(client, nodeInfo, address)
getFromFaucet(nodeInfo, address)
} catch (t: Throwable) {
throw RuntimeException("failed to get tokens from faucet: $t")
}
Expand All @@ -68,14 +67,14 @@ object BalanceUtils {
}
}

suspend fun getScrtBalance(client: SigningCosmWasmClient, address: String = client.senderAddress): Coin {
suspend fun getScrtBalance(client: SigningCosmWasmClient, address: String): Coin {
val balance = client.getBalance(address).balances
return balance.getOrNull(0) ?: zeroUscrt
}

suspend fun getSnip20Balance(
client: SigningCosmWasmClient,
senderAddress: String = client.senderAddress,
senderAddress: String,
contract: ContractInfo
): BigInteger? {
val viewingKey =
Expand All @@ -94,7 +93,6 @@ object BalanceUtils {
}

private suspend fun getFromFaucet(
client: SigningCosmWasmClient,
nodeInfo: NodeInfo, address: String
): String {
val response = when (nodeInfo) {
Expand Down Expand Up @@ -124,13 +122,11 @@ object BalanceUtils {
senderAddress: String,
contract: ContractInfo
): SnipMsgs.ExecuteAnswer.ViewingKey {
val originalSenderAddress = client.senderAddress
client.senderAddress = senderAddress
val entropy = Random.nextBytes(40).encodeBase64()
val msg = Json.encodeToString(SnipMsgs.Execute(createViewingKey = SnipMsgs.Execute.CreateViewingKey(entropy)))
val msgs = listOf(
MsgExecuteContract(
sender = client.senderAddress,
sender = senderAddress,
contractAddress = contract.address,
codeHash = contract.codeHash,
msg = msg,
Expand All @@ -143,7 +139,6 @@ object BalanceUtils {
msgs,
txOptions = TxOptions(gasLimit = gasLimit)
)
client.senderAddress = originalSenderAddress
return Json.decodeFromString<SnipMsgs.ExecuteAnswer>(response.data[0]).viewingKey!!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,54 @@ object DeployContractUtils {

suspend fun getOrStoreCode(
client: SigningCosmWasmClient,
contractCodePath: Path
senderAddress: String,
contractCodePath: Path,
gasLimit: Int?,
): CodeInfo {
val wasmBytes =
fileSystem.read(contractCodePath) {
readByteArray()
}

return storedCode.getOrPut(wasmBytes.contentHashCode()) {
storeCode(client, wasmBytes)
storeCode(client, senderAddress, wasmBytes, gasLimit)
}
}

suspend fun storeCode(
client: SigningCosmWasmClient,
contractCodePath: Path
senderAddress: String,
contractCodePath: Path,
gasLimit: Int?,
): CodeInfo {
val wasmBytes =
fileSystem.read(contractCodePath) {
readByteArray()
}
return storeCode(client, wasmBytes)
return storeCode(client, senderAddress, wasmBytes, gasLimit)
}

private suspend fun storeCode(
client: SigningCosmWasmClient,
wasmBytes: ByteArray
senderAddress: String,
wasmBytes: ByteArray,
gasLimit: Int?,
): CodeInfo {
val msgs0 = listOf(
MsgStoreCode(
sender = client.senderAddress,
sender = senderAddress,
wasmByteCode = wasmBytes.toUByteArray(),
)
)
val simulate = client.simulate(msgs0)
val gasLimit = (simulate.gasUsed.toDouble() * 1.1).toInt()
val limit = if (gasLimit == null) {
val simulate = client.simulate(msgs0)
(simulate.gasUsed.toDouble() * 1.1).toInt()
} else {
gasLimit
}
val response = client.execute(
msgs0,
txOptions = TxOptions(gasLimit = gasLimit)
txOptions = TxOptions(gasLimit = limit)
)

val codeId = response.logs[0].events
Expand All @@ -67,16 +77,21 @@ object DeployContractUtils {
client: SigningCosmWasmClient,
codeInfo: CodeInfo,
instantiateMsgs: List<MsgInstantiateContract>,
gasLimit: Int?,
): ContractInstance {
instantiateMsgs.forEach {
it.codeId = codeInfo.codeId.toInt()
it.codeHash = codeInfo.codeHash
}
val simulate = client.simulate(instantiateMsgs)
val gasLimit = (simulate.gasUsed.toDouble() * 1.1).toInt()
val limit = if (gasLimit == null) {
val simulate = client.simulate(instantiateMsgs)
(simulate.gasUsed.toDouble() * 1.1).toInt()
} else {
gasLimit
}
val instantiateResponse = client.execute(
instantiateMsgs,
txOptions = TxOptions(gasLimit = gasLimit)
txOptions = TxOptions(gasLimit = limit)
)
val contractAddress = instantiateResponse.logs[0].events
.find { it.type == "message" }
Expand All @@ -88,11 +103,14 @@ object DeployContractUtils {

suspend fun getOrStoreCodeAndInstantiate(
client: SigningCosmWasmClient,
senderAddress: String,
codePath: Path,
instantiateMsgs: List<MsgInstantiateContract>,
storeCodeGasLimit: Int?,
instantiateGasLimit: Int?,
): ContractInstance {
val codeInfo = getOrStoreCode(client, codePath)
return instantiateCode(client, codeInfo, instantiateMsgs)
val codeInfo = getOrStoreCode(client, senderAddress, codePath, storeCodeGasLimit)
return instantiateCode(client, codeInfo, instantiateMsgs, instantiateGasLimit)
}

}
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=3.1.2
VERSION_NAME=4.0.0

# Publishing : Optional
POM_NAME=secretk
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ androidx-appcompat = "1.6.1"

kermit = "1.2.2"

getenv = "0.2.0"
getenv = "0.1.0"

compose-jb = "1.4.1"

Expand Down
16 changes: 8 additions & 8 deletions secretk.xcframework/Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c0321fa

Please sign in to comment.