Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sim details #158

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ out/

.DS_Store
.env
scripts/simulationData/simulationOutput/simulationResults*.json
scripts/simulationData/simulationOutput/simulationResults*.json
scripts/simulationData/simulationSheet*.json
17 changes: 16 additions & 1 deletion scripts/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { TestRunner } from "./utils/facades/TestRunner";
import { Constants, MarketData } from "./utils/facades/types";
import { simulationTesting } from "./simulationTests";
import { SimulationParameters } from "./utils/facades/RoundSimulator";
import { Account, CallData, hash, Provider } from "starknet";
import { ERC20Facade } from "./utils/facades/erc20Facade";
async function main(environment: string, port?: string) {
const provider = getProvider(environment, port);
const devAccount = getAccount(environment, provider);
Expand Down Expand Up @@ -33,15 +35,28 @@ async function main(environment: string, port?: string) {
constants
);

const feeTokenFacade = new ERC20Facade(
"0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
provider
);

feeTokenFacade.erc20Contract.connect(devAccount);
await feeTokenFacade.erc20Contract.transfer(
"0x0134f47366096198eb8f86e3ae6b075d399ca7abd918a56e01bb3b24963c2f75",
BigInt(1000000000000000000)
);
await feeTokenFacade.erc20Contract.transfer(
"0x01577908d02E0a3A6B243A149Eb91BB4514f3aAb948CFE63b2f8bb52397618D4",
BigInt(1000000000000000000)
);
await testRunner.ethFacade.supplyERC20(
devAccount,
provider,
ethAddress,
vaultAddress
);

await simulationTesting(testRunner);
}

main(process.argv[2], process.argv[3]);

11 changes: 8 additions & 3 deletions scripts/simulationTests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ async function simulationTesting(testRunner: TestRunner) {
const roundData = await simulator.simulateRound(roundParams);
data.results.push(roundData);
}
console.log("DATA", data);
const stringified = JSON.stringify(data);
fs.writeFile(
`./simulationData/simulationOutput/simulationResults-${Math.floor(
Expand All @@ -53,13 +52,13 @@ const initial = {
liquidityProviders: [1, 2],
depositAmounts: ["50000000000000", "50000000000000"],
optionBidders: [1, 3],
bidAmounts: [5000, 7000],

};
const repeating = {
liquidityProviders: [],
depositAmounts: [],
optionBidders: [1, 3],
bidAmounts: [5000, 7000],

};

export const generateSheet = () => {
Expand All @@ -82,10 +81,16 @@ export const generateSheet = () => {
return marketData.reservePrice;
}),
marketData,
bidAmounts:[Math.random(),Math.random()],
withdrawals:[1,2],
withdrawalAmounts:[Math.random()/2,Math.random()/2],
} as SimulationSheet;
} else
return {
...repeating,
bidAmounts:[Math.random(),Math.random()],
withdrawals:[1,2],
withdrawalAmounts:[Math.random()/2,Math.random()/2],
bidPrices: initial.optionBidders.map((bidder) => {
return marketData.reservePrice;
}),
Expand Down
9 changes: 6 additions & 3 deletions scripts/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type VaultConstructorArgs = {
roundTransitionPeriod: number;
auctionRunTime: number;
optionRunTime: number;
kFactor:number;
};

type ConstructorArgs = {
Expand All @@ -39,12 +40,14 @@ const constructorArgs: { [key: string]: ConstructorArgs } = {
"0x7ce7089cb75a590b9485f6851d8998fa885494cc7a70dbae8f3db572586b8a8",
},
vault: {
roundTransitionPeriod: 32,
auctionRunTime: 23,
optionRunTime: 23,
roundTransitionPeriod: 32000,
auctionRunTime: 23000,
optionRunTime: 23000,
kFactor:10000,
},
optionRound: "",
marketAggregator: "",

},
};
const accountDetailsMapping: { [key: string]: AccountDetailsType } = {
Expand Down
131 changes: 99 additions & 32 deletions scripts/utils/facades/RoundSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MarketData,
PlaceBidArgs,
RefundUnusedBidsArgs,
WithdrawArgs,
} from "./types";
import { TestRunner } from "./TestRunner";
import { getOptionRoundContract, getOptionRoundFacade } from "../helpers/setup";
Expand All @@ -18,6 +19,11 @@ export type SimulationSheet = {
depositAmounts: Array<number | string>;
bidAmounts: Array<number | string>;
bidPrices: Array<number | string>;
withdrawalsPremium?: Array<number>;
withdrawalsFromQueue?: Array<number>;
withdrawalsFromQueueAmounts?: Array<number | string>;
withdrawals?: Array<number>;
withdrawalAmounts?: Array<number | string>;
marketData: MarketData;
};

Expand All @@ -27,7 +33,9 @@ export type SimulationParameters = {
refundAllArgs: Array<RefundUnusedBidsArgs>;
lpAccounts?: Array<Account>;
bidderAccounts?: Array<Account>;

withdrawPremiumArgs: Array<WithdrawArgs>;
withdrawalQueueArgs?: Array<WithdrawArgs>;
withdrawalArgs: Array<WithdrawArgs>;
exerciseOptionsAllArgs: Array<ExerciseOptionArgs>;
marketData: MarketData;
};
Expand All @@ -37,10 +45,10 @@ export type StateData = {
lpLockedBalances: Array<string>;
lpUnlockedBalances: Array<string>;
};
vaultBalances:{
vaultLocked:string;
vaultUnlocked:string;
}
vaultBalances: {
vaultLocked: string;
vaultUnlocked: string;
};
ethBalancesBidders: Array<string>;
timeStamp?: string | number;
};
Expand Down Expand Up @@ -75,19 +83,26 @@ export class RoundSimulator {
params.bidAllArgs,
params.marketData
);
const optionsAvailable = await this.optionRoundFacade.getTotalOptionsAvailable();
const optionsAvailable =
await this.optionRoundFacade.getTotalOptionsAvailable();
const runningStateData: StateData = await this.simulateRunningState(
params.refundAllArgs
params.refundAllArgs,
params.withdrawPremiumArgs
);
const settledStateData: StateData = await this.simulateSettledState(
params.exerciseOptionsAllArgs
params.exerciseOptionsAllArgs,
params.withdrawalArgs
);
const optionsSold = await this.optionRoundFacade.optionRoundContract.total_options_sold();
const optionsSold =
await this.optionRoundFacade.optionRoundContract.total_options_sold();

const ethBalanceVault = await this.testRunner.ethFacade.getBalance(this.testRunner.vaultFacade.vaultContract.address);
const ethBalanceRound = await this.testRunner.ethFacade.getBalance(this.optionRoundFacade.optionRoundContract.address);
const ethBalanceVault = await this.testRunner.ethFacade.getBalance(
this.testRunner.vaultFacade.vaultContract.address
);
const ethBalanceRound = await this.testRunner.ethFacade.getBalance(
this.optionRoundFacade.optionRoundContract.address
);
if (params.marketData.startTime && params.marketData.endTime) {

//Mock timestamps if present on the marketData
const difference =
Number(params.marketData.endTime) - Number(params.marketData.startTime);
Expand All @@ -102,10 +117,10 @@ export class RoundSimulator {
}

return {
ethBalanceRound:ethBalanceRound.toString(),
ethBalanceVault:ethBalanceVault.toString(),
optionsAvailable:optionsAvailable.toString(),
optionsSold:optionsSold.toString(),
ethBalanceRound: ethBalanceRound.toString(),
ethBalanceVault: ethBalanceVault.toString(),
optionsAvailable: optionsAvailable.toString(),
optionsSold: optionsSold.toString(),
openStateData,
auctioningStateData,
runningStateData,
Expand All @@ -114,12 +129,11 @@ export class RoundSimulator {
}

async captureLockedUnlockedBalances() {
const lpLockedBalancesBigInt =
await this.testRunner.getLPLockedBalanceAll(this.lpAccounts);
const lpLockedBalancesBigInt = await this.testRunner.getLPLockedBalanceAll(
this.lpAccounts
);
const lpUnlockedBalancesBigint =
await this.testRunner.getLPUnlockedBalanceAll(
this.lpAccounts
);
await this.testRunner.getLPUnlockedBalanceAll(this.lpAccounts);
const lpLockedBalances = lpLockedBalancesBigInt.map((balance) => {
return balance.toString();
});
Expand All @@ -143,9 +157,9 @@ export class RoundSimulator {
const locked = await this.testRunner.vaultFacade.getTotalLocked();
const unlocked = await this.testRunner.vaultFacade.getTotalUnLocked();
return {
vaultLocked:locked.toString(),
vaultUnlocked:unlocked.toString()
}
vaultLocked: locked.toString(),
vaultUnlocked: unlocked.toString(),
};
}

async captureEthBalancesOptionBidders() {
Expand Down Expand Up @@ -177,44 +191,97 @@ export class RoundSimulator {

const lockedUnlockedBalances = await this.captureLockedUnlockedBalances();
const vaultBalances = await this.captureVaultBalances();
const approvalArgs = bidAllArgs.map((arg) => {
const optionsAvailable =
await this.optionRoundFacade.getTotalOptionsAvailable();

const bidAllArgsAdjusted = bidAllArgs.map((args) => {
return {
from: args.from,
amount: Math.floor(Number(args.amount) * Number(optionsAvailable)),
price: args.price,
} as PlaceBidArgs;
});
const approvalArgs = bidAllArgsAdjusted.map((arg) => {
const data: ApprovalArgs = {
owner: arg.from,
spender: this.optionRoundFacade.optionRoundContract.address,
amount: BigInt(arg.amount) * BigInt(arg.price),
};
return data;
});

await this.testRunner.approveAll(approvalArgs);

await this.optionRoundFacade.placeBidsAll(bidAllArgs);
await this.optionRoundFacade.placeBidsAll(bidAllArgsAdjusted);
const ethBalancesBidders = await this.captureEthBalancesOptionBidders();
return {
lockedUnlockedBalances,
ethBalancesBidders,
vaultBalances
vaultBalances,
};
}
async simulateRunningState(refundAllArgs: Array<RefundUnusedBidsArgs>) {
const data = await this.testRunner.endAuctionBystander();
async simulateRunningState(
refundAllArgs: Array<RefundUnusedBidsArgs>,
withdrawPremiumArgs: Array<WithdrawArgs>
) {
await this.testRunner.endAuctionBystander();

const totalPremiums = await this.optionRoundFacade.getTotalPremiums();
const startingLiquidity =
await this.optionRoundFacade.getStartingLiquidity();
const withdrawPremiumArgsAdjusted: Array<WithdrawArgs> = [];
for (const args of withdrawPremiumArgs) {
const lockedBalance =
await this.testRunner.vaultFacade.getLPLockedBalance(
args.account.address
);
const premiumsToWithdraw =
(BigInt(lockedBalance) * BigInt(totalPremiums)) /
BigInt(startingLiquidity);
withdrawPremiumArgs.push({
account: args.account,
amount: Math.floor(Number(premiumsToWithdraw)),
});
}

await this.testRunner.withdrawAll(withdrawPremiumArgsAdjusted);
const lockedUnlockedBalances = await this.captureLockedUnlockedBalances();
const vaultBalances = await this.captureVaultBalances();
await this.optionRoundFacade.refundUnusedBidsAll(refundAllArgs);
const ethBalancesBidders = await this.captureEthBalancesOptionBidders();
return {
lockedUnlockedBalances,
ethBalancesBidders,
vaultBalances
vaultBalances,
};
}
async simulateSettledState(exerciseOptionsArgs: Array<ExerciseOptionArgs>) {
async simulateSettledState(
exerciseOptionsArgs: Array<ExerciseOptionArgs>,
withdrawalArgs: Array<WithdrawArgs>
) {
const data = await this.optionRoundFacade.optionRoundContract.get_state();
await this.testRunner.settleOptionRoundBystander();

const withdrawArgsAdjusted: Array<WithdrawArgs> = [];

for (const args of withdrawalArgs) {
const unlockedBalance =
await this.testRunner.vaultFacade.getLPUnlockedBalance(
args.account.address
);
console.log("UNLOCKED",unlockedBalance);
withdrawArgsAdjusted.push({
account:args.account,
amount:Math.floor(Number(args.amount)*Number(unlockedBalance))
})
}

const lpBefore = await this.captureLockedUnlockedBalances();
await this.testRunner.withdrawAll(withdrawArgsAdjusted);
const lpAfter = await this.captureLockedUnlockedBalances();
console.log("ARGS:",withdrawalArgs,"\nADjusted:",withdrawArgsAdjusted)
const lockedUnlockedBalances = await this.captureLockedUnlockedBalances();
const vaultBalances = await this.captureVaultBalances();
console.log("3");
await this.optionRoundFacade.exerciseOptionsAll(exerciseOptionsArgs);
const ethBalancesBidders = await this.captureEthBalancesOptionBidders();

Expand Down
1 change: 1 addition & 0 deletions scripts/utils/facades/erc20Facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class ERC20Facade {
provider
).typedv2(erc20ABI);

await this.supply(devAccount, "0x06Fb643e5c834feA33EACeFc10A2F856E1C317E700523c8eA45681F52D2B1D60",BigInt(10000000000));
for (let i = 0; i < 6; i++) {
const lp = getCustomAccount(
provider,
Expand Down
8 changes: 6 additions & 2 deletions scripts/utils/facades/optionRoundFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class OptionRoundFacade {
}



async getStartingLiquidity(){
const res = await this.optionRoundContract.starting_liquidity();
return convertToBigInt(res);
}
async getRoundId() {
const res = await this.optionRoundContract.get_round_id();
return convertToBigInt(res);
Expand Down Expand Up @@ -95,10 +100,9 @@ export class OptionRoundFacade {
this.optionRoundContract.connect(from);
try {
const data = await this.optionRoundContract.place_bid(amount, price);
console.log("SUCCESS", data);
} catch (err) {
const error = err as LibraryError;
console.log(error.name);
console.log(error.name,from,amount,price,error.message,error.cause);
}
}

Expand Down
Loading
Loading