Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { SettlementMethod } from '@ston-fi/omniston-sdk'
import type { CommonTradeQuoteInput, TradeQuote, TradeQuoteResult } from '../../../types'
import { SwapperName, TradeQuoteError } from '../../../types'
import { makeSwapErrorRight } from '../../../utils'
import { getTreasuryAddressFromChainId } from '../../utils/helpers/helpers'
import type { OmnistonAssetAddress, StonfiTradeSpecific } from '../types'
import { STONFI_DEFAULT_SLIPPAGE_BPS, STONFI_QUOTE_TIMEOUT_MS } from '../utils/constants'
import {
affiliateBpsToNumber,
calculateRate,
slippageDecimalToBps,
tonAddressToOmnistonAddress,
validateTonAssets,
waitForQuote,
} from '../utils/helpers'
Expand Down Expand Up @@ -47,6 +50,7 @@ export const getTradeQuote = async (input: CommonTradeQuoteInput): Promise<Trade
receiveAddress,
accountNumber,
slippageTolerancePercentageDecimal,
affiliateBps,
} = input

const validation = validateTonAssets(sellAsset, buyAsset)
Expand All @@ -63,16 +67,23 @@ export const getTradeQuote = async (input: CommonTradeQuoteInput): Promise<Trade
STONFI_DEFAULT_SLIPPAGE_BPS,
)

const referrerFeeBps = affiliateBpsToNumber(affiliateBps)
const tonTreasuryAddress = getTreasuryAddressFromChainId(sellAsset.chainId)
const referrerAddress = tonAddressToOmnistonAddress(tonTreasuryAddress)

const quoteResult = await waitForQuote(
omniston,
{
settlementMethods: [SettlementMethod.SETTLEMENT_METHOD_SWAP],
bidAssetAddress,
askAssetAddress,
amount: { bidUnits: sellAmountIncludingProtocolFeesCryptoBaseUnit },
referrerAddress,
referrerFeeBps,
settlementParams: {
maxPriceSlippageBps: slippageBps,
gaslessSettlement: 'GASLESS_SETTLEMENT_PROHIBITED',
flexibleReferrerFee: true,
},
},
STONFI_QUOTE_TIMEOUT_MS,
Expand Down Expand Up @@ -122,7 +133,7 @@ export const getTradeQuote = async (input: CommonTradeQuoteInput): Promise<Trade
id: quote.quoteId,
rate,
receiveAddress,
affiliateBps: '0',
affiliateBps,
slippageTolerancePercentageDecimal:
slippageTolerancePercentageDecimal ?? String(STONFI_DEFAULT_SLIPPAGE_BPS / 10000),
quoteOrRate: 'quote',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { SettlementMethod } from '@ston-fi/omniston-sdk'
import type { GetTradeRateInput, TradeRate, TradeRateResult } from '../../../types'
import { SwapperName, TradeQuoteError } from '../../../types'
import { makeSwapErrorRight } from '../../../utils'
import { getTreasuryAddressFromChainId } from '../../utils/helpers/helpers'
import type { OmnistonAssetAddress } from '../types'
import { STONFI_DEFAULT_SLIPPAGE_BPS, STONFI_QUOTE_TIMEOUT_MS } from '../utils/constants'
import {
affiliateBpsToNumber,
calculateRate,
slippageDecimalToBps,
tonAddressToOmnistonAddress,
validateTonAssets,
waitForQuote,
} from '../utils/helpers'
Expand Down Expand Up @@ -46,6 +49,7 @@ export const getTradeRate = async (input: GetTradeRateInput): Promise<TradeRateR
sellAmountIncludingProtocolFeesCryptoBaseUnit,
receiveAddress,
slippageTolerancePercentageDecimal,
affiliateBps,
} = input

const validation = validateTonAssets(sellAsset, buyAsset)
Expand All @@ -62,16 +66,23 @@ export const getTradeRate = async (input: GetTradeRateInput): Promise<TradeRateR
STONFI_DEFAULT_SLIPPAGE_BPS,
)

const referrerFeeBps = affiliateBpsToNumber(affiliateBps)
const tonTreasuryAddress = getTreasuryAddressFromChainId(sellAsset.chainId)
const referrerAddress = tonAddressToOmnistonAddress(tonTreasuryAddress)

const quoteResult = await waitForQuote(
omniston,
{
settlementMethods: [SettlementMethod.SETTLEMENT_METHOD_SWAP],
bidAssetAddress,
askAssetAddress,
amount: { bidUnits: sellAmountIncludingProtocolFeesCryptoBaseUnit },
referrerAddress,
referrerFeeBps,
settlementParams: {
maxPriceSlippageBps: slippageBps,
gaslessSettlement: 'GASLESS_SETTLEMENT_PROHIBITED',
flexibleReferrerFee: true,
},
},
STONFI_QUOTE_TIMEOUT_MS,
Expand Down Expand Up @@ -101,7 +112,7 @@ export const getTradeRate = async (input: GetTradeRateInput): Promise<TradeRateR
id: quote.quoteId,
rate,
receiveAddress,
affiliateBps: '0',
affiliateBps,
slippageTolerancePercentageDecimal:
slippageTolerancePercentageDecimal ?? String(STONFI_DEFAULT_SLIPPAGE_BPS / 10000),
quoteOrRate: 'rate',
Expand Down
14 changes: 14 additions & 0 deletions packages/swapper/src/swappers/StonfiSwapper/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ export const slippageDecimalToBps = (
return Math.round(parsed * 10000)
}

export const tonAddressToOmnistonAddress = (address: string): OmnistonAssetAddress => {
return {
blockchain: Blockchain.TON,
address,
}
}

export const affiliateBpsToNumber = (affiliateBps: string | undefined): number => {
if (!affiliateBps) return 0
const parsed = parseInt(affiliateBps, 10)
if (Number.isNaN(parsed) || !Number.isFinite(parsed) || parsed < 0) return 0
return parsed
}

export const waitForQuote = (
omniston: Omniston,
request: QuoteRequest,
Expand Down
2 changes: 2 additions & 0 deletions packages/swapper/src/swappers/utils/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
DAO_TREASURY_POLYGON,
DAO_TREASURY_SOLANA,
DAO_TREASURY_STARKNET,
DAO_TREASURY_TON,
isTreasuryChainId,
} from '@shapeshiftoss/utils'

Expand Down Expand Up @@ -72,6 +73,7 @@ const DAO_TREASURY_BY_CHAIN_ID: Record<TreasuryChainId, string> = {
[KnownChainIds.SolanaMainnet]: DAO_TREASURY_SOLANA,
[KnownChainIds.BitcoinMainnet]: DAO_TREASURY_BITCOIN,
[KnownChainIds.StarknetMainnet]: DAO_TREASURY_STARKNET,
[KnownChainIds.TonMainnet]: DAO_TREASURY_TON,
}

export const getTreasuryAddressFromChainId = (chainId: ChainId): string => {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/treasury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const treasuryChainIds = [
KnownChainIds.SolanaMainnet,
KnownChainIds.BitcoinMainnet,
KnownChainIds.StarknetMainnet,
KnownChainIds.TonMainnet,
] as const

export type TreasuryChainId = (typeof treasuryChainIds)[number]
Expand All @@ -38,3 +39,4 @@ export const DAO_TREASURY_BITCOIN = 'bc1qr2whxtd0gvqnctcxlynwejp6fvntv0mtxkv0dlv
export const DAO_TREASURY_NEAR = 'f471d0b0f90593d85125f38aaf5458748d6f23fd5b437b844d293d8e87557070'
export const DAO_TREASURY_STARKNET =
'0x052a1132ea4db81Bde863AFb18a4d4CE5de9d3efdfda6b3DaA6484e26425D467'
export const DAO_TREASURY_TON = 'UQBGXUskbTDkLXJO_Q6cQFVbbkgvXKplcIhijiO5oDcB5qkI'
Loading