chainInfo from server, routes, wallet rework
This commit is contained in:
@@ -1,3 +1,34 @@
|
||||
|
||||
export const factoryAbi = [
|
||||
]
|
||||
|
||||
export const queryHelperAbi = [
|
||||
'function getRoutes(address tokenA,address tokenB) view returns((uint8,uint24,address)[])',
|
||||
]
|
||||
|
||||
export const poolAbi = [
|
||||
// {
|
||||
// // the current price
|
||||
// uint160 sqrtPriceX96;
|
||||
// // the current tick
|
||||
// int24 tick;
|
||||
// // the most-recently updated index of the observations array
|
||||
// uint16 observationIndex;
|
||||
// // the current maximum number of observations that are being stored
|
||||
// uint16 observationCardinality;
|
||||
// // the next maximum number of observations to store, triggered in observations.write
|
||||
// uint16 observationCardinalityNext;
|
||||
// // the current protocol fee as a percentage of the swap fee taken on withdrawal
|
||||
// // represented as an integer denominator (1/x)%
|
||||
// uint8 feeProtocol;
|
||||
// // whether the pool is locked
|
||||
// bool unlocked;
|
||||
// }
|
||||
'function slot0() view returns(uint160,int24,uint16,uint16,uint16,uint8,bool)',
|
||||
'function token0() view returns(address)',
|
||||
'function token1() view returns(address)',
|
||||
]
|
||||
|
||||
export const erc20Abi = [
|
||||
'function name() view returns (string)',
|
||||
'function symbol() view returns (string)',
|
||||
@@ -38,5 +69,6 @@ export const timedOrderAbi = [
|
||||
export const abi = {
|
||||
'ERC20': erc20Abi,
|
||||
'TimedOrder': timedOrderAbi,
|
||||
'QueryHelper': queryHelperAbi,
|
||||
}
|
||||
|
||||
|
||||
20
src/blockchain/contract.js
Normal file
20
src/blockchain/contract.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import {ethers} from "ethers";
|
||||
import {factoryAbi, queryHelperAbi} from "@/blockchain/abi.js";
|
||||
import {useStore} from "@/store/store.js";
|
||||
import {provider} from "@/blockchain/wallet.js";
|
||||
|
||||
|
||||
export async function factoryContract() {
|
||||
const s = useStore()
|
||||
return new ethers.Contract(s.helper, factoryAbi, provider)
|
||||
}
|
||||
|
||||
export async function queryHelperContract() {
|
||||
const s = useStore()
|
||||
return new ethers.Contract(s.helper, queryHelperAbi, provider)
|
||||
}
|
||||
|
||||
export async function poolContract(addr) {
|
||||
const s = useStore()
|
||||
return new ethers.Contract(addr, poolAbi, provider)
|
||||
}
|
||||
@@ -1,86 +1,36 @@
|
||||
import {ethers} from "ethers";
|
||||
import {useStore} from "@/store/store";
|
||||
|
||||
const store = useStore()
|
||||
export let provider = null
|
||||
|
||||
function onChainChanged(chainId) {
|
||||
chainId = Number(chainId)
|
||||
console.log('chain changed', chainId)
|
||||
const store = useStore()
|
||||
store.chainId = chainId
|
||||
provider = new ethers.BrowserProvider(window.ethereum, chainId)
|
||||
}
|
||||
|
||||
class Wallet {
|
||||
|
||||
connected = false
|
||||
_provider = null
|
||||
_signer = null
|
||||
_onSignerInits = {}
|
||||
_enabled = true // prevents tight loops on errors
|
||||
|
||||
async connect() {
|
||||
this._enabled = true
|
||||
if( !this.connected )
|
||||
await this.signer()
|
||||
function onAccountsChanged(accounts) {
|
||||
console.log('accounts changed', accounts)
|
||||
const store = useStore()
|
||||
if( accounts.length === 0 ) {
|
||||
store.account = null
|
||||
}
|
||||
|
||||
async provider() {
|
||||
if (this._provider === null && this._enabled) {
|
||||
console.log('creating provider')
|
||||
const provider = new ethers.BrowserProvider(window.ethereum, store.chain.id)
|
||||
await provider.getNetwork() // this invokes a check on having the correct network connected
|
||||
this._provider = provider
|
||||
console.log('wallet connected')
|
||||
}
|
||||
return this._provider
|
||||
}
|
||||
|
||||
async signer() {
|
||||
/*
|
||||
if( !store.geo.approved ) {
|
||||
console.log('not approved')
|
||||
this._connected(false)
|
||||
return null
|
||||
}
|
||||
*/
|
||||
const provider = await this.provider()
|
||||
if( provider === null ) {
|
||||
console.log('provider null')
|
||||
this._connected(false)
|
||||
return null
|
||||
}
|
||||
const signer = await provider.getSigner()
|
||||
if( this._signer?.address !== signer.address ) {
|
||||
console.log('new signer', signer.address)
|
||||
this._signer = signer
|
||||
for (const key in this._onSignerInits) {
|
||||
try {
|
||||
await this._onSignerInits[key](signer)
|
||||
}
|
||||
catch (e) {
|
||||
console.log('during onSignerInit', e)
|
||||
}
|
||||
}
|
||||
console.log('wallet connected')
|
||||
}
|
||||
this._connected(true)
|
||||
return signer
|
||||
}
|
||||
|
||||
async address() {
|
||||
const signer = await this.signer()
|
||||
if( signer === null )
|
||||
return null
|
||||
return await signer.getAddress()
|
||||
}
|
||||
|
||||
_connected(value) {
|
||||
this.connected = value
|
||||
store.$patch({wallet:{connected:value}})
|
||||
}
|
||||
|
||||
onSignerInit(id, cb) {
|
||||
this._onSignerInits[id] = cb
|
||||
else if (accounts[0] !== store.account) {
|
||||
store.account = accounts[0]
|
||||
}
|
||||
}
|
||||
|
||||
export async function watchWallet() {
|
||||
const chainId = (await new ethers.BrowserProvider(window.ethereum).getNetwork()).chainId
|
||||
onChainChanged(chainId)
|
||||
window.ethereum.on('chainChanged', onChainChanged);
|
||||
window.ethereum.on('accountsChanged', onAccountsChanged);
|
||||
}
|
||||
|
||||
|
||||
const handler = {
|
||||
const errorHandlingProxy = {
|
||||
get(target, prop, proxy) {
|
||||
const got = Reflect.get(target, prop, proxy);
|
||||
if( typeof got !== 'function' ) {
|
||||
@@ -95,8 +45,11 @@ const handler = {
|
||||
target._connected(false)
|
||||
target._enabled = false
|
||||
if( x.code === 'NETWORK_ERROR' ) {
|
||||
store.error('Wrong Blockchain', 'Your wallet is connected to a different blockchain. Please select '+import.meta.env.VITE_CHAIN_NAME+' in your wallet.')
|
||||
console.log('wallet network error', x)
|
||||
// todo available chain names
|
||||
// store.error('Wrong Blockchain', 'Your wallet is connected to a different blockchain. Please select Arbitrum in your wallet.') // todo hardcoded arb
|
||||
console.log('wallet chain error', x)
|
||||
// store.chainId = store.chainInfo[]
|
||||
throw x
|
||||
}
|
||||
else {
|
||||
console.log('wallet error')
|
||||
@@ -109,4 +62,4 @@ const handler = {
|
||||
}
|
||||
|
||||
|
||||
export default new Proxy(new Wallet(), handler)
|
||||
// const wallet = new Proxy(new Wallet(), errorHandlingProxy);
|
||||
|
||||
Reference in New Issue
Block a user