Skip to content

Commit

Permalink
Merge pull request #3004 from cowprotocol/release/1.44-rc1
Browse files Browse the repository at this point in the history
Release 1.44-RC.1
  • Loading branch information
anxolin authored Aug 4, 2023
2 parents 244c3d6 + 651cc84 commit 02f7926
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/common/hooks/useGetSurplusFiatValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ function shouldShowSurplus(
): boolean | null {
if (fiatAmount) {
// When there's a fiat amount, use that to decide whether to display the modal
return Number(fiatAmount.toFixed(3)) > MIN_FIAT_SURPLUS_VALUE_MODAL
return Number(fiatAmount.toFixed(fiatAmount.currency.decimals)) > MIN_FIAT_SURPLUS_VALUE_MODAL
} else if (surplusAmount) {
// If no fiat value, check whether surplus units are > MIN_SURPLUS_UNITS
return Number(surplusAmount.toFixed(3)) > MIN_SURPLUS_UNITS
return Number(surplusAmount.toFixed(surplusAmount.currency.decimals)) > MIN_SURPLUS_UNITS
}

// Otherwise, we don't know whether surplus should, return `null` to indicate that
Expand Down
4 changes: 3 additions & 1 deletion src/modules/appData/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export interface UploadAppDataProps {
*/
export const uploadAppDataDocOrderbookApi: UploadAppDataDoc = async (props) => {
const { appDataKeccak256, fullAppData, chainId, env } = props
orderBookApi.uploadAppData(appDataKeccak256, fullAppData, { chainId, env })

const contextOverride = env ? { chainId, env } : { chainId }
orderBookApi.uploadAppData(appDataKeccak256, fullAppData, contextOverride)
}
8 changes: 2 additions & 6 deletions src/modules/swap/services/safeBundleFlow/safeBundleEthFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export async function safeBundleEthFlow(
spender,
context,
callbacks,
appDataInfo,
swapConfirmManager,
dispatch,
orderParams,
Expand Down Expand Up @@ -124,13 +123,10 @@ export async function safeBundleEthFlow(
)
tradeFlowAnalytics.sign(swapFlowAnalyticsContext)

logTradeFlow(LOG_PREFIX, 'STEP 8: add app data to upload queue')
callbacks.uploadAppData({ chainId: context.chainId, orderId, appData: appDataInfo })

logTradeFlow(LOG_PREFIX, 'STEP 9: show UI of the successfully sent transaction')
logTradeFlow(LOG_PREFIX, 'STEP 8: show UI of the successfully sent transaction')
swapConfirmManager.transactionSent(orderId)
} catch (error) {
logTradeFlow(LOG_PREFIX, 'STEP 10: error', error)
logTradeFlow(LOG_PREFIX, 'STEP 9: error', error)
const swapErrorMessage = getSwapErrorMessage(error)

tradeFlowAnalytics.error(error, swapErrorMessage, swapFlowAnalyticsContext)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/twap/hooks/useFetchTwapOrdersFromSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useSafeApiKit } from 'api/gnosisSafe/hooks/useSafeApiKit'
import { fetchTwapOrdersFromSafe } from '../services/fetchTwapOrdersFromSafe'
import { TwapOrdersSafeData } from '../types'

const PENDING_TWAP_UPDATE_INTERVAL = ms`10s`
const PENDING_TWAP_UPDATE_INTERVAL = ms`15s`

export function useFetchTwapOrdersFromSafe({
safeAddress,
Expand Down
60 changes: 56 additions & 4 deletions src/modules/twap/services/fetchTwapOrdersFromSafe.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,76 @@
import { ComposableCoW } from '@cowprotocol/abis'
import type SafeApiKit from '@safe-global/api-kit'
import type { AllTransactionsListResponse } from '@safe-global/api-kit'
import type { SafeMultisigTransactionResponse } from '@safe-global/safe-core-sdk-types'

import { isTruthy } from 'legacy/utils/misc'
import ms from 'ms.macro'

import { delay, isTruthy } from 'legacy/utils/misc'

import { SafeTransactionParams } from 'common/types'

import { ConditionalOrderParams, TwapOrdersSafeData } from '../types'

// ComposableCoW.createWithContext method
const CREATE_COMPOSABLE_ORDER_SELECTOR = '0d0d9800'
// Each page contains 20 transactions by default, so we need to fetch 10 pages to get 200 transactions
const SAFE_TX_HISTORY_DEPTH = 10
// Just in case, make a short delay between requests
const SAFE_TX_REQUEST_DELAY = ms`100ms`

export async function fetchTwapOrdersFromSafe(
safeAddress: string,
safeApiKit: SafeApiKit,
composableCowContract: ComposableCoW
composableCowContract: ComposableCoW,
/**
* Example of the second chunk url:
* https://safe-transaction-goerli.safe.global/api/v1/safes/0xe9B79591E270B3bCd0CC7e84f7B7De74BA3D0E2F/all-transactions/?executed=false&limit=20&offset=40&queued=true&trusted=true
*/
nextUrl?: string,
accumulator: TwapOrdersSafeData[][] = []
): Promise<TwapOrdersSafeData[]> {
const allTxs = await safeApiKit.getAllTransactions(safeAddress)
const results = allTxs?.results || []
const response = await fetchSafeTransactionsChunk(safeAddress, safeApiKit, nextUrl)

const results = response?.results || []
const parsedResults = parseSafeTranasctionsResult(safeAddress, composableCowContract, results)

accumulator.push(parsedResults)

// Exit from the recursion if we have enough transactions or there is no next page
if (accumulator.length >= SAFE_TX_HISTORY_DEPTH || !response?.next) {
return accumulator.flat()
}

return fetchTwapOrdersFromSafe(safeAddress, safeApiKit, composableCowContract, response.next, accumulator)
}

async function fetchSafeTransactionsChunk(
safeAddress: string,
safeApiKit: SafeApiKit,
nextUrl?: string
): Promise<AllTransactionsListResponse> {
if (nextUrl) {
try {
const response: AllTransactionsListResponse = await fetch(nextUrl).then((res) => res.json())

await delay(SAFE_TX_REQUEST_DELAY)

return response
} catch (error) {
console.error('Error fetching Safe transactions', { safeAddress, nextUrl }, error)

return { results: [], count: 0 }
}
}

return safeApiKit.getAllTransactions(safeAddress)
}

function parseSafeTranasctionsResult(
safeAddress: string,
composableCowContract: ComposableCoW,
results: AllTransactionsListResponse['results']
): TwapOrdersSafeData[] {
return results
.map<TwapOrdersSafeData | null>((result) => {
if (!result.data || !isSafeMultisigTransactionListResponse(result)) return null
Expand Down

0 comments on commit 02f7926

Please sign in to comment.