From 0d0200009e90f292875d50b3d67ac3eb70958cf9 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 23 Mar 2024 19:17:26 -0400 Subject: [PATCH] orders submitting but breaking due to fee send --- src/blockchain/orderlib.js | 41 +++++++----- src/blockchain/wallet.js | 89 ++++++++++++++++--------- src/components/chart/ChartOrder.vue | 20 +++--- src/components/chart/ChartOrderPane.vue | 50 ++++++++++++-- src/components/chart/LimitBuilder.vue | 34 +++++++++- src/components/chart/MarketBuilder.vue | 12 +++- src/orderbuild.js | 32 ++++++++- 7 files changed, 208 insertions(+), 70 deletions(-) diff --git a/src/blockchain/orderlib.js b/src/blockchain/orderlib.js index 44f12a9..d01978c 100644 --- a/src/blockchain/orderlib.js +++ b/src/blockchain/orderlib.js @@ -22,13 +22,17 @@ export const DISTANT_FUTURE = uint32max // Exchange exchange; // uint24 fee; // } -export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput, tranches, minAmount=null, - outputToOwner = false, chainOrder = NO_CHAIN) { +export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput, tranches, + minFillAmount=null, outputDirectlyToOwner = false, chainOrder = NO_CHAIN) { if (!tranches) tranches = [newTranche({marketOrder: true})] // todo this is just a swap: issue warning? - if( minAmount === null ) - minAmount = BigInt(amount) / 100n // default to min trade size of 1% - return [tokenIn, tokenOut, [exchange, fee], amount, minAmount, amountIsInput, outputToOwner, chainOrder, tranches] + if( minFillAmount === null ) + minFillAmount = BigInt(amount) / 100n // default to min trade size of 1% + return { + tokenIn, tokenOut, route:{exchange, fee}, + amount, minFillAmount, amountIsInput, + outputDirectlyToOwner, chainOrder, tranches + } } // struct Tranche { @@ -55,23 +59,24 @@ export function newOrder(tokenIn, tokenOut, exchange, fee, amount, amountIsInput // float maxSlope; // } export function newTranche({ - fraction = MAX_FRACTION, - marketOrder = false, - startTimeIsRelative = false, - startTime = DISTANT_PAST, - endTimeIsRelative = false, - endTime = DISTANT_FUTURE, - minIsBarrier = false, - minIntercept = 0, - minSlope = 0, - maxIsBarrier = false, - maxIntercept = 0, - maxSlope = 0, + fraction = MAX_FRACTION, + marketOrder = false, + startTimeIsRelative = false, + startTime = DISTANT_PAST, + endTimeIsRelative = false, + endTime = DISTANT_FUTURE, + minIsBarrier = false, + minIntercept = 0, + slippage = 0, // may also set minIntercept instead + minSlope = 0, + maxIsBarrier = false, + maxIntercept = 0, + maxSlope = 0, } = {}) { if( minIntercept === 0 && minSlope === 0 && maxIntercept === 0 && maxSlope === 0 ) marketOrder = true if( marketOrder ) - minIntercept = encodeIEE754(minIntercept) // this is the slippage field for market orders + minIntercept = encodeIEE754(slippage) // this is the slippage field for market orders else { minIntercept = encodeIEE754(minIntercept) minSlope = encodeIEE754(minSlope) diff --git a/src/blockchain/wallet.js b/src/blockchain/wallet.js index 394ba1e..8d43a48 100644 --- a/src/blockchain/wallet.js +++ b/src/blockchain/wallet.js @@ -3,15 +3,34 @@ import {useStore} from "@/store/store"; import {socket} from "@/socket.js"; import {contractOrNull, vaultAddress} from "@/blockchain/contract.js"; import {vaultAbi} from "@/blockchain/abi.js"; -import {SingletonCoroutine, sleep} from "@/misc.js"; +import {SingletonCoroutine} from "@/misc.js"; +import {defineStore} from "pinia"; +import {ref} from "vue"; + + +export const useWalletStore = defineStore('wallet', ()=>{ + // Pending Order Format + // { + // chainId: 31337, // must never be null, even if no wallet plugin exists. chosen by app, not wallet. + // placementTime: Date.now(), + // vault: '0x...', // or null if account not logged in yet + // order: {tokenIn:..., tokenOut:..., ...} // blockchain binary order object + // } + const pendingOrders = ref([]) + + return { + pendingOrders, + } +}) export function onChainChanged(chainId) { chainId = Number(chainId) const store = useStore() - if( chainId !== store.chainId ) { + if( chainId !== store.chainId.value ) { + // todo check pending orders and confirm cancellation console.log('chain changed', chainId) - store.chainId = chainId + store.chainId.value = chainId store.account = null const provider = new ethers.BrowserProvider(window.ethereum, chainId); store.provider = provider @@ -86,12 +105,10 @@ const errorHandlingProxy = { } -export async function connectWallet() { - await new ethers.BrowserProvider(window.ethereum).getSigner(); +export async function connectWallet(chainId) { + await new ethers.BrowserProvider(window.ethereum, chainId).getSigner(); } -let pendingOrders = [] - function discoverVaults(owner) { const s = useStore() @@ -106,7 +123,7 @@ const doDiscoverVaults = new SingletonCoroutine(_discoverVaults, 50, false) async function _discoverVaults(owner) { const result = [] const s = useStore() - if( !owner || !s.chainId || !s.account) { + if( !owner || !s.chainId.value || !s.account) { s.vaults = [] return } @@ -138,7 +155,7 @@ async function _discoverVaults(owner) { } if( s.account === owner ) { // double-check the account since it could have changed during our await s.vaults = result - if( pendingOrders.length ) + if( useWalletStore().pendingOrders.length ) if( result.length ) flushOrders(result[0]) else @@ -148,15 +165,21 @@ async function _discoverVaults(owner) { export function ensureVault() { - const s = useStore() - const owner = s.account; - console.log('ensureVault', s.chainId.value, owner) - if( !owner ) - return - ensureVault2(s.chainId.value, owner, 0) + ensureVaultFunc.invoke() } +const ensureVaultFunc = new SingletonCoroutine(ensureVault1,1) + + +async function ensureVault1() { + const s = useStore() + const owner = s.account; + if (owner===null) + await connectWallet(s.chainId.value) + ensureVault2(s.chainId.value, owner, 0) +} + export function ensureVault2(chainId, owner, num) { console.log('ensureVault2', chainId, owner, num) if( !chainId ) { @@ -179,20 +202,19 @@ async function doEnsureVault(chainId, owner, num) { // await sleep(5000) // prevent this process from running more than once every 5 seconds } -const ensureVaultRoutine = new SingletonCoroutine(doEnsureVault, 100, false) +const ensureVaultRoutine = new SingletonCoroutine(doEnsureVault, 100) export async function pendOrder(order) { console.log('order', JSON.stringify(order)) const s = useStore() - if (!s.vaults.length) { - pendingOrders.push(order) - ensureVault() - } - else { - const vault = s.vaults[0]; - pendOrderAsTransaction(vault, order) - } + useWalletStore().pendingOrders.push({ + chainId: s.chainId.value, + placementTime: new Date(), + vault: s.vaults.length ? s.vaults[0] : null, + order + }) + ensureVault() } @@ -220,21 +242,26 @@ export async function cancelAll(vault) { } export function flushOrders(vault) { - for( const order of pendingOrders ) - pendOrderAsTransaction(vault, order) - pendingOrders = [] + const ws = useWalletStore(); + for( const order of ws.pendingOrders ) { + if (order.vault === null) + order.vault = vault + pendOrderAsTransaction(order) + } + ws.pendingOrders = [] flushTransactions() } -function pendOrderAsTransaction(vault, order) { +function pendOrderAsTransaction(order) { pendTransaction(async (signer)=> { - const contract = contractOrNull(vault, vaultAbi, signer) + const contract = contractOrNull(order.vault, vaultAbi, signer) if( contract === null ) { - console.error('vault contract was null while sending order transaction', vault) + console.error('vault contract was null while sending order transaction', order.vault) return null } - return await contract.placeOrder(order) + console.log('placing order', order) + return await contract.placeOrder(order.order) // todo update status }) } diff --git a/src/components/chart/ChartOrder.vue b/src/components/chart/ChartOrder.vue index fc06789..d906fd3 100644 --- a/src/components/chart/ChartOrder.vue +++ b/src/components/chart/ChartOrder.vue @@ -18,7 +18,7 @@ :color="color" variant="text" @click="order.amountIsTokenA=!order.amountIsTokenA"/> - - - diff --git a/src/components/chart/LimitBuilder.vue b/src/components/chart/LimitBuilder.vue index f079824..77d9484 100644 --- a/src/components/chart/LimitBuilder.vue +++ b/src/components/chart/LimitBuilder.vue @@ -60,9 +60,9 @@