/
finish-transport.js
192 lines (177 loc) 路 6.48 KB
/
finish-transport.js
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
/*
* Copyright 漏 2020 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
const {
BaseTransaction,
TransactionError
} = require('@liskhq/lisk-transactions');
class FinishTransportTransaction extends BaseTransaction {
static get TYPE () {
return 22;
}
async prepare(store) {
/**
* Get packet account
*/
await store.account.cache([
{
address: this.asset.packetId,
}
]);
/**
* Get sender and recipient accounts of the packet
*/
const pckt = await store.account.get(this.asset.packetId);
await store.account.cache([
{
address: pckt.asset.carrier,
},
{
address: pckt.asset.sender,
},
]);
}
validateAsset() {
const errors = [];
if (!this.asset.packetId || typeof this.asset.packetId !== 'string') {
errors.push(
new TransactionError(
'Invalid "asset.packetId" defined on transaction',
this.id,
'.asset.packetId',
this.asset.packetId
)
);
}
return errors;
}
async applyAsset(store) {
const errors = [];
let packet = await store.account.get(this.asset.packetId);
let carrier = await store.account.get(packet.asset.carrier);
let sender = await store.account.get(packet.asset.sender);
// if the transaction has been signed by the packet recipient
if (this.asset.senderId === packet.carrier) {
// if the packet status isn't "ongoing" or "alarm"
if (packet.asset.status !== "ongoing" && packet.asset.status !== "alarm") {
errors.push(
new TransactionError(
'FinishTransport can only be triggered, if packet status is "ongoing" or "alarm" ',
this.id,
'ongoing or alarm',
this.asset.status
)
);
return errors;
}
// if the transport was a success
if (this.asset.status === "success") {
/**
* Update the Carrier account:
* - Unlock security
* - Add postage & security to balance
* - Earn 1 trustpoint
*/
/* Write your own code here */
/**
* Update the Packet account:
* - Remove postage from balance
* - Change status to "success"
*/
/* Write your own code here */
return errors;
}
// if the transport failed
/**
* Update the Sender account:
* - Add postage and security to balance
*/
sender.balance = sender.balance + BigInt(packet.asset.security) + BigInt(packet.asset.postage);
store.account.set(sender.address, sender);
/**
* Update the Carrier account:
* - Reduce trust by 1
* - Set lockedSecurity to 0
*/
const trustDec = carrier.asset.trust ? BigInt(carrier.asset.trust) - BigInt(1) : BigInt(-1);
carrier.asset = {
...carrier.asset,
trust: trustDec.toString(),
lockedSecurity: null
};
store.account.set(carrier.address, carrier);
/**
* Update the Packet account:
* - set status to "fail"
* - Remove postage from balance
*/
packet.balance = BigInt('0');
packet.asset = {
...packet.asset,
status: 'fail'
};
store.account.set(packet.address, packet);
return errors;
}
errors.push(
new TransactionError(
'FinishTransport transaction needs to be signed by the recipient of the packet',
this.id,
'.asset.recipient',
this.asset.recipient
)
);
return errors;
}
async undoAsset(store) {
const errors = [];
const packet = await store.account.get(this.asset.packetId);
const carrier = await store.account.get(packet.asset.carrier);
const sender = await store.account.get(packet.asset.sender);
/* --- Revert successful transport --- */
if ( this.asset.status === "success") {
/* --- Revert carrier account --- */
carrier.balance = carrier.balance - BigInt(packet.asset.security) - BigInt(packet.asset.postage);
const trustDec = carrier.asset.trust ? BigInt(carrier.asset.trust) - BigInt(1) : BigInt(-1);
carrier.asset = {
...carrier.asset,
trust: trustDec.toString(),
lockedSecurity: packet.asset.security
};
store.account.set(carrier.address, carrier);
/* --- Revert failed transport --- */
} else {
/* --- Revert sender account --- */
sender.balance = sender.balance - BigInt(packet.asset.security) - BigInt(packet.asset.postage);
store.account.set(sender.address, sender);
/* --- Revert carrier account --- */
const trustInc = carrier.asset.trust ? BigInt(carrier.asset.trust) + BigInt(1) : BigInt(1);
carrier.asset = {
...carrier.asset,
trust: trustInc.toString(),
lockedSecurity: packet.asset.security
};
store.account.set(carrier.address, carrier);
}
/* --- Revert packet account --- */
packet.balance = packet.asset.postage;
packet.asset.status = "ongoing";
packet.asset = {
...packet.asset,
status: 'ongoing'
};
store.account.set(packet.address, packet);
return errors;
}
}
module.exports = FinishTransportTransaction;