|
| 1 | +import { artifacts, run, web3 } from "hardhat"; |
| 2 | +import { PriceGuesserInstance } from "../../typechain-types"; |
| 3 | + |
| 4 | +// --- Configuration --- |
| 5 | +const PriceGuesser: PriceGuesserInstance = artifacts.require("PriceGuesser"); |
| 6 | +const FTSO_FEED_ID = "0x01464c522f55534400000000000000000000000000"; |
| 7 | +const DESCRIPTION = "FTSOv2 FLR/USD adapted for API3"; |
| 8 | +const MAX_AGE_SECONDS = 3600; |
| 9 | +const STRIKE_PRICE_USD = 0.025; |
| 10 | +const ROUND_DURATION_SECONDS = 300; |
| 11 | +const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 12 | + |
| 13 | +async function deployContracts(): Promise<{ guesser: PriceGuesserInstance }> { |
| 14 | + const strikePriceWei = BigInt(STRIKE_PRICE_USD * 1e18); |
| 15 | + const guesserArgs: (string | number)[] = [ |
| 16 | + FTSO_FEED_ID, |
| 17 | + DESCRIPTION, |
| 18 | + MAX_AGE_SECONDS, |
| 19 | + strikePriceWei.toString(), |
| 20 | + ROUND_DURATION_SECONDS, |
| 21 | + ]; |
| 22 | + console.log("\nDeploying integrated PriceGuesser contract with arguments:"); |
| 23 | + console.log(` - FTSO Feed ID: ${guesserArgs[0]}`); |
| 24 | + console.log(` - Description: ${guesserArgs[1]}`); |
| 25 | + console.log(` - Max Age (seconds): ${guesserArgs[2]}`); |
| 26 | + console.log(` - Strike Price: ${STRIKE_PRICE_USD} (${guesserArgs[3]} wei)`); |
| 27 | + console.log(` - Round Duration: ${guesserArgs[4]} seconds`); |
| 28 | + const guesser = await PriceGuesser.new( |
| 29 | + ...(guesserArgs as [string, string, number, string, number]), |
| 30 | + ); |
| 31 | + console.log("\n✅ PriceGuesser deployed to:", guesser.address); |
| 32 | + |
| 33 | + try { |
| 34 | + console.log("\nVerifying PriceGuesser on block explorer..."); |
| 35 | + await run("verify:verify", { |
| 36 | + address: guesser.address, |
| 37 | + constructorArguments: guesserArgs, |
| 38 | + }); |
| 39 | + console.log("PriceGuesser verification successful."); |
| 40 | + } catch (e: unknown) { |
| 41 | + if (e instanceof Error) { |
| 42 | + console.error("PriceGuesser verification failed:", e.message); |
| 43 | + } else { |
| 44 | + console.error("An unknown error occurred during verification:", e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return { guesser }; |
| 49 | +} |
| 50 | + |
| 51 | +async function interactWithMarket(guesser: PriceGuesserInstance) { |
| 52 | + const accounts = await web3.eth.getAccounts(); |
| 53 | + const deployer = accounts[0]; |
| 54 | + const bettorAbove = accounts.length > 1 ? accounts[1] : deployer; |
| 55 | + const bettorBelow = accounts.length > 2 ? accounts[2] : deployer; |
| 56 | + const betAmountAbove = 10n * 10n ** 18n; |
| 57 | + const betAmountBelow = 20n * 10n ** 18n; |
| 58 | + |
| 59 | + console.log(`\n--- Simulating Prediction Market ---`); |
| 60 | + console.log(` - Deployer/Settler: ${deployer}`); |
| 61 | + console.log(` - Bettor "Above": ${bettorAbove}`); |
| 62 | + console.log(` - Bettor "Below": ${bettorBelow}`); |
| 63 | + |
| 64 | + console.log("\nStep 1: Bettors are placing their bets..."); |
| 65 | + await guesser.betAbove({ |
| 66 | + from: bettorAbove, |
| 67 | + value: betAmountAbove.toString(), |
| 68 | + }); |
| 69 | + console.log( |
| 70 | + ` - Bettor "Above" placed ${web3.utils.fromWei(betAmountAbove.toString())} tokens.`, |
| 71 | + ); |
| 72 | + await guesser.betBelow({ |
| 73 | + from: bettorBelow, |
| 74 | + value: betAmountBelow.toString(), |
| 75 | + }); |
| 76 | + console.log( |
| 77 | + ` - Bettor "Below" placed ${web3.utils.fromWei(betAmountBelow.toString())} tokens.`, |
| 78 | + ); |
| 79 | + |
| 80 | + console.log( |
| 81 | + `\nStep 2: Betting round is live. Waiting ${ROUND_DURATION_SECONDS} seconds for it to expire...`, |
| 82 | + ); |
| 83 | + await wait(ROUND_DURATION_SECONDS * 1000); |
| 84 | + console.log(" - The betting round has now expired."); |
| 85 | + |
| 86 | + console.log( |
| 87 | + "\nStep 3: Refreshing the FTSO price on the contract post-expiry...", |
| 88 | + ); |
| 89 | + await guesser.refresh({ from: deployer }); |
| 90 | + console.log(" - Price has been updated on the PriceGuesser contract."); |
| 91 | + |
| 92 | + console.log("\nStep 4: Settling the prediction market..."); |
| 93 | + const settleTx = await guesser.settle({ from: deployer }); |
| 94 | + const settledEvent = settleTx.logs.find((e) => e.event === "MarketSettled"); |
| 95 | + const finalPrice = BigInt(settledEvent.args.finalPrice.toString()); |
| 96 | + const outcome = Number(settledEvent.args.outcome); |
| 97 | + const finalPriceFormatted = Number(finalPrice / 10n ** 14n) / 10000; |
| 98 | + const outcomeString = outcome === 1 ? "ABOVE" : "BELOW"; |
| 99 | + console.log( |
| 100 | + `✅ Market settled! Final Price: ${finalPriceFormatted.toFixed(4)}`, |
| 101 | + ); |
| 102 | + console.log(`✅ Outcome: The price was ${outcomeString} the strike price.`); |
| 103 | + |
| 104 | + console.log("\nStep 5: Distributing winnings..."); |
| 105 | + const [winner, loser] = |
| 106 | + outcome === 1 ? [bettorAbove, bettorBelow] : [bettorBelow, bettorAbove]; |
| 107 | + const prizePool = outcome === 1 ? betAmountBelow : betAmountAbove; |
| 108 | + const winnerBet = outcome === 1 ? betAmountAbove : betAmountBelow; |
| 109 | + |
| 110 | + if (prizePool > 0n || winnerBet > 0n) { |
| 111 | + console.log(` - Attempting to claim for WINNER ("${outcomeString}")`); |
| 112 | + await guesser.claimWinnings({ from: winner }); |
| 113 | + const totalWinnings = winnerBet + prizePool; |
| 114 | + console.log( |
| 115 | + ` - WINNER claimed their prize of ${web3.utils.fromWei(totalWinnings.toString())} tokens.`, |
| 116 | + ); |
| 117 | + } else { |
| 118 | + console.log(" - WINNER's pool won, but no bets were placed to claim."); |
| 119 | + } |
| 120 | + |
| 121 | + if (winner !== loser) { |
| 122 | + try { |
| 123 | + await guesser.claimWinnings({ from: loser }); |
| 124 | + } catch (error: unknown) { |
| 125 | + if (error instanceof Error && error.message.includes("NothingToClaim")) { |
| 126 | + console.log( |
| 127 | + " - LOSER correctly failed to claim winnings as expected.", |
| 128 | + ); |
| 129 | + } else if (error instanceof Error) { |
| 130 | + console.error( |
| 131 | + " - An unexpected error occurred for the loser:", |
| 132 | + error.message, |
| 133 | + ); |
| 134 | + } else { |
| 135 | + console.error(" - An unknown error occurred for the loser:", error); |
| 136 | + } |
| 137 | + } |
| 138 | + } else { |
| 139 | + console.log( |
| 140 | + " - Skipping loser claim attempt as winner and loser are the same account.", |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +async function main() { |
| 146 | + console.log("🚀 Starting Prediction Market Management Script 🚀"); |
| 147 | + const { guesser } = await deployContracts(); |
| 148 | + await interactWithMarket(guesser); |
| 149 | + console.log("\n🎉 Script finished successfully! 🎉"); |
| 150 | +} |
| 151 | + |
| 152 | +void main() |
| 153 | + .then(() => process.exit(0)) |
| 154 | + .catch((error) => { |
| 155 | + console.error(error); |
| 156 | + process.exit(1); |
| 157 | + }); |
0 commit comments