DCABuilder slippage parameter fix

This commit is contained in:
tim
2025-03-26 23:17:28 -04:00
parent 7d04d23a89
commit e86fbfa8e9
6 changed files with 59 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import {uint32max, uint64max} from "@/misc.js";
import {encodeIEE754} from "@/common.js";
import {DEFAULT_SLIPPAGE} from "@/orderbuild.js";
export const MAX_FRACTION = 65535;
export const NO_CONDITIONAL_ORDER = uint64max;
@@ -86,6 +87,10 @@ export function newTranche({
if( marketOrder ) {
if (minIntercept !== 0 || minSlope !== 0 || maxIntercept !== 0 || maxSlope !== 0)
console.warn('Ignoring line information in a market order')
if (slippage === 0) {
console.warn(`setting market order slippage to ${DEFAULT_SLIPPAGE}`)
slippage = DEFAULT_SLIPPAGE
}
minIntercept = encodeIEE754(slippage) // this is the slippage field for market orders
minSlope = 0
maxIntercept = 0

View File

@@ -1,4 +1,4 @@
import {nav, uuid} from "@/misc.js";
import {nav, sleep, uuid} from "@/misc.js";
import {vaultContract} from "@/blockchain/contract.js";
import {ensureVault, provider, switchChain, useWalletStore} from "@/blockchain/wallet.js";
import {toRaw} from "vue";
@@ -128,9 +128,15 @@ export class Transaction {
this.failed('vault contract was null while sending order transaction')
return null
}
const tx = toRaw(await this.createTx(contract))
this.signed(tx)
console.log(`sent transaction`, tx)
try {
const tx = toRaw(await this.createTx(contract))
this.signed(tx)
console.log(`sent transaction`, tx)
}
catch (e) {
this.failed(e)
return null
}
tx.wait().then(this.mined.bind(this)).catch(this.failed.bind(this))
return this.tx
}
@@ -159,7 +165,20 @@ export class PlaceOrderTransaction extends Transaction {
async createTx(vaultContract) {
this.fee = await placementFee(this.vault, this.order)
const tries = 3;
let i;
for (i=0; i<tries; i++ ) {
try {
this.fee = await placementFee(vaultContract, this.order)
break
}
catch (e) {
console.warn('failed to get placement fee', e)
await sleep(1000)
}
}
if (i===tries)
throw Error('failed to get placement fee')
console.log('placing order', this.id, this.fee, this.order)
return await vaultContract.placeDexorder(this.order, {value: this.fee.reduce((a, b) => a + b)})
}

View File

@@ -75,8 +75,11 @@ function changeAccounts(chainId, accounts) {
console.log('account logged in', addr)
store.account = addr
store.vaults = []
discoverVaults(addr)
flushTransactions()
// one of these two methods will call flushTransactions()
if (useWalletStore().transaction!==null)
ensureVault()
else
discoverVaults(addr)
socket.emit('address', chainId, addr)
}
}
@@ -215,6 +218,7 @@ async function _discoverVaults(owner) {
return // do not change what was already found todo is this correct?
}
}
console.log('new account === owner?', s.account, owner)
if( s.account === owner ) { // double-check the account since it could have changed during our await
s.vaults = result
s.vaultVersions = versions
@@ -291,6 +295,7 @@ export async function cancelAll(vault) {
export function flushOrders(chainId, owner, num, vault) {
const ws = useWalletStore();
console.log('flushOrders', ws.transaction)
if (ws.transaction!==null && ws.transaction.state < TransactionState.Proposed)
ws.transaction.propose(owner, vault)
let needsFlush = false
@@ -376,6 +381,22 @@ export function flushTransactions() {
async function asyncFlushTransactions() {
const s = useStore()
const ws = useWalletStore()
console.log('flushTransactions', ws.transaction, s.vault)
if (ws.transaction !== null) {
if (s.vault === null) {
await ensureVault()
if (s.vault === null) {
console.error('vault could not be created')
const tx = ws.transaction
if (tx) {
tx.state = TransactionState.Error
ws.transaction = null
}
return
}
}
}
if( provider === null ) {
console.log('warning: asyncFlushOrders() cancelled due to null provider')
return

View File

@@ -102,7 +102,8 @@ function buildTranches() {
const endTime = Math.ceil(props.builder.endTime + 10*props.builder.tranches)
const startTime = Math.floor(props.builder.startTime);
const tranches = [newTranche({marketOrder:true,
startTime, endTime, rateLimitFraction: rate, rateLimitPeriod: Math.floor(interval)})]
startTime, endTime, rateLimitFraction: rate, rateLimitPeriod: Math.floor(interval),
slippage: props.builder.slippage})]
return {tranches, warnings}
}

View File

@@ -28,10 +28,11 @@ export async function getFeeSchedule(vaultAddr) {
}
export async function placementFee(vaultAddr, order, window = 300) {
export async function placementFee(vaultContractOrAddr, order, window = 300) {
// If the fees are about to change within `window` seconds of now, we send the higher native amount of the two fees.
// If the fees sent are too much, the vault will refund the sender.
const vault = await vaultContract(vaultAddr, provider)
console.log('placementFee', vaultContractOrAddr, order)
const vault = typeof vaultContractOrAddr === 'string' ? await vaultContract(vaultContractOrAddr, provider) : vaultContractOrAddr
const feeManager = await getFeeManagerContract(vault);
const [sched, changeTimestamp] = await Promise.all([feeManager.fees(), feeManager.proposedFeeActivationTime()])
console.log('sched', order, sched)

View File

@@ -4,7 +4,8 @@
<v-dialog v-model="showTransactionDialog" max-width="300">
<v-card :title="title">
<v-card-text v-if="description!==null">{{description}}</v-card-text>
<v-card-text>Confirm this {{noun}} in your wallet.</v-card-text>
<v-card-text v-if="s.vault">Confirm this {{noun}} in your wallet.</v-card-text>
<v-card-text v-if="!s.vault">Creating your trading vault smart contract. Please wait a few seconds...</v-card-text>
</v-card>
</v-dialog>
</v-app>