withdrawls
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
import {ethers} from "ethers";
|
||||
import {useStore} from "@/store/store";
|
||||
import {setProvider, useStore} from "@/store/store";
|
||||
import {socket} from "@/socket.js";
|
||||
import {vaultContract} from "@/blockchain/contract.js";
|
||||
import {contractOrNull} from "@/blockchain/contract.js";
|
||||
import {vaultAbi} from "@/blockchain/abi.js";
|
||||
|
||||
export let provider = null
|
||||
|
||||
export function onChainChanged(chainId) {
|
||||
chainId = Number(chainId)
|
||||
console.log('chain changed', chainId)
|
||||
const store = useStore()
|
||||
if( chainId !== store.chainId ) {
|
||||
store.chainId = chainId
|
||||
const provider = new ethers.BrowserProvider(window.ethereum, chainId);
|
||||
setProvider(provider, chainId)
|
||||
store.account = null
|
||||
provider = new ethers.BrowserProvider(window.ethereum, chainId)
|
||||
provider.listAccounts().then(changeAccounts)
|
||||
new ethers.Interface([
|
||||
// 'event DexorderSwapCreated' // todo
|
||||
])
|
||||
store.chainId = chainId // touch the chainId last. will cause any clients of the store's provider getter to refresh
|
||||
}
|
||||
}
|
||||
|
||||
function changeAccounts(accounts) {
|
||||
console.log('change accounts', accounts)
|
||||
const store = useStore()
|
||||
if( accounts.length === 0 ) {
|
||||
store.account = null
|
||||
store.vaults = []
|
||||
@@ -29,16 +29,15 @@ function changeAccounts(accounts) {
|
||||
else {
|
||||
const store = useStore()
|
||||
store.account = accounts[0].address
|
||||
flushOrders()
|
||||
flushTransactions()
|
||||
socket.emit('address', store.chainId, accounts[0].address)
|
||||
}
|
||||
}
|
||||
|
||||
function onAccountsChanged(accounts) {
|
||||
// console.log('accounts changed', accounts)
|
||||
const store = useStore()
|
||||
if (accounts.length === 0 || accounts[0] !== store.account)
|
||||
changeAccounts(store, accounts);
|
||||
changeAccounts(accounts);
|
||||
}
|
||||
|
||||
export async function watchWallet() {
|
||||
@@ -82,31 +81,56 @@ const errorHandlingProxy = {
|
||||
|
||||
|
||||
export async function connectWallet() {
|
||||
return provider.getSigner()
|
||||
console.log('TODO connect wallet')
|
||||
// eth_getaccounts
|
||||
}
|
||||
|
||||
|
||||
export async function pendOrder(order) {
|
||||
export function pendOrder(order) {
|
||||
console.log('order', JSON.stringify(order))
|
||||
const s = useStore()
|
||||
s.pendingOrders.push(order)
|
||||
flushOrders()
|
||||
}
|
||||
|
||||
|
||||
export function flushOrders() {
|
||||
// noinspection JSIgnoredPromiseFromCall
|
||||
asyncFlushOrders()
|
||||
}
|
||||
|
||||
export async function asyncFlushOrders() {
|
||||
const s = useStore()
|
||||
const orders = s.pendingOrders
|
||||
if (!orders.length)
|
||||
const vault = s.vault;
|
||||
if(vault === null ) {
|
||||
console.error('vault was null during pendOrder')
|
||||
return
|
||||
}
|
||||
pendTransaction(async (signer)=> {
|
||||
const contract = contractOrNull(vault, vaultAbi, signer)
|
||||
if( contract === null ) {
|
||||
console.error('vault contract was null while sending order transaction', vault)
|
||||
return null
|
||||
}
|
||||
return await contract.placeOrder(order)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export function pendTransaction(sender) {
|
||||
const s = useStore()
|
||||
s.transactionSenders.push(sender)
|
||||
flushTransactions()
|
||||
}
|
||||
|
||||
|
||||
export function flushTransactions() {
|
||||
// noinspection JSIgnoredPromiseFromCall
|
||||
asyncFlushTransactions()
|
||||
}
|
||||
|
||||
export async function asyncFlushTransactions() {
|
||||
// todo rework into flushTransactions()
|
||||
const s = useStore()
|
||||
if( s.provider === null ) {
|
||||
console.log('warning: asyncFlushOrders() cancelled due to null provider')
|
||||
return
|
||||
}
|
||||
const senders = s.transactionSenders
|
||||
if (!senders.length)
|
||||
return
|
||||
console.log(`flushing ${s.transactionSenders.length} transactions`)
|
||||
let signer
|
||||
try {
|
||||
signer = await provider.getSigner();
|
||||
signer = await s.provider.getSigner();
|
||||
} catch (e) {
|
||||
// {
|
||||
// "code": -32002,
|
||||
@@ -119,28 +143,27 @@ export async function asyncFlushOrders() {
|
||||
socket.emit('ensureVault', s.chainId, await signer.getAddress(), 0)
|
||||
return
|
||||
}
|
||||
const contract = await vaultContract(0, signer)
|
||||
if (!contract) {
|
||||
console.error(`no contract for vault 0 of ${signer.address}`)
|
||||
return
|
||||
}
|
||||
for (const order of orders)
|
||||
doPlaceOrder(s, contract, order)
|
||||
for (const sender of senders)
|
||||
doSendTransaction(sender, signer)
|
||||
}
|
||||
|
||||
function doPlaceOrder(s, contract, order) {
|
||||
contract.placeOrder(order).then((tx)=>{
|
||||
console.log('placed order', tx)
|
||||
s.removePendingOrder(order)
|
||||
function doSendTransaction(sender, signer) {
|
||||
const s = useStore();
|
||||
sender(signer).then((tx)=>{
|
||||
console.log('sent transaction', tx)
|
||||
s.removeTransactionSender(sender)
|
||||
tx.wait().then((tr)=>console.log('tx receipt',tr))
|
||||
}).catch((e)=>{
|
||||
if( e.info.error.code === 4001 ) {
|
||||
console.log(`user rejected order`, order)
|
||||
s.removePendingOrder(order)
|
||||
if( e.info?.error?.code === 4001 ) {
|
||||
console.log(`user rejected transaction`)
|
||||
s.removeTransactionSender(sender)
|
||||
}
|
||||
else {
|
||||
console.error('error placing order', order, e.reason, e.info)
|
||||
s.removePendingOrder(order)
|
||||
if( e.reason && e.info )
|
||||
console.error('error sending transaction', e.reason, e.info)
|
||||
else
|
||||
console.error('error sending transaction', e)
|
||||
s.removeTransactionSender(sender)
|
||||
// todo retry?
|
||||
}
|
||||
})
|
||||
@@ -150,5 +173,5 @@ socket.on('vaults', (vaults)=>{
|
||||
const s = useStore()
|
||||
console.log('vaults', vaults)
|
||||
s.vaults = vaults
|
||||
flushOrders()
|
||||
flushTransactions()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user