Skip to content

Commit a765000

Browse files
committed
test: autoRetire chain state checks
- token balance checks for user and contract - NCT/BCT supply checks
1 parent 7a97b3e commit a765000

File tree

1 file changed

+158
-10
lines changed

1 file changed

+158
-10
lines changed

test/OffsetHelper.test.ts

Lines changed: 158 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,24 +471,98 @@ describe("Offset Helper - autoOffset", function () {
471471
});
472472

473473
describe("Testing autoRetire()", function () {
474-
it("Should retire using an NCT deposit", async function () {
475-
await (await nct.approve(offsetHelper.address, ONE_ETHER)).wait();
474+
it("should retire using an NCT deposit", async function () {
475+
// first we set the initial state
476+
const state: {
477+
userNctBalance: BigNumber;
478+
contractNctBalance: BigNumber;
479+
nctSupply: BigNumber;
480+
}[] = [];
481+
state.push({
482+
userNctBalance: await nct.balanceOf(addr2.address),
483+
contractNctBalance: await nct.balanceOf(offsetHelper.address),
484+
nctSupply: await nct.totalSupply(),
485+
});
476486

487+
// we deposit NCT into OH
488+
await (await nct.approve(offsetHelper.address, ONE_ETHER)).wait();
477489
await (await offsetHelper.deposit(addresses.nct, ONE_ETHER)).wait();
478490

491+
// and we check the state after the deposit
492+
state.push({
493+
userNctBalance: await nct.balanceOf(addr2.address),
494+
contractNctBalance: await nct.balanceOf(offsetHelper.address),
495+
nctSupply: await nct.totalSupply(),
496+
});
497+
expect(
498+
formatEther(state[0].userNctBalance.sub(state[1].userNctBalance)),
499+
"User should have 1 less NCT post deposit"
500+
).to.equal(formatEther(ONE_ETHER));
501+
expect(
502+
formatEther(
503+
state[1].contractNctBalance.sub(state[0].contractNctBalance)
504+
),
505+
"Contract should have 1 more NCT post deposit"
506+
).to.equal(formatEther(ONE_ETHER));
507+
expect(
508+
formatEther(state[0].nctSupply),
509+
"NCT supply should be the same post deposit"
510+
).to.equal(formatEther(state[1].nctSupply));
511+
512+
// we redeem NCT for TCO2 within OH
479513
const redeemReceipt = await (
480514
await offsetHelper.autoRedeem(addresses.nct, ONE_ETHER)
481515
).wait();
482516

483-
if (!redeemReceipt.events) {
484-
return;
485-
}
517+
// and we check the state after the redeem
518+
state.push({
519+
userNctBalance: await nct.balanceOf(addr2.address),
520+
contractNctBalance: await nct.balanceOf(offsetHelper.address),
521+
nctSupply: await nct.totalSupply(),
522+
});
523+
expect(
524+
formatEther(state[1].userNctBalance),
525+
"User should have the same amount of NCT post redeem"
526+
).to.equal(formatEther(state[2].userNctBalance));
527+
expect(
528+
formatEther(
529+
state[1].contractNctBalance.sub(state[2].contractNctBalance)
530+
),
531+
"Contract should have 1 less NCT post redeem"
532+
).to.equal(formatEther(ONE_ETHER));
533+
expect(
534+
formatEther(state[1].nctSupply.sub(state[2].nctSupply)),
535+
"NCT supply should be less by 1 post redeem"
536+
).to.equal(formatEther(ONE_ETHER));
537+
538+
// we get the tco2s and amounts that were redeemed
539+
if (!redeemReceipt.events) throw new Error("No events emitted");
486540
const tco2s =
487541
redeemReceipt.events[redeemReceipt.events.length - 1].args?.tco2s;
488542
const amounts =
489543
redeemReceipt.events[redeemReceipt.events.length - 1].args?.amounts;
490544

545+
// we retire the tco2s
491546
await offsetHelper.autoRetire(tco2s, amounts);
547+
548+
// and we check the state after the retire
549+
state.push({
550+
userNctBalance: await nct.balanceOf(addr2.address),
551+
contractNctBalance: await nct.balanceOf(offsetHelper.address),
552+
nctSupply: await nct.totalSupply(),
553+
});
554+
expect(
555+
formatEther(state[2].userNctBalance),
556+
"User should have the same amount of NCT post retire"
557+
).to.equal(formatEther(state[3].userNctBalance));
558+
expect(
559+
formatEther(state[2].contractNctBalance),
560+
"Contract should have the same amount of NCT post retire"
561+
).to.equal(formatEther(state[3].contractNctBalance));
562+
expect(
563+
formatEther(state[2].nctSupply),
564+
"NCT supply should be the same post retire"
565+
).to.equal(formatEther(state[3].nctSupply));
492566
});
493567

494568
it("Should fail because we haven't redeemed any TCO2", async function () {
@@ -500,24 +574,98 @@ describe("Offset Helper - autoOffset", function () {
500574
).to.be.revertedWith("Insufficient TCO2 balance");
501575
});
502576

503-
it("Should retire using an NCT deposit", async function () {
504-
await (await bct.approve(offsetHelper.address, ONE_ETHER)).wait();
577+
it("should retire using an BCT deposit", async function () {
578+
// first we set the initial state
579+
const state: {
580+
userBctBalance: BigNumber;
581+
contractBctBalance: BigNumber;
582+
bctSupply: BigNumber;
583+
}[] = [];
584+
state.push({
585+
userBctBalance: await bct.balanceOf(addr2.address),
586+
contractBctBalance: await bct.balanceOf(offsetHelper.address),
587+
bctSupply: await bct.totalSupply(),
588+
});
505589

590+
// we deposit BCT into OH
591+
await (await bct.approve(offsetHelper.address, ONE_ETHER)).wait();
506592
await (await offsetHelper.deposit(addresses.bct, ONE_ETHER)).wait();
507593

594+
// and we check the state after the deposit
595+
state.push({
596+
userBctBalance: await bct.balanceOf(addr2.address),
597+
contractBctBalance: await bct.balanceOf(offsetHelper.address),
598+
bctSupply: await bct.totalSupply(),
599+
});
600+
expect(
601+
formatEther(state[0].userBctBalance.sub(state[1].userBctBalance)),
602+
"User should have 1 less BCT post deposit"
603+
).to.equal(formatEther(ONE_ETHER));
604+
expect(
605+
formatEther(
606+
state[1].contractBctBalance.sub(state[0].contractBctBalance)
607+
),
608+
"Contract should have 1 more BCT post deposit"
609+
).to.equal(formatEther(ONE_ETHER));
610+
expect(
611+
formatEther(state[0].bctSupply),
612+
"BCT supply should be the same post deposit"
613+
).to.equal(formatEther(state[1].bctSupply));
614+
615+
// we redeem BCT for TCO2 within OH
508616
const redeemReceipt = await (
509617
await offsetHelper.autoRedeem(addresses.bct, ONE_ETHER)
510618
).wait();
511619

512-
if (!redeemReceipt.events) {
513-
return;
514-
}
620+
// and we check the state after the redeem
621+
state.push({
622+
userBctBalance: await bct.balanceOf(addr2.address),
623+
contractBctBalance: await bct.balanceOf(offsetHelper.address),
624+
bctSupply: await bct.totalSupply(),
625+
});
626+
expect(
627+
formatEther(state[1].userBctBalance),
628+
"User should have the same amount of BCT post redeem"
629+
).to.equal(formatEther(state[2].userBctBalance));
630+
expect(
631+
formatEther(
632+
state[1].contractBctBalance.sub(state[2].contractBctBalance)
633+
),
634+
"Contract should have 1 less BCT post redeem"
635+
).to.equal(formatEther(ONE_ETHER));
636+
expect(
637+
formatEther(state[1].bctSupply.sub(state[2].bctSupply)),
638+
"BCT supply should be less by 1 post redeem"
639+
).to.equal(formatEther(ONE_ETHER));
640+
641+
// we get the tco2s and amounts that were redeemed
642+
if (!redeemReceipt.events) throw new Error("No events emitted");
515643
const tco2s =
516644
redeemReceipt.events[redeemReceipt.events.length - 1].args?.tco2s;
517645
const amounts =
518646
redeemReceipt.events[redeemReceipt.events.length - 1].args?.amounts;
519647

648+
// we retire the tco2s
520649
await offsetHelper.autoRetire(tco2s, amounts);
650+
651+
// and we check the state after the retire
652+
state.push({
653+
userBctBalance: await bct.balanceOf(addr2.address),
654+
contractBctBalance: await bct.balanceOf(offsetHelper.address),
655+
bctSupply: await bct.totalSupply(),
656+
});
657+
expect(
658+
formatEther(state[2].userBctBalance),
659+
"User should have the same amount of BCT post retire"
660+
).to.equal(formatEther(state[3].userBctBalance));
661+
expect(
662+
formatEther(state[2].contractBctBalance),
663+
"Contract should have the same amount of BCT post retire"
664+
).to.equal(formatEther(state[3].contractBctBalance));
665+
expect(
666+
formatEther(state[2].bctSupply),
667+
"BCT supply should be the same post retire"
668+
).to.equal(formatEther(state[3].bctSupply));
521669
});
522670
});
523671

0 commit comments

Comments
 (0)