Skip to content

Instantly share code, notes, and snippets.

@ajb413
Last active November 1, 2022 20:23
Show Gist options
  • Save ajb413/deb208e411c7e812620a4eb9ede7cdd5 to your computer and use it in GitHub Desktop.
Save ajb413/deb208e411c7e812620a4eb9ede7cdd5 to your computer and use it in GitHub Desktop.
An example scenario for Comet. This will only work on mainnet. It is not generic enough to be a core scenario. For educational purposes only.
import { scenario } from './context/CometContext';
import { expect } from 'chai';
import { utils } from 'ethers';
import { exp } from '../test/helpers';
import { calldata } from '../src/deploy';
import { impersonateAddress } from '../plugins/scenario/utils';
import { erc20 } from '../plugins/scenario/utils/ERC20';
import { isBridgedDeployment } from './utils';
const ZRX_WHALES = {
mainnet: [
'0x57ca561798413a20508B6bC997481E784F3E6e5f',
'0xBE0eB53F46cd790Cd13851d5EFf43D12404d33E8',
]
};
scenario.only('add new asset zrx',
{
filter: async (ctx) => !isBridgedDeployment(ctx),
tokenBalances: {
$comet: { $base: '>= 1000' },
},
},
async ({ comet, configurator, proxyAdmin, actors }, context) => {
const { albert } = actors;
const dm = context.world.deploymentManager;
const zerox = await dm.existing('ZRX', '0xE41d2489571d322189246DaFA5ebDe1F4699F498');
const zeroxPricefeed = await dm.existing('ZRX:priceFeed', '0x2885d15b8Af22648b98B122b22FDF4D2a56c6023');
// Allocate some tokens to Albert
const zrxWhaleSigner = await impersonateAddress(dm, ZRX_WHALES.mainnet[0]);
await zerox.connect(zrxWhaleSigner).transfer(albert.address, exp(100_000, 18).toString());
// Execute a governance proposal to:
// 1. Add new asset via Configurator
// 2. Deploy and upgrade to new implementation of Comet
const newAssetConfig = {
asset: zerox.address,
priceFeed: zeroxPricefeed.address,
decimals: await zerox.decimals(),
borrowCollateralFactor: exp(0.8, 18),
liquidateCollateralFactor: exp(0.85, 18),
liquidationFactor: exp(0.95, 18),
supplyCap: exp(1_000_000, 18),
};
const addAssetCalldata = await calldata(configurator.populateTransaction.addAsset(comet.address, newAssetConfig));
const deployAndUpgradeToCalldata = utils.defaultAbiCoder.encode(['address', 'address'], [configurator.address, comet.address]);
await context.fastGovernanceExecute(
[configurator.address, proxyAdmin.address],
[0, 0],
['addAsset(address,(address,address,uint8,uint64,uint64,uint64,uint128))', 'deployAndUpgradeTo(address,address)'],
[addAssetCalldata, deployAndUpgradeToCalldata]
);
// Try to supply new token and borrow base
const baseAssetAddress = await comet.baseToken();
const borrowAmount = 1_000n * (await comet.baseScale()).toBigInt();
const supplyAmount = exp(75_000, 18);
await zerox.connect(albert.signer)['approve(address,uint256)'](comet.address, supplyAmount);
await albert.supplyAsset({ asset: zerox.address, amount: supplyAmount });
await albert.withdrawAsset({ asset: baseAssetAddress, amount: borrowAmount });
expect(await albert.getCometCollateralBalance(zerox.address)).to.be.equal(supplyAmount);
expect(await albert.getCometBaseBalance()).to.be.equal(-borrowAmount);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment