From 623cf6d6da0f8a9757a89979284166632e6a6a9a Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sat, 25 Sep 2021 15:55:11 +0200 Subject: [PATCH] Fix deprecation warninsg --- lib/contracts/reimbursement.js | 80 +++++++++++++++++----------------- scripts/list-reimbursements.js | 45 +++++++++++++------ 2 files changed, 73 insertions(+), 52 deletions(-) diff --git a/lib/contracts/reimbursement.js b/lib/contracts/reimbursement.js index 24c992f..7bd81df 100644 --- a/lib/contracts/reimbursement.js +++ b/lib/contracts/reimbursement.js @@ -1,64 +1,66 @@ -const Record = require('./record'); -const ExpenseSerializer = require('../serializers/expense'); +const Record = require("./record"); +const ExpenseSerializer = require("../serializers/expense"); class Reimbursement extends Record { - get count () { - return this.functions.reimbursementsCount(); + get count() { + return this.contract.reimbursementsCount(); } - getById (id) { - return this.functions.get(id) - .then(data => { - return this.ipfs.catAndMerge(data, (ipfsDocument) => { - const expenses = JSON.parse(ipfsDocument); - return { expenses }; - }); + getById(id) { + return this.contract.get(id).then((data) => { + return this.ipfs.catAndMerge(data, (ipfsDocument) => { + const expenses = JSON.parse(ipfsDocument); + return { expenses }; }); + }); } - getData (id) { - return this.functions.getReimbursement(id); + getData(id) { + return this.contract.getReimbursement(id); } - async add (attrs, callOptions = {}) { + async add(attrs, callOptions = {}) { const amount = parseInt(attrs.amount); const token = attrs.token; const recipientId = attrs.recipientId; - const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) ); + const expenses = attrs.expenses.map((e) => new ExpenseSerializer(e)); let errorMessage; - if (typeof amount !== 'number' || amount <= 0) { - errorMessage = 'Invalid data: amount must be a positive number.'; + if (typeof amount !== "number" || amount <= 0) { + errorMessage = "Invalid data: amount must be a positive number."; } - if (!token || token === '') { - errorMessage = 'Invalid data: token must be a token address.'; + if (!token || token === "") { + errorMessage = "Invalid data: token must be a token address."; } - if (!recipientId || recipientId === '') { - errorMessage = 'Invalid data: recipientId is required.'; + if (!recipientId || recipientId === "") { + errorMessage = "Invalid data: recipientId is required."; } if (expenses.length === 0) { - errorMessage = 'Invalid data: at least one expense item is required.'; + errorMessage = "Invalid data: at least one expense item is required."; + } + if (errorMessage) { + return Promise.reject(new Error(errorMessage)); } - if (errorMessage) { return Promise.reject(new Error(errorMessage)); } - return Promise.all(expenses.map(e => e.validate())) - .then(() => { - const jsonStr = JSON.stringify(expenses.map(e => e.data), null, 2); - return this.ipfs - .add(jsonStr) - .then(ipfsHashAttr => { - const reimbursement = [ - amount, - token, - parseInt(recipientId), - ipfsHashAttr.hashDigest, - ipfsHashAttr.hashFunction, - ipfsHashAttr.hashSize, - ]; + return Promise.all(expenses.map((e) => e.validate())).then(() => { + const jsonStr = JSON.stringify( + expenses.map((e) => e.data), + null, + 2 + ); + return this.ipfs.add(jsonStr).then((ipfsHashAttr) => { + const reimbursement = [ + amount, + token, + parseInt(recipientId), + ipfsHashAttr.hashDigest, + ipfsHashAttr.hashFunction, + ipfsHashAttr.hashSize, + ]; - return this.functions.add(...reimbursement, callOptions); - }); + return this.contract.add(...reimbursement, callOptions); }); + }); } } diff --git a/scripts/list-reimbursements.js b/scripts/list-reimbursements.js index fa38e81..ae05da3 100644 --- a/scripts/list-reimbursements.js +++ b/scripts/list-reimbursements.js @@ -1,25 +1,38 @@ -const promptly = require('promptly'); -const Table = require('cli-table'); +const promptly = require("promptly"); +const Table = require("cli-table"); const { ethers } = require("hardhat"); -const Kredits = require('../lib/kredits'); +const Kredits = require("../lib/kredits"); async function main() { - kredits = new Kredits(hre.ethers.provider, hre.ethers.provider.getSigner()) + kredits = new Kredits(hre.ethers.provider, hre.ethers.provider.getSigner()); await kredits.init(); - console.log(`Using Reimbursement at: ${kredits.Reimbursement.contract.address}`); + console.log( + `Using Reimbursement at: ${kredits.Reimbursement.contract.address}` + ); const table = new Table({ - head: ['ID', 'Amount', 'Token', 'recipientId', 'Confirmed?', 'Vetoed?', 'IPFS', 'Expenses'] - }) + head: [ + "ID", + "Amount", + "Token", + "recipientId", + "Confirmed?", + "Vetoed?", + "IPFS", + "Expenses", + ], + }); let blockNumber = await kredits.provider.getBlockNumber(); - let reimbursements = await kredits.Reimbursement.all({page: {size: 1000}}); + let reimbursements = await kredits.Reimbursement.all({ + page: { size: 1000 }, + }); let kreditsSum = 0; console.log(`Current block number: ${blockNumber}`); - reimbursements.forEach(r => { + reimbursements.forEach((r) => { const confirmed = r.confirmedAtBlock <= blockNumber; table.push([ @@ -30,15 +43,21 @@ async function main() { `${confirmed}`, `${r.vetoed}`, `${r.ipfsHash}`, - `${r.expenses.length}` + `${r.expenses.length}`, ]); }); console.log(table.toString()); - let totalAmountUnconfirmed = await kredits.Reimbursement.functions.totalAmount(false); - let totalAmountConfirmed = await kredits.Reimbursement.functions.totalAmount(true); - console.log(`Total: ${totalAmountConfirmed} (confirmed) | ${totalAmountUnconfirmed} (including unconfirmed)`); + let totalAmountUnconfirmed = await kredits.Reimbursement.contract.totalAmount( + false + ); + let totalAmountConfirmed = await kredits.Reimbursement.contract.totalAmount( + true + ); + console.log( + `Total: ${totalAmountConfirmed} (confirmed) | ${totalAmountUnconfirmed} (including unconfirmed)` + ); } main();