75 lines
2.6 KiB
JavaScript
75 lines
2.6 KiB
JavaScript
// Utilities
|
|
import { defineStore } from 'pinia'
|
|
import {knownTokens} from "@/knownTokens.js";
|
|
|
|
let rawProvider = null
|
|
let rawProviderChainId = null
|
|
|
|
export function setProvider( provider, chainId ) {
|
|
rawProvider = provider
|
|
rawProviderChainId = chainId
|
|
}
|
|
|
|
export const useStore = defineStore('app', {
|
|
state: () => ({
|
|
chainId: null,
|
|
chainInfo: {},
|
|
vaultInitCodeHash: null,
|
|
account: null,
|
|
vaults: [],
|
|
transactionSenders: [], // a list of function(signer) that send transactions
|
|
errors: [
|
|
// todo re-enable danger warning
|
|
// {title: 'DANGER!', text: 'This is early development (alpha) software. There could be severe bugs that lose all your money. Thank you for testing a SMALL amount!', closeable: true}
|
|
],
|
|
extraTokens: {},
|
|
poolPrices: {},
|
|
vaultBalances: {}, // indexed by vault addr then by token addr. value is an int
|
|
orders: {}, // indexed by vault, value is another dictionary with orderIndex as key and order status values
|
|
}),
|
|
getters: {
|
|
vault: (s)=>s.vaults.length===0 ? null : s.vaults[0],
|
|
provider: (s)=>s.chainId===rawProviderChainId ? rawProvider : null,
|
|
chain: (s)=> !s.chainInfo ? null : (s.chainInfo[s.chainId] || null),
|
|
tokens: (s)=>{
|
|
const chains = s.chainId in s.chainInfo && s.chainInfo[s.chainId].tokens !== undefined ? s.chainInfo[s.chainId].tokens : []
|
|
let known = knownTokens[s.chainId]
|
|
known = known ? Object.values(known) : []
|
|
let extras = s.extraTokens[s.chainId]
|
|
extras = extras ? Object.values(extras) : []
|
|
const result = {}
|
|
const all = [...chains, ...known, ...extras]; // put chains first so the Mockcoin pool is automatically selected
|
|
for( const token of all)
|
|
result[token.address] = token
|
|
return result
|
|
},
|
|
factory: (s)=>!s.chain?null:s.chain.factory,
|
|
helper: (s)=>!s.chain?null:s.chain.helper,
|
|
mockenv: (s)=>!s.chain?null:s.chain.mockenv,
|
|
mockCoins: (s)=>!s.chain?[]:!s.chain.mockCoins?[]:s.chain.mockCoins,
|
|
},
|
|
actions: {
|
|
removeTransactionSender(sender) {
|
|
this.transactionSenders = this.transactionSenders.filter((v) => v !== sender)
|
|
},
|
|
error(title, text, closeable=true) {
|
|
this.errors.push({title, text, closeable})
|
|
},
|
|
closeError(title, text) {
|
|
const result = []
|
|
this.errors.forEach((i)=>{if(i.title!==title && i.text!==text) result.push(i)})
|
|
this.errors = result
|
|
},
|
|
addToken(chainId, info) {
|
|
this.$patch((s) => {
|
|
let extras = s.extraTokens[chainId]
|
|
if (extras === undefined) {
|
|
extras = {}
|
|
s.extraTokens[chainId] = extras
|
|
}
|
|
extras[info.address] = info
|
|
})
|
|
},
|
|
},
|
|
})
|