/
start-transport.js
116 lines (105 loc) 路 3.7 KB
/
start-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
const {
BaseTransaction,
TransactionError,
utils
} = require('@liskhq/lisk-transactions');
class StartTransportTransaction extends BaseTransaction {
static get TYPE () {
return 21;
}
static get FEE () {
//return `${10 ** 8}`;
return '0';
};
async prepare(store) {
await store.account.cache([
{
address: this.recipientId,
},
{
address: this.senderId,
}
]);
}
validateAsset() {
const errors = [];
return errors;
}
applyAsset(store) {
const errors = [];
const packet = store.account.get(this.recipientId);
if (packet.asset.status === "pending"){
const carrier = store.account.get(this.senderId);
// If the carrier has the trust to transport the packet
const carrierTrust = carrier.asset.trust ? carrier.asset.trust : 0;
const carrierBalance = new utils.BigNum(carrier.balance);
const packetSecurity = new utils.BigNum(packet.asset.security);
if (packet.asset.minTrust <= carrierTrust && carrierBalance.gte(packetSecurity)) {
/**
* Update the Carrier account:
* - Lock security inside the account
* - Remove the security form balance
* - initialize carriertrust, if not present already
*/
const carrierBalanceWithoutSecurity = carrierBalance.sub(packetSecurity);
const carrierTrust = carrier.asset.trust ? carrier.asset.trust : 0;
const updatedCarrier = { /* Write your code here */ };
store.account.set(carrier.address, updatedCarrier);
/**
* Update the Packet account:
* - Set status to "ongoing"
* - set carrier to ID of the carrier
*/
packet.asset.status = "ongoing";
packet.asset.carrier = carrier.address;
store.account.set(packet.address, packet);
} else {
errors.push(
new TransactionError(
'carrier has not enough trust to deliver the packet, or not enough balance to pay the security',
packet.asset.minTrust,
carrier.asset.trust,
packet.asset.security,
carrier.balance
)
);
}
} else {
errors.push(
new TransactionError(
'packet status needs to be "pending"',
packet.asset.status
)
);
}
return errors;
}
undoAsset(store) {
const errors = [];
const packet = store.account.get(this.recipientId);
const carrier = store.account.get(this.senderId);
/* --- Revert carrier account --- */
const carrierBalanceWithSecurity = new utils.BigNum(carrier.balance).add(
new utils.BigNum(packet.assset.security)
);
const updatedCarrier = {
...carrier,
balance: carrierBalanceWithSecurity.toString()
};
store.account.set(carrier.address, updatedCarrier);
/* --- Revert packet account --- */
const updatedData = {
asset: {
deliveryStatus: "pending",
carrier: null
}
};
const newObj = {
...packet,
...updatedData
};
store.account.set(packet.address, newObj);
return errors;
}
}
module.exports = StartTransportTransaction;