Skip to content

OMNI asset sweeping #5757

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions examples/ts/btc/omni/index.ts
Original file line number Diff line number Diff line change
@@ -8,12 +8,7 @@
import * as superagent from 'superagent';
import * as utxolib from '@bitgo/utxo-lib';

const RECEIVE_ADDRESS = '';
const SEND_ADDRESS = '';
const ASSET_ID = 31;
const BASE_AMOUNT = 729100000n; // this is currently 7.291 USDT

async function getWallet() {
export async function getWallet(): Promise<Wallet> {
return await omniConfig.sdk.coin(omniConfig.coin).wallets().get({ id: omniConfig.walletId });
}

@@ -37,21 +32,21 @@
* This is 31 for USDT.
* @param feeRateSatPerKB - The fee rate to use for the transaction, in satoshis per kilobyte.
*/
async function sendOmniAsset(
export async function sendOmniAsset(
wallet: Wallet,
receiver: string,
sender: string,
omniBaseAmount: bigint,
assetId = 31,
feeRateSatPerKB = 20_000
) {
if (!['1', '3', 'n', 'm'].includes(receiver.slice(0, 1))) {
): Promise<void> {
if (!['1', '2', '3', 'n', 'm'].includes(receiver.slice(0, 1))) {
throw new Error(
'Omni has only been verified to work with legacy and wrapped segwit addresses - use other address formats at your own risk'
);
}

const res = await superagent.get(`https://mempool.space/${omniConfig.MEMPOOL_PREFIX}api/address/${sender}/utxo`);

Check warning

Code scanning / CodeQL

File data in outbound network request Medium

Outbound network request depends on
file data
.
const unspent = res.body[0];
const unspent_id = unspent.txid + ':' + unspent.vout;

@@ -92,22 +87,3 @@
});
console.log('Omni asset sent: ', tx);
}

/*
* Usage: npx ts-node btc/omni/index.ts
* */
async function main() {
console.log('Starting...');

const feeRateRes = await superagent.get(`https://mempool.space/${omniConfig.MEMPOOL_PREFIX}api/v1/fees/recommended`);
const feeRate = feeRateRes.body.fastestFee;

const wallet = await getWallet();
// we multiply feeRate by 1000 because mempool returns sat/vB and BitGo uses sat/kvB
await sendOmniAsset(wallet, RECEIVE_ADDRESS, SEND_ADDRESS, BASE_AMOUNT, ASSET_ID, feeRate * 1000);
}

main().catch((e) => {
console.error(e);
process.exit(1);
});
86 changes: 86 additions & 0 deletions examples/ts/btc/omni/sweep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Sweep omni USDT to a single external address.
*
* Copyright 2024, BitGo, Inc. All Rights Reserved.
*/
import { Wallet } from 'modules/bitgo/src';
import { omniConfig } from './config';
import * as superagent from 'superagent';
import { getWallet, sendOmniAsset } from './index';
import * as fs from 'fs';

/**
* Fund addresses with a small amount of BTC if they have no UTXOs.
* @param wallet - The wallet to send the BTC from.
* @param addresses - The addresses to fund.
* @param feeRateSatPerKB - The fee rate to use for the transaction, in satoshis per kilo vbytes.
* @param fundAmount - The amount to send to each address in satoshis.
*/
async function fundAddressesIfEmpty(wallet: Wallet, addresses: string[], feeRateSatPerKB: number, fundAmount = 2000) {
const addressesToFund = addresses.filter(async (address) => {
const res = await superagent.get(`https://mempool.space/${omniConfig.MEMPOOL_PREFIX}api/address/${address}/utxo`);
if (res.statusCode !== 200) {
throw new Error(`Failed to get utxos for ${address}`);
}
return res.body.length === 0;
});
const { txid } = await wallet.sendMany({
recipients: addressesToFund.map((address) => ({ address, amount: fundAmount })),
feeRate: feeRateSatPerKB,
walletPassphrase: omniConfig.walletPassphrase,
});
console.log('Funded addresses', addressesToFund, 'with txid', txid);
}

/**
* Reads a header-less CSV file with the format:
* address,amount
* e.g., 2NCeUg5WCxn692CfoSJ86gh2pKdN3jdk9a3,100000000
* @param file - The path to the CSV file
*/
function readCsvForSenders(file: string): { address: string; baseAmount: bigint }[] {
const data = fs.readFileSync(file, 'utf8').trim();
const lines = data.split('\n');
const senders: { address: string; baseAmount: bigint }[] = [];
for (const line of lines) {
const [address, amount] = line.split(',');
senders.push({ address, baseAmount: BigInt(amount) });
}
return senders;
}

/*
* Usage: npx ts-node btc/omni/sweep.ts,
* and have a senders.csv file in the same directory with the format:
* address,amount without header, where amount is 1e-8 dollars
* e.g., 2NCeUg5WCxn692CfoSJ86gh2pKdN3jdk9a3,100000000 for 1 USDT contained within 2NCeUg5WCxn692CfoSJ86gh2pKdN3jdk9a3
* */
async function main() {
console.log('Starting...');
const RECEIVE_ADDRESS = '';
const ASSET_ID = 31;
const FUNDING_AMOUNT = 5000;

const feeRateRes = await superagent.get(`https://mempool.space/${omniConfig.MEMPOOL_PREFIX}api/v1/fees/recommended`);
const feeRate = feeRateRes.body.fastestFee;

const wallet = await getWallet();
const senders = readCsvForSenders('senders.csv');
await fundAddressesIfEmpty(
wallet,
senders.map((sender) => sender.address),
feeRate * 1000,
FUNDING_AMOUNT
);
await new Promise((resolve) => setTimeout(resolve, 30000));
for (const sender of senders) {
await sendOmniAsset(wallet, RECEIVE_ADDRESS, sender.address, sender.baseAmount, ASSET_ID, feeRate * 1000);
console.log(`Sent ${sender.baseAmount} to ${sender.address}`);
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}

main().catch((e) => {
console.error(e);
process.exit(1);
});