order placement works, including automatic vault creation
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import {ethers} from "ethers";
|
||||
import {useStore} from "@/store/store";
|
||||
import {socket} from "@/socket.js";
|
||||
import {vaultContract} from "@/blockchain/contract.js";
|
||||
|
||||
export let provider = null
|
||||
|
||||
function onChainChanged(chainId) {
|
||||
export function onChainChanged(chainId) {
|
||||
chainId = Number(chainId)
|
||||
// console.log('chain changed', chainId)
|
||||
const store = useStore()
|
||||
store.chainId = chainId
|
||||
store.account = null
|
||||
provider = new ethers.BrowserProvider(window.ethereum, chainId)
|
||||
provider.listAccounts().then(onAccountsChanged)
|
||||
}
|
||||
|
||||
function onAccountsChanged(accounts) {
|
||||
@@ -18,8 +22,10 @@ function onAccountsChanged(accounts) {
|
||||
store.account = null
|
||||
}
|
||||
else if (accounts[0] !== store.account) {
|
||||
store.account = accounts[0]
|
||||
store.account = accounts[0].address
|
||||
flushOrders()
|
||||
}
|
||||
socket.emit('address', store.chainId, accounts[0].address)
|
||||
}
|
||||
|
||||
export async function watchWallet() {
|
||||
@@ -62,4 +68,48 @@ const errorHandlingProxy = {
|
||||
}
|
||||
|
||||
|
||||
// const wallet = new Proxy(new Wallet(), errorHandlingProxy);
|
||||
export async function connectWallet() {
|
||||
return provider.getSigner()
|
||||
}
|
||||
|
||||
|
||||
export async function pendOrder(order) {
|
||||
console.log('order', order)
|
||||
const s = useStore()
|
||||
s.pendingOrders.push(order)
|
||||
const signer = await connectWallet()
|
||||
if (!s.vaults.length)
|
||||
socket.emit('ensureVault', s.chainId, await signer.getAddress(), 0)
|
||||
else
|
||||
flushOrders()
|
||||
}
|
||||
|
||||
|
||||
export function flushOrders() {
|
||||
// noinspection JSIgnoredPromiseFromCall
|
||||
asyncFlushOrders()
|
||||
}
|
||||
|
||||
export async function asyncFlushOrders() {
|
||||
const s = useStore()
|
||||
const orders = s.pendingOrders
|
||||
if(!orders.length || !s.account)
|
||||
return
|
||||
const contract = await vaultContract(0, await provider.getSigner())
|
||||
if( !contract )
|
||||
return
|
||||
const proms = []
|
||||
for (const order of orders)
|
||||
proms.push(contract.placeOrder(order))
|
||||
s.pendingOrders = []
|
||||
const txs = await Promise.all(proms)
|
||||
for( const tx of txs )
|
||||
console.log('placed order', tx)
|
||||
}
|
||||
|
||||
socket.on('vaults', (vaults)=>{
|
||||
const s = useStore()
|
||||
console.log('vaults', vaults)
|
||||
s.vaults = vaults
|
||||
flushOrders()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user