@cowprotocol/cow-sdk 0.0.8-RC.0 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +102 -23
- package/dist/CowSdk.d.ts +2 -1
- package/dist/api/cow/index.d.ts +1 -1
- package/dist/api/cow/types.d.ts +34 -1
- package/dist/api/cow-subgraph/graphql.d.ts +2603 -0
- package/dist/api/cow-subgraph/index.d.ts +17 -0
- package/dist/api/cow-subgraph/queries.d.ts +3 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/metadata/index.d.ts +3 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +28 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +28 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +28 -1
- package/dist/index.module.js.map +1 -1
- package/dist/utils/context.d.ts +12 -3
- package/dist/utils/ipfs.d.ts +8 -0
- package/package.json +27 -6
- package/.babelrc +0 -4
- package/.eslintrc.json +0 -15
- package/.github/workflows/build.yml +0 -50
- package/.github/workflows/lint.yml +0 -19
- package/.github/workflows/publish.yml +0 -20
- package/.github/workflows/test.yml +0 -47
- package/.nvmrc +0 -1
- package/.prettierignore +0 -1
- package/.prettierrc +0 -5
- package/COPYRIGHT.md +0 -13
- package/LICENSE-APACHE +0 -201
- package/LICENSE-MIT +0 -21
- package/babel.config.js +0 -3
- package/docs/images/CoW.png +0 -0
- package/src/CowSdk.ts +0 -53
- package/src/api/cow/errors/OperatorError.ts +0 -142
- package/src/api/cow/errors/QuoteError.ts +0 -115
- package/src/api/cow/index.ts +0 -367
- package/src/api/cow/types.ts +0 -82
- package/src/api/index.ts +0 -2
- package/src/api/metadata/index.ts +0 -37
- package/src/api/metadata/types.ts +0 -17
- package/src/constants/chains.ts +0 -11
- package/src/constants/index.ts +0 -16
- package/src/constants/tokens.ts +0 -16
- package/src/index.ts +0 -4
- package/src/schemas/appData.schema.json +0 -70
- package/src/types/index.ts +0 -6
- package/src/utils/appData.spec.ts +0 -109
- package/src/utils/appData.ts +0 -58
- package/src/utils/common.ts +0 -35
- package/src/utils/context.ts +0 -89
- package/src/utils/price.ts +0 -44
- package/src/utils/sign.ts +0 -224
- package/src/utils/tokens.ts +0 -12
- package/src/workflows/publish.sh +0 -49
- package/tsconfig.json +0 -17
package/src/utils/sign.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
domain as domainGp,
|
|
3
|
-
signOrder as signOrderGp,
|
|
4
|
-
signOrderCancellation as signOrderCancellationGp,
|
|
5
|
-
EcdsaSignature,
|
|
6
|
-
Order,
|
|
7
|
-
OrderCancellation as OrderCancellationGp,
|
|
8
|
-
Signature,
|
|
9
|
-
TypedDataV3Signer,
|
|
10
|
-
IntChainIdTypedDataV4Signer,
|
|
11
|
-
SigningScheme,
|
|
12
|
-
} from '@gnosis.pm/gp-v2-contracts'
|
|
13
|
-
import log from 'loglevel'
|
|
14
|
-
|
|
15
|
-
import { SupportedChainId as ChainId } from '../constants/chains'
|
|
16
|
-
import { GP_SETTLEMENT_CONTRACT_ADDRESS } from '../constants'
|
|
17
|
-
import { TypedDataDomain, Signer } from '@ethersproject/abstract-signer'
|
|
18
|
-
import { CowError, logPrefix } from './common'
|
|
19
|
-
|
|
20
|
-
// For error codes, see:
|
|
21
|
-
// - https://eth.wiki/json-rpc/json-rpc-error-codes-improvement-proposal
|
|
22
|
-
// - https://www.jsonrpc.org/specification#error_object
|
|
23
|
-
const METAMASK_SIGNATURE_ERROR_CODE = -32603
|
|
24
|
-
const METHOD_NOT_FOUND_ERROR_CODE = -32601
|
|
25
|
-
const V4_ERROR_MSG_REGEX = /eth_signTypedData_v4 does not exist/i
|
|
26
|
-
const V3_ERROR_MSG_REGEX = /eth_signTypedData_v3 does not exist/i
|
|
27
|
-
const RPC_REQUEST_FAILED_REGEX = /RPC request failed/i
|
|
28
|
-
const METAMASK_STRING_CHAINID_REGEX = /provided chainid .* must match the active chainid/i
|
|
29
|
-
|
|
30
|
-
export type UnsignedOrder = Omit<Order, 'receiver'> & { receiver: string }
|
|
31
|
-
|
|
32
|
-
export interface SignOrderParams {
|
|
33
|
-
chainId: ChainId
|
|
34
|
-
signer: Signer
|
|
35
|
-
order: UnsignedOrder
|
|
36
|
-
signingScheme: SigningScheme
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// posted to /api/v1/orders on Order creation
|
|
40
|
-
// serializable, so no BigNumbers
|
|
41
|
-
// See https://protocol-rinkeby.dev.gnosisdev.com/api/
|
|
42
|
-
export interface OrderCreation extends UnsignedOrder {
|
|
43
|
-
signingScheme: SigningScheme // signed method
|
|
44
|
-
|
|
45
|
-
// Signature is used for:
|
|
46
|
-
// - Signature: EIP-712,ETHSIGN
|
|
47
|
-
// - Owner address: for PRESIGN
|
|
48
|
-
signature: string // 65 bytes encoded as hex without `0x` prefix. r + s + v from the spec
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface SingOrderCancellationParams {
|
|
52
|
-
chainId: ChainId
|
|
53
|
-
signer: Signer
|
|
54
|
-
orderId: string
|
|
55
|
-
signingScheme: SigningScheme
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface OrderCancellation extends OrderCancellationGp {
|
|
59
|
-
signature: string
|
|
60
|
-
signingScheme: SigningScheme
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export type SigningSchemeValue = 'eip712' | 'ethsign' | 'eip1271' | 'presign'
|
|
64
|
-
|
|
65
|
-
interface SchemaInfo {
|
|
66
|
-
libraryValue: number
|
|
67
|
-
apiValue: SigningSchemeValue
|
|
68
|
-
}
|
|
69
|
-
const mapSigningSchema: Map<SigningScheme, SchemaInfo> = new Map([
|
|
70
|
-
[SigningScheme.EIP712, { libraryValue: 0, apiValue: 'eip712' }],
|
|
71
|
-
[SigningScheme.ETHSIGN, { libraryValue: 1, apiValue: 'ethsign' }],
|
|
72
|
-
[SigningScheme.EIP1271, { libraryValue: 2, apiValue: 'eip1271' }],
|
|
73
|
-
[SigningScheme.PRESIGN, { libraryValue: 3, apiValue: 'presign' }],
|
|
74
|
-
])
|
|
75
|
-
|
|
76
|
-
function _getSigningSchemeInfo(ecdaSigningScheme: SigningScheme): SchemaInfo {
|
|
77
|
-
const value = mapSigningSchema.get(ecdaSigningScheme)
|
|
78
|
-
if (value === undefined) {
|
|
79
|
-
throw new CowError('Unknown schema ' + ecdaSigningScheme)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return value
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
interface ProviderRpcError extends Error {
|
|
86
|
-
message: string
|
|
87
|
-
code: number
|
|
88
|
-
data?: unknown
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function isProviderRpcError(error: unknown): error is ProviderRpcError {
|
|
92
|
-
return (error as ProviderRpcError).code !== undefined || (error as ProviderRpcError).message !== undefined
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function getSigningSchemeApiValue(ecdaSigningScheme: SigningScheme): string {
|
|
96
|
-
return _getSigningSchemeInfo(ecdaSigningScheme).apiValue
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function getSigningSchemeLibValue(ecdaSigningScheme: SigningScheme): number {
|
|
100
|
-
return _getSigningSchemeInfo(ecdaSigningScheme).libraryValue
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function _getDomain(chainId: ChainId): TypedDataDomain {
|
|
104
|
-
// Get settlement contract address
|
|
105
|
-
const settlementContract = GP_SETTLEMENT_CONTRACT_ADDRESS[chainId]
|
|
106
|
-
|
|
107
|
-
if (!settlementContract) {
|
|
108
|
-
throw new CowError('Unsupported network. Settlement contract is not deployed')
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return domainGp(chainId, settlementContract)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function _signOrder(params: SignOrderParams): Promise<Signature> {
|
|
115
|
-
const { chainId, signer, order, signingScheme } = params
|
|
116
|
-
|
|
117
|
-
const domain = _getDomain(chainId)
|
|
118
|
-
|
|
119
|
-
return signOrderGp(domain, order, signer, getSigningSchemeLibValue(signingScheme))
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function _signOrderCancellation(params: SingOrderCancellationParams): Promise<Signature> {
|
|
123
|
-
const { chainId, signer, signingScheme, orderId } = params
|
|
124
|
-
|
|
125
|
-
const domain = _getDomain(chainId)
|
|
126
|
-
|
|
127
|
-
return signOrderCancellationGp(domain, orderId, signer, getSigningSchemeLibValue(signingScheme))
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type SigningResult = { signature: string; signingScheme: SigningScheme }
|
|
131
|
-
|
|
132
|
-
async function _signPayload(
|
|
133
|
-
payload: any,
|
|
134
|
-
signFn: typeof _signOrder | typeof _signOrderCancellation,
|
|
135
|
-
signer: Signer,
|
|
136
|
-
signingMethod: 'v4' | 'int_v4' | 'v3' | 'eth_sign' = 'v4'
|
|
137
|
-
): Promise<SigningResult> {
|
|
138
|
-
const signingScheme = signingMethod === 'eth_sign' ? SigningScheme.ETHSIGN : SigningScheme.EIP712
|
|
139
|
-
let signature: Signature | null = null
|
|
140
|
-
|
|
141
|
-
let _signer
|
|
142
|
-
try {
|
|
143
|
-
switch (signingMethod) {
|
|
144
|
-
case 'v3':
|
|
145
|
-
_signer = new TypedDataV3Signer(signer)
|
|
146
|
-
break
|
|
147
|
-
case 'int_v4':
|
|
148
|
-
_signer = new IntChainIdTypedDataV4Signer(signer)
|
|
149
|
-
break
|
|
150
|
-
default:
|
|
151
|
-
_signer = signer
|
|
152
|
-
}
|
|
153
|
-
} catch (e) {
|
|
154
|
-
log.error(logPrefix, 'Wallet not supported:', e)
|
|
155
|
-
throw new CowError('Wallet not supported')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
signature = (await signFn({ ...payload, signer: _signer, signingScheme })) as EcdsaSignature // Only ECDSA signing supported for now
|
|
160
|
-
} catch (e) {
|
|
161
|
-
if (!isProviderRpcError(e)) {
|
|
162
|
-
// Some other error signing. Let it bubble up.
|
|
163
|
-
log.error(logPrefix, e)
|
|
164
|
-
throw e
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (e.code === METHOD_NOT_FOUND_ERROR_CODE || RPC_REQUEST_FAILED_REGEX.test(e.message)) {
|
|
168
|
-
// Maybe the wallet returns the proper error code? We can only hope 🤞
|
|
169
|
-
// OR it failed with a generic message, there's no error code set, and we also hope it'll work
|
|
170
|
-
// with other methods...
|
|
171
|
-
switch (signingMethod) {
|
|
172
|
-
case 'v4':
|
|
173
|
-
return _signPayload(payload, signFn, signer, 'v3')
|
|
174
|
-
case 'v3':
|
|
175
|
-
return _signPayload(payload, signFn, signer, 'eth_sign')
|
|
176
|
-
default:
|
|
177
|
-
throw e
|
|
178
|
-
}
|
|
179
|
-
} else if (METAMASK_STRING_CHAINID_REGEX.test(e.message)) {
|
|
180
|
-
// Metamask now enforces chainId to be an integer
|
|
181
|
-
return _signPayload(payload, signFn, signer, 'int_v4')
|
|
182
|
-
} else if (e.code === METAMASK_SIGNATURE_ERROR_CODE) {
|
|
183
|
-
// We tried to sign order the nice way.
|
|
184
|
-
// That works fine for regular MM addresses. Does not work for Hardware wallets, though.
|
|
185
|
-
// See https://github.com/MetaMask/metamask-extension/issues/10240#issuecomment-810552020
|
|
186
|
-
// So, when that specific error occurs, we know this is a problem with MM + HW.
|
|
187
|
-
// Then, we fallback to ETHSIGN.
|
|
188
|
-
return _signPayload(payload, signFn, signer, 'eth_sign')
|
|
189
|
-
} else if (V4_ERROR_MSG_REGEX.test(e.message)) {
|
|
190
|
-
// Failed with `v4`, and the wallet does not set the proper error code
|
|
191
|
-
return _signPayload(payload, signFn, signer, 'v3')
|
|
192
|
-
} else if (V3_ERROR_MSG_REGEX.test(e.message)) {
|
|
193
|
-
// Failed with `v3`, and the wallet does not set the proper error code
|
|
194
|
-
return _signPayload(payload, signFn, signer, 'eth_sign')
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return { signature: signature!.data.toString(), signingScheme }
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Returns the signature for the specified order with the signing scheme encoded
|
|
201
|
-
* into the signature.
|
|
202
|
-
* @export
|
|
203
|
-
* @param {UnsignedOrder} order The order to sign.
|
|
204
|
-
* @param {ChainId} chainId The chain Id
|
|
205
|
-
* @param {Signer} signer The owner for the order used to sign.
|
|
206
|
-
* @return {*} Encoded signature including signing scheme for the order.
|
|
207
|
-
*/
|
|
208
|
-
export async function signOrder(order: UnsignedOrder, chainId: ChainId, signer: Signer): Promise<SigningResult> {
|
|
209
|
-
return _signPayload({ order, chainId }, _signOrder, signer)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Returns the signature for the Order Cancellation with the signing scheme encoded
|
|
214
|
-
* into the signature.
|
|
215
|
-
*
|
|
216
|
-
* @export
|
|
217
|
-
* @param {string} orderId The unique identifier of the order being cancelled.
|
|
218
|
-
* @param {ChainId} chainId The chain Id
|
|
219
|
-
* @param {Signer} signer The owner for the order used to sign.
|
|
220
|
-
* @return {*} Encoded signature including signing scheme for the order.
|
|
221
|
-
*/
|
|
222
|
-
export async function signOrderCancellation(orderId: string, chainId: ChainId, signer: Signer): Promise<SigningResult> {
|
|
223
|
-
return _signPayload({ orderId, chainId }, _signOrderCancellation, signer)
|
|
224
|
-
}
|
package/src/utils/tokens.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { SupportedChainId as ChainId } from '../constants/chains'
|
|
2
|
-
import { NATIVE, WRAPPED_NATIVE_TOKEN } from '../constants/tokens'
|
|
3
|
-
|
|
4
|
-
export function toErc20Address(tokenAddress: string, chainId: ChainId): string {
|
|
5
|
-
let checkedAddress = tokenAddress
|
|
6
|
-
|
|
7
|
-
if (tokenAddress === NATIVE[chainId]) {
|
|
8
|
-
checkedAddress = WRAPPED_NATIVE_TOKEN[chainId].address
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
return checkedAddress
|
|
12
|
-
}
|
package/src/workflows/publish.sh
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
|
|
3
|
-
set -o nounset
|
|
4
|
-
set -o pipefail
|
|
5
|
-
set -o errexit
|
|
6
|
-
|
|
7
|
-
fail_if_unset () {
|
|
8
|
-
local var_name="$1"
|
|
9
|
-
if [[ -z "${!var_name:-""}" ]]; then
|
|
10
|
-
printf '%s not set\n' "$var_name" >&2
|
|
11
|
-
exit 1
|
|
12
|
-
fi
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
package_exists () {
|
|
16
|
-
npm view --json "$1" &>/dev/null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
fail_if_unset NODE_AUTH_TOKEN
|
|
20
|
-
|
|
21
|
-
git_username="GitHub Actions"
|
|
22
|
-
git_useremail="GitHub-Actions@cow.fi"
|
|
23
|
-
|
|
24
|
-
package_name="$(jq --raw-output .name ./package.json)"
|
|
25
|
-
version="$(jq --raw-output .version ./package.json)"
|
|
26
|
-
|
|
27
|
-
if package_exists "$package_name" && grep --silent --line-regexp --fixed-strings -- "$version" \
|
|
28
|
-
<(npm view --json "$package_name" | jq '.versions[] | .' --raw-output); then
|
|
29
|
-
echo "Version $version already published"
|
|
30
|
-
exit 1
|
|
31
|
-
fi
|
|
32
|
-
|
|
33
|
-
version_tag="v$version"
|
|
34
|
-
if git fetch --end-of-options origin "refs/tags/$version_tag" 2>/dev/null; then
|
|
35
|
-
echo "Tag $version_tag is already present"
|
|
36
|
-
exit 1
|
|
37
|
-
fi
|
|
38
|
-
|
|
39
|
-
yarn publish --access public
|
|
40
|
-
|
|
41
|
-
if ! git config --get user.name &>/dev/null; then
|
|
42
|
-
git config user.name "$git_username"
|
|
43
|
-
git config user.email "$git_useremail"
|
|
44
|
-
fi
|
|
45
|
-
git tag -m "Version $version" --end-of-options "$version_tag"
|
|
46
|
-
|
|
47
|
-
git push origin "refs/tags/$version_tag"
|
|
48
|
-
|
|
49
|
-
echo "Package $package_name version $version successfully published."
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"moduleResolution": "node",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"strict": true,
|
|
7
|
-
"alwaysStrict": true,
|
|
8
|
-
"resolveJsonModule": true,
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
|
-
"noEmit": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"noUnusedLocals": true
|
|
13
|
-
},
|
|
14
|
-
"exclude": [
|
|
15
|
-
"dist"
|
|
16
|
-
]
|
|
17
|
-
}
|