Skip to content

Commit 5e319ce

Browse files
committed
fix(app2): unwrapping when sending from cosmos
1 parent 30c5c57 commit 5e319ce

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

ts-sdk/src/graphql/unwrapped-quote-token.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { fetchDecodeGraphql } from "../utils/graphql-query.js"
44
import { Effect, Schema, Struct, Array as Arr, flow, Option } from "effect"
55
import { graphql } from "gql.tada"
66
import type { ChannelId } from "../schema/channel.js"
7+
import type { Hex } from "../schema/hex.js"
78

89
export const graphqlQuoteTokenUnwrapQuery = (args: {
9-
baseToken: TokenRawDenom
10+
baseToken: Hex // token raw denom
1011
sourceChainId: UniversalChainId
1112
sourceChannelId: ChannelId
1213
}) =>

ts-sdk/src/ucs03/fungible-asset-order.ts

+57-8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export const createCosmosToEvmFungibleAssetOrder = (intent: {
158158
baseToken: string
159159
baseAmount: bigint
160160
quoteAmount: bigint
161+
sourceChainId: UniversalChainId
162+
sourceChannelId: ChannelId
161163
}) =>
162164
Effect.gen(function* () {
163165
yield* guardAgainstZeroAmount(intent)
@@ -176,7 +178,25 @@ export const createCosmosToEvmFungibleAssetOrder = (intent: {
176178
)
177179
)
178180

179-
const quoteToken = yield* predictEvmQuoteToken(toHex(intent.baseToken))
181+
const graphqlDenom = yield* graphqlQuoteTokenUnwrapQuery({
182+
baseToken: ensureHex(intent.baseToken),
183+
sourceChainId: intent.sourceChainId,
184+
sourceChannelId: intent.sourceChannelId
185+
})
186+
187+
yield* Effect.log("graphql quote", graphqlDenom)
188+
let finalQuoteToken: Hex
189+
const unwrapping = Option.isSome(graphqlDenom)
190+
if (unwrapping) {
191+
yield* Effect.log("using the graphql quote token unwrapped", graphqlDenom.value)
192+
finalQuoteToken = graphqlDenom.value
193+
} else {
194+
yield* Effect.log("predicting quote token on chain")
195+
finalQuoteToken = yield* predictEvmQuoteToken(toHex(intent.baseToken))
196+
yield* Effect.log("received quote token onchain", finalQuoteToken)
197+
}
198+
199+
// const quoteToken = yield* predictEvmQuoteToken(toHex(intent.baseToken))
180200

181201
console.log("here", intent)
182202
return yield* S.decode(FungibleAssetOrder)({
@@ -189,8 +209,8 @@ export const createCosmosToEvmFungibleAssetOrder = (intent: {
189209
tokenMeta.symbol,
190210
tokenMeta.name,
191211
tokenMeta.decimals,
192-
0n, // channel if unwrapping
193-
quoteToken,
212+
unwrapping ? BigInt(intent.sourceChannelId) : 0n, // path is source channel when unwrapping, else 0
213+
finalQuoteToken,
194214
intent.quoteAmount
195215
]
196216
})
@@ -205,14 +225,43 @@ export const createCosmosToCosmosFungibleAssetOrder = (intent: {
205225
baseToken: string
206226
baseAmount: bigint
207227
quoteAmount: bigint
228+
sourceChainId: UniversalChainId
229+
sourceChannelId: ChannelId
208230
}) =>
209231
Effect.gen(function* () {
210232
yield* guardAgainstZeroAmount(intent)
211233
const sourceClient = (yield* CosmWasmClientSource).client
212-
const tokenMeta = yield* readCw20TokenInfo(intent.baseToken).pipe(
213-
Effect.provideService(CosmWasmClientContext, { client: sourceClient })
234+
235+
const tokenMeta = yield* pipe(
236+
readCw20TokenInfo(intent.baseToken),
237+
Effect.provideService(CosmWasmClientContext, { client: sourceClient }),
238+
Effect.either,
239+
Effect.map(
240+
Either.getOrElse(() => ({
241+
symbol: intent.baseToken,
242+
name: intent.baseToken,
243+
decimals: 0
244+
}))
245+
)
214246
)
215-
const quoteToken = yield* predictCosmosQuoteToken(toHex(intent.baseToken))
247+
248+
const graphqlDenom = yield* graphqlQuoteTokenUnwrapQuery({
249+
baseToken: ensureHex(intent.baseToken),
250+
sourceChainId: intent.sourceChainId,
251+
sourceChannelId: intent.sourceChannelId
252+
})
253+
254+
yield* Effect.log("graphql quote", graphqlDenom)
255+
let finalQuoteToken: Hex
256+
const unwrapping = Option.isSome(graphqlDenom)
257+
if (unwrapping) {
258+
yield* Effect.log("using the graphql quote token unwrapped", graphqlDenom.value)
259+
finalQuoteToken = graphqlDenom.value
260+
} else {
261+
yield* Effect.log("predicting quote token on chain")
262+
finalQuoteToken = yield* predictCosmosQuoteToken(intent.baseToken)
263+
yield* Effect.log("received quote token onchain", finalQuoteToken)
264+
}
216265

217266
return yield* S.decode(FungibleAssetOrder)({
218267
_tag: "FungibleAssetOrder",
@@ -224,8 +273,8 @@ export const createCosmosToCosmosFungibleAssetOrder = (intent: {
224273
tokenMeta.symbol,
225274
tokenMeta.name,
226275
tokenMeta.decimals,
227-
0n, // channel if unwrapping
228-
quoteToken,
276+
unwrapping ? BigInt(intent.sourceChannelId) : 0n, // path is source channel when unwrapping, else 0
277+
finalQuoteToken,
229278
intent.quoteAmount
230279
]
231280
})

0 commit comments

Comments
 (0)