New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Teton Finance] v4 support #384
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c0a40f
initial v4 support--fill a limit order
alexkroeger 19ec275
build out v4 support
alexkroeger 6fb7688
swap out screenshot
alexkroeger 45572dc
fix chain ID hash issue
alexkroeger 36d3cd9
add mock 0x api swap/quote scenario
alexkroeger 99a7b0e
add 0x API example to readme
alexkroeger File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,119 @@ | ||
import { ContractWrappers, ERC20TokenContract } from '@0x/contract-wrappers'; | ||
import { RfqOrder, SignatureType } from '@0x/protocol-utils'; | ||
import { Web3ProviderEngine } from '@0x/subproviders'; | ||
import { BigNumber, hexUtils } from '@0x/utils'; | ||
import { Web3Wrapper } from '@0x/web3-wrapper'; | ||
import { AxiosInstance } from 'axios'; | ||
import AxiosMockAdapter from 'axios-mock-adapter'; | ||
import * as HttpStatus from 'http-status-codes'; | ||
|
||
import { NETWORK_CONFIGS, TX_DEFAULTS } from './configs'; | ||
import { DECIMALS, MOCK_0x_API_BASE_URL, UNLIMITED_ALLOWANCE_IN_BASE_UNITS } from './constants'; | ||
import { getRandomFutureDateInSeconds } from './utils'; | ||
|
||
/** | ||
* Set up mock responses to hypothetical 0x API calls. | ||
* | ||
*/ | ||
export async function setUpMock0xApiResponsesAsync( | ||
axiosInstance: AxiosInstance, | ||
maker: string, | ||
taker: string, | ||
providerEngine: Web3ProviderEngine, | ||
): Promise<void> { | ||
const axiosMock = new AxiosMockAdapter(axiosInstance); | ||
|
||
await setUpWethZrxMockResponseAsync(axiosMock, providerEngine, maker, taker); | ||
} | ||
|
||
/** | ||
* Set up a mock swap/quote response to buy ZRX with WETH | ||
* | ||
*/ | ||
async function setUpWethZrxMockResponseAsync( | ||
axiosMock: AxiosMockAdapter, | ||
providerEngine: Web3ProviderEngine, | ||
maker: string, | ||
taker: string, | ||
): Promise<void> { | ||
const contractWrappers = new ContractWrappers(providerEngine, { chainId: NETWORK_CONFIGS.chainId }); | ||
const web3Wrapper = new Web3Wrapper(providerEngine); | ||
const exchangeProxyAddress = contractWrappers.contractAddresses.exchangeProxy; | ||
|
||
const zrxTokenAddress = contractWrappers.contractAddresses.zrxToken; | ||
const zrkToken = new ERC20TokenContract(zrxTokenAddress, providerEngine); | ||
const etherTokenAddress = contractWrappers.contractAddresses.etherToken; | ||
const makerAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(5), DECIMALS); | ||
const takerAmount = Web3Wrapper.toBaseUnitAmount(new BigNumber(0.1), DECIMALS); | ||
|
||
// make sure maker has an allowance | ||
await zrkToken | ||
.approve(exchangeProxyAddress, UNLIMITED_ALLOWANCE_IN_BASE_UNITS) | ||
.sendTransactionAsync({ from: maker }); | ||
|
||
// Create the order | ||
const randomExpiration = getRandomFutureDateInSeconds(); | ||
const pool = hexUtils.leftPad(1); | ||
|
||
const rfqOrder: RfqOrder = new RfqOrder({ | ||
chainId: NETWORK_CONFIGS.chainId, | ||
verifyingContract: exchangeProxyAddress, | ||
maker, | ||
taker, | ||
makerToken: zrxTokenAddress, | ||
takerToken: etherTokenAddress, | ||
makerAmount, | ||
takerAmount, | ||
txOrigin: taker, | ||
expiry: randomExpiration, | ||
pool, | ||
salt: new BigNumber(Date.now()), | ||
}); | ||
|
||
// Generate the order hash and sign it | ||
const signature = await rfqOrder.getSignatureWithProviderAsync( | ||
web3Wrapper.getProvider(), | ||
SignatureType.EthSign, | ||
maker, | ||
); | ||
|
||
const callData = contractWrappers.exchangeProxy | ||
.fillRfqOrder(rfqOrder, signature, takerAmount) | ||
.getABIEncodedTransactionData(); | ||
|
||
const mockWethZrxResponse = { | ||
chainId: NETWORK_CONFIGS.chainId, | ||
// If buyAmount was specified in the request it provides the price of | ||
// buyToken in sellToken and vice versa. This price does not include | ||
// the slippage provided in the request above, and therefore represents | ||
// the best possible price. | ||
price: makerAmount.div(takerAmount).toString(), | ||
// The price which must be met or else the entire transaction will | ||
// revert. This price is influenced by the slippagePercentage parameter. | ||
// On-chain sources may encounter price movements from quote to settlement. | ||
// This quote will be totally RFQ, returning the order price. | ||
guaranteedPrice: makerAmount.div(takerAmount).toString(), | ||
to: exchangeProxyAddress, | ||
data: callData, | ||
value: '0', | ||
gas: '200000', | ||
estimatedGas: '200000', | ||
from: taker, | ||
gasPrice: TX_DEFAULTS.gasPrice.toString(), | ||
protocolFee: '0', | ||
minimumProtocolFee: '0', | ||
buyTokenAddress: zrxTokenAddress, | ||
sellTokenAddress: etherTokenAddress, | ||
buyAmount: makerAmount.toString(), | ||
sellAmount: takerAmount.toString(), | ||
sources: [ | ||
{ | ||
name: '0x', | ||
proportion: '1', | ||
}, | ||
], | ||
allowanceTarget: exchangeProxyAddress, | ||
}; | ||
|
||
axiosMock.onGet(`${MOCK_0x_API_BASE_URL}/swap/v1/quote`).reply(HttpStatus.StatusCodes.OK, mockWethZrxResponse); | ||
} |
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the "Fill a 0x API swap/quote response" to this list?