Skip to content

Commit

Permalink
refactor: new swap cmd can take refund params
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd committed Aug 1, 2024
1 parent 8050ef3 commit 7408beb
Showing 1 changed file with 72 additions and 20 deletions.
92 changes: 72 additions & 20 deletions bouncer/commands/new_swap.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,87 @@
#!/usr/bin/env -S pnpm tsx
// INSTRUCTIONS
//
// This command takes four arguments.
// It will request a new swap with the provided parameters
// Argument 1 is the source currency ("Btc", "Eth", "Dot" or "Usdc")
// Argument 2 is the destination currency ("Btc", "Eth", "Dot" or "Usdc")
// Argument 3 is the destination address
// Argument 4 (optional) is the max boost fee bps (default: 0 (no boosting))
// For example: ./commands/new_swap.ts Dot Btc n1ocq2FF95qopwbEsjUTy3ZrawwXDJ6UsX
// Request a new swap with the provided parameters:
// <sourceAsset> <destAsset> <destAddress> [maxBoostFeeBps] [refundAddress] [minPrice] [refundDuration]
// Use `-h` for help.
// If the refundAddress is provided, the minPrice must also be provided. The refundDuration will default to 0 if not provided.
// Example: ./commands/new_swap.ts Dot Btc n1ocq2FF95qopwbEsjUTy3ZrawwXDJ6UsX --refundAddress "0xa0b52be60216f8e0f2eb5bd17fa3c66908cc1652f3080a90d3ab20b2d352b610" --minPrice 100000000000000000

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { InternalAsset } from '@chainflip/cli';
import { parseAssetString, executeWithTimeout } from '../shared/utils';
import { requestNewSwap } from '../shared/perform_swap';
import { RefundParameters } from '../shared/new_swap';

async function newSwapCommand() {
const sourceAsset = parseAssetString(process.argv[2]);
const destAsset = parseAssetString(process.argv[3]);
const destAddress = process.argv[4];
const maxBoostFeeBps = Number(process.argv[5] || 0);
const args = yargs(hideBin(process.argv))
.command(
'$0 <sourceAsset> <destAsset> <destAddress> [maxBoostFeeBps] [refundAddress] [minPrice] [refundDuration]',
'Request a new swap with the provided parameters',
(a) => {
console.log('Parsing options');
return a
.positional('sourceAsset', {
describe: 'The source currency ("Btc", "Eth", "Dot", "Usdc")',
type: 'string',
})
.positional('destAsset', {
describe: 'The destination currency ("Btc", "Eth", "Dot", "Usdc")',
type: 'string',
})
.positional('destAddress', {
describe: 'The destination address',
type: 'string',
})
.option('maxBoostFeeBps', {
describe: 'The max boost fee bps (default: 0 (no boosting))',
type: 'number',
default: 0,
demandOption: false,
})
.option('refundAddress', {
describe: 'Fill or Kill refund address',
type: 'string',
demandOption: false,
})
.option('minPrice', {
describe: 'Fill or Kill minimum price',
type: 'string',
demandOption: false,
})
.option('refundDuration', {
describe: 'Fill or kill duration in blocks to retry swap before refunding',
type: 'number',
demandOption: false,
default: 0,
});
},
)
.help('h').argv;

console.log(`Requesting swap ${sourceAsset} -> ${destAsset}`);
if ((args.refundAddress === undefined) !== (args.minPrice === undefined)) {
throw new Error('Must specify both refundAddress and minimumPrice when using refund options');
}
const refundParameters: RefundParameters | undefined =
args.refundAddress !== undefined
? {
retryDurationBlocks: args.refundDuration,
refundAddress: args.refundAddress,
minPrice: args.minPrice,
}
: undefined;

await requestNewSwap(
sourceAsset as InternalAsset,
destAsset as InternalAsset,
destAddress,
undefined,
undefined,
undefined,
true,
maxBoostFeeBps,
parseAssetString(args.sourceAsset) as InternalAsset,
parseAssetString(args.destAsset) as InternalAsset,
args.destAddress,
undefined, // tag
undefined, // messageMetadata
undefined, // brokerCommissionBps
true, // log
args.maxBoostFeeBps,
refundParameters,
);
}

Expand Down

0 comments on commit 7408beb

Please sign in to comment.