Skip to content

Commit

Permalink
Holesky -> Stargaze in TypeScript SDK (#3634)
Browse files Browse the repository at this point in the history
- **feat(ts-sdk): stargaze-to-holesky**
- **chore: fmt**
  • Loading branch information
cor authored Jan 24, 2025
2 parents 3833452 + 86d0da9 commit 3d77750
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 7 deletions.
104 changes: 104 additions & 0 deletions typescript-sdk/playground/holesky-to-stargaze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { bech32AddressToHex, createUnionClient } from "#mod.ts"
import {
getChannelInfo,
getQuoteToken,
getRecommendedChannels
} from "#query/offchain/ucs03-channels"
import { privateKeyToAccount } from "viem/accounts"

// hack to encode bigints to json
declare global {
interface BigInt {
toJSON: () => string
}
}

if (!BigInt.prototype.toJSON) {
Object.defineProperty(BigInt.prototype, "toJSON", {
value: function () {
return this.toString()
},
writable: true,
configurable: true
})
}
// end hack

const cliArgs = parseArgs({
args: process.argv.slice(2),
options: {
"private-key": { type: "string" },
"estimate-gas": { type: "boolean", default: false }
}
})

const PRIVATE_KEY = cliArgs.values["private-key"]
const STARS_DENOM = "0xa3570de1c0603051a08a5b2aa1d9ddd4e4e5339d" // wrapped STARS on holesky
const AMOUNT = 1n
const RECEIVER = bech32AddressToHex({ address: "stars1qcvavxpxw3t8d9j7mwaeq9wgytkf5vwputv5x4" })
const SOURCE_CHAIN_ID = "17000"
const DESTINATION_CHAIN_ID = "elgafar-1"

const channels = await getRecommendedChannels()

const channel = getChannelInfo(SOURCE_CHAIN_ID, DESTINATION_CHAIN_ID, channels)
if (channel === null) {
consola.info("no channel found")
process.exit(1)
}

consola.info("channel", channel)

const quoteToken = await getQuoteToken(SOURCE_CHAIN_ID, STARS_DENOM, channel)
if (quoteToken.isErr()) {
consola.info("could not get quote token")
consola.error(quoteToken.error)
process.exit(1)
}

consola.info("quote token", quoteToken.value)

const transferArgs = {
baseToken: STARS_DENOM,
baseAmount: AMOUNT,
quoteToken: quoteToken.value.quote_token,
quoteAmount: AMOUNT,
receiver: RECEIVER,
sourceChannelId: channel.source_channel_id,
ucs03address: `0x${channel.source_port_id}`
}

consola.info("transfer args", transferArgs)

if (!PRIVATE_KEY) {
consola.error("no private key provided")
process.exit(1)
}

const evmClient = createUnionClient({
chainId: SOURCE_CHAIN_ID,
account: privateKeyToAccount(`0x${PRIVATE_KEY}`),
transport: http("https://rpc.17000.holesky.chain.kitchen")
})

const approveResponse = await evmClient.approveErc20(transferArgs)

if (approveResponse.isErr()) {
consola.error(approveResponse.error)
process.exit(1)
}

consola.info("approval tx hash", approveResponse.value)

const transfer = await evmClient.transferAsset(transferArgs)

if (transfer.isErr()) {
consola.info("transfer submission failed")
consola.error(transfer.error)
process.exit(1)
}

consola.info("transfer tx hash", transfer.value)
2 changes: 1 addition & 1 deletion typescript-sdk/playground/stargaze-to-holesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const cliArgs = parseArgs({
const PRIVATE_KEY = cliArgs.values["private-key"]
const STARS_DENOM = "ustars"
const AMOUNT = 420n
const RECEIVER = "0x153919669Edc8A5D0c8D1E4507c9CE60435A1177"
const RECEIVER = "0xE6831e169d77a861A0E71326AFA6d80bCC8Bc6aA"
const SOURCE_CHAIN_ID = "elgafar-1"
const DESTINATION_CHAIN_ID = "17000"

Expand Down
43 changes: 40 additions & 3 deletions typescript-sdk/src/abi/ucs-03.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const ucs03ZkgmAbi = [
},
{
type: "function",
name: "OP_FUNGIBLE_ASSET_TRANSFER",
name: "OP_FUNGIBLE_ASSET_ORDER",
inputs: [],
outputs: [{ name: "", type: "uint8", internalType: "uint8" }],
stateMutability: "view"
Expand All @@ -89,13 +89,15 @@ export const ucs03ZkgmAbi = [
{ type: "error", name: "ErrInvalidAssetName", inputs: [] },
{ type: "error", name: "ErrInvalidAssetOrigin", inputs: [] },
{ type: "error", name: "ErrInvalidAssetSymbol", inputs: [] },
{ type: "error", name: "ErrInvalidBatchInstruction", inputs: [] },
{ type: "error", name: "ErrInvalidFillType", inputs: [] },
{ type: "error", name: "ErrInvalidHops", inputs: [] },
{ type: "error", name: "ErrInvalidIBCVersion", inputs: [] },
{ type: "error", name: "ErrInvalidMultiplexSender", inputs: [] },
{ type: "error", name: "ErrOnlyMaker", inputs: [] },
{ type: "error", name: "ErrUnauthorized", inputs: [] },
{ type: "error", name: "ErrUnimplemented", inputs: [] },
{ type: "error", name: "ErrUnknownSyscall", inputs: [] },
{ type: "error", name: "ErrUnknownOpcode", inputs: [] },
{ type: "error", name: "ErrUnsupportedVersion", inputs: [] },
{ type: "constructor", inputs: [], stateMutability: "nonpayable" },
{
Expand All @@ -105,6 +107,20 @@ export const ucs03ZkgmAbi = [
outputs: [{ name: "", type: "string", internalType: "string" }],
stateMutability: "view"
},
{
type: "function",
name: "call",
inputs: [
{ name: "channelId", type: "uint32", internalType: "uint32" },
{ name: "contractAddress", type: "bytes", internalType: "bytes" },
{ name: "contractCalldata", type: "bytes", internalType: "bytes" },
{ name: "timeoutHeight", type: "uint64", internalType: "uint64" },
{ name: "timeoutTimestamp", type: "uint64", internalType: "uint64" },
{ name: "salt", type: "bytes32", internalType: "bytes32" }
],
outputs: [],
stateMutability: "nonpayable"
},
{
type: "function",
name: "channelBalance",
Expand Down Expand Up @@ -417,6 +433,25 @@ export const ucs03ZkgmAbi = [
outputs: [],
stateMutability: "nonpayable"
},
{
type: "function",
name: "transferAndCall",
inputs: [
{ name: "channelId", type: "uint32", internalType: "uint32" },
{ name: "receiver", type: "bytes", internalType: "bytes" },
{ name: "baseToken", type: "address", internalType: "address" },
{ name: "baseAmount", type: "uint256", internalType: "uint256" },
{ name: "quoteToken", type: "bytes", internalType: "bytes" },
{ name: "quoteAmount", type: "uint256", internalType: "uint256" },
{ name: "contractAddress", type: "bytes", internalType: "bytes" },
{ name: "contractCalldata", type: "bytes", internalType: "bytes" },
{ name: "timeoutHeight", type: "uint64", internalType: "uint64" },
{ name: "timeoutTimestamp", type: "uint64", internalType: "uint64" },
{ name: "salt", type: "bytes32", internalType: "bytes32" }
],
outputs: [],
stateMutability: "nonpayable"
},
{
type: "function",
name: "transferOwnership",
Expand Down Expand Up @@ -490,15 +525,17 @@ export const ucs03ZkgmAbi = [
{ type: "error", name: "ErrInvalidAssetName", inputs: [] },
{ type: "error", name: "ErrInvalidAssetOrigin", inputs: [] },
{ type: "error", name: "ErrInvalidAssetSymbol", inputs: [] },
{ type: "error", name: "ErrInvalidBatchInstruction", inputs: [] },
{ type: "error", name: "ErrInvalidFillType", inputs: [] },
{ type: "error", name: "ErrInvalidHops", inputs: [] },
{ type: "error", name: "ErrInvalidIBCVersion", inputs: [] },
{ type: "error", name: "ErrInvalidMultiplexSender", inputs: [] },
{ type: "error", name: "ErrNotIBC", inputs: [] },
{ type: "error", name: "ErrNotImplemented", inputs: [] },
{ type: "error", name: "ErrOnlyMaker", inputs: [] },
{ type: "error", name: "ErrUnauthorized", inputs: [] },
{ type: "error", name: "ErrUnimplemented", inputs: [] },
{ type: "error", name: "ErrUnknownSyscall", inputs: [] },
{ type: "error", name: "ErrUnknownOpcode", inputs: [] },
{ type: "error", name: "ErrUnsupportedVersion", inputs: [] },
{ type: "error", name: "ExpectedPause", inputs: [] },
{ type: "error", name: "FailedInnerCall", inputs: [] },
Expand Down
Loading

0 comments on commit 3d77750

Please sign in to comment.