@rsksmart/rif-storage-pinning
Version:
Application for providing your storage space to other to use in exchange of RIF Tokens
148 lines (147 loc) • 6.82 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_typescript_1 = require("sequelize-typescript");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const sequelize_1 = require("../sequelize");
const utils_1 = require("../utils");
const job_model_1 = __importDefault(require("./job.model"));
let Agreement = class Agreement extends sequelize_typescript_1.Model {
periodPrice() {
return this.size.times(this.billingPrice);
}
getPeriodsSinceLastPayout(floor = true) {
// Date.now = ms
// this.lastPayout.getTime = ms
// this.billingPeriod = seconds ==> * 1000
const periods = new bignumber_js_1.default(Date.now() - this.lastPayout.getTime()).div(this.billingPeriod.times(1000));
return floor
? utils_1.bnFloor(periods)
: periods;
}
getToBePayedOut(floor = true) {
const amountToPay = this.getPeriodsSinceLastPayout(floor).times(this.periodPrice());
return amountToPay.lte(this.availableFunds)
? utils_1.bnFloor(amountToPay)
: this.availableFunds;
}
get numberOfPrepaidPeriods() {
return this.periodPrice().gt(0)
? utils_1.bnFloor(this.availableFunds.div(this.periodPrice()))
: new bignumber_js_1.default(0);
}
get periodsSinceLastPayout() {
return this.getPeriodsSinceLastPayout();
}
get toBePayedOut() {
return this.getToBePayedOut();
}
/**
* Helper which specifies if the Agreement has at the moment of the call
* sufficient funds for at least one more period.
*/
get hasSufficientFunds() {
return this.availableFunds.minus(this.getToBePayedOut()).gte(this.periodPrice());
}
/**
* Field represents the time (in seconds) until the agreement expires
*/
get expiresIn() {
if (!this.hasSufficientFunds)
return new bignumber_js_1.default(0);
const availableFundsAfterPayout = this.availableFunds.minus(this.getToBePayedOut(false));
return utils_1.bnFloor(availableFundsAfterPayout.div(this.periodPrice()).times(this.billingPeriod));
}
};
__decorate([
sequelize_typescript_1.Column({ type: sequelize_typescript_1.DataType.STRING(67), primaryKey: true, allowNull: false }),
__metadata("design:type", String)
], Agreement.prototype, "agreementReference", void 0);
__decorate([
sequelize_typescript_1.Column({ type: sequelize_typescript_1.DataType.STRING(), allowNull: false }),
__metadata("design:type", String)
], Agreement.prototype, "dataReference", void 0);
__decorate([
sequelize_typescript_1.Column({ type: sequelize_typescript_1.DataType.STRING(64), allowNull: false }),
__metadata("design:type", String)
], Agreement.prototype, "consumer", void 0);
__decorate([
sequelize_typescript_1.Column(Object.assign({ allowNull: false }, sequelize_1.BigNumberStringType('size'))),
__metadata("design:type", bignumber_js_1.default)
], Agreement.prototype, "size", void 0);
__decorate([
sequelize_typescript_1.Column({ defaultValue: true }),
__metadata("design:type", Boolean)
], Agreement.prototype, "isActive", void 0);
__decorate([
sequelize_typescript_1.Column(Object.assign({ allowNull: false }, sequelize_1.BigNumberStringType('billingPeriod'))),
__metadata("design:type", bignumber_js_1.default)
], Agreement.prototype, "billingPeriod", void 0);
__decorate([
sequelize_typescript_1.Column(Object.assign({ allowNull: false }, sequelize_1.BigNumberStringType('billingPrice'))),
__metadata("design:type", bignumber_js_1.default)
], Agreement.prototype, "billingPrice", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], Agreement.prototype, "tokenAddress", void 0);
__decorate([
sequelize_typescript_1.Column(Object.assign({ allowNull: false }, sequelize_1.BigNumberStringType('availableFunds'))),
__metadata("design:type", bignumber_js_1.default)
], Agreement.prototype, "availableFunds", void 0);
__decorate([
sequelize_typescript_1.Column({ allowNull: false }),
__metadata("design:type", Date)
], Agreement.prototype, "lastPayout", void 0);
__decorate([
sequelize_typescript_1.Column({ type: sequelize_typescript_1.DataType.NUMBER() }),
__metadata("design:type", Object)
], Agreement.prototype, "expiredAtBlockNumber", void 0);
__decorate([
sequelize_typescript_1.HasMany(() => job_model_1.default),
__metadata("design:type", Array)
], Agreement.prototype, "jobs", void 0);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.VIRTUAL),
__metadata("design:type", bignumber_js_1.default),
__metadata("design:paramtypes", [])
], Agreement.prototype, "numberOfPrepaidPeriods", null);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.VIRTUAL),
__metadata("design:type", bignumber_js_1.default),
__metadata("design:paramtypes", [])
], Agreement.prototype, "periodsSinceLastPayout", null);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.VIRTUAL),
__metadata("design:type", bignumber_js_1.default),
__metadata("design:paramtypes", [])
], Agreement.prototype, "toBePayedOut", null);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.VIRTUAL),
__metadata("design:type", Boolean),
__metadata("design:paramtypes", [])
], Agreement.prototype, "hasSufficientFunds", null);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.VIRTUAL),
__metadata("design:type", bignumber_js_1.default),
__metadata("design:paramtypes", [])
], Agreement.prototype, "expiresIn", null);
Agreement = __decorate([
sequelize_typescript_1.Table({
freezeTableName: true,
tableName: 'storage_agreement',
timestamps: false
})
], Agreement);
exports.default = Agreement;