diff --git a/src/blockchain/prices.js b/src/blockchain/prices.js
index 8273d07..bb76c25 100644
--- a/src/blockchain/prices.js
+++ b/src/blockchain/prices.js
@@ -5,6 +5,7 @@ import {uniswapV3PoolAddress} from "@/blockchain/uniswap.js";
import {ethers, FixedNumber} from "ethers";
import {uniswapV3PoolAbi} from "@/blockchain/abi.js";
import {subOHLCs} from "@/blockchain/ohlcs.js";
+import {provider} from "@/blockchain/wallet.js";
const subscriptionCounts = {} // key is route and value is a subscription counter
export const WIDE_PRICE_FORMAT = {decimals:38, width:512, signed:false}; // 38 decimals is 127 bits
@@ -79,7 +80,6 @@ async function getPriceForRoute(route) {
if( route.exchange === Exchange.UniswapV3 ) {
const addr = uniswapV3PoolAddress(route.chainId, route.token0.address, route.token1.address, route.fee)
const store = useStore();
- const provider = store.provider;
if( provider === null ) {
console.error('provider was null during getPriceForRoute')
return null
diff --git a/src/blockchain/route.js b/src/blockchain/route.js
index 21d127f..0f1a36f 100644
--- a/src/blockchain/route.js
+++ b/src/blockchain/route.js
@@ -2,6 +2,7 @@ import {Exchange} from "@/blockchain/orderlib.js";
import {useOrderStore, useStore} from "@/store/store.js";
import {queryHelperContract} from "@/blockchain/contract.js";
import {SingletonCoroutine} from "@/misc.js";
+import {provider} from "@/blockchain/wallet.js";
export async function findRoute(helper, chainId, tokenA, tokenB) {
@@ -40,7 +41,7 @@ async function componentFindRoute() {
os.routesPending = true
try {
console.log('getting query helper')
- const helper = await queryHelperContract(s.helper, s.provider)
+ const helper = await queryHelperContract(s.helper, provider)
if (!helper) {
console.log('no helper')
} else {
diff --git a/src/blockchain/token.js b/src/blockchain/token.js
index ac3370e..702205d 100644
--- a/src/blockchain/token.js
+++ b/src/blockchain/token.js
@@ -3,6 +3,7 @@ import {useStore} from "@/store/store.js";
import {erc20Abi} from "@/blockchain/abi.js";
import {ethers} from "ethers";
import {metadata, metadataMap} from "@/version.js";
+import {provider} from "@/blockchain/wallet.js";
// synchronous version may return null but will trigger a lookup
@@ -58,12 +59,12 @@ export async function addExtraToken(chainId, addr) {
resolve(info)
}
else {
- if( s.provider===null ) {
+ if( provider===null ) {
console.log('warning: token lookup cancelled due to null provider', addr)
resolve(null)
}
else {
- const token = new ethers.Contract(addr, erc20Abi, s.provider)
+ const token = new ethers.Contract(addr, erc20Abi, provider)
Promise.all( [token.name(), token.symbol(), token.decimals()] ).then((name,symbol,decimals)=>{
info = {
a: addr,
diff --git a/src/blockchain/wallet.js b/src/blockchain/wallet.js
index 0eedf98..3da7735 100644
--- a/src/blockchain/wallet.js
+++ b/src/blockchain/wallet.js
@@ -1,4 +1,4 @@
-import {ethers} from "ethers";
+import {BrowserProvider, ethers} from "ethers";
import {useStore} from "@/store/store";
import {socket} from "@/socket.js";
import {contractOrNull, vaultAddress} from "@/blockchain/contract.js";
@@ -9,6 +9,9 @@ import {ref} from "vue";
import {metadataMap} from "@/version.js";
+export let provider = null
+
+
export const useWalletStore = defineStore('wallet', ()=>{
// this is what the wallet is logged into. it could be different than the application's store.chainId.
const chainId = ref(0)
@@ -31,6 +34,7 @@ export const useWalletStore = defineStore('wallet', ()=>{
export function onChainChanged(chainId) {
+ console.log('onChainChanged', chainId)
chainId = Number(chainId)
const store = useStore()
const ws = useWalletStore()
@@ -41,6 +45,8 @@ export function onChainChanged(chainId) {
console.log('app chain changed', chainId)
store.chainId = chainId
store.account = null
+ provider = new BrowserProvider(window.ethereum, chainId)
+ updateAccounts(chainId, provider)
}
else {
console.log('app chain NOT changed')
@@ -77,6 +83,7 @@ function changeAccounts(chainId, accounts) {
}
function onAccountsChanged(accounts) {
+ console.log('onAccountsChanged', accounts)
const store = useStore()
const ws = useWalletStore()
if (accounts.length === 0 || accounts[0] !== store.account)
@@ -84,11 +91,17 @@ function onAccountsChanged(accounts) {
}
export function detectChain() {
+ try {
+ window.ethereum.on('chainChanged', onChainChanged);
+ window.ethereum.on('accountsChanged', onAccountsChanged);
+ }
+ catch (e) {
+ console.log('Could not connect change hooks to wallet', e)
+ return
+ }
new ethers.BrowserProvider(window.ethereum).getNetwork().then((network)=>{
const chainId = network.chainId
onChainChanged(chainId)
- window.ethereum.on('chainChanged', onChainChanged);
- window.ethereum.on('accountsChanged', onAccountsChanged);
})
}
@@ -125,50 +138,50 @@ const errorHandlingProxy = {
}
-export async function connectProvider(chainId) {
- console.log('connectProvider', chainId)
- try {
- return new ethers.BrowserProvider(window.ethereum, chainId)
- }
- catch (e) {
- console.log('provider error', e)
- }
- return null
-}
-
-
export async function connectWallet(chainId) {
console.log('connectWallet', chainId)
- const ws = useWalletStore()
- if (ws.chainId !== chainId && !await switchChain(chainId))
- return null
- const p = await connectProvider(chainId)
- if (p!==null) {
+ await switchChain(chainId)
+ if (provider!==null) {
try {
console.log('getSigner')
- return await p.getSigner();
+ await provider.getSigner()
+ await updateAccounts(chainId, provider)
}
catch (e) {
if (e.reason!=='rejected')
console.error(e, e.reason)
- return null
}
}
+ else
+ console.error('provider was null after chain switch')
}
export async function switchChain(chainId) {
if (useWalletStore().chainId === chainId)
- return true
- try {
- await window.ethereum.request({
- "method": "wallet_switchEthereumChain",
- "params": [{"chainId": '0x' + chainId.toString(16)}]
- })
- return true
- } catch (e) {
- return false
- }
+ return
+ await window.ethereum.request({
+ "method": "wallet_switchEthereumChain",
+ "params": [{"chainId": '0x' + chainId.toString(16)}]
+ })
+}
+
+
+export async function addTestnet() {
+ const info = {
+ "chainId": "0x539",
+ "chainName": "Dexorder Alpha Testnet",
+ "rpcUrls": ["https://rpc.alpha.dexorder.trade"],
+ "nativeCurrency": {
+ "name": "Test Ethereum",
+ "symbol": "TETH",
+ "decimals": 18
+ }
+ };
+ await window.ethereum.request({
+ "method": "wallet_addEthereumChain",
+ "params": [info]
+ });
}
@@ -198,7 +211,7 @@ async function _discoverVaults(owner) {
s.vaults = []
return
}
- const provider = s.provider;
+ console.log('provider', provider)
if (!provider) {
console.log('No provider')
return // do not change whatever was already found
@@ -350,10 +363,19 @@ function pendOrderAsTransaction(pend) {
console.error('vault contract was null while sending order transaction', pend.vault)
return null
}
- if (!await switchChain(pend.chainId)) {
- console.log('user refused chain switch')
- pend.state = PendingOrderState.Rejected
- return null
+ try {
+ await switchChain(pend.chainId)
+ }
+ catch (e) {
+ if(e.code===4001) {
+ console.log('user refused chain switch')
+ pend.state = PendingOrderState.Rejected
+ return null
+ }
+ else {
+ console.error('Unknown error while switching chain to pend order', e)
+ return null
+ }
}
console.log('placing order', pend.id)
const tx = await contract.placeDexorder(pend.order) // todo update status
@@ -393,7 +415,7 @@ export function flushTransactions() {
async function asyncFlushTransactions() {
const s = useStore()
- if( s.provider === null ) {
+ if( provider === null ) {
console.log('warning: asyncFlushOrders() cancelled due to null provider')
return
}
@@ -403,7 +425,7 @@ async function asyncFlushTransactions() {
console.log(`flushing ${senders.length} transactions`)
let signer
try {
- signer = await s.provider.getSigner();
+ signer = await provider.getSigner();
} catch (e) {
// {
// "code": -32002,
diff --git a/src/components/CopyButton.vue b/src/components/CopyButton.vue
index 7ea4c6b..4b9345e 100644
--- a/src/components/CopyButton.vue
+++ b/src/components/CopyButton.vue
@@ -7,7 +7,7 @@
:icon="error?'mdi-close-box-outline':copied?'mdi-check-circle-outline':'mdi-content-copy'"
:text="showText?text:''"
/>
-
This alpha test runs on the Dexorder Testnet blockchain, which gives you free testnet tokens to trade. @@ -52,54 +56,42 @@ Open Metamask again and select the "Dexorder Alpha" blockchain for use with this website. +-->