Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
store.account.set(sender.address, updatedSender);
/* --- Modify packet account --- */
/**
* Update the packet account:
* - Add the postage to the packet account balance
* - Add all important data about the packet inside the asset field:
* - recipient: ID of the packet recipient
* - sender: ID of the packet sender
* - carrier: ID of the packet carrier
* - security: Number of tokens the carrier needs to lock during the transport of the packet
* - postage: Number of tokens the sender needs to pay for transportation of the packet
* - minTrust: Minimal trust that is needed to be carrier for the packet
* - status: Status of the transport (pending|ongoing|success|fail)
*/
const packetBalanceWithPostage = new utils.BigNum(packet.balance).add(
new utils.BigNum(this.asset.postage)
);
const updatedPacketAccount = {
...packet,
...{
balance: packetBalanceWithPostage.toString(),
asset: {
recipient: this.recipientId,
sender: this.senderId,
security: this.asset.security,
postage: this.asset.postage,
minTrust: this.asset.minTrust,
status: 'pending',
carrier: null
}
undoAsset(store) {
const errors = [];
const packet = store.account.get(this.recipientId);
const carrier = store.account.get(packet.carrier);
const sender = store.account.get(packet.sender);
/* --- Revert successful transport --- */
if ( this.asset.status === "success") {
/* --- Revert carrier account --- */
const carrierBalanceWithoutSecurityAndPostage = new utils.BigNum(carrier.balance).sub(new utils.BigNum(packet.asset.security)).sub(new utils.BigNum(packet.asset.postage));
carrier.balance = carrierBalanceWithoutSecurityAndPostage.toString();
carrier.asset.lockedSecurity = packet.asset.security;
carrier.asset.trust--;
store.account.set(carrier.address, carrier);
/* --- Revert failed transport --- */
} else {
/* --- Revert sender account --- */
const senderBalanceWithoutSecurityAndPostage = new utils.BigNum(sender.balance).sub(new utils.BigNum(packet.asset.security)).sub(new utils.BigNum(packet.asset.postage));
sender.balance = senderBalanceWithoutSecurityAndPostage.toString();
store.account.set(sender.address, sender);
/* --- Revert carrier account --- */
carrier.asset.trust++;
carrier.asset.lockedSecurity = packet.asset.security;
applyAsset(store) {
const errors = [];
const packet = store.account.get(this.asset.packetId);
if (!packet.asset.status) {
/* --- Modify sender account --- */
/**
* Update the sender account:
* - Deduct the postage from senders' account balance
*/
const sender = store.account.get(this.senderId);
const senderBalancePostageDeducted = new utils.BigNum(sender.balance).sub(
new utils.BigNum(this.asset.postage)
);
const updatedSender = {
...sender,
balance: senderBalancePostageDeducted.toString(),
};
store.account.set(sender.address, updatedSender);
/* --- Modify packet account --- */
/**
* Update the packet account:
* - Add the postage to the packet account balance
* - Add all important data about the packet inside the asset field:
* - recipient: ID of the packet recipient
* - sender: ID of the packet sender
* - carrier: ID of the packet carrier
* - security: Number of tokens the carrier needs to lock during the transport of the packet
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
*/
*/
/* 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
*/
const senderBalanceWithSecurityAndPostage = new utils.BigNum(sender.balance).add(new utils.BigNum(packet.asset.security)).add(new utils.BigNum(packet.asset.postage));
sender.balance = senderBalanceWithSecurityAndPostage.toString();
store.account.set(sender.address, sender);
/**
* Update the Carrier account:
* - Reduce trust by 1
* - Set lockedSecurity to 0
*/
carrier.asset.trust = carrier.asset.trust ? --carrier.asset.trust : -1;
carrier.asset.lockedSecurity = null;
store.account.set(carrier.address, carrier);
/**
* Update the Packet account:
* - set status to "fail"
applyAsset(store) {
const errors = [];
const packet = store.account.get(this.asset.packetId);
if (!packet.asset.status) {
/* --- Modify sender account --- */
/**
* Update the sender account:
* - Deduct the postage from senders' account balance
*/
const sender = store.account.get(this.senderId);
const senderBalancePostageDeducted = new utils.BigNum(sender.balance).sub(
new utils.BigNum(this.asset.postage)
);
const updatedSender = {
...sender,
balance: senderBalancePostageDeducted.toString(),
};
store.account.set(sender.address, updatedSender);
/* --- Modify packet account --- */
/**
* Update the packet account:
* - Add the postage to the packet account balance
* - Add all important data about the packet inside the asset field:
* - recipient: ID of the packet recipient
* - sender: ID of the packet sender
* - carrier: ID of the packet carrier
undoAsset(store) {
const errors = [];
/* --- Revert sender account --- */
const sender = store.account.get(this.senderId);
const senderBalanceWithPostage = new utils.BigNum(sender.balance).add(
new utils.BigNum(this.asset.postage)
);
const updatedSender = {
...sender,
balance: senderBalanceWithPostage.toString()
};
store.account.set(sender.address, updatedSender);
/* --- Revert packet account --- */
const packet = store.account.get(this.asset.packetId);
const originalPacketAccount = /* Task: The missing UndoAsset logic comes here */
store.account.set(packet.address, originalPacketAccount);
return errors;
}
applyAsset(store) {
const errors = [];
const packet = store.account.get(this.asset.packetId);
const owner = store.account.get(this.senderId);
const ownerBalanceWithoutPorto = new utils.BigNum(owner.balance).sub(
new utils.BigNum(this.asset.porto)
);
const packetBalanceWithPorto = new utils.BigNum(packet.balance).add(
new utils.BigNum(this.asset.porto)
);
const updatedOwner = {
...owner,
balance: ownerBalanceWithoutPorto.toString()
};
store.account.set(owner.address, updatedOwner);
const newObj = {
...packet,
balance : packetBalanceWithPorto.toString(),
asset: {
receipientId: this.asset.receipientId,
receipientLocation: this.asset.receipientLocation,
ownerId: this.senderId,
ownerLocation: this.asset.senderLocation,
security: this.asset.security,
undoAsset(store) {
const packet = store.account.get(this.asset.packetId);
const oldObj = { ...packet, balance: 0, asset: null };
store.account.set(packet.address, oldObj);
const owner = store.account.get(this.senderId);
const ownerBalanceWithPorto = new utils.BigNum(owner.balance).add(
new utils.BigNum(this.asset.porto)
);
const updatedOwner = {
...owner,
balance: ownerBalanceWithPorto.toString()
};
store.account.set(owner.address, updatedOwner);
return [];
}
applyAsset(store) {
const errors = [];
const packet = store.account.get(this.asset.packetId);
const owner = store.account.get(this.senderId);
const ownerBalanceWithoutPorto = new utils.BigNum(owner.balance).sub(
new utils.BigNum(this.asset.porto)
);
const packetBalanceWithPorto = new utils.BigNum(packet.balance).add(
new utils.BigNum(this.asset.porto)
);
const updatedOwner = {
...owner,
balance: ownerBalanceWithoutPorto.toString()
};
store.account.set(owner.address, updatedOwner);
const newObj = {
...packet,
balance : packetBalanceWithPorto.toString(),
asset: {
receipientId: this.asset.receipientId,
receipientLocation: this.asset.receipientLocation,