-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.ts
77 lines (68 loc) · 1.88 KB
/
relay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import express from 'express'
import { parse } from 'ts-command-line-args'
import { web3, Wallet } from '@coral-xyz/anchor'
import {
setAnchorProvider,
withdrawInit,
allWithdrawAdvance,
withdrawFinalize
} from './contract'
const app = express()
// Parse CLI args.
type Network = 'devnet' | 'mainnet'
interface RelayerArguments {
network: Network
fee: number
port?: number
}
function parseNetwork (value?: string): Network {
if (value !== 'devnet' && value !== 'mainnet') {
throw new Error(`Invalid network: ${value}`)
}
return value as Network
}
export const args = parse<RelayerArguments>({
network: parseNetwork,
fee: Number,
port: { type: Number, optional: true }
})
// Set the default args.
if (!args.port) {
args.port = 2008
}
console.log('args:', args)
setAnchorProvider(args.network)
app.use(express.json())
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
// Set up routes.
app.get('/ping', (req: express.Request, res: express.Response): void => {
res.send({ ok: true })
})
const walletAddress = Wallet.local().payer.publicKey.toString()
app.get('/feeAndAddress', (req: express.Request, res: express.Response): void => {
console.log('GET /feeAndAddress')
res.send({ fee: args.fee, address: walletAddress })
})
app.post('/relay', async (req: express.Request, res: express.Response): Promise<void> => {
console.log('POST /relay')
const proof = req.body
const withdrawState = web3.Keypair.generate()
res.send({
ok: true,
err: null,
withdrawState: withdrawState.publicKey.toString()
})
try {
await withdrawInit(withdrawState, proof)
await allWithdrawAdvance(withdrawState)
await withdrawFinalize(withdrawState, proof)
} catch (err) {
console.warn(err)
}
})
// Serve.
app.listen(args.port!)