Skip to content

Commit 4389cb2

Browse files
committed
chore: update ts sdk examples
1 parent ea2e817 commit 4389cb2

20 files changed

+244
-8976
lines changed

typescript-sdk/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
NODE_ENV="development"
22
DRY_RUN=0 # for ./scripts/publish.ts
3-
HUBBLE_URL="https://noble-pika-27.hasura.app"
3+
HUBBLE_URL=""

typescript-sdk/bun.lockb

272 Bytes
Binary file not shown.

typescript-sdk/package.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,26 @@
2323
},
2424
"dependencies": {
2525
"@cosmjs/cosmwasm-stargate": "0.32.4",
26-
"@cosmjs/encoding": "^0.32.4",
27-
"@cosmjs/proto-signing": "^0.32.4",
2826
"@cosmjs/stargate": "0.32.4",
29-
"@cosmjs/tendermint-rpc": "0.32.4",
3027
"ofetch": "^1.3.4",
31-
"viem": "^2.18.8"
28+
"viem": "^2.19.3"
3229
},
3330
"devDependencies": {
34-
"@arethetypeswrong/cli": "^0.15.3",
31+
"@cosmjs/tendermint-rpc": "^0.32.4",
32+
"@cosmjs/proto-signing": "^0.32.4",
33+
"@arethetypeswrong/cli": "^0.15.4",
3534
"@cosmjs/amino": "0.32.4",
3635
"@scure/base": "^1.1.7",
3736
"@total-typescript/ts-reset": "^0.5.1",
3837
"@types/bun": "^1.1.6",
39-
"@types/node": "^22.1.0",
38+
"@types/node": "^22.2.0",
4039
"consola": "^3.2.3",
4140
"cosmjs-types": "^0.9.0",
4241
"jsr": "^0.13.1",
4342
"patch-package": "^8.0.0",
44-
"tsx": "^4.16.5",
43+
"tsx": "^4.17.0",
4544
"typescript": "^5.5.4",
46-
"vite-tsconfig-paths": "^4.3.2",
45+
"vite-tsconfig-paths": "^5.0.1",
4746
"vitest": "^2.0.5"
4847
},
4948
"overrides": {

typescript-sdk/playground/all.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# list all the files in the playground directory
77
for file in playground/*.ts; do
88
echo "$file"
9-
# bun "$file" --private-key "1bdd5c2105f62c51d72c90d9e5ca6854a94337bcbcbb0b959846b85813d69380" || true
9+
bun "$file" --private-key "1bdd5c2105f62c51d72c90d9e5ca6854a94337bcbcbb0b959846b85813d69380" || true
1010
done
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bun
2+
import { parseArgs } from "node:util"
3+
import { fallback, http } from "viem"
4+
import { consola } from "scripts/logger"
5+
import { cosmosHttp } from "#transport.ts"
6+
import { raise } from "#utilities/index.ts"
7+
import { arbitrumSepolia } from "viem/chains"
8+
import { privateKeyToAccount } from "viem/accounts"
9+
import { hexStringToUint8Array } from "#convert.ts"
10+
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
11+
import { createCosmosSdkClient, offchainQuery, type TransferAssetsParameters } from "#mod.ts"
12+
13+
/* `bun playground/arbitrum-to-union.ts --private-key "..."` */
14+
15+
const { values } = parseArgs({
16+
args: process.argv.slice(2),
17+
options: {
18+
"private-key": { type: "string" },
19+
"estimate-gas": { type: "boolean", default: false }
20+
}
21+
})
22+
23+
const PRIVATE_KEY = values["private-key"]
24+
if (!PRIVATE_KEY) raise("Private key not found")
25+
const ONLY_ESTIMATE_GAS = values["estimate-gas"] ?? false
26+
27+
const evmAccount = privateKeyToAccount(`0x${PRIVATE_KEY}`)
28+
29+
const cosmosAccount = await DirectSecp256k1Wallet.fromKey(
30+
Uint8Array.from(hexStringToUint8Array(PRIVATE_KEY)),
31+
"union"
32+
)
33+
34+
const LINK_CONTRACT_ADDRESS = "0xb1d4538b4571d411f07960ef2838ce337fe1e80e"
35+
36+
try {
37+
/**
38+
* Calls Hubble, Union's indexer, to grab desired data that's always up-to-date.
39+
*/
40+
const {
41+
data: [arbitrumInfo]
42+
} = await offchainQuery.chain({
43+
chainId: `${arbitrumSepolia.id}`,
44+
includeEndpoints: true,
45+
includeContracts: true
46+
})
47+
if (!arbitrumInfo) raise("Berachain info not found")
48+
49+
const ucsConfiguration = arbitrumInfo.ucs1_configurations
50+
?.filter(config => config.destination_chain.chain_id === "union-testnet-8")
51+
.at(0)
52+
if (!ucsConfiguration) raise("UCS configuration not found")
53+
54+
const { channel_id, contract_address, source_chain, destination_chain } = ucsConfiguration
55+
56+
const client = createCosmosSdkClient({
57+
evm: {
58+
account: evmAccount,
59+
chain: arbitrumSepolia,
60+
transport: fallback([http(arbitrumSepolia?.rpcUrls.default.http.at(0))])
61+
},
62+
cosmos: {
63+
account: cosmosAccount,
64+
gasPrice: { amount: "0.0025", denom: "muno" },
65+
transport: cosmosHttp("https://rpc.testnet-8.union.build")
66+
}
67+
})
68+
69+
const transactionPayload = {
70+
amount: 1n,
71+
approve: true,
72+
sourceChannel: channel_id,
73+
network: arbitrumInfo.rpc_type,
74+
denomAddress: LINK_CONTRACT_ADDRESS,
75+
relayContractAddress: contract_address,
76+
// or `client.cosmos.account.address` if you want to send to yourself
77+
recipient: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv",
78+
path: [source_chain.chain_id, destination_chain.chain_id]
79+
} satisfies TransferAssetsParameters
80+
81+
const gasEstimationResponse = await client.simulateTransaction(transactionPayload)
82+
83+
consola.info(`Gas cost: ${gasEstimationResponse.data}`)
84+
85+
if (ONLY_ESTIMATE_GAS) process.exit(0)
86+
87+
if (!gasEstimationResponse.success) {
88+
console.info("Transaction simulation failed")
89+
process.exit(1)
90+
}
91+
92+
const transfer = await client.transferAsset(transactionPayload)
93+
94+
consola.info(transfer)
95+
} catch (error) {
96+
const errorMessage = error instanceof Error ? error.message : error
97+
console.error(errorMessage)
98+
} finally {
99+
process.exit(0)
100+
}

typescript-sdk/playground/stride-to-berachain.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const cosmosAccount = await DirectSecp256k1Wallet.fromKey(
3131
"stride"
3232
)
3333

34+
const [account] = await cosmosAccount.getAccounts()
35+
3436
try {
3537
/**
3638
* Calls Hubble, Union's indexer, to grab desired data that's always up-to-date.
@@ -58,11 +60,7 @@ try {
5860
cosmos: {
5961
account: cosmosAccount,
6062
gasPrice: { amount: "0.0025", denom: "ustrd" },
61-
transport: cosmosHttp(
62-
//
63-
// "https://stride.testnet-1.stridenet.co/"
64-
"https://stride-testnet-rpc.polkachu.com/"
65-
)
63+
transport: cosmosHttp("https://stride-testnet-rpc.polkachu.com")
6664
},
6765
evm: {
6866
account: berachainAccount,
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bun
2+
import { parseArgs } from "node:util"
3+
import { fallback, http } from "viem"
4+
import { consola } from "scripts/logger"
5+
import { cosmosHttp } from "#transport.ts"
6+
import { raise } from "#utilities/index.ts"
7+
import { arbitrumSepolia } from "viem/chains"
8+
import { privateKeyToAccount } from "viem/accounts"
9+
import { hexStringToUint8Array } from "#convert.ts"
10+
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
11+
import { createCosmosSdkClient, offchainQuery, type TransferAssetsParameters } from "#mod.ts"
12+
13+
/* `bun playground/union-to-arbitrum.ts --private-key "..."` --estimate-gas */
14+
15+
const { values } = parseArgs({
16+
args: process.argv.slice(2),
17+
options: {
18+
"private-key": { type: "string" },
19+
"estimate-gas": { type: "boolean", default: false }
20+
}
21+
})
22+
23+
const PRIVATE_KEY = values["private-key"]
24+
if (!PRIVATE_KEY) raise("Private key not found")
25+
const ONLY_ESTIMATE_GAS = values["estimate-gas"] ?? false
26+
27+
const evmAccount = privateKeyToAccount(`0x${PRIVATE_KEY}`)
28+
29+
const cosmosAccount = await DirectSecp256k1Wallet.fromKey(
30+
Uint8Array.from(hexStringToUint8Array(PRIVATE_KEY)),
31+
"union"
32+
)
33+
34+
const [account] = await cosmosAccount.getAccounts()
35+
36+
try {
37+
/**
38+
* Calls Hubble, Union's indexer, to grab desired data that's always up-to-date.
39+
*/
40+
const {
41+
data: [unionTestnetInfo]
42+
} = await offchainQuery.chain({
43+
chainId: "union-testnet-8",
44+
includeEndpoints: true,
45+
includeContracts: true
46+
})
47+
48+
if (!unionTestnetInfo) raise("Union testnet info not found")
49+
50+
const ucsConfiguration = unionTestnetInfo.ucs1_configurations
51+
?.filter(config => config.destination_chain.chain_id === `${arbitrumSepolia.id}`)
52+
.at(0)
53+
if (!ucsConfiguration) raise("UCS configuration not found")
54+
55+
const { channel_id, contract_address, source_chain, destination_chain } = ucsConfiguration
56+
57+
const client = createCosmosSdkClient({
58+
evm: {
59+
account: evmAccount,
60+
chain: arbitrumSepolia,
61+
transport: fallback([http(arbitrumSepolia?.rpcUrls.default.http.at(0))])
62+
},
63+
cosmos: {
64+
account: cosmosAccount,
65+
gasPrice: { amount: "0.0025", denom: "muno" },
66+
transport: cosmosHttp("https://rpc.testnet-8.union.build")
67+
}
68+
})
69+
70+
const transactionPayload = {
71+
amount: 1n,
72+
denomAddress: "muno",
73+
sourceChannel: channel_id,
74+
// or `client.evm.account.address` if you want to send to yourself
75+
recipient: evmAccount.address,
76+
network: unionTestnetInfo.rpc_type,
77+
relayContractAddress: contract_address,
78+
path: [source_chain.chain_id, destination_chain.chain_id]
79+
} satisfies TransferAssetsParameters
80+
81+
const gasEstimationResponse = await client.simulateTransaction(transactionPayload)
82+
83+
consola.info(`Gas cost: ${gasEstimationResponse.data}`)
84+
85+
if (ONLY_ESTIMATE_GAS) process.exit(0)
86+
87+
if (!gasEstimationResponse.success) {
88+
console.info("Transaction simulation failed")
89+
process.exit(1)
90+
}
91+
92+
const transfer = await client.transferAsset(transactionPayload)
93+
94+
console.info(transfer)
95+
} catch (error) {
96+
const errorMessage = error instanceof Error ? error.message : error
97+
console.error(errorMessage)
98+
} finally {
99+
process.exit(0)
100+
}

typescript-sdk/playground/union-to-berachain.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { privateKeyToAccount } from "viem/accounts"
88
import { hexStringToUint8Array } from "#convert.ts"
99
import { berachainTestnetbArtio } from "viem/chains"
1010
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
11-
import { createCosmosSdkClient, offchainQuery } from "#mod.ts"
11+
import { createCosmosSdkClient, offchainQuery, type TransferAssetsParameters } from "#mod.ts"
1212

1313
/* `bun playground/union-to-berachain.ts --private-key "..."` --estimate-gas */
1414

@@ -69,14 +69,11 @@ try {
6969
cosmos: {
7070
account: cosmosAccount,
7171
gasPrice: { amount: "0.0025", denom: "muno" },
72-
transport: cosmosHttp(
73-
// "https://rpc.testnet.bonlulu.uno"
74-
"https://rpc.testnet.seed.poisonphang.com:443"
75-
)
72+
transport: cosmosHttp("https://rpc.testnet-8.union.build")
7673
}
7774
})
7875

79-
const gasEstimationResponse = await client.simulateTransaction({
76+
const transactionPayload = {
8077
amount: 1n,
8178
denomAddress: "muno",
8279
sourceChannel: channel_id,
@@ -85,7 +82,9 @@ try {
8582
recipient: berachainAccount.address,
8683
relayContractAddress: contract_address,
8784
path: [source_chain.chain_id, destination_chain.chain_id]
88-
})
85+
} satisfies TransferAssetsParameters
86+
87+
const gasEstimationResponse = await client.simulateTransaction(transactionPayload)
8988

9089
consola.box("Union to Berachain gas cost:", gasEstimationResponse)
9190

@@ -96,16 +95,7 @@ try {
9695
process.exit(1)
9796
}
9897

99-
const transfer = await client.transferAsset({
100-
amount: 1n,
101-
denomAddress: "muno",
102-
sourceChannel: channel_id,
103-
network: unionTestnetInfo.rpc_type,
104-
// or `client.evm.account.address` if you want to send to yourself
105-
recipient: berachainAccount.address,
106-
relayContractAddress: contract_address,
107-
path: [source_chain.chain_id, destination_chain.chain_id]
108-
})
98+
const transfer = await client.transferAsset(transactionPayload)
10999

110100
console.info(transfer)
111101
} catch (error) {

typescript-sdk/playground/union-to-union.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ try {
4141
cosmos: {
4242
account: cosmosAccount,
4343
gasPrice: { amount: "0.025", denom: "muno" },
44-
transport: cosmosHttp("https://rpc.testnet.bonlulu.uno")
44+
transport: cosmosHttp("https://rpc.testnet-8.union.build")
4545
}
4646
})
4747

typescript-sdk/scripts/chains.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (chainId) {
1717
}
1818

1919
consola.info(JSON.stringify(data, undefined, 2))
20+
process.exit(0)
2021
}
2122

2223
const data = await offchainQuery.chains({

0 commit comments

Comments
 (0)