-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
404 additions
and
7 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
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,20 @@ | ||
import prompts from 'prompts' | ||
import { swapParametersSchema } from '../consts' | ||
import { SwapParameters } from '../../types' | ||
import { getQuote } from '../../getQuote' | ||
import kleur from 'kleur' | ||
import { printJSON } from '../utils' | ||
|
||
export async function getQuoteAction(hasArgv: boolean) { | ||
const params = (await prompts(swapParametersSchema)) as SwapParameters | ||
|
||
const quote = await getQuote(params) | ||
|
||
if (hasArgv) { | ||
console.log(printJSON(quote)) | ||
return | ||
} | ||
|
||
console.log(kleur.green().bold('Quote: ')) | ||
console.log(kleur.white().underline(printJSON(quote))) | ||
} |
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,19 @@ | ||
import prompts from 'prompts' | ||
import { limitParametersSchema } from '../consts' | ||
import { LimitOrderParameters } from '../../types' | ||
import kleur from 'kleur' | ||
import { postLimitOrder } from '../../postLimitOrder' | ||
|
||
export async function postLimitOrderAction(hasArgv: boolean) { | ||
const params = (await prompts(limitParametersSchema)) as LimitOrderParameters | ||
|
||
const orderId = await postLimitOrder(params) | ||
|
||
if (hasArgv) { | ||
console.log(orderId) | ||
return | ||
} | ||
|
||
console.log(kleur.green().bold('Order id: ')) | ||
console.log(kleur.white().underline(orderId)) | ||
} |
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,19 @@ | ||
import prompts from 'prompts' | ||
import { swapParametersSchema } from '../consts' | ||
import { SwapParameters } from '../../types' | ||
import kleur from 'kleur' | ||
import { postSwapOrder } from '../../postSwapOrder' | ||
|
||
export async function postSwapOrderAction(hasArgv: boolean) { | ||
const params = (await prompts(swapParametersSchema)) as SwapParameters | ||
|
||
const orderId = await postSwapOrder(params) | ||
|
||
if (hasArgv) { | ||
console.log(orderId) | ||
return | ||
} | ||
|
||
console.log(kleur.green().bold('Order id: ')) | ||
console.log(kleur.white().underline(orderId)) | ||
} |
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,165 @@ | ||
import { PromptObject } from 'prompts' | ||
import { ALL_SUPPORTED_CHAIN_IDS, SupportedChainId } from '../../common' | ||
import { OrderKind } from '../../order-book' | ||
|
||
export const traderParametersSchema: PromptObject[] = [ | ||
{ | ||
type: 'select', | ||
name: 'chainId', | ||
message: 'Network', | ||
choices: ALL_SUPPORTED_CHAIN_IDS.map((key) => ({ | ||
title: key.toString(), | ||
value: SupportedChainId[key], | ||
})), | ||
initial: SupportedChainId.MAINNET, | ||
}, | ||
{ | ||
type: 'password', | ||
name: 'signer', | ||
message: 'Signer`s private key', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'appCode', | ||
message: 'Your app code (for analytics purposes)', | ||
}, | ||
] | ||
|
||
export const tradeBaseParametersSchema: PromptObject[] = [ | ||
{ | ||
type: 'select', | ||
name: 'orderKind', | ||
choices: [ | ||
{ title: 'Sell', value: OrderKind.SELL }, | ||
{ title: 'Buy', value: OrderKind.BUY }, | ||
], | ||
message: 'Choose operation for limit order RFQ: create, fill, cancel', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'sellToken', | ||
message: 'Sell token address', | ||
}, | ||
{ | ||
type: 'number', | ||
name: 'sellTokenDecimals', | ||
message: 'Sell token decimals', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'buyToken', | ||
message: 'Buy token address', | ||
}, | ||
{ | ||
type: 'number', | ||
name: 'buyTokenDecimals', | ||
message: 'Buy token decimals', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'amount', | ||
message: 'Amount to trade (in units)', | ||
}, | ||
] | ||
|
||
export const tradeOptionalParametersSchema: PromptObject[] = [ | ||
{ | ||
type: 'select', | ||
name: 'env', | ||
choices: [ | ||
{ title: 'Prod', value: 'prod' }, | ||
{ title: 'Staging', value: 'staging' }, | ||
], | ||
message: 'Environment', | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'partiallyFillable', | ||
message: 'Is order partially fillable?', | ||
initial: false, | ||
}, | ||
{ | ||
type: 'number', | ||
name: 'slippageBps', | ||
message: 'Slippage in BPS', | ||
initial: 0, | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'receiver', | ||
message: 'Receiver address', | ||
initial: '', | ||
}, | ||
{ | ||
type: 'number', | ||
name: 'validFor', | ||
message: 'Order time to life (in seconds)', | ||
initial: 300, | ||
format: (val) => +val, | ||
}, | ||
{ | ||
type: 'number', | ||
name: 'partnerFeeBps', | ||
message: 'Partner fee percent (in BPS)', | ||
initial: 0, | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'partnerFeeRecipient', | ||
message: 'Partner fee recipient address', | ||
initial: '', | ||
}, | ||
] | ||
|
||
export const limitSpecificParametersSchema: PromptObject[] = [ | ||
{ | ||
type: 'text', | ||
name: 'sellAmount', | ||
message: 'Sell amount (in units)', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'buyAmount', | ||
message: 'Buy amount (in units)', | ||
}, | ||
] | ||
|
||
export const orderToSignParametersSchema: PromptObject[] = [ | ||
{ | ||
type: 'text', | ||
name: 'from', | ||
message: 'Account address', | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'networkCostsAmount', | ||
message: 'Network costs amount (in units) (from quote result)', | ||
}, | ||
] | ||
|
||
export const actionsSchema: PromptObject[] = [ | ||
{ | ||
type: 'select', | ||
name: 'action', | ||
choices: [ | ||
{ title: 'Get quote', value: 'getQuote' }, | ||
{ title: 'Post swap order', value: 'postSwapOrder' }, | ||
{ title: 'Post limit order', value: 'postLimitOrder' }, | ||
{ title: 'Get order to sign', value: 'getOrderToSign' }, | ||
], | ||
message: 'Choose action to do:', | ||
}, | ||
] | ||
|
||
export const swapParametersSchema = [ | ||
...traderParametersSchema, | ||
...tradeBaseParametersSchema, | ||
...tradeOptionalParametersSchema, | ||
] | ||
|
||
export const limitParametersSchema = [ | ||
...traderParametersSchema, | ||
...tradeBaseParametersSchema.filter((p) => p.name !== 'amount'), | ||
...limitSpecificParametersSchema, | ||
...tradeOptionalParametersSchema, | ||
] |
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,41 @@ | ||
import yargvParser from 'yargs-parser' | ||
import prompts from 'prompts' | ||
import { actionsSchema, swapParametersSchema } from './consts' | ||
import { getQuoteAction } from './actions/getQuoteAction' | ||
import { toggleLog } from '../consts' | ||
import { postSwapOrderAction } from './actions/postSwapOrderAction' | ||
import { postLimitOrderAction } from './actions/postLimitOrderAction' | ||
|
||
const promptSchemas = [swapParametersSchema] | ||
|
||
// IIFE | ||
;(async () => { | ||
const argv = yargvParser(process.argv.slice(2), { configuration: { 'parse-numbers': false } }) | ||
const argvKeys = Object.keys(argv) | ||
const hasArgv = promptSchemas.some((schema) => | ||
schema.map((i) => i.name as string).every((param) => argvKeys.includes(param)) | ||
) | ||
|
||
prompts.override(argv) | ||
|
||
if (hasArgv) { | ||
toggleLog(false) | ||
} | ||
|
||
const actionsResult = await prompts(actionsSchema) | ||
|
||
switch (actionsResult.action) { | ||
case 'getQuote': | ||
await getQuoteAction(hasArgv) | ||
break | ||
case 'postSwapOrder': | ||
await postSwapOrderAction(hasArgv) | ||
break | ||
case 'postLimitOrder': | ||
await postLimitOrderAction(hasArgv) | ||
break | ||
default: | ||
console.log('Unknown action: ', actionsResult.action) | ||
break | ||
} | ||
})() |
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,3 @@ | ||
export function printJSON(data: any): string { | ||
return JSON.stringify(data, (_, value) => (typeof value === 'bigint' ? value.toString() : value), 4) | ||
} |
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.