Last active
May 1, 2020 02:29
-
-
Save Alirun/1bdfda1809262a3d53ad5f5c34dcacb6 to your computer and use it in GitHub Desktop.
SwapRate SDK Release: Example
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
import { Api, Utils } from '@opiumteam/swaprate-js' | |
import { PostOrdersFormRequestBody, PayReceiveType, AggregateType } from '@opiumteam/swaprate-js/lib/Utils/types' | |
const api = new Api() | |
// Define type of product we want to find: AAVE SUPPLY rate | |
const type = Utils.types.ProductType.AAVE | |
const subtype = Utils.types.ProductSubtype.SUPPLY | |
const tokenTitle = 'DAI' | |
// Define type of orders we want to pick up: receive fixed >= 3% | |
const quoteGte = 0.03 | |
// Define order: 1000 DAI nominal order (10% -> 100 DAI margin) | |
const nominal = 1000 | |
async function findTokenAndMaturity() { | |
const config = await api.getMetaConfig() | |
// Find DAI's address | |
const tokenAddress = config.supportedTokens.find(token => token.title === tokenTitle).address | |
// Get 3rd month from now | |
const maturity = config.supportedMaturities[2] | |
return { | |
tokenAddress, | |
maturity | |
} | |
} | |
async function findProduct(tokenAddress: string) { | |
// Get all products | |
const products = await api.getProducts() | |
// Find the one we need | |
return products.find(product => product.token === tokenAddress && product.subtype === subtype && product.type === type) | |
} | |
async function signMessage(message: any) { | |
/** | |
* Sign received blockchain message | |
* | |
* You can sign it using: | |
* 1) Web3 | |
* web3.provider.eth.signTypedData(message) | |
* 2) Utils and Private key (below) | |
*/ | |
const privateKey = Buffer.from('0x00112233', 'hex') | |
return Utils.signMessage({ data: message, privateKey }) | |
} | |
async function login() { | |
// Get login data | |
const loginData = await api.getAuthLoginData() | |
// Sign login data | |
const signature = await signMessage(loginData.data) | |
const publicKey = '0x12345679abcdef...' | |
// Form access token from login data, address and signature | |
return Utils.formAccessToken({ ttl: loginData.ttl, address: publicKey, signature }) | |
} | |
async function placeOrder(productId: string, quote: number, maturity: number) { | |
// Get access token | |
const accessToken = await login() | |
// Prepare order | |
const order: PostOrdersFormRequestBody = { | |
productId, | |
pay: { | |
type: PayReceiveType.FLOATING, | |
rate: null | |
}, | |
receive: { | |
type: PayReceiveType.FIXED, | |
rate: quote | |
}, | |
nominal, | |
maturity, | |
partialFill: true, | |
aggregate: AggregateType.NONE | |
} | |
// Post order in the orderbook and receive blockchain message for signing | |
const formedOrder = await api.postOrderForm(order, accessToken) | |
// Sign order | |
const signature = await signMessage(formedOrder.orderToSign) | |
// Post signed order | |
await api.postOrderSign({ | |
formedOrderId: formedOrder.formedOrderId, | |
signature | |
}, accessToken) | |
} | |
async function start() { | |
/** | |
* Retrieve SwapRate configuration and get the DAI's address and maturity of swaps in 3 month | |
*/ | |
const { tokenAddress, maturity } = await findTokenAndMaturity() | |
/** | |
* Find product with DAI token | |
*/ | |
const product = await findProduct(tokenAddress) | |
/** | |
* Connect to charts (curves) updates to react on changes in the market | |
* | |
* 1) Setup reaction function `onCharts` | |
* 2) Subscribe to changes | |
*/ | |
api.onCharts(charts => { | |
// Find quote for the desired maturity | |
const quote = charts.d.receiveFixed.find(data => data.timestamp === maturity).value | |
// If current quote on product is more than or equal desired, place an order to pick it up | |
if (quote >= quoteGte) { | |
placeOrder(product.productId, quote, maturity) | |
} | |
}) | |
api.subscribeCharts(product.productId) | |
} | |
// Start bot | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment