Skip to content

Commit

Permalink
fix: non-pfm transfer (#2966)
Browse files Browse the repository at this point in the history
  • Loading branch information
cor authored Sep 17, 2024
2 parents ee6cad8 + 17d7c5b commit cff86d3
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 91 deletions.
2 changes: 1 addition & 1 deletion app/app.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
packages = {
app = unstablePkgs.buildNpmPackage {
npmDepsHash = "sha256-edh9+hs4uXoXk96Trb2MHbTNNnk89Ocwc0Nqdk3lQoo=";
npmDepsHash = "sha256-8zhT3p1jem0kR6kGQpMGPb8lDPsshzUNSlT5hYuUb7k=";
src = ./.;
sourceRoot = "app";
npmFlags = [ "--legacy-peer-deps" "--ignore-scripts" ];
Expand Down
16 changes: 8 additions & 8 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@tanstack/svelte-query": "^5.55.4",
"@tanstack/svelte-table": "^8.20.5",
"@tanstack/svelte-virtual": "^3.10.7",
"@unionlabs/client": "^0.0.18",
"@unionlabs/client": "^0.0.22",
"@wagmi/connectors": "^5.1.9",
"@wagmi/core": "^2.13.4",
"bits-ui": "^0.21.13",
Expand Down
7 changes: 4 additions & 3 deletions app/src/lib/queries/balance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { raise } from "$lib/utilities/index.ts"
import { getCosmosChainBalances } from "./cosmos.ts"
import { createQueries } from "@tanstack/svelte-query"
import { erc20ReadMulticall } from "./evm/multicall.ts"
import { bytesToBech32Address } from "@unionlabs/client"
import { bech32ToBech32Address } from "@unionlabs/client"
import type { Chain, UserAddresses } from "$lib/types.ts"
import { getBalancesFromAlchemy } from "./evm/alchemy.ts"
import { getBalancesFromRoutescan } from "./evm/routescan.ts"
Expand Down Expand Up @@ -72,10 +72,11 @@ export function userBalancesQuery({
const url = chain.rpcs.filter(rpc => rpc.type === "rest").at(0)?.url
if (!url) raise(`No REST RPC available for chain ${chain.chain_id}`)

const bech32Address = bytesToBech32Address({
const bech32Address = bech32ToBech32Address({
toPrefix: chain.addr_prefix,
bytes: userAddr.cosmos.bytes
address: userAddr.cosmos.canonical
})

return getCosmosChainBalances({ url, walletAddress: bech32Address })
}

Expand Down
7 changes: 5 additions & 2 deletions app/src/lib/utilities/address.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Chain, UserAddresses } from "$lib/types"
import { bytesToBech32Address } from "@unionlabs/client"
import { bech32ToBech32Address } from "@unionlabs/client"

export const userAddrOnChain = (userAddr: UserAddresses, chain?: Chain): string | null => {
if (!chain) return null

if (chain.rpc_type === "cosmos") {
if (userAddr.cosmos?.bytes) {
return bytesToBech32Address({ bytes: userAddr.cosmos.bytes, toPrefix: chain.addr_prefix })
return bech32ToBech32Address({
toPrefix: chain.addr_prefix,
address: userAddr.cosmos.canonical
})
}
console.log("userAddrOnChain got no cosmos address")
return null
Expand Down
13 changes: 9 additions & 4 deletions app/src/lib/wallet/cosmos/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ function createCosmosStore(

export const cosmosStore = createCosmosStore()

export const getCosmosOfflineSigner = (chainId: string): OfflineSigner =>
get(cosmosStore).connectedWallet === "keplr"
? window.keplr?.getOfflineSigner(chainId, { disableBalanceCheck: false })
: window.leap?.getOfflineSigner(chainId, { disableBalanceCheck: false })
export const getCosmosOfflineSigner = ({
chainId,
connectedWallet
}: {
chainId: string
connectedWallet: CosmosWalletId
}): Promise<OfflineSigner> =>
// @ts-expect-error
window[connectedWallet]?.getOfflineSignerAuto(chainId, { disableBalanceCheck: false })

export const userAddrCosmos: Readable<UserAddressCosmos | null> = derived(
[cosmosStore],
Expand Down
17 changes: 8 additions & 9 deletions app/src/routes/transfer/(components)/transfer-form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type EvmChainId,
createUnionClient,
type CosmosChainId,
type OfflineSigner,
evmChainFromChainId,
bech32ToBech32Address
} from "@unionlabs/client"
Expand All @@ -29,6 +30,7 @@ import ChainButton from "./chain-button.svelte"
import { toIsoString } from "$lib/utilities/date"
import AssetsDialog from "./assets-dialog.svelte"
import { truncate } from "$lib/utilities/format.ts"
import { userAddrCosmos } from "$lib/wallet/cosmos"
import Stepper from "$lib/components/stepper.svelte"
import { raise, sleep } from "$lib/utilities/index.ts"
import { userBalancesQuery } from "$lib/queries/balance"
Expand All @@ -41,10 +43,9 @@ import { Button } from "$lib/components/ui/button/index.ts"
import { getSupportedAsset } from "$lib/utilities/helpers.ts"
import CardSectionHeading from "./card-section-heading.svelte"
import ArrowLeftRight from "virtual:icons/lucide/arrow-left-right"
import { parseUnits, formatUnits, type HttpTransport, getAddress } from "viem"
import { getCosmosChainInfo } from "$lib/wallet/cosmos/chain-info.ts"
import { submittedTransfers } from "$lib/stores/submitted-transfers.ts"
import { userAddrCosmos, getCosmosOfflineSigner } from "$lib/wallet/cosmos"
import { parseUnits, formatUnits, type HttpTransport, getAddress } from "viem"
import { type Writable, writable, derived, get, type Readable } from "svelte/store"
import { type TransferState, stepBefore, stepAfter } from "$lib/transfer/transfer.ts"
Expand Down Expand Up @@ -143,10 +144,6 @@ let receiver = derived([toChain, userAddress], ([$toChain, $userAddress]) => {
}
})
$: {
console.info($receiver)
}
let ucs01Configuration = derived(
[fromChain, toChainId, receiver],
([$fromChain, $toChainId, $receiver]) => {
Expand Down Expand Up @@ -234,7 +231,7 @@ const transfer = async () => {
return
}
const wallet = window[connectedWallet as "keplr" | "leap"]
const wallet = window[connectedWallet]
if (!wallet) {
transferState.set({
Expand All @@ -244,6 +241,10 @@ const transfer = async () => {
return
}
const cosmosOfflineSigner = (await wallet.getOfflineSignerAuto($fromChainId, {
disableBalanceCheck: false
})) as OfflineSigner
// @ts-ignore
transferState.set({ kind: "SWITCHING_TO_CHAIN" })
Expand Down Expand Up @@ -284,8 +285,6 @@ const transfer = async () => {
if (stepBefore($transferState, "TRANSFERRING")) {
try {
const cosmosOfflineSigner = getCosmosOfflineSigner($fromChainId)
const unionClient = createUnionClient({
account: cosmosOfflineSigner,
transport: http(`https://${rpcUrl}`),
Expand Down
Binary file modified typescript-sdk/bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion typescript-sdk/jsr.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@union/client",
"version": "0.0.19",
"version": "0.0.22",
"license": "MIT",
"exports": {
".": "./src/mod.ts"
Expand Down
8 changes: 4 additions & 4 deletions typescript-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unionlabs/client",
"version": "0.0.19",
"version": "0.0.22",
"homepage": "https://jsr.io/@union/client",
"description": "Union Labs cross-chain transfers client",
"type": "module",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@scure/base": "1.1.8",
"neverthrow": "^8.0.0",
"ofetch": "^1.3.4",
"viem": "^2.21.6"
"viem": "^2.21.7"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.16.2",
Expand All @@ -45,7 +45,7 @@
"@cosmjs/tendermint-rpc": "^0.32.4",
"@total-typescript/ts-reset": "^0.6.1",
"@types/bun": "^1.1.9",
"@types/node": "^22.5.4",
"@types/node": "^22.5.5",
"consola": "^3.2.3",
"cosmjs-types": "^0.9.0",
"jsr": "^0.13.2",
Expand All @@ -54,7 +54,7 @@
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.0"
"vitest": "^2.1.1"
},
"repository": {
"type": "git",
Expand Down
3 changes: 0 additions & 3 deletions typescript-sdk/playground/stride-to-berachain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ try {
amount: 1n,
autoApprove: true,
denomAddress: "ustrd",
// or `client.evm.account.address` if you want to send to yourself
receiver: berachainAccount.address,
destinationChainId: `${berachainTestnetbArtio.id}`
} satisfies TransferAssetsParameters<"stride-internal-1">

consola.info(transactionPayload)

const gasEstimationResponse = await client.simulateTransaction(transactionPayload)

if (gasEstimationResponse.isErr()) {
Expand Down
12 changes: 5 additions & 7 deletions typescript-sdk/playground/stride-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bun
import "#patch.ts"
import { http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
Expand Down Expand Up @@ -31,17 +32,14 @@ try {
const client = createUnionClient({
account: cosmosAccount,
chainId: "stride-internal-1",
gasPrice: { amount: "0.0025", denom: "strd" },
transport: http(
//
// "https://stride.testnet-1.stridenet.co/"
"https://stride-testnet-rpc.polkachu.com/"
)
gasPrice: { amount: "0.0025", denom: "ustrd" },
transport: http("https://stride-testnet-rpc.polkachu.com")
})

const transactionPayload = {
amount: 1n,
denomAddress: "strd",
autoApprove: true,
denomAddress: "ustrd",
destinationChainId: "union-testnet-8",
receiver: "union14qemq0vw6y3gc3u3e0aty2e764u4gs5lnxk4rv"
} satisfies TransferAssetsParameters<"stride-internal-1">
Expand Down
52 changes: 52 additions & 0 deletions typescript-sdk/playground/union-to-stride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bun
import { http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { createUnionClient } from "#mod.ts"
import { raise } from "#utilities/index.ts"
import { hexStringToUint8Array } from "#convert.ts"
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"

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

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

const PRIVATE_KEY = values["private-key"]
if (!PRIVATE_KEY) raise("Private key not found")
const ONLY_ESTIMATE_GAS = values["estimate-gas"] ?? false

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

try {
const client = createUnionClient({
account: cosmosAccount,
chainId: "union-testnet-8",
gasPrice: { amount: "0.0025", denom: "muno" },
transport: http("https://rpc.testnet-8.union.build")
})

const transfer = await client.transferAsset({
amount: 1n,
autoApprove: true,
denomAddress: "muno",
destinationChainId: "stride-internal-1",
receiver: "stride14qemq0vw6y3gc3u3e0aty2e764u4gs5l66hpe3"
})

consola.info(transfer)
process.exit(0)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
console.error(errorMessage)
} finally {
process.exit(0)
}
Loading

0 comments on commit cff86d3

Please sign in to comment.