forked from machinomy/machinomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.ts
68 lines (51 loc) · 1.73 KB
/
receiver.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
import path from 'path'
import fs from 'fs-extra'
import { IohTee } from '@riaskov/iohtee'
import Logger from '@machinomy/logger'
import { mnemonicToAccount } from 'viem/accounts'
const payment = require(path.resolve('./payment.json'))
const LOG = new Logger('iohtee-receiver')
async function run() {
const dbPath = path.resolve(__dirname, '../sender-receiver.db')
fs.removeSync(dbPath)
const MNEMONIC = String(process.env.ACCOUNT_MNEMONIC).trim()
const RPC_URL = String(process.env.RPC_URL).trim()
const CHAIN_ID = Number(process.env.CHAIN_ID)
const minimumChannelAmount = 1n * 10n ** 4n
LOG.info(`PROVIDER = ${RPC_URL}`)
LOG.info(`MNEMONIC = ${MNEMONIC}`)
const senderAccountHdPath = `m/44'/60'/0'/0/0`
const receiverAccountHdPath = `m/44'/60'/0'/0/1`
const senderAccount = mnemonicToAccount(MNEMONIC, {
path: senderAccountHdPath,
})
const receiverAccount = mnemonicToAccount(MNEMONIC, {
path: receiverAccountHdPath,
})
const iohtee = new IohTee({
networkId: CHAIN_ID,
httpRpcUrl: RPC_URL,
mnemonic: MNEMONIC,
hdPath: senderAccountHdPath,
options: {
databaseUrl: `sqlite://${dbPath}`,
minimumChannelAmount: minimumChannelAmount,
},
})
LOG.info(`Receiver: ${receiverAccount}`)
LOG.info(`Accept payment: ${JSON.stringify(payment)}`)
await iohtee.acceptPayment({
payment: payment,
})
LOG.info(`Start closing channel with channelID ${payment.channelId}`)
await iohtee.close(payment.channelId)
LOG.info(`Channel ${payment.channelId} was successfully closed.`)
LOG.info(
`Trace the last transaction via https://amoy.polygonscan.com/address/${receiverAccount}`,
)
LOG.info(`Receiver done.`)
process.exit(0)
}
run().catch((err) => {
console.error(err)
})