Skip to content

Commit

Permalink
feat: typescript sdk enhancements (#3236)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az authored Dec 4, 2024
2 parents 568ad67 + 8282fbb commit 78a6f46
Show file tree
Hide file tree
Showing 24 changed files with 393 additions and 220 deletions.
2 changes: 2 additions & 0 deletions typescript-sdk/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<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) ![NPM Version](https://img.shields.io/npm/v/@unionlabs/client)

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

```sh
Expand Down
Binary file modified typescript-sdk/bun.lockb
Binary file not shown.
33 changes: 17 additions & 16 deletions typescript-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,41 @@
"build": "tsup --config='tsup.config.ts'",
"typecheck": "tsc --project tsconfig.json --noEmit",
"test": "vitest --run",
"test:typecheck": "vitest --typecheck.only",
"check-package": "bunx publint@latest --strict && bunx @arethetypeswrong/cli --pack --ignore-rules 'cjs-resolves-to-esm'",
"clean": "rm -rf dist node_modules",
"knip": "knip --config='knip.json'",
"postinstall": "patch-package",
"prepublish": "bun run build",
"typedoc": "bun x typedoc@latest --out .docs ./src/mod.ts"
"postinstall": "patch-package",
"knip": "knip --config='knip.json'",
"clean": "rm -rf dist node_modules",
"test:typecheck": "vitest --typecheck.only",
"typedoc": "deno run --allow-all npm:typedoc@latest --out .docs ./src/mod.ts",
"check-package": "deno run --allow-all npm:publint --strict && deno run --allow-all npm:@arethetypeswrong/cli@latest --pack --ignore-rules 'cjs-resolves-to-esm'"
},
"dependencies": {
"@scure/base": "^1.1.9",
"neverthrow": "^8.1.1",
"ofetch": "^1.4.1",
"patch-package": "^8.0.0",
"@aptos-labs/ts-sdk": "^1.31.0",
"@aptos-labs/ts-sdk": "^1.33.0",
"@cosmjs/amino": "^0.32.4",
"@cosmjs/cosmwasm-stargate": "0.32.4",
"@cosmjs/proto-signing": "^0.32.4",
"@cosmjs/stargate": "0.32.4",
"@cosmjs/tendermint-rpc": "^0.32.4",
"viem": "^2.21.43"
"@scure/base": "^1.1.9",
"neverthrow": "^8.1.1",
"ofetch": "^1.4.1",
"ox": "^0.2.2",
"patch-package": "^8.0.0",
"viem": "^2.21.48"
},
"devDependencies": {
"@total-typescript/ts-reset": "^0.6.1",
"@types/bun": "^1.1.13",
"@types/node": "^22.9.0",
"@types/node": "^22.9.1",
"consola": "^3.2.3",
"cosmjs-types": "^0.9.0",
"jsr": "^0.13.2",
"knip": "^5.36.3",
"knip": "^5.37.1",
"tsup": "^8.3.5",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
"vite-tsconfig-paths": "^5.1.0",
"vitest": "^2.1.4"
"vite-tsconfig-paths": "^5.1.3",
"vitest": "^2.1.5"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/aptos-to-aptos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import "scripts/patch"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/aptos-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import "scripts/patch"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/arbitrum-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import "scripts/patch"
import { parseArgs } from "node:util"
import { fallback, http } from "viem"
import { consola } from "scripts/logger"
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/stride-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import "scripts/patch"
import { http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/union-to-sepolia.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import "scripts/patch.ts"
import { http } from "viem"
import { sepolia } from "viem/chains"
import { parseArgs } from "node:util"
Expand Down
3 changes: 3 additions & 0 deletions typescript-sdk/playground/union-to-union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const cosmosAccount = await DirectSecp256k1Wallet.fromKey(
"union"
)

const [account] = await cosmosAccount.getAccounts()
if (!account) raise("no account found")

try {
const client = createUnionClient({
account: cosmosAccount,
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions typescript-sdk/src/aptos/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import {
aptosTransferSimulate,
aptosSameChainTransfer,
transferAssetFromAptos,
type AptosTransferParams,
waitForTransactionReceipt,
type AptosPublicAccountInfo
} from "./transfer.ts"
import { err, type Result } from "neverthrow"
import { bech32AddressToHex } from "../convert.ts"
import { cosmosChainId } from "../client/cosmos.ts"
import { cosmosChainId } from "../cosmos/client.ts"
import type { TransferAssetsParameters } from "../types.ts"
import { Aptos, Network, AptosConfig } from "@aptos-labs/ts-sdk"
import { createPfmMemo, getHubbleChainDetails } from "../pfm.ts"
import { createClient, fallback, type HttpTransport } from "viem"
import type { AptosBrowserWallet, AuthAccess } from "./wallet.ts"

export type { AptosAccount, AptosBrowserWallet, AptosTransferParams, AptosPublicAccountInfo }
export type { AptosAccount, AptosBrowserWallet }

export const aptosChainId = ["2"] as const
export type AptosChainId = `${(typeof aptosChainId)[number]}`
Expand Down
17 changes: 10 additions & 7 deletions typescript-sdk/src/aptos/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {
type PendingTransactionResponse,
type InputGenerateTransactionPayloadData
} from "@aptos-labs/ts-sdk"
import type { Prettify } from "../types.ts"
import { err, ok, type Result, ResultAsync } from "neverthrow"
import { isValidBech32Address } from "../utilities/address.ts"
import type { AptosBrowserWallet, AuthAccess } from "./wallet.ts"
import { bech32AddressToHex, bech32ToBytes, hexToBytes } from "../convert.ts"

export type { AptosAccount, AptosTransferParams, AptosPublicAccountInfo }
export type { AptosAccount, AptosPublicAccountInfo }

type AptosPublicAccountInfo = { address: string; publicKey: string }

Expand All @@ -32,10 +33,12 @@ type AptosTransferBaseParams = {
signer?: AptosAccount | AptosBrowserWallet
}

type AptosTransferParams = AptosTransferBaseParams & {
sourceChannel: string
relayContractAddress: string
}
type AptosTransferParams = Prettify<
AptosTransferBaseParams & {
sourceChannel: string
relayContractAddress: string
}
>

export const waitForTransactionReceipt: (args: { aptos: Aptos; hash: string }) => ResultAsync<
string,
Expand Down Expand Up @@ -67,7 +70,7 @@ export const buildSimpleTransaction: (args: {
error => new Error(`Build simple transaction failed`, { cause: error })
)

export const submitSimpleTransaction: (args: {
const submitSimpleTransaction: (args: {
aptos: Aptos
transaction: AnyRawTransaction
accountAuthenticator: AccountAuthenticator
Expand All @@ -80,7 +83,7 @@ export const submitSimpleTransaction: (args: {
error => new Error(`Submit simple transaction failed`, { cause: error })
)

export const simulateSimpleTransaction: (args: {
const simulateSimpleTransaction: (args: {
aptos: Aptos
signerPublicKey: PublicKey
transaction: AnyRawTransaction
Expand Down
11 changes: 5 additions & 6 deletions typescript-sdk/src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isHex } from "viem"
import { raise } from "./utilities/index.ts"
import { bech32, hex, bytes } from "@scure/base"
import type { Bech32Address, HexAddress } from "./types.ts"
Expand All @@ -13,9 +12,8 @@ import { isValidBech32Address } from "./utilities/address.ts"
*/
export function bech32AddressToHex({ address }: { address: string }): HexAddress {
if (!isValidBech32Address(address)) raise(`Invalid bech32 address: ${address}`)
const { words } = bech32.decode(address)
const byteArray = bech32.fromWords(words)
return `0x${bytesToHex(byteArray)}`
const { bytes } = bech32.decodeToBytes(address)
return `0x${bytesToHex(bytes)}`
}

/**
Expand All @@ -32,7 +30,6 @@ export function hexAddressToBech32({
address,
bech32Prefix
}: { address: HexAddress; bech32Prefix: string }): Bech32Address {
if (!isHex(address)) raise("Invalid hex address")
const words = bech32.toWords(hexToBytes(address))
return bech32.encode(bech32Prefix, words, false)
}
Expand Down Expand Up @@ -84,7 +81,9 @@ export function bech32ToBytes(bech32Address: string): Uint8Array {
* bytesToHex(new Uint8Array([1, 2, 3]))
* ```
*/
export const bytesToHex = (bytes: Uint8Array): string => hex.encode(bytes)
export function bytesToHex(bytes: Uint8Array): string {
return hex.encode(bytes)
}

/**
* convert a hex string to a byte array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
cosmosSameChainTransfer,
cosmwasmTransferSimulate,
cosmosSameChainTransferSimulate
} from "../transfer/cosmos.ts"
} from "./transfer.ts"
import { err, type Result } from "neverthrow"
import { timestamp } from "../utilities/index.ts"
import { bech32AddressToHex } from "../convert.ts"
Expand Down
Loading

0 comments on commit 78a6f46

Please sign in to comment.