fix: fixed the swap message for multihop swaps (#701)

* fix: fixed the swap message for multihop swaps

* fix: according to feedback
This commit is contained in:
Linkie Link 2023-12-20 16:20:21 +01:00 committed by GitHub
parent 532316e955
commit d7bee522fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 24 deletions

View File

@ -30,21 +30,26 @@ function isPromise(object?: any): object is ToastPending {
export function generateToastContent(content: ToastSuccess['content']): ReactNode { export function generateToastContent(content: ToastSuccess['content']): ReactNode {
return content.map((item, index) => ( return content.map((item, index) => (
<div className='flex flex-wrap w-full' key={index}> <div className='flex flex-wrap w-full mb-1' key={index}>
{item.coins.length > 0 && ( {item.text && (
<> <>
<Text size='sm' className='w-full mb-1 text-white'> <Text size='sm' className='w-full mb-1 text-white'>
{item.text} {item.text}
</Text> </Text>
<ul className='flex flex-wrap w-full gap-1 p-1 pl-4 list-disc'> {item.coins.length > 0 && (
{item.coins.map((coin) => <ul className='flex flex-wrap w-full gap-1 p-1 pl-4 list-disc'>
BN(coin.amount).isZero() ? null : ( {item.coins.map((coin, index) => {
<li className='w-full p-0 text-sm text-white' key={coin.denom}> let prefix = ''
{formatAmountWithSymbol(coin)} if (item.text === 'Swapped') prefix = index === 0 ? 'from ' : 'to '
</li>
), return BN(coin.amount).isZero() ? null : (
)} <li className='w-full p-0 text-sm text-white' key={coin.denom}>
</ul> {`${prefix}${formatAmountWithSymbol(coin)}`}
</li>
)
})}
</ul>
)}
</> </>
)} )}
</div> </div>

View File

@ -24,7 +24,6 @@ export default function AssetSelectorPair(props: Props) {
const onChangeTradingPair = useCallback( const onChangeTradingPair = useCallback(
(tradingPair: TradingPair) => { (tradingPair: TradingPair) => {
console.log(tradingPair.buy, tradingPair.sell)
setTradingPairSimple(tradingPair) setTradingPairSimple(tradingPair)
}, },
[setTradingPairSimple], [setTradingPairSimple],

View File

@ -118,6 +118,33 @@ export default function createBroadcastSlice(
}) })
break break
case 'swap':
if (changes.debts) {
toast.content.push({
coins: [changes.debts[0].toCoin()],
text: 'Borrowed',
})
}
if (changes.reclaims) {
toast.content.push({
coins: [changes.reclaims[0].toCoin()],
text: 'Unlent',
})
}
if (changes.swap) {
toast.content.push({
coins: [changes.swap.from, changes.swap.to],
text: 'Swapped',
})
}
if (changes.repays) {
toast.content.push({
coins: [changes.repays[0].toCoin()],
text: 'Repaid',
})
}
break
case 'vault': case 'vault':
case 'vaultCreate': case 'vaultCreate':
toast.content.push({ toast.content.push({
@ -766,6 +793,11 @@ export default function createBroadcastSlice(
options: { options: {
action: 'swap', action: 'swap',
accountId: options.accountId, accountId: options.accountId,
changes: {
reclaims: options.reclaim ? [options.reclaim] : undefined,
debts: options.borrow ? [options.borrow] : undefined,
},
repay: options.repay,
}, },
swapOptions, swapOptions,
}) })
@ -815,9 +847,12 @@ export default function createBroadcastSlice(
const coinOut = getTokenOutFromSwapResponse(response, toast.swapOptions.denomOut) const coinOut = getTokenOutFromSwapResponse(response, toast.swapOptions.denomOut)
if (toast.options.action === 'swap') { if (toast.options.action === 'swap') {
toast.options.message = `Swapped ${formatAmountWithSymbol( if (!toast.options.changes) toast.options.changes = {}
toast.swapOptions.coinIn.toCoin(), toast.options.changes.swap = {
)} for ${formatAmountWithSymbol(coinOut)}` from: toast.swapOptions.coinIn.toCoin(),
to: getTokenOutFromSwapResponse(response, toast.swapOptions.denomOut),
}
if (toast.options.repay) toast.options.changes.repays = [BNCoin.fromCoin(coinOut)]
} }
if (toast.options.action === 'hls-staking') { if (toast.options.action === 'hls-staking') {

View File

@ -16,6 +16,9 @@ export class BNCoin {
static fromDenomAndBigNumber(denom: string, amount: BigNumber) { static fromDenomAndBigNumber(denom: string, amount: BigNumber) {
return new BNCoin({ denom, amount: amount.toString() }) return new BNCoin({ denom, amount: amount.toString() })
} }
static fromCoin(coin: Coin) {
return new BNCoin({ denom: coin.denom, amount: coin.amount.toString() })
}
toCoin(): Coin { toCoin(): Coin {
return { return {

View File

@ -18,6 +18,7 @@ interface ToastObjectOptions extends HandleResponseProps {
interface ToastObject { interface ToastObject {
response: Promise<BroadcastResult> response: Promise<BroadcastResult>
options: ToastObjectOptions options: ToastObjectOptions
swapOptions?: { swapOptions?: {
coinIn: BNCoin coinIn: BNCoin
denomOut: string denomOut: string
@ -74,9 +75,17 @@ interface HandleResponseProps {
| 'hls-staking' | 'hls-staking'
lend?: boolean lend?: boolean
accountId?: string accountId?: string
changes?: { debts?: BNCoin[]; deposits?: BNCoin[]; lends?: BNCoin[] } changes?: {
debts?: BNCoin[]
deposits?: BNCoin[]
lends?: BNCoin[]
reclaims?: BNCoin[]
repays?: BNCoin[]
swap?: { from: Coin; to: Coin }
}
target?: 'wallet' | 'account' target?: 'wallet' | 'account'
message?: string message?: string
repay?: boolean
} }
interface BroadcastSlice { interface BroadcastSlice {

View File

@ -1,3 +1,8 @@
interface Event {
type: string
attributes: { key: string; value: string }[]
}
export default function getTokenOutFromSwapResponse( export default function getTokenOutFromSwapResponse(
response: BroadcastResult, response: BroadcastResult,
denom: string, denom: string,
@ -5,19 +10,26 @@ export default function getTokenOutFromSwapResponse(
try { try {
if (response.result?.response.code === 0) { if (response.result?.response.code === 0) {
const rawLogs = JSON.parse(response.result.rawLogs) const rawLogs = JSON.parse(response.result.rawLogs)
const events = rawLogs[0].events const events = rawLogs[0].events as Event[]
const tokenSwappedEvent = events.find((e: { type: string }) => e.type === 'token_swapped') let tokensOutValue = '0'
const tokensOutValue = tokenSwappedEvent.attributes.find( events.forEach((event: Event) => {
(a: { key: string; value: string }) => const attributes = event.attributes
a.key === 'tokens_out' && a.value.toLowerCase().includes(denom.toLowerCase()), const type = event.type
).value if (type === 'token_swapped') {
const amount = tokensOutValue.split(denom)[0] attributes.forEach((a) => {
if (a.key === 'tokens_out' && a.value.toLowerCase().includes(denom.toLowerCase())) {
tokensOutValue = a.value
}
})
}
})
const amount = tokensOutValue.split(denom)[0]
return { denom, amount } return { denom, amount }
} }
} catch (ex) { } catch (ex) {
console.error(ex) console.error(ex)
} }
return { denom: '', amount: '' } return { denom, amount: '0' }
} }