Skip to content

Commit

Permalink
feat(ts-sdk): next dev iteration (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az authored Sep 2, 2024
2 parents e292ee7 + 11f6a28 commit ca66ecd
Show file tree
Hide file tree
Showing 44 changed files with 1,488 additions and 1,740 deletions.
4 changes: 4 additions & 0 deletions dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

0xbonlulu
0xc0dejug
0xkaiserkarel
Expand Down Expand Up @@ -144,6 +145,7 @@ Proxiable
Pruijs
QUMGLDJ
Quickstart
Qyby
RUSTFLAGS
Recvs
Redelegate
Expand Down Expand Up @@ -789,6 +791,7 @@ neondb
netgo
netutil
networkid
neverthrow
newacc
newtype
nextest
Expand Down Expand Up @@ -1069,6 +1072,7 @@ storetypes
storyblok
strangelove
stretchr
stridenet
stridepub
stridevalcons
stridevalconspub
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
./site/site.nix
./site/openapi.nix
./lib/near/near.nix
./typescript-sdk/typescript-sdk.nix
./light-clients/ethereum-light-client/ethereum-light-client.nix
./light-clients/cometbls-light-client/cometbls-light-client.nix
./light-clients/tendermint-light-client/tendermint-light-client.nix
Expand Down
92 changes: 41 additions & 51 deletions typescript-sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,61 @@
> [!NOTE] Work in progress
<br />

<p align="center">
<img width="675" src="https://i.imgur.com/yFpIuzm.jpeg" alt="Union logo" />
</p>
<br />
<p align="center">
<!-- <a href="https://npmjs.com/package/@unionlabs/client"><img src="https://img.shields.io/npm/v/@unionlabs/client.svg" alt="npm package"></a> -->
</p>

<h1 align="center" style="font-size: 2.75rem; font-weight: 900; color: white;">Union Labs TypeScript SDK</h1>

[![JSR](https://jsr.io/badges/@union/client)](https://jsr.io/@union/client)

Union Labs TypeScript SDK providing utilities for cross-chain transfers and more.

```sh
npx jsr add @union/client
```

### Quick Start
## Usage

### Initiate a client

```ts
import { http } from "viem"
import { sepolia } from "viem/chains"
import { createCosmosSdkClient, cosmosHttp } from "@union/client"


const unionClient = createCosmosSdkClient({
evm: {
chain: sepolia,
// browser wallet or `privateKeyToAccount` from `viem/accounts`
account: evmAccount,
transport: http("https://rpc2.sepolia.org")
},
cosmos: {
// browser wallet or `DirectSecp256k1Wallet.fromKey`
account: cosmosAccount,
gasPrice: { amount: "0.0025", denom: "muno" },
transport: cosmosHttp("https://rpc.testnet.bonlulu.uno")
}
})
import { createUnionClient } from "@union/client"
import { privateKeyToAccount } from "viem/accounts"

const gasCostResponse = await unionClient.simulateTransaction({
amount: 1n,
network: "evm",
sourceChannel: "channel-69",
path: ["11155111", "union-testnet-8"],
recipient: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv",
denomAddress: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238",
relayContractAddress: "0x3C148Ec863404e48d88757E88e456963A14238ef"
const client = createUnionClient({
chainId: "11155111",
transport: http("https://rpc.sepolia.org"),
account: privateKeyToAccount(`0x${PRIVATE_KEY}`) // or from wagmi configuration
})
```

if(!gasCostResponse.success) {
throw new Error("Failed to simulate transaction")
}
### Examples

Transfer `strd` from Stride Testnet on Cosmos (`stride-internal-1`) chain to Sepolia on EVM (`1111551111`) chain.

console.info("Gas cost", gasCostResponse.data)
```ts
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
import { createUnionClient, hexStringToUint8Array } from "@union/client"

const PRIVATE_KEY = process.env.PRIVATE_KEY

const cosmosAccount = await DirectSecp256k1Wallet.fromKey(
Uint8Array.from(hexStringToUint8Array(PRIVATE_KEY)),
"stride"
)

const client = createUnionClient({
account: cosmosAccount,
chainId: "stride-internal-1",
transport: http("stride.testnet-1.stridenet.co"),
})

const transfer = await unionClient.transferAsset({
const transfer = await client.transferAsset({
amount: 1n,
network: "evm",
sourceChannel: "channel-69",
path: ["11155111", "union-testnet-8"],
recipient: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv",
denomAddress: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238",
relayContractAddress: "0x3C148Ec863404e48d88757E88e456963A14238ef"
denomAddress: "strd",
destinationChainId: "11155111",
recipient: "0x8478B37E983F520dBCB5d7D3aAD8276B82631aBd",
})
```

See [`./playground/berachain-to-union.ts`](./playground/berachain-to-union.ts) and [`./playground`](./playground) in general for more examples.
if (transfer.isErr()) {
return console.error("Transfer failed", transfer.error)
}

return console.info("Transfer successful", transfer.value)
```
Binary file modified typescript-sdk/bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions typescript-sdk/jsr.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@union/client",
"version": "0.0.1-rc.53",
"version": "0.0.1-rc.66",
"license": "MIT",
"exports": {
".": "./src/mod.ts"
},
"publish": {
"include": ["./src/**/*.ts", "./jsr.json"]
"include": ["./src/**/*.ts", "./jsr.json", "./README.md"]
}
}
18 changes: 8 additions & 10 deletions typescript-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@union/client",
"version": "0.0.1-rc.53",
"description": "Union Labs cross-chain transfers client",
"version": "0.0.1-rc.66",
"homepage": "https://jsr.io/@union/client",
"description": "Union Labs cross-chain transfers client",
"type": "module",
"exports": "./src/mod.ts",
"scripts": {
Expand All @@ -18,29 +18,26 @@
"clean": "rm -rf dist node_modules",
"postinstall": "patch-package"
},
"imports": {
"#*": "./src/*"
},
"dependencies": {
"@cosmjs/cosmwasm-stargate": "0.32.4",
"@cosmjs/stargate": "0.32.4",
"neverthrow": "^7.1.0",
"ofetch": "^1.3.4",
"viem": "^2.19.3"
"viem": "^2.21.1"
},
"devDependencies": {
"@cosmjs/tendermint-rpc": "^0.32.4",
"@cosmjs/proto-signing": "^0.32.4",
"@arethetypeswrong/cli": "^0.15.4",
"@cosmjs/amino": "0.32.4",
"@scure/base": "^1.1.7",
"@total-typescript/ts-reset": "^0.5.1",
"@types/bun": "^1.1.6",
"@types/node": "^22.2.0",
"@total-typescript/ts-reset": "^0.6.0",
"@types/bun": "^1.1.8",
"@types/node": "^22.5.2",
"consola": "^3.2.3",
"cosmjs-types": "^0.9.0",
"jsr": "^0.13.1",
"patch-package": "^8.0.0",
"tsx": "^4.17.0",
"typescript": "^5.5.4",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.0.5"
Expand All @@ -54,6 +51,7 @@
"url": "https://github.com/unionlabs/union.git",
"directory": "typescript-sdk"
},
"license": "MIT",
"engineStrict": true,
"sideEffects": false
}
58 changes: 19 additions & 39 deletions typescript-sdk/playground/arbitrum-to-union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { arbitrumSepolia } from "viem/chains"
import { privateKeyToAccount } from "viem/accounts"
import { createCosmosSdkClient, offchainQuery, type TransferAssetsParameters } from "#mod.ts"
import { createUnionClient, type TransferAssetsParameters } from "#mod.ts"

/* `bun playground/arbitrum-to-union.ts --private-key "..."` */

Expand All @@ -26,60 +26,40 @@ const evmAccount = privateKeyToAccount(`0x${PRIVATE_KEY}`)
const LINK_CONTRACT_ADDRESS = "0xb1d4538b4571d411f07960ef2838ce337fe1e80e"

try {
/**
* Calls Hubble, Union's indexer, to grab desired data that's always up-to-date.
*/
const {
data: [arbitrumInfo]
} = await offchainQuery.chain({
const client = createUnionClient({
account: evmAccount,
chainId: `${arbitrumSepolia.id}`,
includeEndpoints: true,
includeContracts: true
})
if (!arbitrumInfo) raise("Berachain info not found")

const ucsConfiguration = arbitrumInfo.ucs1_configurations
?.filter(config => config.destination_chain.chain_id === "union-testnet-8")
.at(0)

if (!ucsConfiguration) raise("UCS configuration not found")

const { channel_id, contract_address, source_chain, destination_chain } = ucsConfiguration

const client = createCosmosSdkClient({
evm: {
account: evmAccount,
chain: arbitrumSepolia,
transport: fallback([http(arbitrumSepolia?.rpcUrls.default.http.at(0))])
}
transport: fallback([http(arbitrumSepolia?.rpcUrls.default.http.at(0))])
})

const transactionPayload = {
amount: 1n,
approve: true,
sourceChannel: channel_id,
network: arbitrumInfo.rpc_type,
autoApprove: true,
denomAddress: LINK_CONTRACT_ADDRESS,
relayContractAddress: contract_address,
destinationChainId: "union-testnet-8",
// or `client.cosmos.account.address` if you want to send to yourself
recipient: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv",
path: [source_chain.chain_id, destination_chain.chain_id]
} satisfies TransferAssetsParameters
recipient: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv"
} satisfies TransferAssetsParameters<"421614">

const gasEstimationResponse = await client.simulateTransaction(transactionPayload)

consola.info(`Gas cost: ${gasEstimationResponse.data}`)
if (gasEstimationResponse.isErr()) {
consola.error(gasEstimationResponse.error)
process.exit(1)
}

consola.success("Union to Berachain gas cost:", gasEstimationResponse.value)

if (ONLY_ESTIMATE_GAS) process.exit(0)

if (!gasEstimationResponse.success) {
console.info("Transaction simulation failed")
const transfer = await client.transferAsset(transactionPayload)

if (transfer.isErr()) {
console.error(transfer.error)
process.exit(1)
}

const transfer = await client.transferAsset(transactionPayload)

consola.info(transfer)
consola.info(transfer.value)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
console.error(errorMessage)
Expand Down
Loading

0 comments on commit ca66ecd

Please sign in to comment.