Skip to content

Commit

Permalink
add transfers to upgrade tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrowDom committed Dec 3, 2024
1 parent 7225364 commit 6d95c43
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contracts/deploy/base/021_upgrade_oeth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { deployWithConfirmation } = require("../../utils/deploy");
module.exports = deployOnBaseWithGuardian(
{
deployName: "021_upgrade_oeth",
forceSkip: true
forceSkip: false
},
async ({ ethers }) => {
const dOETHb = await deployWithConfirmation("OETHBase");
Expand Down
2 changes: 1 addition & 1 deletion contracts/deploy/mainnet/110_oeth_upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = deploymentWithGovernanceProposal(
{
deployName: "110_oeth_upgrade",
forceDeploy: false,
forceSkip: true,
forceSkip: false,
reduceQueueTime: true,
deployerIsProposer: false,
proposalId: "",
Expand Down
45 changes: 45 additions & 0 deletions contracts/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { parseUnits, formatUnits, keccak256, toUtf8Bytes } =
const { BigNumber } = require("ethers");

const addresses = require("../utils/addresses");
const { impersonateAndFund } = require("../utils/signers");
const { decimalsFor, units } = require("../utils/units");
const fs = require('fs');

Expand Down Expand Up @@ -788,6 +789,49 @@ async function addActualBalancesToSquidData(squidDataCsvFile, outputFileName, to
fs.writeFileSync(outputFileName, csvContent);
}

async function testTransfersOnTokenContract(balancesFile, upgradedTokenContract) {
// CSV file in format account, squidBalance, onChainBalance
const data = fs.readFileSync(balancesFile, "utf8").split('\n');
let transferBalanceMissmatches = 0;
for (let i = 1; i < data.length; i++) {
const [accountPrevious,,] = data[i-1].split(',');
const [account,,] = data[i].split(',');

const balanceSender = await upgradedTokenContract.balanceOf(account);
const balanceReceiver = await upgradedTokenContract.balanceOf(accountPrevious);

if (i % 50 == 0) {
console.log(`Validated transfers of ${i + 1} accounts`);
}

if (balanceSender.lte(BigNumber.from(2))) {
// skip small balances
continue;
}

const accountSigner = await impersonateAndFund(account);
// 1/2 balance
const balanceToTransfer = balanceSender.div(BigNumber.from("2"));
await upgradedTokenContract
.connect(accountSigner)
.transfer(accountPrevious, balanceToTransfer);


const balanceSenderAfter = await upgradedTokenContract.balanceOf(account);
const balanceReceiverAfter = await upgradedTokenContract.balanceOf(accountPrevious);

if (!balanceSender.sub(balanceToTransfer).eq(balanceSenderAfter)) {
transferBalanceMissmatches += 1;
console.log(`Balance miss match ${account} expected to have ${balanceSender.sub(balanceToTransfer).toString(10)} actually has ${balanceSenderAfter.toString(10)}`)
}
if (!balanceReceiver.add(balanceToTransfer).eq(balanceReceiverAfter)) {
transferBalanceMissmatches += 1;
console.log(`Balance miss match ${accountPrevious} expected to have ${balanceReceiver.add(balanceToTransfer).toString(10)} actually has ${balanceReceiverAfter.toString(10)}`)
}
}
console.log(`Accounts with unexpected balances: ${transferBalanceMissmatches}`)
}

async function compareUpgradedContractBalances(balancesFile, upgradedTokenContract) {
// CSV file in format account, squidBalance, onChainBalance
const data = fs.readFileSync(balancesFile, "utf8").split('\n');
Expand Down Expand Up @@ -911,4 +955,5 @@ module.exports = {
differenceInStrategyBalance,
addActualBalancesToSquidData,
compareUpgradedContractBalances,
testTransfersOnTokenContract,
};
47 changes: 45 additions & 2 deletions contracts/test/token/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The purpose of upgrade tests is to verify that the token contract upgrades will
1. fetch all accounts from Sub Squid which have positive token balance
2. run a forked node on the same block number and add actual/precise on-chain token balances to those addresses (there are errors / deviations in SubSquid)
3. run a forked node with token contracts upgraded and compare balances
4. run a forked node with token contracts upgraded and validate transfer amounts

## 1. Fetch data from SubSquid

Expand All @@ -13,7 +14,7 @@ yarn run fetch-pre-upgrade
```
This will execute the `scripts/yield-delegation/fetchAllAddresses.js` which fetches the accounts with positive balances and their SubSquid balances. This script should create files: `oethBalances.csv`, `ousdBalances.csv`, `soethBalances.csv`. They are all of format `address, token_balance, block_number`

## 2. & 3. Supplement account with pre-upgrade on-chain data and verify with post upgrade balances
## 2. & 3 & 4. Supplement account with pre-upgrade on-chain data and verify with post upgrade balances

Since this is a 1 off test there is less automation and more manual work required. Open the 3 token upgrade file:
- contracts/deploy/mainnet/109_ousd_upgrade.js
Expand Down Expand Up @@ -58,6 +59,20 @@ After the script executes there should be 0 accounts with not matching balances:
Accounts with difference balances: 0
```

#### Verify post-upgrade token transfers
Keep settings in `contracts/deploy/mainnet/109_ousd_upgrade.js` to `forceSkip=false`
Open test file (`contracts/test/token/ousd.mainnet.fork-test.js`) and remove the previous `.only` modifier and set the `.only` modifier to `Execute transfer and inspect balances` test.

run
```
yarn run test:fork
```

After the script executes there should be 0 accounts with not matching balances:
```
Accounts with difference balances: 0
```

### OETH verification
#### Add pre-upgrade OETH token balances
run
Expand Down Expand Up @@ -89,6 +104,20 @@ After the script executes there should be 0 accounts with not matching balances:
Accounts with difference balances: 0
```

#### Verify post-upgrade token balances
Keep settings in `contracts/deploy/mainnet/110_oeth_upgrade.js` to `forceSkip=false`
Open test file (`contracts/test/token/oeth.mainnet.fork-test.js`) and remove the previous `.only` modifier and set the `.only` modifier to `Execute transfer and inspect balances` test.

run
```
yarn run test:fork
```

After the script executes there should be 0 accounts with not matching balances:
```
Accounts with difference balances: 0
```

### Super OETH verification
#### Add pre-upgrade Super OETH token balances
run
Expand Down Expand Up @@ -118,4 +147,18 @@ yarn run test:base-fork
After the script executes there should be 0 accounts with not matching balances:
```
Accounts with difference balances: 0
```
```

#### Verify post-upgrade token balances
Keep settings in `contracts/deploy/base/021_upgrade_oeth.js` to `forceSkip=false`
Open test file (`contracts/test/token/oeth.base.fork-test.js`) and remove the previous `.only` modifier and set the `.only` modifier to `Execute transfer and inspect balances` test.

run
```
yarn run test:fork
```

After the script executes there should be 0 accounts with not matching balances:
```
Accounts with difference balances: 0
```
14 changes: 11 additions & 3 deletions contracts/test/token/oeth.base.fork-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const { createFixtureLoader } = require("../_fixture");
const { defaultBaseFixture } = require("../_fixture-base");
const { expect } = require("chai");
const addresses = require("../../utils/addresses");
const { addActualBalancesToSquidData, compareUpgradedContractBalances } = require("./../helpers");
const {
addActualBalancesToSquidData,
compareUpgradedContractBalances,
testTransfersOnTokenContract
} = require("./../helpers");

const baseFixture = createFixtureLoader(defaultBaseFixture);

Expand Down Expand Up @@ -40,15 +44,19 @@ describe("ForkTest: OETHb", function () {
// has the actual balances on chain before the contract upgrade
it("Fetch the actual on chain data", async () => {
const { oethb } = fixture;
this.timeout(1000000000);
await addActualBalancesToSquidData('./soethBalances.csv', './soethBalancesCombined.csv', oethb);
});

// run this test with the token contract upgrade so the balances from the previous
// test can be compared to the balances after the upgrade
it("Compare the data before and after the upgrade", async () => {
const { oethb } = fixture;
this.timeout(1000000000);
await compareUpgradedContractBalances('./soethBalancesCombined.csv', oethb);
});

// execute transfer and compare balances
it("Execute transfer and inspect balances", async () => {
const { oethb } = fixture;
await testTransfersOnTokenContract('./soethBalancesCombined.csv', oethb);
});
});
15 changes: 12 additions & 3 deletions contracts/test/token/oeth.mainnet.fork-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const { expect } = require("chai");

const { loadDefaultFixture } = require("./../_fixture");
const { isCI, addActualBalancesToSquidData, compareUpgradedContractBalances } = require("./../helpers");
const {
isCI,
addActualBalancesToSquidData,
compareUpgradedContractBalances,
testTransfersOnTokenContract
} = require("./../helpers");

/**
* Regarding hardcoded addresses:
Expand Down Expand Up @@ -42,16 +47,20 @@ describe("ForkTest: OETH", function () {
// has the actual balances on chain before the contract upgrade
it("Fetch the actual on chain data", async () => {
const { oeth } = fixture;
this.timeout(1000000000);
await addActualBalancesToSquidData('./oethBalances.csv', './oethBalancesCombined.csv', oeth);
});

// run this test with the token contract upgrade so the balances from the previous
// test can be compared to the balances after the upgrade
it("Compare the data before and after the upgrade", async () => {
const { oeth } = fixture;
this.timeout(1000000000);
await compareUpgradedContractBalances('./oethBalancesCombined.csv', oeth);
});

// execute transfer and compare balances
it("Execute transfer and inspect balances", async () => {
const { oeth } = fixture;
await testTransfersOnTokenContract('./oethBalancesCombined.csv', oeth);
});
});
});
15 changes: 12 additions & 3 deletions contracts/test/token/ousd.mainnet.fork-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { loadDefaultFixture } = require("./../_fixture");
const { isCI, addActualBalancesToSquidData, compareUpgradedContractBalances } = require("./../helpers");
const {
isCI,
addActualBalancesToSquidData,
compareUpgradedContractBalances,
testTransfersOnTokenContract
} = require("./../helpers");

/**
* Regarding hardcoded addresses:
Expand Down Expand Up @@ -31,16 +36,20 @@ describe("ForkTest: OUSD", function () {
// has the actual balances on chain before the contract upgrade
it("Fetch the actual on chain data", async () => {
const { ousd } = fixture;
this.timeout(1000000000);
await addActualBalancesToSquidData('./ousdBalances.csv', './ousdBalancesCombined.csv', ousd);
});

// run this test with the token contract upgrade so the balances from the previous
// test can be compared to the balances after the upgrade
it("Compare the data before and after the upgrade", async () => {
const { ousd } = fixture;
this.timeout(1000000000);
await compareUpgradedContractBalances('./ousdBalancesCombined.csv', ousd);
});

// execute transfer and compare balances
it("Execute transfer and inspect balances", async () => {
const { ousd } = fixture;
await testTransfersOnTokenContract('./ousdBalancesCombined.csv', ousd);
});
});
});

0 comments on commit 6d95c43

Please sign in to comment.