Skip to content

Commit

Permalink
Add new example for MsgSwap and MsgTransfer (#23)
Browse files Browse the repository at this point in the history
* chore: bump finschia-proto:4.0.0-rc2

* chore: add new example for swap and transfer in single tx

* chore: jdk 8(no longer available) -> 11

* chore: update changelog
  • Loading branch information
jaeseung-bae authored May 16, 2024
1 parent 4188a78 commit 58872d3
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 8
java-version: 11

- name: Gradle Cache
uses: burrunan/gradle-cache-action@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: 8
distribution: temurin
java-version: 11

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@ccb4328a959376b642e027874838f60f8e596de3
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Added
* [\#21](https://github.com/Finschia/finschia-kt/pull/21) add MsgCreateValidator examples for single and multi-sig
* [\#22](https://github.com/Finschia/finschia-kt/pull/22) bump up finschia-proto:2.0.0
* [\#23](https://github.com/Finschia/finschia-kt/pull/23) add new example for MsgSwap&MsgTransfer Tx

### Changed
* [\#19](https://github.com/Finschia/finschia-kt/pull/19) change the value of AminoMsg to the general type
Expand Down
2 changes: 1 addition & 1 deletion examples/multisig-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

// Finschia sdk
implementation("network.finschia:finschia-kt-crypto:0.2.2")
implementation("network.finschia:finschia-proto:2.0.0")
implementation("network.finschia:finschia-proto:4.0.0-rc2")

implementation(project(":tx"))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/query-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {

// Finschia sdk
implementation("network.finschia:finschia-kt-crypto:0.2.2")
implementation("network.finschia:finschia-proto:2.0.0")
implementation("network.finschia:finschia-proto:4.0.0-rc2")

implementation(project(":tx"))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/with-tx-wrapper-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {

// Finschia sdk
implementation("network.finschia:finschia-kt-crypto:0.2.2")
implementation("network.finschia:finschia-proto:2.0.0")
implementation("network.finschia:finschia-proto:4.0.0-rc2")

implementation(project(":tx"))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package network.finschia.sdk.example

import io.grpc.ManagedChannelBuilder
import network.finschia.sdk.account.HDWallet
import network.finschia.sdk.base.Tx

suspend fun main() {
// Change me for your environment
val chainId = "sim"
val hrpPrefix = "link"
val host = "localhost"
val port = 9090
val fromDenom = "cony"
val toDenom = "peb"
val conyAmount = "1"
val swapRateFromConyToPeb = "148079656000000"
val amountToTransfer = swapRateFromConyToPeb.toBigInteger().multiply(conyAmount.toBigInteger())
val kaiaAddress = "0xf7bAc63fc7CEaCf0589F25454Ecf5C2ce904997c"
val gasLimit: Long = 150000
val seqNum: Long = 14

// Initialize gRPC channel
val channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build()

// Sender's info
val mnemonic = "mind flame tobacco sense move hammer drift crime ring globe art gaze cinnamon helmet cruise special produce notable negative wait path scrap recall have"
val hdWallet = HDWallet.loadFromMnemonic(mnemonic)
val aliceKey = hdWallet.getKeyWallet(0)
val aliceAddress = aliceKey.address.toBech32(hrpPrefix)

// Build MsgSwap
val amountFromDenom = cosmos.base.v1beta1.coin {
amount = conyAmount
denom = fromDenom
}
val msgSwap = lbm.fswap.v1.msgSwap {
this.fromCoinAmount = amountFromDenom
this.toDenom = toDenom
this.fromAddress = aliceAddress
}

// Build MsgTransfer
val msgTransfer = lbm.fbridge.v1.msgTransfer {
this.amount = amountToTransfer.toString()
this.sender = aliceAddress
this.receiver = kaiaAddress
}

// Build Tx with two messages
val tx = Tx.newBuilder()
.setChainId(chainId)
.addMessage(com.google.protobuf.any {
typeUrl = "/lbm.fswap.v1.MsgSwap"
value = msgSwap.toByteString()
})
.addMessage(com.google.protobuf.any {
typeUrl = "/lbm.fbridge.v1.MsgTransfer"
value = msgTransfer.toByteString()
})
.addSigner(aliceKey, seqNum)
.setFee(cosmos.tx.v1beta1.fee { this.gasLimit = gasLimit })
.setMemo("")
.build()

// Sign Tx
tx.sign(aliceKey, 0)

// Send Tx
val resultTxHash = TxClient(channel).use {
it.broadcastTx(tx)
}

println(resultTxHash)
}
2 changes: 1 addition & 1 deletion examples/without-tx-wrapper-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {

// Finschia sdk
implementation("network.finschia:finschia-kt-crypto:0.2.2")
implementation("network.finschia:finschia-proto:2.0.0")
implementation("network.finschia:finschia-proto:4.0.0-rc2")
}

application {
Expand Down

0 comments on commit 58872d3

Please sign in to comment.