server works with backend via redis, serves chainInfo, more
This commit is contained in:
57
vault.js
57
vault.js
@@ -1,62 +1,43 @@
|
||||
import {ethers, keccak256} from "ethers";
|
||||
import {getAbi} from "./abi.js";
|
||||
import {getSigner} from "./blockchain.js";
|
||||
import {Cache} from './cache.js';
|
||||
import vaultCode from 'contract/out/Vault.sol/Vault.json'
|
||||
import assert from "assert";
|
||||
import fs from 'fs';
|
||||
import {ALL_CHAINS} from "./misc.js";
|
||||
import {vaults} from './cache.js';
|
||||
|
||||
const VAULT_INIT_CODE_HASH = keccak256(vaultCode.bytecode.object)
|
||||
|
||||
console.log(VAULT_INIT_CODE_HASH)
|
||||
|
||||
const deployerAddresses = {}
|
||||
|
||||
for (const chainId of ALL_CHAINS) {
|
||||
const path = `contract/broadcast/Deploy.sol/${chainId}/run-${process.env['DEXORDER_DEPLOYMENT_' + chainId]}.json`;
|
||||
const deployment = JSON.parse(fs.readFileSync(path, 'utf8')) // synchronous is ok we only do this once on init
|
||||
assert(deployment.transactions[0].contractName === 'VaultDeployer')
|
||||
deployerAddresses[chainId] = deployment.transactions[0].contractAddress
|
||||
}
|
||||
|
||||
const vaults = new Cache('vaults') // vault:owner
|
||||
// Vault
|
||||
// address owner
|
||||
// balances { tokenAddress: amount }
|
||||
// recentOrders []
|
||||
|
||||
const deployerAbi = await getAbi('VaultDeployer');
|
||||
const deployerAbi = await getAbi('Factory');
|
||||
|
||||
|
||||
function newVault(address, owner) {
|
||||
return {address, owner, tokens: {}} // internal json version
|
||||
}
|
||||
|
||||
export function vaultAddress(chainId, ownerAddress) {
|
||||
const encoded = ethers.AbiCoder.defaultAbiCoder().encode(['address'], [ownerAddress])
|
||||
export function vaultAddress(chainId, ownerAddress, num=0) {
|
||||
const encoded = ethers.AbiCoder.defaultAbiCoder().encode(['address','uint8'], [ownerAddress,num])
|
||||
const salt = ethers.keccak256(encoded)
|
||||
return ethers.getCreate2Address(getDeployerAddress(chainId), salt, VAULT_INIT_CODE_HASH)
|
||||
}
|
||||
|
||||
|
||||
export function loginAddress(socket, chainId, address) {
|
||||
// todo check for existing vault
|
||||
if (!vaults[address]) {
|
||||
//
|
||||
} else {
|
||||
export async function loginAddress(socket, chainId, address) {
|
||||
// todo send known tokens
|
||||
if (await vaults.contains(chainId,address)) {
|
||||
// todo send welcome with basic info and extra tokens
|
||||
|
||||
socket.send('welcome',{})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function ensureVault(socket, chainId, owner) {
|
||||
const address = vaultAddress(chainId, owner)
|
||||
if (!vaults[address]) {
|
||||
const deployer = new ethers.Contract(DEPLOYER_ADDRESS, deployerAbi, getSigner(chainId))
|
||||
await deployer.deployVault(owner)
|
||||
}
|
||||
export async function ensureVault(chainId, owner, num) {
|
||||
const address = vaultAddress(chainId, owner, num)
|
||||
if (!await vaults.contains(chainId,address))
|
||||
await createVault(chainId, owner, num)
|
||||
}
|
||||
|
||||
|
||||
async function createVault(chainId, owner, num) {
|
||||
// todo create vault request for backend to pick up
|
||||
const deployer = new ethers.Contract(factoryAddresses[chainId], deployerAbi, getSigner(chainId))
|
||||
await deployer.deployVault(owner, num)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user