orders submitting but breaking due to fee send

This commit is contained in:
Tim
2024-03-23 19:17:26 -04:00
parent 68a820a9f6
commit 0d0200009e
7 changed files with 208 additions and 70 deletions

View File

@@ -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)

View File

@@ -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
})
}