@urbanisierung/flethly
Version:
easily sell digital assets with ether
166 lines • 5.61 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FlassetsController = void 0;
const compiledFlassets = __importStar(require("../build/Flassets.json"));
class FlassetsController {
constructor(web3) {
this.web3 = web3;
this.abi = compiledFlassets.abi;
}
async init(address) {
this.flassets = await new this.web3.eth.Contract(this.abi, address);
}
async getAccounts() {
const accounts = await this.web3.eth.getAccounts();
return accounts;
}
async deploy(request) {
this.flassets = await new this.web3.eth.Contract(this.abi)
.deploy({
data: `0x${compiledFlassets.evm.bytecode.object}`,
arguments: [request.priceFeedAddress],
})
.send({ from: request.from, gas: request.gas });
return this.flassets;
}
getAddress() {
if (!this.flassets) {
throw new Error(`Flassets contract not set`);
}
return this.flassets.options.address;
}
async getManager() {
const manager = await this.flassets.methods.manager().call();
return manager;
}
async addAsset(request) {
const transaction = await this.flassets.methods
.add(request.uuid, request.price)
.send({
from: request.from,
gas: request.gas,
});
return transaction;
}
async getAsset(uuid) {
const response = await this.flassets.methods.assets(uuid).call();
const flasset = {
active: Boolean(response.active),
buyersCount: Number(response.buyersCount),
manager: response.manager,
price: Number(response.price),
};
return flasset;
}
async getAssetId(index) {
const response = await this.flassets.methods.assetIds(index).call();
return response;
}
async deactivateAsset(request) {
const transaction = await this.flassets.methods
.deactivateAsset(request.uuid)
.send({
from: request.from,
gas: request.gas,
});
return transaction;
}
async activateAsset(request) {
const transaction = await this.flassets.methods
.activateAsset(request.uuid)
.send({
from: request.from,
gas: request.gas,
});
return transaction;
}
async updateAssetPrice(request) {
const transaction = await this.flassets.methods
.changePrice(request.uuid, request.newPrice)
.send({
from: request.from,
gas: request.gas,
});
return transaction;
}
async buy(request) {
const price = await this.getAssetPrice({
from: request.from,
uuid: request.uuid,
});
const transaction = await this.flassets.methods.buy(request.uuid).send({
from: request.from,
gas: request.gas,
value: price,
});
return transaction;
}
async getBalance() {
const balance = await this.web3.eth.getBalance(this.getAddress());
return Number(balance);
}
async hasBought(request) {
const count = await this.flassets.methods
.hasBought(request.buyer, request.uuid)
.call();
return Number(count);
}
async getMyBoughtAssets(request) {
const assetIds = await this.flassets.methods
.getMyBoughtAssets()
.call({
from: request.from,
});
return assetIds;
}
async getMyBoughtAssetCount(request) {
const count = await this.flassets.methods
.getMyBoughtAssetCount(request.uuid)
.call({ from: request.from });
return Number(count);
}
async getOfferedAssets(request) {
const assetIds = await this.flassets.methods
.getOfferedAssets()
.call({ from: request.from });
return assetIds;
}
async getAssetPrice(request) {
const assetPrice = await this.flassets.methods
.getAssetPrice(request.uuid)
.call();
return assetPrice;
}
async getPrice() {
const response = await this.flassets.methods.getPrice().call();
const price = {
roundId: Number(response.roundID),
price: Number(response.price),
startedAt: Number(response.startedAt),
timestamp: Number(response.timeStamp),
answeredInRound: Number(response.answeredInRound),
};
return price;
}
}
exports.FlassetsController = FlassetsController;
//# sourceMappingURL=flassets.controller.js.map