function testStakeGetPointsAndunstakeInstantly() public {
uint256 totalAmountStakedBefore = steaking.totalAmountStaked();
uint256 dealAmount = 1 ether;
_stake(user1, dealAmount, user1);
_unstake(user1, dealAmount, user1);
uint256 totalAmountStakedAfter = steaking.totalAmountStaked();
assert(totalAmountStakedAfter == 0 && totalAmountStakedBefore == 0);
}
import dotenv from "dotenv";
import { ethers } from "ethers";
import connectToMongodb from "./utils/connectToMongodb.js";
import getConfig from "./utils/getConfig.js";
import steakingAbi from "./utils/steakingAbi.js";
import steakPointsModel from "./models/steakPoints.js";
import { STAKED, PRECISION } from "./utils/constants.js";
dotenv.config();
async function main() {
await connectToMongodb();
const { rpcUrl, steakingAddress } = getConfig();
const provider = new ethers.JsonRpcProvider(rpcUrl);
const steaking = new ethers.Contract(steakingAddress, steakingAbi, provider);
steaking.on(STAKED, async (_, amount, onBehalfOf) => {
let steakPoints;
steakPoints = await steakPointsModel.findOne({ walletAddress: onBehalfOf });
if (!steakPoints) {
steakPoints = new steakPointsModel({
walletAddress: onBehalfOf,
points: +ethers.formatEther(amount) * PRECISION,
});
} else {
steakPoints.points += +ethers.formatEther(amount) * PRECISION;
}
await steakPoints.save();
});
+ steaking.on("Unstaked", async (_, amount, onBehalfOf) => {
+ let staker = await steakPointsModel.findOne({ walletAddress: onBehalfOf });
+ let steakPoints = {
+ points: staker && staker.points > 0 ? staker.points - +ethers.formatEther(amount) * PRECISION : 0
+ }
+ await steakPointsModel.updateOne({ walletAddress: onBehalfOf },
+ steakPoints
+ );
+
+ });
}
main().catch((error) => {
console.log(error.message);
process.exit(1);
});