Skip to content

Commit 2d7e1dc

Browse files
committed
chore: update
1 parent 44b671e commit 2d7e1dc

File tree

10 files changed

+378
-48
lines changed

10 files changed

+378
-48
lines changed

packages/neuron-ui/src/components/PaymentChannel/api.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import { getParticipantByAddressAndPubkey, isSuccessResponse } from "utils";
33
import * as wire from "utils/perun-wallet-wrapper/wire";
44

55

6-
6+
export async function startupChannelServiceRunner(publicKey: string) {
7+
const actionRes = await perunServiceAction({
8+
type: 'startup',
9+
payload: {
10+
network: "testnet",
11+
publicKey,
12+
},
13+
})
14+
if (!isSuccessResponse(actionRes)) {
15+
return []
16+
}
17+
}
718
export async function getChannels(publicKey: string, address: string) {
819
const actionRes = await perunServiceAction({
920
type: 'get',
@@ -37,6 +48,7 @@ export async function updateChannel(channelId: string, swapAmount: number) {
3748
type: 'update',
3849
payload: {
3950
channelId: channelId,
51+
// todo 找到正确属于己方的balance
4052
index: 0,
4153
amount: swapAmount,
4254
},
@@ -45,4 +57,14 @@ export async function updateChannel(channelId: string, swapAmount: number) {
4557

4658
return res;
4759

60+
}
61+
62+
export async function restoreChannels() {
63+
const actionRes = await perunServiceAction({
64+
type: 'restore',
65+
payload: {
66+
data: new Uint8Array(0),
67+
},
68+
})
69+
return actionRes;
4870
}

packages/neuron-ui/src/components/PerunOpenChannel/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const PerunOpenChannel = ({ show, onClose, myPubKey }: { show: boolean; onClose?
2929

3030
const [formData, setFormData] = useState({
3131
myAmount: 123,
32-
peerAddress: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqd4a33y7unx66rqh03vwngh3e4t0x6yrhcd9sfns',
33-
peerPubKey: '0x02a5b7bb6196db5edcd38c55de70ae61d4bc52cd8b1195cfa6278ca43c11a35ab3',
32+
peerAddress: 'ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvujnwcyyexhcsddzu74yks6ytchq26y0svu4675',
33+
peerPubKey: '0x02372431f7ce5e18e100e56d6d8e74145ec00ad59878887a353aca8ca52e64119c',
3434
peerAmount: 125,
3535
})
3636

packages/neuron-ui/src/types/Controller/index.d.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,19 @@ declare namespace Controller {
212212
interface GetChannelParams {
213213
requester: Uint8Array
214214
}
215-
interface PerunServiceActionParams {
216-
type: 'open' | 'update' | 'close' | 'get' | 'restore'
217-
payload: OpenChannelParams | UpdateChannelParams | CloseChannelParams | GetChannelParams | RestoreChannelsParams
218-
}
215+
type PerunChannelServiceRunnerStartupsParams = {
216+
network: "testnet" | "mainnet"
217+
publicKey: string;
218+
}
219+
type PerunServiceActionParams =
220+
| { type: "startup", payload: PerunChannelServiceRunnerStartupsParams }
221+
| { type: "shutdown" }
222+
| { type: "open", payload: OpenChannelParams }
223+
| { type: "update", payload: UpdateChannelParams }
224+
| { type: "close", payload: CloseChannelParams }
225+
| { type: "get", payload: GetChannelParams }
226+
| { type: "restore", payload: RestoreChannelsParams }
227+
219228
interface RespondPerunRequestParams {
220229
type: 'SignMessage' | 'SignTransaction' | 'UpdateNotification'
221230
response: { rejected?: { reason: string }; data: any }

packages/neuron-wallet/src/controllers/app/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ export default class AppController {
6666
if (env.isTestMode) {
6767
return
6868
}
69-
await Promise.all([stopCkbNode(), CKBLightRunner.getInstance().stop(), PerunServiceRunner.getInstance().stop()])
69+
await Promise.all([
70+
stopCkbNode(),
71+
CKBLightRunner.getInstance().stop(),
72+
PerunServiceRunner.getInstance().stop(),
73+
PerunServiceRunner.getInstance().stopChannelServiceRunner(),
74+
])
7075
}
7176

7277
public registerChannels(win: BrowserWindow, channels: string[]) {

packages/neuron-wallet/src/controllers/perun.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import {
99
channelIdFromString,
1010
channelIdToString,
1111
} from '../utils/perun-wallet-wrapper/translator'
12-
import { interval } from 'rxjs'
12+
// import { interval } from 'rxjs'
1313
import { mkSimpleChannelServiceClient } from '../utils/perun-wallet-wrapper/client'
1414
import { bytes } from '@ckb-lumos/codec'
1515
import { Allocation, Balances } from '../utils/perun-wallet-wrapper/wire'
1616
// import PerunPersistorService from '../services/perun/persistor'
1717
// import PerunChannelEntity from '../database/chain/entities/perun-channel'
1818

19+
1920
const defaultAddressEncoder: AddressEncoder = (add: Uint8Array | string) => {
2021
if (typeof add === 'string') {
2122
return bytes.bytify(add)
@@ -62,27 +63,27 @@ export default class PerunController {
6263
logger.info('PerunController: mount-----PerunController-----')
6364
this.registerHandlers()
6465

65-
interval(20000).subscribe(async () => {
66-
try {
67-
// const res = await PerunController.serviceClient.restoreChannels(new Uint8Array([]))
68-
// if (res.accepted) {
69-
// for (const channel of channels) {
70-
// const perunChannel = PerunChannelEntity.fromObject({
71-
// channelId: channelIdToString(new Uint8Array(channel.id.data)),
72-
// allocation: channel.allocation,
73-
// data: channel.data,
74-
// isFinal: channel.isFinal,
75-
// version: channel.version.toString(),
76-
// })
77-
// await PerunPersistorService.updateChannel(perunChannel)
78-
// const res = await PerunPersistorService.getChannels()
79-
// PerunChannelSubject.next(res)
80-
// }
81-
// }
82-
} catch (err) {
83-
logger.warn(`restoreChannels error: ${err}`)
84-
}
85-
})
66+
// interval(20000).subscribe(async () => {
67+
// try {
68+
// // const res = await PerunController.serviceClient.restoreChannels(new Uint8Array([]))
69+
// // if (res.accepted) {
70+
// // for (const channel of channels) {
71+
// // const perunChannel = PerunChannelEntity.fromObject({
72+
// // channelId: channelIdToString(new Uint8Array(channel.id.data)),
73+
// // allocation: channel.allocation,
74+
// // data: channel.data,
75+
// // isFinal: channel.isFinal,
76+
// // version: channel.version.toString(),
77+
// // })
78+
// // await PerunPersistorService.updateChannel(perunChannel)
79+
// // const res = await PerunPersistorService.getChannels()
80+
// // PerunChannelSubject.next(res)
81+
// // }
82+
// // }
83+
// } catch (err) {
84+
// logger.warn(`restoreChannels error: ${err}`)
85+
// }
86+
// })
8687
}
8788

8889
private registerHandlers = () => {
@@ -110,21 +111,32 @@ export default class PerunController {
110111
public perunServiceAction(params: Controller.Params.PerunServiceActionParams): Promise<Controller.Response> {
111112
logger.info('PerunController: perunServiceAction-----PerunController-----', params.type)
112113
switch (params.type) {
114+
case 'startup':
115+
return this.startupChannelServiceRunner(params.payload);
113116
case 'open':
114-
return this.openChannel(params.payload as Controller.Params.OpenChannelParams)
117+
return this.openChannel(params.payload)
115118
case 'update':
116-
return this.updateChannel(params.payload as Controller.Params.UpdateChannelParams)
119+
return this.updateChannel(params.payload)
117120
case 'close':
118-
return this.closeChannel(params.payload as Controller.Params.CloseChannelParams)
121+
return this.closeChannel(params.payload)
119122
case 'get':
120-
return this.getChannels(params.payload as Controller.Params.GetChannelsParams)
123+
return this.getChannels(params.payload)
121124
case 'restore':
122-
return this.restoreChannels(params.payload as Controller.Params.RestoreChannelsParams)
125+
return this.restoreChannels(params.payload)
123126
default:
124127
return Promise.reject(new Error('Invalid perun service action type'))
125128
}
126129
}
127130

131+
async startupChannelServiceRunner(opt: Controller.Params.PerunChannelServiceRunnerStartupsParams) {
132+
const flag = await PerunService.getInstance().startChannelServiceRunner(opt)
133+
return {
134+
status: flag ? ResponseCode.Success : ResponseCode.Fail,
135+
result: flag,
136+
message: flag ? "" : "Failed to start PerunChannelServiceRunner"
137+
} as Controller.Response
138+
}
139+
128140
async openChannel(params: Controller.Params.OpenChannelParams): Promise<Controller.Response> {
129141
logger.info('PerunController: openChannel----------', params)
130142
const alloc = Allocation.create({
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import path from "path";
2+
import SettingsService from "../settings";
3+
// import { NetworkType } from "../../models/network";
4+
5+
6+
const pathToFile = (...pathes: string[]) => path.join(SettingsService.getInstance().getPeurnDataFolderPath(), ...pathes);
7+
8+
export type ConfigFileOptions = Controller.Params.PerunChannelServiceRunnerStartupsParams
9+
export default function generateConfigFiles(opt: ConfigFileOptions) {
10+
const config = genConfig(opt);
11+
const contractCellDeps = genContractCellDeps();
12+
const systemScripts = genSystemScripts();
13+
return { config, contractCellDeps, systemScripts }
14+
}
15+
16+
const genConfig = (opt: ConfigFileOptions) => {
17+
const network = opt.network; // testnet | mainnet
18+
return {
19+
"host": ":4322",
20+
"ws_url": "localhost:50051",
21+
"network": network, // testnet | devnet
22+
"testnet_rpc_node_url": "https://testnet.ckbapp.dev/",
23+
"devnet_rpc_node_url": "http://localhost:8114",
24+
"public_key": opt.publicKey.startsWith("0x") ? opt.publicKey.slice(2) : opt.publicKey,
25+
// todo
26+
"sudt_owner_lock_arg": "0x4472b33b4e1845ebe82f2ce5f511bbe012f144c5f3d7b539909adffc83ccda61",
27+
"database": pathToFile(network, "db"),
28+
"logfile": pathToFile(network, ".logfile")
29+
}
30+
}
31+
32+
33+
const genContractCellDeps = () => {
34+
return {
35+
"cell_recipes": [{
36+
"name": "pcts",
37+
"tx_hash": "0x53a626d4da20647af1a616e97c00aa942dc70c130dddc5cccd17f231b75f201d",
38+
"index": 0,
39+
"occupied_capacity": 16981300000000,
40+
"data_hash": "0xbee5391c58d4c1eeb37838ca39837406aa2fd2c7f65a1222210dd609f876faa7",
41+
"type_id": null
42+
}, {
43+
"name": "pcls",
44+
"tx_hash": "0xd196a37a04320b185fba078691ccca65e7ac8d6dba3936218258274af37afb48",
45+
"index": 0,
46+
"occupied_capacity": 4670100000000,
47+
"data_hash": "0x4d86e1541c0b95fb41b7ad2c1b78a543c72187e0a0bbd60e8e8afb1fb3c46744",
48+
"type_id": null
49+
}, {
50+
"name": "pfls",
51+
"tx_hash": "0x5a8a6246cda19bc542ee1265de6d5a31a86e9139fb7564af5ae4fbf35c1d93f1",
52+
"index": 0,
53+
"occupied_capacity": 3441300000000,
54+
"data_hash": "0x8c2d2f2f9468cf847823286ac11da09bfaef3e72a8acbac4e457ca4beadedfe4",
55+
"type_id": null
56+
}, {
57+
// todo
58+
"name": "sudt",
59+
"tx_hash": "0x8067400013eda32e47ee5bfd98758493e80cc6e0dd369d618bc8870a3b1d1eca",
60+
"index": 0,
61+
"occupied_capacity": 2622100000000,
62+
"data_hash": "0x2a8bdd8f9f9877e3243a5ecc4a6b23799db03ec9bdd46b69153842c5ad75d632",
63+
"type_id": null
64+
}],
65+
"dep_group_recipes": []
66+
}
67+
}
68+
69+
70+
const genSystemScripts = () => ({
71+
"dao": {
72+
"cell_dep": {
73+
"dep_type": "code",
74+
"out_point": {
75+
"index": "0x2",
76+
"tx_hash": "0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f"
77+
}
78+
},
79+
"script_id": {
80+
"code_hash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
81+
"hash_type": "type"
82+
}
83+
},
84+
"secp256k1_blake160_multisig_all": {
85+
"cell_dep": {
86+
"dep_type": "dep_group",
87+
"out_point": {
88+
"index": "0x1",
89+
"tx_hash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37"
90+
}
91+
},
92+
"script_id": {
93+
"code_hash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8",
94+
"hash_type": "type"
95+
}
96+
},
97+
"secp256k1_blake160_sighash_all": {
98+
"cell_dep": {
99+
"dep_type": "dep_group",
100+
"out_point": {
101+
"index": "0x0",
102+
"tx_hash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37"
103+
}
104+
},
105+
"script_id": {
106+
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
107+
"hash_type": "type"
108+
}
109+
},
110+
"secp256k1_data": {
111+
"out_point": {
112+
"index": "0x3",
113+
"tx_hash": "0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f"
114+
}
115+
},
116+
"type_id": {
117+
"script_id": {
118+
"code_hash": "0x00000000000000000000000000000000000000000000000000545950455f4944",
119+
"hash_type": "type"
120+
}
121+
}
122+
}
123+
)
124+
125+
// todo 现在channel-service里锁定了sudt的hashType为data1,导致也不能更换为usdi
126+
// const mapCodehashToCellDeps = {
127+
// // sudt
128+
// "0x2a8bdd8f9f9877e3243a5ecc4a6b23799db03ec9bdd46b69153842c5ad75d632": {
129+
// "name": "sudt",
130+
// "tx_hash": "0x8067400013eda32e47ee5bfd98758493e80cc6e0dd369d618bc8870a3b1d1eca",
131+
// "index": 0,
132+
// "occupied_capacity": 2622100000000,
133+
// "data_hash": "0x2a8bdd8f9f9877e3243a5ecc4a6b23799db03ec9bdd46b69153842c5ad75d632",
134+
// "type_id": null
135+
// },
136+
// }
137+
138+
// const usdiCellDeps = {
139+
// "name": "sudt",
140+
// "tx_hash": "0xaec423c2af7fe844b476333190096b10fc5726e6d9ac58a9b71f71ffac204fee",
141+
// "index": 0,
142+
// "occupied_capacity": 10804600000000,
143+
// "data_hash": "0x28a734e118e2f972004f44063a2e795088ca2ed1340dc48ad85ac9d84dcc94d8",
144+
// "type_id": null
145+
// }

0 commit comments

Comments
 (0)