Fix deprecation warninsg

This commit is contained in:
bumi 2021-09-25 15:55:11 +02:00
parent cc9cc53d0b
commit 623cf6d6da
2 changed files with 73 additions and 52 deletions

View File

@ -1,64 +1,66 @@
const Record = require('./record'); const Record = require("./record");
const ExpenseSerializer = require('../serializers/expense'); const ExpenseSerializer = require("../serializers/expense");
class Reimbursement extends Record { class Reimbursement extends Record {
get count () { get count() {
return this.functions.reimbursementsCount(); return this.contract.reimbursementsCount();
} }
getById (id) { getById(id) {
return this.functions.get(id) return this.contract.get(id).then((data) => {
.then(data => { return this.ipfs.catAndMerge(data, (ipfsDocument) => {
return this.ipfs.catAndMerge(data, (ipfsDocument) => { const expenses = JSON.parse(ipfsDocument);
const expenses = JSON.parse(ipfsDocument); return { expenses };
return { expenses };
});
}); });
});
} }
getData (id) { getData(id) {
return this.functions.getReimbursement(id); return this.contract.getReimbursement(id);
} }
async add (attrs, callOptions = {}) { async add(attrs, callOptions = {}) {
const amount = parseInt(attrs.amount); const amount = parseInt(attrs.amount);
const token = attrs.token; const token = attrs.token;
const recipientId = attrs.recipientId; const recipientId = attrs.recipientId;
const expenses = attrs.expenses.map( e => new ExpenseSerializer(e) ); const expenses = attrs.expenses.map((e) => new ExpenseSerializer(e));
let errorMessage; let errorMessage;
if (typeof amount !== 'number' || amount <= 0) { if (typeof amount !== "number" || amount <= 0) {
errorMessage = 'Invalid data: amount must be a positive number.'; errorMessage = "Invalid data: amount must be a positive number.";
} }
if (!token || token === '') { if (!token || token === "") {
errorMessage = 'Invalid data: token must be a token address.'; errorMessage = "Invalid data: token must be a token address.";
} }
if (!recipientId || recipientId === '') { if (!recipientId || recipientId === "") {
errorMessage = 'Invalid data: recipientId is required.'; errorMessage = "Invalid data: recipientId is required.";
} }
if (expenses.length === 0) { 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())) return Promise.all(expenses.map((e) => e.validate())).then(() => {
.then(() => { const jsonStr = JSON.stringify(
const jsonStr = JSON.stringify(expenses.map(e => e.data), null, 2); expenses.map((e) => e.data),
return this.ipfs null,
.add(jsonStr) 2
.then(ipfsHashAttr => { );
const reimbursement = [ return this.ipfs.add(jsonStr).then((ipfsHashAttr) => {
amount, const reimbursement = [
token, amount,
parseInt(recipientId), token,
ipfsHashAttr.hashDigest, parseInt(recipientId),
ipfsHashAttr.hashFunction, ipfsHashAttr.hashDigest,
ipfsHashAttr.hashSize, ipfsHashAttr.hashFunction,
]; ipfsHashAttr.hashSize,
];
return this.functions.add(...reimbursement, callOptions); return this.contract.add(...reimbursement, callOptions);
});
}); });
});
} }
} }

View File

@ -1,25 +1,38 @@
const promptly = require('promptly'); const promptly = require("promptly");
const Table = require('cli-table'); const Table = require("cli-table");
const { ethers } = require("hardhat"); const { ethers } = require("hardhat");
const Kredits = require('../lib/kredits'); const Kredits = require("../lib/kredits");
async function main() { 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(); 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({ 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 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; let kreditsSum = 0;
console.log(`Current block number: ${blockNumber}`); console.log(`Current block number: ${blockNumber}`);
reimbursements.forEach(r => { reimbursements.forEach((r) => {
const confirmed = r.confirmedAtBlock <= blockNumber; const confirmed = r.confirmedAtBlock <= blockNumber;
table.push([ table.push([
@ -30,15 +43,21 @@ async function main() {
`${confirmed}`, `${confirmed}`,
`${r.vetoed}`, `${r.vetoed}`,
`${r.ipfsHash}`, `${r.ipfsHash}`,
`${r.expenses.length}` `${r.expenses.length}`,
]); ]);
}); });
console.log(table.toString()); console.log(table.toString());
let totalAmountUnconfirmed = await kredits.Reimbursement.functions.totalAmount(false); let totalAmountUnconfirmed = await kredits.Reimbursement.contract.totalAmount(
let totalAmountConfirmed = await kredits.Reimbursement.functions.totalAmount(true); false
console.log(`Total: ${totalAmountConfirmed} (confirmed) | ${totalAmountUnconfirmed} (including unconfirmed)`); );
let totalAmountConfirmed = await kredits.Reimbursement.contract.totalAmount(
true
);
console.log(
`Total: ${totalAmountConfirmed} (confirmed) | ${totalAmountUnconfirmed} (including unconfirmed)`
);
} }
main(); main();