Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/backend/api/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import app from '../src/index.mjs'

export default app
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"start": "node ./src/index.mjs",
"test": "hardhat test",
"test": "hardhat test --network local",
"lint": "prettier .",
"lint:fix": "prettier . --write"
},
Expand Down
Empty file.
4 changes: 1 addition & 3 deletions packages/backend/src/config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { config } from 'dotenv'
import { ethers } from 'ethers'
import pkg from 'hardhat'
const { ethers: hardhatEthers } = pkg
config()

export const DB_PATH = process.env.DB_PATH ?? ':memory:'
Expand Down Expand Up @@ -44,7 +42,7 @@ export const NETWORK = {
},
// for test
local: {
provider: (await hardhatEthers.getSigners())[0].provider,
provider: new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545'),
unirepAddress: '0x4D137bb44553d55AE6B28B5391c6f537b06C9cc3',
},
}
2 changes: 2 additions & 0 deletions packages/backend/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ for (const routeFile of routes) {
const { default: route } = await import(path.join(routeDir, routeFile))
route({ app, db })
}

export default app
28 changes: 17 additions & 11 deletions packages/backend/test/attesterDescription.test.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import pkg from 'hardhat'
const { ethers } = pkg
import { ethers } from 'ethers'
import { HTTP_SERVER } from './server.mjs'
import { expect } from 'chai'
import fetch from 'node-fetch'

import { startServer } from './environment.mjs'

import app from '../src/index.mjs' // start server

const random = () => Math.floor(Math.random() * 100000)

let attesters
let body

const res = await startServer()
const attesterF = await ethers.getContractFactory('Attester')
const attesterC = await attesterF
.connect(res.attester)
.deploy(res.unirep.address)
await attesterC.deployed()
const { attester, attesterC } = await startServer()

attesters = [
{
type: 'contract',
deployer: res.attester,
deployer: attester,
appAddress: attesterC.address,
},
{ type: 'EOA', deployer: res.attester, appAddress: res.attester.address },
{ type: 'EOA', deployer: attester, appAddress: attester.address },
]

describe('Attester Description Tests', function () {
this.timeout(0)

before(async () => {
const url = new URL(`/api/info`, HTTP_SERVER)
const res = await fetch(url.toString(), {
method: 'get',
headers: {
network: 'local',
},
})
expect(res.status).to.equal(200)
})

beforeEach(() => {
body = {
description: 'example description',
Expand Down
63 changes: 35 additions & 28 deletions packages/backend/test/environment.mjs
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
import url from 'url'
import schema from '../src/schema.mjs'
import path from 'path'
import fs from 'fs'
import express from 'express'
import { SQLiteConnector } from 'anondb/node.js'
import { deployUnirep } from '@unirep/contracts/deploy/index.js'
import pkg from 'hardhat'
const { ethers } = pkg

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))
import { spawn } from 'child_process'
const providerURL = `http://127.0.0.1:8545`

const db = await SQLiteConnector.create(schema, ':memory:')
const hardhat = spawn('npx hardhat node', { shell: true })
hardhat.stderr.on('data', (data) => {
console.error(`hardhat stderr: ${data}`)
})
hardhat.on('close', (code) => {
console.error(`hardhat exit with code: ${code}`)
hardhat.kill(9)
process.exit(code)
})

export const startServer = async () => {
const [signer, attester] = await ethers.getSigners()
for (;;) {
await new Promise((r) => setTimeout(r, 1000))
try {
const provider = new ethers.providers.JsonRpcProvider(providerURL)
await provider.getNetwork()
break
} catch (_) {}
}
const provider = new ethers.providers.JsonRpcProvider(providerURL)
const signer = new ethers.Wallet(
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
provider
)
const attester = new ethers.Wallet.createRandom().connect(provider)
await signer.sendTransaction({
to: attester.address,
value: ethers.utils.parseEther('1'), // 1 ether
})
const unirep = await deployUnirep(signer)
await unirep
.connect(attester)
.attesterSignUp(300)
.then((t) => t.wait())

const app = express()
const port = process.env.PORT ?? 8000
app.listen(port, () => console.log(`Listening on port ${port}`))
app.use('*', (req, res, next) => {
res.set('access-control-allow-origin', '*')
res.set('access-control-allow-headers', '*')
next()
})
app.use(express.json())

const routeDir = path.join(__dirname, '../src/routes')
const routes = await fs.promises.readdir(routeDir)
for (const routeFile of routes) {
const { default: route } = await import(path.join(routeDir, routeFile))
route({ app, db })
}
const attesterF = await ethers.getContractFactory('Attester')
const attesterC = await attesterF.connect(attester).deploy(unirep.address)
await attesterC.deployed()

return {
app,
db,
attester,
unirep,
attesterC,
}
}
3 changes: 3 additions & 0 deletions packages/backend/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rewrites": [{ "source": "/(.*)", "destination": "/api" }]
}
3 changes: 2 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"scripts": {
"start": "webpack-dev-server",
"build": "webpack",
"test": "jest"
"test": "jest",
"vercel-build": "scripts/vercelBuild.sh && webpack"
},
"devDependencies": {
"@babel/core": "^7.20.5",
Expand Down
11 changes: 11 additions & 0 deletions packages/frontend/scripts/vercelBuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

set -e

URL=$VERCEL_BRANCH_URL
echo $VERCEL_BRANCH_URL
find="frontend"
replace="backend"
result=${URL//$find/$replace}
echo $result
echo SERVER = \"https://$result\" >> ./src/config.js
3 changes: 1 addition & 2 deletions packages/frontend/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ export const network = 'arbitrum-goerli'
// arbitrum-goerli, goerli, mumbai, sepolia

// in CI we append a change to this value
let SERVER = 'http://127.0.0.1:8000'
export let SERVER = 'http://127.0.0.1:8000'
// let SERVER = 'https://api.explorer.unirep.io'
export { SERVER }
1 change: 1 addition & 0 deletions packages/frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = (env) => ({
template: 'public/index.html',
filename: 'index.html',
inlineSource: '.(js|css)',
favicon: 'public/favicon.ico',
}),
new MiniCssExtractPlugin(),
],
Expand Down