initial checkin with timed order ui
This commit is contained in:
42
src/blockchain/abi.js
Normal file
42
src/blockchain/abi.js
Normal file
@@ -0,0 +1,42 @@
|
||||
export const erc20Abi = [
|
||||
'function name() view returns (string)',
|
||||
'function symbol() view returns (string)',
|
||||
'function decimals() view returns (uint8)',
|
||||
'function totalSupply() view returns (uint256)',
|
||||
'function balanceOf(address) view returns (uint256)',
|
||||
'function transfer(address,uint256) returns (bool)',
|
||||
'function transferFrom(address,address,uint256) returns (bool)',
|
||||
'function approve(address,uint256) returns (bool success)',
|
||||
'function allowance(address,address) view returns (uint256)',
|
||||
'event Transfer(address indexed,address indexed,uint256)',
|
||||
'event Approval(address indexed,address indexed,uint256)',
|
||||
]
|
||||
|
||||
|
||||
const TimedOrderSpec = '(' +
|
||||
'address tokenIn,' +
|
||||
'address tokenOut,' +
|
||||
'uint24 fee,' +
|
||||
'uint32 deadline,' +
|
||||
'uint32 leeway,' +
|
||||
'uint160 minSqrtPriceX96,' +
|
||||
'uint160 maxSqrtPriceX96,' +
|
||||
'uint8 numTranches,' +
|
||||
'uint256 amount,' +
|
||||
'bool amountIsInput' +
|
||||
')'
|
||||
|
||||
export const timedOrderAbi = [
|
||||
'event TimedOrderCreated (address owner, uint64 index, Spec spec)',
|
||||
'event TimedOrderFilled (address owner, uint64 index, uint256 amountIn, uint256 amountOut)',
|
||||
'event TimedOrderCompleted (address owner, uint64 index)',
|
||||
'event TimedOrderError (address owner, uint64 index, string reason)',
|
||||
`timedOrder(${TimedOrderSpec}) returns (uint64 index)`,
|
||||
]
|
||||
|
||||
|
||||
export const abi = {
|
||||
'ERC20': erc20Abi,
|
||||
'TimedOrder': timedOrderAbi,
|
||||
}
|
||||
|
||||
112
src/blockchain/wallet.js
Normal file
112
src/blockchain/wallet.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import {ethers} from "ethers";
|
||||
import {useStore} from "@/store/store";
|
||||
|
||||
const store = useStore()
|
||||
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
const handler = {
|
||||
get(target, prop, proxy) {
|
||||
const got = Reflect.get(target, prop, proxy);
|
||||
if( typeof got !== 'function' ) {
|
||||
return got
|
||||
}
|
||||
else {
|
||||
return async function (...args) {
|
||||
try {
|
||||
return await got.apply(target, args)
|
||||
}
|
||||
catch (x) {
|
||||
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)
|
||||
}
|
||||
else {
|
||||
console.log('wallet error')
|
||||
throw x
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default new Proxy(new Wallet(), handler)
|
||||
Reference in New Issue
Block a user