TypeScript SDK
Installation
bun add @unionlabs/client
pnpm add @unionlabs/client
npm install @unionlabs/client
yarn add @unionlabs/client
The Union Client is TypeScript-first and framework agnostic. At app.union.build we use Svelte but you can use any framework or no frameworks at all. If you’re a React developer, checkout this demo on Stackblitz.
Client Initialization
import { privateKeyToAccount } from "viem/accounts"import { createUnionClient, http } from "@unionlabs/client"
const client = createUnionClient({ chainId: "80084", transport: http("https://bartio.rpc.berachain.com"), account: privateKeyToAccount(`0x${process.env.PRIVATE_KEY}`),})
import { http, createUnionClient, hexStringToUint8Array} from "@unionlabs/client"import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
const PRIVATE_KEY = process.env["PRIVATE_KEY"]if (!PRIVATE_KEY) throw new Error("Private key not found")
const account = await DirectSecp256k1Wallet.fromKey( Uint8Array.from(hexStringToUint8Array(PRIVATE_KEY)), "stride")
const client = createUnionClient({ account, chainId: "stride-internal-1", transport: http("stride.testnet-1.stridenet.co"),})
import { http, hexStringToUint8Array, createMultiUnionClient} from "@unionlabs/client"import { privateKeyToAccount } from "viem/accounts"import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"
const PRIVATE_KEY = process.env["PRIVATE_KEY"]if (!PRIVATE_KEY) throw new Error("Private key not found")
const clients = createMultiUnionClient([ { chainId: "80084", transport: http("https://bartio.rpc.berachain.com"), account: privateKeyToAccount(`0x${PRIVATE_KEY}`), }, { chainId: "stride-internal-1", transport: http("stride.testnet-1.stridenet.co"), account: await DirectSecp256k1Wallet.fromKey( Uint8Array.from(hexStringToUint8Array(PRIVATE_KEY)), "stride" ), }])
Transferring Assets
We will transfer 1 HONEY
from Berachain bArtio to Stride Testnet.
You have the option to trigger the approval transaction manually by setting autoApprove
to false
then calling .approveTransaction
before calling .transferAsset
.
import { unionClient } from "./client.ts"import type { TransferAssetsParameters } from "@unionlabs/client"
const transferPayload = { amount: 1n, autoApprove: false, // ^ we will approve manually for this example destinationChainId: "stride-internal-1", receiver: "stride17ttpfu2xsmfxu6shl756mmxyqu33l5ljegnwps", denomAddress: "0x0E4aaF1351de4c0264C5c7056Ef3777b41BD8e03", // ^ HONEY contract address} satisfies TransferAssetsParameters<"80084">
const approval = await unionClient.approveTransaction(transferPayload)
if (approval.isErr()) { console.error(approval.error) process.exit(1)}
console.info(`Approval hash: ${approval.value}`)
const transfer = await unionClient.transferAsset(transferPayload)
if (transfer.isErr()) { console.error(transfer.error) process.exit(1)}
console.info(`Transfer hash: ${transfer.value}`)
import { privateKeyToAccount } from "viem/accounts"import { createUnionClient, http } from "@unionlabs/client"
let PRIVATE_KEY = process.env["PRIVATE_KEY"]if (!PRIVATE_KEY) throw new Error("PRIVATE_KEY is not set")PRIVATE_KEY = PRIVATE_KEY.startsWith("0x") ? PRIVATE_KEY : `0x${PRIVATE_KEY}`
export const unionClient = createUnionClient({ chainId: "80084", account: privateKeyToAccount(PRIVATE_KEY), transport: http("https://bartio.rpc.berachain.com")})
Run the above example
PRIVATE_KEY="" bun x tsx main.ts
PRIVATE_KEY="" pnpm dlx tsx main.ts
PRIVATE_KEY="" npm x tsx main.ts
PRIVATE_KEY="" yarn dlx tsx main.ts