Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
feat: keep funding reserve on exchange (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastien Verreault <sebver@pm.me>
  • Loading branch information
sebastienverreault and Sebastien Verreault authored Jul 14, 2022
1 parent 94d832f commit 0d764e8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 60 deletions.
1 change: 1 addition & 0 deletions dealer/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hedging:
HIGH_SAFEBOUND_LEVERAGE: 2.25
HIGH_BOUND_LEVERAGE: 3

MINIMUM_FUNDING_BALANCE_BTC: 1
MINIMUM_POSITIVE_LIABILITY_USD: 1
MINIMUM_TRANSFER_AMOUNT_USD: 100
MINIMUM_ORDER_SIZE_IN_CONTRACT: 1
Expand Down
154 changes: 94 additions & 60 deletions dealer/src/OkexPerpetualSwapStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,75 +525,109 @@ export class OkexPerpetualSwapStrategy implements HedgingStrategy {
}
await database.internalTransfers.insert(intTransferRecord)

// then initiate the withdrawal which by default uses the funding account
const config = this.exchangeConfig as OkexExchangeConfiguration
const averageFee = config.minOnChainWithdrawalFee
const withdrawArgs = {
currency: TradeCurrency.BTC,
quantity: transferSizeInBtc - averageFee,
address: withdrawOnChainAddress,
params: {
fee: averageFee,
dest: DestinationAddressType.External,
pwd: this.exchange.fundingPassword,
},
}
const withdrawalResult = await this.exchange.withdraw(withdrawArgs)
// only withdraw if funding balance > MINIMUM_FUNDING_BALANCE_BTC
// and if so, withdraw (funding balance - MINIMUM_FUNDING_BALANCE_BTC)
// i.e. newTransferSizeInBtc = max(0, funding balance - MINIMUM_FUNDING_BALANCE_BTC)
let newTransferSizeInBtc = transferSizeInBtc
const fundingAccountBalanceResult = await this.getFundingAccountBalance()
this.logger.debug(
{ withdrawArgs, withdrawalResult },
"withdraw() returned: {withdrawalResult}",
{ fundingAccountBalanceResult },
"getFundingAccountBalance() returned: {fundingAccountBalanceResult}",
)

// keep record of the attempt
const extTransferRecord: ExternalTransfer = {
isDepositNotWithdrawal: false,
currency: withdrawArgs.currency,
quantity: withdrawArgs.quantity,
destinationAddressTypeId: withdrawArgs.params.dest,
toAddress: withdrawArgs.address,
fundPassword: withdrawArgs.params.pwd,
fee: Number(withdrawArgs.params.fee),
chain: SupportedChain.BTC_Bitcoin,
// transferId: null,
success: false,
}

if (!withdrawalResult.ok) {
await database.externalTransfers.insert(extTransferRecord)
return { ok: false, error: withdrawalResult.error }
if (fundingAccountBalanceResult.ok) {
newTransferSizeInBtc = Math.max(
0,
fundingAccountBalanceResult.value.btcFreeBalance -
hedgingBounds.MINIMUM_FUNDING_BALANCE_BTC,
)
} else {
this.logger.error(
{ fundingAccountBalanceResult },
"getFundingAccountBalance() error: {fundingAccountBalanceResult}",
)
}
const withdrawalResponse = withdrawalResult.value

if (withdrawalResponse.id) {
extTransferRecord.transferId = withdrawalResponse.id
extTransferRecord.success = true
await database.externalTransfers.insert(extTransferRecord)

const bookingResult = await withdrawBookKeepingCallback(
withdrawOnChainAddress,
withdrawArgs.quantity,
)
this.logger.debug(
{
withdrawOnChainAddress,
transferSizeInBtc: withdrawArgs.quantity,
bookingResult,
if (newTransferSizeInBtc > 0) {
// then initiate the withdrawal which by default uses the funding account
const config = this.exchangeConfig as OkexExchangeConfiguration
const averageFee = config.minOnChainWithdrawalFee
const withdrawArgs = {
currency: TradeCurrency.BTC,
quantity: newTransferSizeInBtc - averageFee,
address: withdrawOnChainAddress,
params: {
fee: averageFee,
dest: DestinationAddressType.External,
pwd: this.exchange.fundingPassword,
},
"withdrawBookKeepingCallback() returned: {bookingResult}",
}
const withdrawalResult = await this.exchange.withdraw(withdrawArgs)
this.logger.debug(
{ withdrawArgs, withdrawalResult },
"withdraw() returned: {withdrawalResult}",
)
if (!bookingResult.ok) {
return { ok: false, error: bookingResult.error }

// keep record of the attempt
const extTransferRecord: ExternalTransfer = {
isDepositNotWithdrawal: false,
currency: withdrawArgs.currency,
quantity: withdrawArgs.quantity,
destinationAddressTypeId: withdrawArgs.params.dest,
toAddress: withdrawArgs.address,
fundPassword: withdrawArgs.params.pwd,
fee: Number(withdrawArgs.params.fee),
chain: SupportedChain.BTC_Bitcoin,
// transferId: null,
success: false,
}

this.logger.info(
{ withdrawalResponse },
"rebalancing withdrawal was successful",
)
if (!withdrawalResult.ok) {
await database.externalTransfers.insert(extTransferRecord)
return { ok: false, error: withdrawalResult.error }
}
const withdrawalResponse = withdrawalResult.value

if (withdrawalResponse.id) {
extTransferRecord.transferId = withdrawalResponse.id
extTransferRecord.success = true
await database.externalTransfers.insert(extTransferRecord)

const bookingResult = await withdrawBookKeepingCallback(
withdrawOnChainAddress,
withdrawArgs.quantity,
)
this.logger.debug(
{
withdrawOnChainAddress,
transferSizeInBtc: withdrawArgs.quantity,
bookingResult,
},
"withdrawBookKeepingCallback() returned: {bookingResult}",
)
if (!bookingResult.ok) {
return { ok: false, error: bookingResult.error }
}

this.logger.info(
{ withdrawalResponse },
"rebalancing withdrawal was successful",
)
} else {
await database.externalTransfers.insert(extTransferRecord)
this.logger.error(
{ withdrawalResponse },
"rebalancing withdrawal was NOT successful",
)
}
} else {
await database.externalTransfers.insert(extTransferRecord)
this.logger.error(
{ withdrawalResponse },
"rebalancing withdrawal was NOT successful",
this.logger.debug(
{
transferSizeInBtc,
newTransferSizeInBtc,
fundingAccountBalanceResult,
hedgingBounds,
},
"no withdraw() required",
)
}
} else if (fundTransferSide === FundTransferSide.Deposit) {
Expand Down

0 comments on commit 0d764e8

Please sign in to comment.