Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaShWoof committed Jan 14, 2025
1 parent 262fb3b commit 1b4307b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
12 changes: 11 additions & 1 deletion plugins/scenario/utils/hreForBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export async function nonForkedHreForBase(base: ForkSpec): Promise<HardhatRuntim
);
}

function getBlockRollback(base: ForkSpec){
if(base.blockNumber)
return base.blockNumber;
else if(base.network === 'arbitrum'){
return undefined;
}
else
return 280;
}

export async function forkedHreForBase(base: ForkSpec): Promise<HardhatRuntimeEnvironment> {
const ctx: HardhatContext = HardhatContext.getHardhatContext();

Expand All @@ -77,8 +87,8 @@ export async function forkedHreForBase(base: ForkSpec): Promise<HardhatRuntimeEn

// noNetwork otherwise
if(!base.blockNumber && baseNetwork.url)
base.blockNumber = await provider.getBlockNumber() - 280; // arbitrary number of blocks to go back, about 1 hour

base.blockNumber = await provider.getBlockNumber() - getBlockRollback(base); // arbitrary number of blocks to go back
if (!baseNetwork) {
throw new Error(`cannot find network config for network: ${base.network}`);
}
Expand Down
31 changes: 21 additions & 10 deletions scenario/TransferScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ scenario(
scenario(
'Comet#transfer > partial withdraw / borrow base to partial repay / supply',
{
cometBalances: {
albert: { $base: 1000, $asset0: 5000 }, // in units of asset, not wei
betty: { $base: -1000 },
charles: { $base: 1000 }, // to give the protocol enough base for others to borrow from
},
cometBalances: async (ctx) => (
{
albert: { $base: getConfigForScenario(ctx).transferBase, $asset0: getConfigForScenario(ctx).transferAsset1 }, // in units of asset, not wei
betty: { $base: -getConfigForScenario(ctx).transferBase },
charles: { $base: getConfigForScenario(ctx).transferBase }, // to give the protocol enough base for others to borrow from
}
),
},
async ({ comet, actors }, context) => {
const { albert, betty } = actors;
Expand All @@ -149,16 +151,25 @@ scenario(
const borrowRate = (await comet.getBorrowRate(utilization)).toBigInt();

// XXX 100 seconds?!
expectApproximately(await albert.getCometBaseBalance(), 1000n * scale, getInterest(1000n * scale, borrowRate, 100n) + 2n);
expectApproximately(await betty.getCometBaseBalance(), -1000n * scale, getInterest(1000n * scale, borrowRate, 100n) + 2n);
expectApproximately(
await albert.getCometBaseBalance(),
BigInt(getConfigForScenario(context).transferBase) * scale,
getInterest(BigInt(getConfigForScenario(context).transferBase) * scale, borrowRate, 100n) + 2n
);

expectApproximately(
await betty.getCometBaseBalance(),
-BigInt(getConfigForScenario(context).transferBase) * scale,
getInterest(BigInt(getConfigForScenario(context).transferBase) * scale, borrowRate, 100n) + 2n
);

// Albert with positive balance transfers to Betty with negative balance
const toTransfer = 2500n * scale;
const toTransfer = BigInt(getConfigForScenario(context).transferBase) * 25n / 10n * scale;
const txn = await albert.transferAsset({ dst: betty.address, asset: baseAsset.address, amount: toTransfer });

// Albert ends with negative balance and Betty with positive balance
expectApproximately(await albert.getCometBaseBalance(), -1500n * scale, getInterest(1500n * scale, borrowRate, 100n) + 4n);
expectApproximately(await betty.getCometBaseBalance(), 1500n * scale, getInterest(1500n * scale, borrowRate, 100n) + 4n);
expectApproximately(await albert.getCometBaseBalance(), -BigInt(getConfigForScenario(context).transferBase) * 15n / 10n * scale, getInterest(BigInt(getConfigForScenario(context).transferBase) * 15n / 10n * scale, borrowRate, 100n) + 4n);
expectApproximately(await betty.getCometBaseBalance(), BigInt(getConfigForScenario(context).transferBase) * 15n / 10n * scale, getInterest(BigInt(getConfigForScenario(context).transferBase) * 15n / 10n * scale, borrowRate, 100n) + 4n);

return txn; // return txn to measure gas
}
Expand Down
21 changes: 14 additions & 7 deletions scenario/utils/relayOptimismMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default async function relayOptimismMessage(
const messageWithoutSigHash = '0x' + messageWithoutPrefix.slice(8);
try {
// 1a. Bridging ERC20 token
const { l1Token, _l2Token, _from, to, amount, _data } = ethers.utils.defaultAbiCoder.decode(
const [ l1Token, _l2Token, _from, to, amount, _data ] = ethers.utils.defaultAbiCoder.decode(
['address l1Token', 'address l2Token', 'address from', 'address to', 'uint256 amount', 'bytes data'],
messageWithoutSigHash
);
Expand All @@ -71,7 +71,7 @@ export default async function relayOptimismMessage(
);
} catch (e) {
// 1a. Bridging ETH
const { _from, to, amount, _data } = ethers.utils.defaultAbiCoder.decode(
const [ _from, to, amount, _data ] = ethers.utils.defaultAbiCoder.decode(
['address from', 'address to', 'uint256 amount', 'bytes data'],
messageWithoutSigHash
);
Expand All @@ -90,11 +90,18 @@ export default async function relayOptimismMessage(
}
} else if (target === bridgeReceiver.address) {
// Cross-chain message passing
const proposalCreatedEvent = relayMessageTxn.events.find(event => event.address === bridgeReceiver.address);
const { args: { id, eta } } = bridgeReceiver.interface.parseLog(proposalCreatedEvent);

// Add the proposal to the list of open bridged proposals to be executed after all the messages have been relayed
openBridgedProposals.push({ id, eta });
try {
const proposalCreatedEvent = relayMessageTxn.events.find(event => event.address === bridgeReceiver.address);
const { args: { id, eta } } = bridgeReceiver.interface.parseLog(proposalCreatedEvent);
// Add the proposal to the list of open bridged proposals to be executed after all the messages have been relayed
openBridgedProposals.push({ id, eta });
} catch (e) {
if(relayMessageTxn.events[0].event === 'FailedRelayedMessage'){
console.log('Failed to relay message');
continue;
}
throw e;
}
} else {
throw new Error(`[${governanceDeploymentManager.network} -> ${bridgeDeploymentManager.network}] Unrecognized target for cross-chain message`);
}
Expand Down
3 changes: 3 additions & 0 deletions scenario/utils/scenarioHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config = {
rewardsBase: 1000,
transferBase: 1000,
transferAsset: 5000,
transferAsset1: 5000,
interestSeconds: 110,
withdrawBase: 1000,
withdrawAsset: 3000,
Expand Down Expand Up @@ -73,6 +74,8 @@ export function getConfigForScenario(ctx: CometContext) {

if (ctx.world.base.network === 'polygon' && ctx.world.base.deployment === 'usdt') {
config.withdrawAsset = 10000;
config.transferAsset = 500000;
config.transferBase = 100;
}

if (ctx.world.base.network === 'scroll' && ctx.world.base.deployment === 'usdc') {
Expand Down

0 comments on commit 1b4307b

Please sign in to comment.