• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cowprotocol / cow-sdk / 13793952932

11 Mar 2025 05:05PM UTC coverage: 6.624% (-72.2%) from 78.821%
13793952932

Pull #246

github

anxolin
fix: dont use deprecated eth flow contract
Pull Request #246: feat: draft bridging SDK

21 of 405 branches covered (5.19%)

Branch coverage included in aggregate %.

0 of 49 new or added lines in 5 files covered. (0.0%)

596 existing lines in 43 files now uncovered.

62 of 848 relevant lines covered (7.31%)

0.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/composable/utils.ts
1
import { utils, providers, BigNumber } from 'ethers'
2
import {
3
  COMPOSABLE_COW_CONTRACT_ADDRESS,
4
  EXTENSIBLE_FALLBACK_HANDLER_CONTRACT_ADDRESS,
5
  SupportedChainId,
6
} from '../common'
7
import { ExtensibleFallbackHandler__factory } from '../common/generated'
8
import { BlockInfo, ConditionalOrderParams, IsValid, IsValidResult } from './types'
9
import { Order, OrderBalance, OrderKind } from '@cowprotocol/contracts'
10
import { GPv2Order } from '../common/generated/ComposableCoW'
11

UNCOV
12
const ERC20_BALANCE_VALUES = ['erc20', '0x5a28e9363bb942b639270062aa6bb295f434bcdfc42c97267bf003f272060dc9']
×
UNCOV
13
const EXTERNAL_BALANCE_VALUES = ['external', '0xabee3b73373acd583a130924aad6dc38cfdc44ba0555ba94ce2ff63980ea0632']
×
UNCOV
14
const INTERNAL_BALANCE_VALUES = ['internal', '0x4ac99ace14ee0a5ef932dc609df0943ab7ac16b7583634612f8dc35a4289a6ce']
×
UNCOV
15
const SELL_KIND_VALUES = ['sell', '0xf3b277728b3fee749481eb3e0b3b48980dbbab78658fc419025cb16eee346775']
×
UNCOV
16
const BUY_KIND_VALUES = ['buy', '0x6ed88e868af0a1983e3886d5f3e95a2fafbd6c3450bc229e27342283dc429ccc']
×
17

18
// Define the ABI tuple for the ConditionalOrderParams struct
UNCOV
19
export const CONDITIONAL_ORDER_PARAMS_ABI = ['tuple(address handler, bytes32 salt, bytes staticInput)']
×
20

UNCOV
21
export const DEFAULT_TOKEN_FORMATTER = (address: string, amount: BigNumber) => `${amount}@${address}`
×
22

23
export function isExtensibleFallbackHandler(handler: string, chainId: SupportedChainId): boolean {
24
  return handler === EXTENSIBLE_FALLBACK_HANDLER_CONTRACT_ADDRESS[chainId]
×
25
}
26

27
export function isComposableCow(handler: string, chainId: SupportedChainId): boolean {
28
  return handler === COMPOSABLE_COW_CONTRACT_ADDRESS[chainId]
×
29
}
30

31
export async function getDomainVerifier(
32
  safe: string,
33
  domain: string,
34
  chainId: SupportedChainId,
35
  provider: providers.Provider
36
): Promise<string> {
37
  const contract = ExtensibleFallbackHandler__factory.connect(
×
38
    EXTENSIBLE_FALLBACK_HANDLER_CONTRACT_ADDRESS[chainId],
39
    provider
40
  )
41
  return await contract.callStatic.domainVerifiers(safe, domain)
×
42
}
43

44
export function createSetDomainVerifierTx(domain: string, verifier: string): string {
45
  return ExtensibleFallbackHandler__factory.createInterface().encodeFunctionData('setDomainVerifier', [
×
46
    domain,
47
    verifier,
48
  ])
49
}
50

51
/**
52
 * Encode the `ConditionalOrderParams` for the conditional order.
53
 *
54
 * @param params The `ConditionalOrderParams` struct representing the conditional order as taken from a merkle tree.
55
 * @returns The ABI-encoded conditional order.
56
 * @see ConditionalOrderParams
57
 */
58
export function encodeParams(params: ConditionalOrderParams): string {
UNCOV
59
  return utils.defaultAbiCoder.encode(CONDITIONAL_ORDER_PARAMS_ABI, [params])
×
60
}
61

62
/**
63
 * Decode the `ConditionalOrderParams` for the conditional order.
64
 *
65
 * @param encoded The encoded conditional order.
66
 * @returns The decoded conditional order.
67
 */
68
export function decodeParams(encoded: string): ConditionalOrderParams {
UNCOV
69
  const { handler, salt, staticInput } = utils.defaultAbiCoder.decode(CONDITIONAL_ORDER_PARAMS_ABI, encoded)[0]
×
UNCOV
70
  return { handler, salt, staticInput }
×
71
}
72

73
/**
74
 * Helper method for validating ABI types.
75
 * @param types ABI types to validate against.
76
 * @param values The values to validate.
77
 * @returns {boolean} Whether the values are valid ABI for the given types.
78
 */
79
export function isValidAbi(types: readonly (string | utils.ParamType)[], values: any[]): boolean {
UNCOV
80
  try {
×
UNCOV
81
    utils.defaultAbiCoder.encode(types, values)
×
82
  } catch (e) {
UNCOV
83
    return false
×
84
  }
UNCOV
85
  return true
×
86
}
87

88
export async function getBlockInfo(provider: providers.Provider): Promise<BlockInfo> {
UNCOV
89
  const block = await provider.getBlock('latest')
×
90

UNCOV
91
  return {
×
92
    blockNumber: block.number,
93
    blockTimestamp: block.timestamp,
94
  }
95
}
96

97
export function formatEpoch(epoch: number): string {
UNCOV
98
  return new Date(epoch * 1000).toISOString()
×
99
}
100

101
/**
102
 * Convert a balance source/destination hash to a string
103
 *
104
 * @param balance balance source/destination hash
105
 * @returns string representation of the balance
106
 * @throws if the balance is not recognized
107
 */
108
function balanceToString(balance: string) {
UNCOV
109
  if (ERC20_BALANCE_VALUES.includes(balance)) {
×
UNCOV
110
    return OrderBalance.ERC20
×
111
  } else if (EXTERNAL_BALANCE_VALUES.includes(balance)) {
×
112
    return OrderBalance.EXTERNAL
×
113
  } else if (INTERNAL_BALANCE_VALUES.includes(balance)) {
×
114
    return OrderBalance.INTERNAL
×
115
  } else {
116
    throw new Error(`Unknown balance type: ${balance}`)
×
117
  }
118
}
119

120
/**
121
 * Convert an order kind hash to a string
122
 * @param kind of order in hash format
123
 * @returns string representation of the order kind
124
 */
125
function kindToString(kind: string) {
UNCOV
126
  if (SELL_KIND_VALUES.includes(kind)) {
×
UNCOV
127
    return OrderKind.SELL
×
UNCOV
128
  } else if (BUY_KIND_VALUES.includes(kind)) {
×
UNCOV
129
    return OrderKind.BUY
×
130
  } else {
131
    throw new Error(`Unknown kind: ${kind}`)
×
132
  }
133
}
134

135
export function fromStructToOrder(order: GPv2Order.DataStruct): Order {
136
  const {
137
    sellToken,
138
    sellAmount,
139
    buyToken,
140
    buyAmount,
141
    buyTokenBalance,
142
    sellTokenBalance,
143
    feeAmount,
144
    kind,
145
    receiver,
146
    validTo,
147
    partiallyFillable,
148
    appData,
UNCOV
149
  } = order
×
150

UNCOV
151
  return {
×
152
    sellToken,
153
    sellAmount,
154
    buyToken,
155
    buyAmount,
156
    feeAmount,
157
    receiver,
158
    partiallyFillable,
159
    appData,
160
    validTo: Number(validTo),
161
    kind: kindToString(kind.toString()),
162
    sellTokenBalance: balanceToString(sellTokenBalance.toString()),
163
    buyTokenBalance: balanceToString(buyTokenBalance.toString()),
164
  }
165
}
166

167
export function getIsValidResult(result: IsValidResult): result is IsValid {
UNCOV
168
  return result.isValid
×
169
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc