-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sdk extension for people to swap (#213)
* feat: SDK extension for people to swap * feat: SDK extension for people to trade * refactor: move files to trading dir * feat: js example for trade-sdk * chore: trade sdk examples * docs: trading SDK docs * chore: encapsulate networkCostsAmount * chore: fix build * chore: fix build * chore: make networkCostsAmount optional * feat: calculate unique OrderId util * feat: support on-chain trades * chore: reduce getOrderToSign parameters * feat: support on-chain orders * test: app data utils * test: test calculateUniqueOrderId * test: test getOrderToSign * test: test getQuote * test: test postCoWProtocolTrade * test: test postLimitOrder * test: test postOnChainTrade * docs: docs for postOnChainTrade * chore: up docs * fix: update eth-flow addresses * chore: fix tests * chore: export helpers * chore: rc version * chore: fix readme * feat: avoid using signer in getQuote * chore: rc * chore: move QuoteResults to types * refactor: make QuoteResults serializable * feat: add json schemas for trading entities * chore: rc * chore: up node v * chore: remove schema gen call * chore: up app-data version * chore: bump version * feat: add order typed data to quote results * fix: fix order signing * fix: override sellToken with wrapped for eth-flow * feat(trading): function to generate eth-flow transaction * chore: export swapParamsToLimitOrderParams * fix: use hex numbers in transaction * fix(trading): fix order amounts calculation * chore: bump rc * chore: fix tests * chore: add mapQuoteAmountsAndCosts util * chore: fix types * chore: do not cache ethflow contract * test: test getEthFlowTransaction * feat(trading): add getPreSignTransaction function * chore: add more description to trading types * chore: fix test * chore: update trading sdk readme * chore: up rc * chore: fix base contract addresses * fix: make quoteId optional for limit orders * refactor: rename postOnChainTrade -> postSellNativeCurrencyTrade * docs: optional params * chore: fix tests * fix: add additional params for eth-flow quote req * feat: add getPreSignTransaction to TradingSdk * docs: get quest for smart-contract wallet * chore: rename postSellNativeCurrencyOrder * docs: create an order with smart-contract wallet * chore: fix tests
- Loading branch information
Showing
55 changed files
with
4,097 additions
and
463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ on: | |
types: [published] | ||
|
||
env: | ||
NODE_VERSION: lts/gallium | ||
NODE_VERSION: lts/hydrogen | ||
|
||
jobs: | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ on: | |
types: [published] | ||
|
||
env: | ||
NODE_VERSION: lts/gallium | ||
NODE_VERSION: lts/hydrogen | ||
|
||
jobs: | ||
deploy: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"components": [ | ||
{ | ||
"internalType": "contract IERC20", | ||
"name": "buyToken", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "receiver", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "sellAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "buyAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "bytes32", | ||
"name": "appData", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "feeAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "validTo", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"internalType": "bool", | ||
"name": "partiallyFillable", | ||
"type": "bool" | ||
}, | ||
{ | ||
"internalType": "int64", | ||
"name": "quoteId", | ||
"type": "int64" | ||
} | ||
], | ||
"internalType": "struct EthFlowOrder.Data", | ||
"name": "order", | ||
"type": "tuple" | ||
} | ||
], | ||
"name": "createOrder", | ||
"outputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "orderHash", | ||
"type": "bytes32" | ||
} | ||
], | ||
"stateMutability": "payable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"components": [ | ||
{ | ||
"internalType": "contract IERC20", | ||
"name": "buyToken", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "address", | ||
"name": "receiver", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "sellAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "buyAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "bytes32", | ||
"name": "appData", | ||
"type": "bytes32" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "feeAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "validTo", | ||
"type": "uint32" | ||
}, | ||
{ | ||
"internalType": "bool", | ||
"name": "partiallyFillable", | ||
"type": "bool" | ||
}, | ||
{ | ||
"internalType": "int64", | ||
"name": "quoteId", | ||
"type": "int64" | ||
} | ||
], | ||
"internalType": "struct EthFlowOrder.Data", | ||
"name": "order", | ||
"type": "tuple" | ||
} | ||
], | ||
"name": "invalidateOrder", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes32", | ||
"name": "", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "orders", | ||
"outputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "owner", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint32", | ||
"name": "validTo", | ||
"type": "uint32" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
[ | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "owner", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "contract IERC20", | ||
"name": "sellToken", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "contract IERC20", | ||
"name": "buyToken", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint256", | ||
"name": "sellAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint256", | ||
"name": "buyAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint256", | ||
"name": "feeAmount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "bytes", | ||
"name": "orderUid", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "Trade", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes", | ||
"name": "orderUid", | ||
"type": "bytes" | ||
}, | ||
{ | ||
"internalType": "bool", | ||
"name": "signed", | ||
"type": "bool" | ||
} | ||
], | ||
"name": "setPreSignature", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "bytes", | ||
"name": "orderUid", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "invalidateOrder", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "domainSeparator", | ||
"outputs": [{ "name": "", "type": "bytes32" }], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx" | ||
}, | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,69 @@ | ||
import { OrderBookApi, SubgraphApi, SupportedChainId } from '@cowprotocol/cow-sdk' | ||
import { SupportedChainId, OrderKind, postSwapOrder, postLimitOrder } from '../../../src' | ||
|
||
// See more examples in /examples/cra | ||
const privateKey = 'xxx' | ||
|
||
// Swap | ||
;(async function () { | ||
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.MAINNET }) | ||
const subgraphApi = new SubgraphApi({ chainId: SupportedChainId.MAINNET }) | ||
return | ||
|
||
const order = await orderBookApi.getOrder( | ||
'0xff2e2e54d178997f173266817c1e9ed6fee1a1aae4b43971c53b543cffcc2969845c6f5599fbb25dbdd1b9b013daf85c03f3c63763e4bc4a' | ||
) | ||
postSwapOrder({ | ||
appCode: 'cow-sdk-example', | ||
signer: privateKey, | ||
chainId: SupportedChainId.SEPOLIA, | ||
|
||
kind: OrderKind.SELL, | ||
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14', | ||
sellTokenDecimals: 18, | ||
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59', | ||
buyTokenDecimals: 18, | ||
amount: '120000000000000000', | ||
}) | ||
})() | ||
|
||
// Limit order | ||
;(async function () { | ||
return | ||
|
||
const lastDaysVolume = await subgraphApi.getTotals() | ||
postLimitOrder({ | ||
appCode: 'cow-sdk-example', | ||
signer: privateKey, | ||
chainId: SupportedChainId.SEPOLIA, | ||
|
||
console.log('[orderBookApi] Order: ', order) | ||
console.log('[subgraphApi] Last day volume: ', lastDaysVolume) | ||
kind: OrderKind.BUY, | ||
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14', | ||
sellTokenDecimals: 18, | ||
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59', | ||
buyTokenDecimals: 18, | ||
sellAmount: '120000000000000000', | ||
buyAmount: '66600000000000000000', | ||
networkCostsAmount: '0', | ||
}) | ||
})() | ||
|
||
// Swap with partner fee | ||
;(async function () { | ||
postSwapOrder( | ||
{ | ||
appCode: 'cow-sdk-example', | ||
signer: privateKey, | ||
chainId: SupportedChainId.SEPOLIA, | ||
|
||
kind: OrderKind.SELL, | ||
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14', | ||
sellTokenDecimals: 18, | ||
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59', | ||
buyTokenDecimals: 18, | ||
amount: '120000000000000000', | ||
}, | ||
{ | ||
appData: { | ||
metadata: { | ||
partnerFee: { | ||
bps: 100, | ||
recipient: '0xfb3c7eb936cAA12B5A884d612393969A557d4307', | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.