forked from machinomy/machinomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.ts
75 lines (58 loc) · 1.94 KB
/
sender.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
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 LOG = new Logger('iohtee-sender')
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
const channelValue = 1n * 10n ** 6n
const paymentPrice = 200000n
LOG.info(`PROVIDER = ${RPC_URL}`)
LOG.info(`MNEMONIC = ${MNEMONIC}`)
const senderAccountHdPath = `m/44'/60'/0'/0/1`
const senderAccount = mnemonicToAccount(MNEMONIC, {
path: senderAccountHdPath,
})
const receiverAccount = mnemonicToAccount(MNEMONIC, {
path: `m/44'/60'/0'/0/0`,
})
const iohtee = new IohTee({
networkId: CHAIN_ID,
httpRpcUrl: RPC_URL,
mnemonic: MNEMONIC,
hdPath: senderAccountHdPath,
options: {
databaseUrl: `sqlite://${dbPath}`,
minimumChannelAmount: minimumChannelAmount,
},
})
LOG.info(
`Start opening IohTee channel between sender ${senderAccount.address} and receiver ${receiverAccount.address} with value ${channelValue} Wei`,
)
LOG.info(
`For remote Ethereum nodes (e.g. Amoy or Sepolia) it can taking a 15-30 seconds.`,
)
await iohtee.open(receiverAccount.address, channelValue)
LOG.info(`Channel was opened.`)
LOG.info(
`Trace the last transaction via https://amoy.polygonscan.com/address/${senderAccount.address}`,
)
const payment = await iohtee.payment({
receiver: receiverAccount.address,
price: paymentPrice,
})
LOG.info('Payment: ')
LOG.info(payment.payment)
fs.writeFileSync('payment.json', JSON.stringify(payment.payment))
LOG.info('Sender done.')
process.exit(0)
}
run().catch((err) => {
console.error(err)
})