transfer.js
6.37 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
const Controller = require('egg').Controller;
const nodeSdk = require('@ddn/node-sdk').default;
const accountData = require('../../config/mem_accounts.json')
class TransferController extends Controller {
async postTransaction() {
const { ctx, config } = this;
const body = ctx.request.body;
const rule = {
secret: { type: 'string' },
amount: { type: 'string' },
transaction: { type: 'string' },
};
console.log(body);
try {
ctx.validate(rule);
} catch (error) {
ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
return;
}
const oldNetTransaction = JSON.parse(body.transaction);
if (oldNetTransaction.recipientId !== config.ed25519Address) {
ctx.helper.err({
ctx,
err: '接收地址不正确,请使用正确的地址',
});
return;
}
// const oldNetTransaction = JSON.stringify(body.transaction);
// 旧网转出
// const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
// console.log('就往转账', oldNetData, typeof oldNetData);
// if (!oldNetData || !oldNetData.success) {
// ctx.helper.err({
// ctx,
// data: oldNetData,
// });
// return;
// }
const some = accountData.RECORDS.some(item => {
if (item.address === body.senderAddress) {
return body.amount * 10 ** 8 == item.balance;
}
});
// 加上手续费
body.amount = (body.amount + 0.1) * 10 ** 8;
if (!some) {
ctx.helper.err({
ctx,
err: '金额不正确,请联系社区工作人员',
});
return;
}
const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
ctx.helper.success({
ctx,
data: result,
});
// const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
// const trs = JSON.stringify({ transaction });
// // 上链操作
// const data = await this.service.sdk.pushTransaction(trs);
// if (data.success) {
// ctx.helper.success({
// ctx,
// data,
// });
// ctx.model.Transfer.update({ id: result.id }, { finish: true });
// } else {
// ctx.helper.err({
// ctx,
// data,
// });
// ctx.model.Transfer.update({ id: result.id }, { finish: false });
// }
}
// async haveBalance() {
// const { ctx, config } = this;
// const query = ctx.request.query;
// const rule = {
// address: { type: 'string' },
// };
// console.log(query);
// try {
// ctx.validate(rule, query);
// } catch (error) {
// console.log(error);
// ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
// return;
// }
// const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
// ctx.helper.success({
// ctx,
// data,
// });
// }
async getBalance() {
const { ctx, config } = this;
const query = ctx.request.query;
let balance = 0;
let total = 0
const rule = {
address: { type: 'string' },
};
try {
ctx.validate(rule, query);
} catch (error) {
console.log(error);
ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
return;
}
const transferCount = await this.ctx.model.Transfer.count({ address: query.address });
if (transferCount === 0) {
balance = 0;
} else {
accountData.RECORDS.some(item => {
if (item.address === query.address && item.balance > 0) {
balance = item.balance / (10 ** 8);
return true;
}
if (item.balance > 0) {
total += Number(item.balance)
}
});
}
console.log(total / (10 ** 8))
ctx.helper.success({
ctx,
data: {
account: { balance }
},
});
}
async getTransaction() {
const { ctx, config } = this;
const query = ctx.request.query;
const rule = {
address: { type: 'string' },
};
console.log(query);
try {
ctx.validate(rule, query);
} catch (error) {
console.log(error);
ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
return;
}
const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
ctx.helper.success({
ctx,
data,
});
}
// async asyncAccount() {
// const { ctx, config } = this;
// const limit = 100;
// let asyncCount = 0;
// let errAsyncAccount = 0;
// let balanceIsZero = 0;
// const data = await this.service.sdk.getAccountCount(config.ed25519Url);
// if (data.success) {
// const length = Math.ceil(data.count / 100);
// console.log(data.count)
// for (let index = 0; index < length; index++) {
// const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
// console.log(acountList)
// if (acountList.success) {
// for (let index = 0; index < acountList.accounts.length; index++) {
// const element = acountList.accounts[index];
// if (element.balance == 0) {
// balanceIsZero++
// continue;
// }
// const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
// if (!result) {
// errAsyncAccount++;
// this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
// } else {
// asyncCount++;
// }
// }
// } else {
// this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
// }
// }
// } else {
// this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
// }
// ctx.helper.success({
// ctx,
// data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
// });
// }
}
module.exports = TransferController;