From ea8e82e9b38fe4ef91931b15c4d9fc8d94dc2e3d Mon Sep 17 00:00:00 2001 From: Philip Date: Mon, 9 May 2022 16:20:39 -0300 Subject: [PATCH 1/4] feat(prediction-v3): Add Prediction v3 subgraph --- .../prediction/v3/abis/PredictionV3.json | 1117 +++++++++++++++++ subgraphs/prediction/v3/mappings/index.ts | 473 +++++++ subgraphs/prediction/v3/package.json | 13 + subgraphs/prediction/v3/schema.graphql | 119 ++ subgraphs/prediction/v3/subgraph.yaml | 50 + 5 files changed, 1772 insertions(+) create mode 100644 subgraphs/prediction/v3/abis/PredictionV3.json create mode 100644 subgraphs/prediction/v3/mappings/index.ts create mode 100644 subgraphs/prediction/v3/package.json create mode 100644 subgraphs/prediction/v3/schema.graphql create mode 100644 subgraphs/prediction/v3/subgraph.yaml diff --git a/subgraphs/prediction/v3/abis/PredictionV3.json b/subgraphs/prediction/v3/abis/PredictionV3.json new file mode 100644 index 00000000..62f6b366 --- /dev/null +++ b/subgraphs/prediction/v3/abis/PredictionV3.json @@ -0,0 +1,1117 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_oracleAddress", + "type": "address" + }, + { + "internalType": "address", + "name": "_adminAddress", + "type": "address" + }, + { + "internalType": "address", + "name": "_operatorAddress", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_intervalSeconds", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_bufferSeconds", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_minBetAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_oracleUpdateAllowance", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_treasuryFee", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "BetBear", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "BetBull", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "Claim", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "roundId", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "price", + "type": "int256" + } + ], + "name": "EndRound", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "roundId", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "price", + "type": "int256" + } + ], + "name": "LockRound", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "admin", + "type": "address" + } + ], + "name": "NewAdminAddress", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "bufferSeconds", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "intervalSeconds", + "type": "uint256" + } + ], + "name": "NewBufferAndIntervalSeconds", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "minBetAmount", + "type": "uint256" + } + ], + "name": "NewMinBetAmount", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "operator", + "type": "address" + } + ], + "name": "NewOperatorAddress", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "oracle", + "type": "address" + } + ], + "name": "NewOracle", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "oracleUpdateAllowance", + "type": "uint256" + } + ], + "name": "NewOracleUpdateAllowance", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "treasuryFee", + "type": "uint256" + } + ], + "name": "NewTreasuryFee", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + } + ], + "name": "Pause", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "Paused", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "rewardBaseCalAmount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "rewardAmount", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "treasuryAmount", + "type": "uint256" + } + ], + "name": "RewardsCalculated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + } + ], + "name": "StartRound", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "token", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "TokenRecovery", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "TreasuryClaim", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + } + ], + "name": "Unpause", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "Unpaused", + "type": "event" + }, + { + "inputs": [], + "name": "MAX_TREASURY_FEE", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "adminAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + } + ], + "name": "betBear", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + } + ], + "name": "betBull", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [], + "name": "bufferSeconds", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256[]", + "name": "epochs", + "type": "uint256[]" + } + ], + "name": "claim", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "claimTreasury", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "internalType": "address", + "name": "user", + "type": "address" + } + ], + "name": "claimable", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "currentEpoch", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "executeRound", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "genesisLockOnce", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "genesisLockRound", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "genesisStartOnce", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "genesisStartRound", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "cursor", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "size", + "type": "uint256" + } + ], + "name": "getUserRounds", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + }, + { + "components": [ + { + "internalType": "enum PancakePredictionV2.Position", + "name": "position", + "type": "uint8" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "claimed", + "type": "bool" + } + ], + "internalType": "struct PancakePredictionV2.BetInfo[]", + "name": "", + "type": "tuple[]" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "user", + "type": "address" + } + ], + "name": "getUserRoundsLength", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "intervalSeconds", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "name": "ledger", + "outputs": [ + { + "internalType": "enum PancakePredictionV2.Position", + "name": "position", + "type": "uint8" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "claimed", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "minBetAmount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "operatorAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "oracle", + "outputs": [ + { + "internalType": "contract AggregatorV3Interface", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "oracleLatestRoundId", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "oracleUpdateAllowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "pause", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "paused", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "recoverToken", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "internalType": "address", + "name": "user", + "type": "address" + } + ], + "name": "refundable", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "rounds", + "outputs": [ + { + "internalType": "uint256", + "name": "epoch", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "startTimestamp", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "lockTimestamp", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "closeTimestamp", + "type": "uint256" + }, + { + "internalType": "int256", + "name": "lockPrice", + "type": "int256" + }, + { + "internalType": "int256", + "name": "closePrice", + "type": "int256" + }, + { + "internalType": "uint256", + "name": "lockOracleId", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "closeOracleId", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "totalAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "bullAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "bearAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "rewardBaseCalAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "rewardAmount", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "oracleCalled", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_adminAddress", + "type": "address" + } + ], + "name": "setAdmin", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_bufferSeconds", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_intervalSeconds", + "type": "uint256" + } + ], + "name": "setBufferAndIntervalSeconds", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_minBetAmount", + "type": "uint256" + } + ], + "name": "setMinBetAmount", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_operatorAddress", + "type": "address" + } + ], + "name": "setOperator", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_oracle", + "type": "address" + } + ], + "name": "setOracle", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_oracleUpdateAllowance", + "type": "uint256" + } + ], + "name": "setOracleUpdateAllowance", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_treasuryFee", + "type": "uint256" + } + ], + "name": "setTreasuryFee", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "treasuryAmount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "treasuryFee", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "unpause", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "userRounds", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + } +] diff --git a/subgraphs/prediction/v3/mappings/index.ts b/subgraphs/prediction/v3/mappings/index.ts new file mode 100644 index 00000000..985c0de8 --- /dev/null +++ b/subgraphs/prediction/v3/mappings/index.ts @@ -0,0 +1,473 @@ +/* eslint-disable prefer-const */ +import { BigDecimal, BigInt, Bytes, log } from "@graphprotocol/graph-ts"; +import { concat } from "@graphprotocol/graph-ts/helper-functions"; +import { Bet, Market, Round, User } from "../generated/schema"; +import { + BetBear, + BetBull, + Claim, + EndRound, + LockRound, + Pause, + RewardsCalculated, + StartRound, + Unpause, +} from "../generated/PredictionV3/PredictionV3"; + +let ZERO_BI = BigInt.fromI32(0); +let ONE_BI = BigInt.fromI32(1); +let ZERO_BD = BigDecimal.fromString("0"); +let HUNDRED_BD = BigDecimal.fromString("100"); +let EIGHT_BD = BigDecimal.fromString("1e8"); +let EIGHTEEN_BD = BigDecimal.fromString("1e18"); + +/** + * PAUSE + */ + +export function handlePause(event: Pause): void { + let market = Market.load("1"); + if (market === null) { + market = new Market("1"); + market.epoch = event.params.epoch.toString(); + market.paused = true; + market.totalUsers = ZERO_BI; + market.totalBets = ZERO_BI; + market.totalBetsBull = ZERO_BI; + market.totalBetsBear = ZERO_BI; + market.totalToken = ZERO_BD; + market.totalTokenBull = ZERO_BD; + market.totalTokenBear = ZERO_BD; + market.totalTokenTreasury = ZERO_BD; + market.totalBetsClaimed = ZERO_BI; + market.totalTokenClaimed = ZERO_BD; + market.winRate = HUNDRED_BD; + market.averageToken = ZERO_BD; + market.netToken = ZERO_BD; + market.save(); + } + market.epoch = event.params.epoch.toString(); + market.paused = true; + market.save(); + + // Pause event was called, cancelling rounds. + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } + round.failed = true; + round.save(); + + // Also fail the previous round because it will not complete. + let previousRound = Round.load(round.previous); + if (previousRound !== null) { + previousRound.failed = true; + previousRound.save(); + } +} + +export function handleUnpause(event: Unpause): void { + let market = Market.load("1"); + if (market === null) { + market = new Market("1"); + market.epoch = event.params.epoch.toString(); + market.paused = false; + market.totalUsers = ZERO_BI; + market.totalBets = ZERO_BI; + market.totalBetsBull = ZERO_BI; + market.totalBetsBear = ZERO_BI; + market.totalToken = ZERO_BD; + market.totalTokenBull = ZERO_BD; + market.totalTokenBear = ZERO_BD; + market.totalTokenTreasury = ZERO_BD; + market.totalBetsClaimed = ZERO_BI; + market.totalTokenClaimed = ZERO_BD; + market.winRate = HUNDRED_BD; + market.averageToken = ZERO_BD; + market.netToken = ZERO_BD; + market.save(); + } + market.epoch = event.params.epoch.toString(); + market.paused = false; + market.save(); +} + +/** + * ROUND + */ + +export function handleStartRound(event: StartRound): void { + let market = Market.load("1"); + if (market === null) { + market = new Market("1"); + market.epoch = event.params.epoch.toString(); + market.paused = false; + market.totalUsers = ZERO_BI; + market.totalBets = ZERO_BI; + market.totalBetsBull = ZERO_BI; + market.totalBetsBear = ZERO_BI; + market.totalToken = ZERO_BD; + market.totalTokenBull = ZERO_BD; + market.totalTokenBear = ZERO_BD; + market.totalTokenTreasury = ZERO_BD; + market.totalBetsClaimed = ZERO_BI; + market.totalTokenClaimed = ZERO_BD; + market.winRate = HUNDRED_BD; + market.averageToken = ZERO_BD; + market.netToken = ZERO_BD; + market.save(); + } + market.epoch = event.params.epoch.toString(); + market.save(); + + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } +} + +export function handleLockRound(event: LockRound): void { + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + log.warning("Tried to lock round without an existing round (epoch: {}).", [event.params.epoch.toString()]); + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } + round.lockAt = event.block.timestamp; + round.lockBlock = event.block.number; + round.lockHash = event.transaction.hash; + round.lockPrice = event.params.price.divDecimal(EIGHT_BD); + round.lockRoundId = event.params.roundId; + round.save(); +} + +export function handleEndRound(event: EndRound): void { + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + log.warning("Tried to end round without an existing round (epoch: {}).", [event.params.epoch.toString()]); + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } + round.closeAt = event.block.timestamp; + round.closeBlock = event.block.number; + round.closeHash = event.transaction.hash; + if (event.params.price) { + round.closePrice = event.params.price.divDecimal(EIGHT_BD); + } + round.closeRoundId = event.params.roundId; + + // Get round result based on lock/close price. + if (round.closePrice) { + if (round.closePrice.equals(round.lockPrice as BigDecimal)) { + round.position = "House"; + + let market = Market.load("1"); + if (market === null) { + log.error("Tried to query market after end round was called for a round (epoch: {})", [ + event.params.epoch.toString(), + ]); + } else { + market.totalTokenTreasury = market.totalTokenTreasury.plus(round.totalAmount); + market.netToken = market.netToken.plus(round.totalAmount); + market.save(); + } + } else if (round.closePrice.gt(round.lockPrice as BigDecimal)) { + round.position = "Bull"; + } else if (round.closePrice.lt(round.lockPrice as BigDecimal)) { + round.position = "Bear"; + } else { + round.position = null; + } + + round.failed = false; + } + + round.save(); +} + +export function handleBetBull(event: BetBull): void { + let market = Market.load("1"); + if (market === null) { + log.error("Tried to query market with bet (Bull)", []); + return; + } + market.totalBets = market.totalBets.plus(ONE_BI); + market.totalBetsBull = market.totalBetsBull.plus(ONE_BI); + market.totalToken = market.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalTokenBull = market.totalTokenBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); + market.averageToken = market.totalToken.div(market.totalBets.toBigDecimal()); + market.netToken = market.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.save(); + + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + log.warning("Tried to bet (bull) without an existing round (epoch: {}).", [event.params.epoch.toString()]); + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } + round.totalBets = round.totalBets.plus(ONE_BI); + round.totalAmount = round.totalAmount.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + round.bullBets = round.bullBets.plus(ONE_BI); + round.bullAmount = round.bullAmount.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + round.save(); + + // Fail safe condition in case the user has not been created yet. + let user = User.load(event.params.sender.toHex()); + if (user === null) { + user = new User(event.params.sender.toHex()); + user.createdAt = event.block.timestamp; + user.updatedAt = event.block.timestamp; + user.block = event.block.number; + user.totalBets = ZERO_BI; + user.totalBetsBull = ZERO_BI; + user.totalBetsBear = ZERO_BI; + user.totalToken = ZERO_BD; + user.totalTokenBull = ZERO_BD; + user.totalTokenBear = ZERO_BD; + user.totalBetsClaimed = ZERO_BI; + user.totalTokenClaimed = ZERO_BD; + user.winRate = HUNDRED_BD; + user.averageToken = ZERO_BD; + user.netToken = ZERO_BD; + + market.totalUsers = market.totalUsers.plus(ONE_BI); + market.save(); + } + user.updatedAt = event.block.timestamp; + user.totalBets = user.totalBets.plus(ONE_BI); + user.totalBetsBull = user.totalBetsBull.plus(ONE_BI); + user.totalToken = user.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalTokenBull = user.totalTokenBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + if (user.totalBets.gt(ZERO_BI)) { + user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); + user.averageToken = user.totalToken.div(user.totalBets.toBigDecimal()); + } + user.netToken = user.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.save(); + + let betId = concat(event.params.sender, Bytes.fromI32(event.params.epoch.toI32())).toHex(); + let bet = new Bet(betId); + bet.round = round.id; + bet.user = user.id; + bet.hash = event.transaction.hash; + bet.amount = event.params.amount.divDecimal(EIGHTEEN_BD); + bet.position = "Bull"; + bet.claimed = false; + bet.createdAt = event.block.timestamp; + bet.updatedAt = event.block.timestamp; + bet.block = event.block.number; + bet.save(); +} + +export function handleBetBear(event: BetBear): void { + let market = Market.load("1"); + if (market === null) { + log.error("Tried to query market with bet (Bear)", []); + return; + } + market.totalBets = market.totalBets.plus(ONE_BI); + market.totalBetsBear = market.totalBetsBear.plus(ONE_BI); + market.totalToken = market.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalTokenBear = market.totalTokenBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); + market.averageToken = market.totalToken.div(market.totalBets.toBigDecimal()); + market.netToken = market.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.save(); + + let round = Round.load(event.params.epoch.toString()); + if (round === null) { + log.warning("Tried to bet (bear) without an existing round (epoch: {}).", [event.params.epoch.toString()]); + round = new Round(event.params.epoch.toString()); + round.epoch = event.params.epoch; + round.previous = event.params.epoch.equals(ZERO_BI) ? null : event.params.epoch.minus(ONE_BI).toString(); + round.startAt = event.block.timestamp; + round.startBlock = event.block.number; + round.startHash = event.transaction.hash; + round.totalBets = ZERO_BI; + round.totalAmount = ZERO_BD; + round.bullBets = ZERO_BI; + round.bullAmount = ZERO_BD; + round.bearBets = ZERO_BI; + round.bearAmount = ZERO_BD; + round.save(); + } + round.totalBets = round.totalBets.plus(ONE_BI); + round.totalAmount = round.totalAmount.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + round.bearBets = round.bearBets.plus(ONE_BI); + round.bearAmount = round.bearAmount.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + round.save(); + + // Fail safe condition in case the user has not been created yet. + let user = User.load(event.params.sender.toHex()); + if (user === null) { + user = new User(event.params.sender.toHex()); + user.createdAt = event.block.timestamp; + user.updatedAt = event.block.timestamp; + user.block = event.block.number; + user.totalBets = ZERO_BI; + user.totalBetsBull = ZERO_BI; + user.totalBetsBear = ZERO_BI; + user.totalToken = ZERO_BD; + user.totalTokenBull = ZERO_BD; + user.totalTokenBear = ZERO_BD; + user.totalBetsClaimed = ZERO_BI; + user.totalTokenClaimed = ZERO_BD; + user.winRate = HUNDRED_BD; + user.averageToken = ZERO_BD; + user.netToken = ZERO_BD; + + market.totalUsers = market.totalUsers.plus(ONE_BI); + market.save(); + } + user.updatedAt = event.block.timestamp; + user.totalBets = user.totalBets.plus(ONE_BI); + user.totalBetsBear = user.totalBetsBear.plus(ONE_BI); + user.totalToken = user.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalTokenBear = user.totalTokenBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + if (user.totalBets.gt(ZERO_BI)) { + user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); + user.averageToken = user.totalToken.div(user.totalBets.toBigDecimal()); + } + user.netToken = user.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.save(); + + let betId = concat(event.params.sender, Bytes.fromI32(event.params.epoch.toI32())).toHex(); + let bet = new Bet(betId); + bet.round = round.id; + bet.user = user.id; + bet.hash = event.transaction.hash; + bet.amount = event.params.amount.divDecimal(EIGHTEEN_BD); + bet.position = "Bear"; + bet.claimed = false; + bet.createdAt = event.block.timestamp; + bet.updatedAt = event.block.timestamp; + bet.block = event.block.number; + bet.save(); +} + +export function handleClaim(event: Claim): void { + let betId = concat(event.params.sender, Bytes.fromI32(event.params.epoch.toI32())).toHex(); + let bet = Bet.load(betId); + if (bet === null) { + log.warning("Tried to query bet without an existing ID (betId: {})", [betId]); + } else { + bet.claimed = true; + bet.claimedAt = event.block.timestamp; + bet.claimedBlock = event.block.number; + bet.claimedHash = event.transaction.hash; + bet.claimedToken = event.params.amount.divDecimal(EIGHTEEN_BD); + if (bet.amount.gt(ZERO_BD)) { + bet.claimedNetToken = event.params.amount.divDecimal(EIGHTEEN_BD).minus(bet.amount); + } + bet.updatedAt = event.block.timestamp; + bet.save(); + } + + let user = User.load(event.params.sender.toHex()); + if (user === null) { + user = new User(event.params.sender.toHex()); + user.createdAt = event.block.timestamp; + user.updatedAt = event.block.timestamp; + user.block = event.block.number; + user.totalBets = ZERO_BI; + user.totalBetsBull = ZERO_BI; + user.totalBetsBear = ZERO_BI; + user.totalToken = ZERO_BD; + user.totalTokenBull = ZERO_BD; + user.totalTokenBear = ZERO_BD; + user.totalBetsClaimed = ZERO_BI; + user.totalTokenClaimed = ZERO_BD; + user.winRate = HUNDRED_BD; + user.averageToken = ZERO_BD; + user.netToken = ZERO_BD; + user.save(); + } + user.totalBetsClaimed = user.totalBetsClaimed.plus(ONE_BI); + user.totalTokenClaimed = user.totalTokenClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + if (user.totalBets.gt(ZERO_BI)) { + user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); + } + user.netToken = user.netToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.save(); + + let market = Market.load("1"); + if (market === null) { + log.error("Tried to query market after a user claimed for a round (epoch: {})", [event.params.epoch.toString()]); + return; + } + market.totalBetsClaimed = market.totalBetsClaimed.plus(ONE_BI); + market.totalTokenClaimed = market.totalTokenClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); + market.netToken = market.netToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.save(); +} + +export function handleRewardsCalculated(event: RewardsCalculated): void { + let market = Market.load("1"); + if (market === null) { + log.error("Tried to query market after rewards were calculated for a round (epoch: {})", [ + event.params.epoch.toString(), + ]); + return; + } + market.totalTokenTreasury = market.totalTokenTreasury.plus(event.params.treasuryAmount.divDecimal(EIGHTEEN_BD)); + market.save(); +} diff --git a/subgraphs/prediction/v3/package.json b/subgraphs/prediction/v3/package.json new file mode 100644 index 00000000..822d80cc --- /dev/null +++ b/subgraphs/prediction/v3/package.json @@ -0,0 +1,13 @@ +{ + "name": "prediction-v3", + "description": "PancakeSwap Subgraph", + "version": "1.0.0", + "repository": "git@github.com:pancakeswap/pancake-subgraph.git", + "author": "PancakeSwap", + "license": "GPL-3.0-or-later", + "scripts": { + "codegen": "graph codegen subgraph.yaml", + "build": "graph build subgraph.yaml", + "deploy": "graph deploy --product hosted-service --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ pancakeswap/prediction-v3 subgraph.yaml" + } +} diff --git a/subgraphs/prediction/v3/schema.graphql b/subgraphs/prediction/v3/schema.graphql new file mode 100644 index 00000000..f6dc42e9 --- /dev/null +++ b/subgraphs/prediction/v3/schema.graphql @@ -0,0 +1,119 @@ +enum Position { + Bull + Bear + House +} + +type Market @entity { + id: ID! + + epoch: Round + paused: Boolean! + + # Users + totalUsers: BigInt! + + # Bets + totalBets: BigInt! + totalBetsBull: BigInt! + totalBetsBear: BigInt! + totalToken: BigDecimal! + totalTokenBull: BigDecimal! + totalTokenBear: BigDecimal! + totalTokenTreasury: BigDecimal! + + # Statistics + totalBetsClaimed: BigInt! + totalTokenClaimed: BigDecimal! + winRate: BigDecimal! + averageToken: BigDecimal! + netToken: BigDecimal! +} + +type Round @entity { + id: ID! + + epoch: BigInt! + position: Position + failed: Boolean + + previous: Round + + # Start + startAt: BigInt! + startBlock: BigInt! + startHash: Bytes! + + # Lock + lockAt: BigInt + lockBlock: BigInt + lockHash: Bytes + lockPrice: BigDecimal + lockRoundId: BigInt + + # End + closeAt: BigInt + closeBlock: BigInt + closeHash: Bytes + closePrice: BigDecimal + closeRoundId: BigInt + + # Bets + totalBets: BigInt! + totalAmount: BigDecimal! + bullBets: BigInt! + bullAmount: BigDecimal! + bearBets: BigInt! + bearAmount: BigDecimal! + bets: [Bet!]! @derivedFrom(field: "round") +} + +type User @entity { + id: ID! + + createdAt: BigInt! + updatedAt: BigInt! + + block: BigInt! + + # Bets + totalBets: BigInt! + totalBetsBull: BigInt! + totalBetsBear: BigInt! + totalToken: BigDecimal! + totalTokenBull: BigDecimal! + totalTokenBear: BigDecimal! + bets: [Bet!]! @derivedFrom(field: "user") + + # Statistics + totalBetsClaimed: BigInt! + totalTokenClaimed: BigDecimal! + winRate: BigDecimal! + averageToken: BigDecimal! + netToken: BigDecimal! +} + +type Bet @entity { + id: ID! + + # epoch + round: Round! + + user: User! + hash: Bytes! + + amount: BigDecimal! + position: Position! + + claimed: Boolean! + claimedAt: BigInt + claimedBlock: BigInt + claimedHash: Bytes + claimedToken: BigDecimal + claimedNetToken: BigDecimal + + createdAt: BigInt! + updatedAt: BigInt! + + block: BigInt! +} diff --git a/subgraphs/prediction/v3/subgraph.yaml b/subgraphs/prediction/v3/subgraph.yaml new file mode 100644 index 00000000..21b97fbd --- /dev/null +++ b/subgraphs/prediction/v3/subgraph.yaml @@ -0,0 +1,50 @@ +specVersion: 0.0.2 +description: Pancake Prediction V3 +features: + - grafting +graft: + base: QmaHRgUYDNKEXueDTA1Ntnv4rTWsignS9LojZFwyBr2mVm + block: 17140600 +repository: https://github.com/pancakeswap +schema: + file: ./schema.graphql +dataSources: + - kind: ethereum/contract + name: PredictionV3 + network: bsc + source: + address: '0x18B2A687610328590Bc8F2e5fEdDe3b582A49cdA' + abi: PredictionV3 + startBlock: 10333825 + mapping: + kind: ethereum/events + apiVersion: 0.0.4 + language: wasm/assemblyscript + file: ./mappings/index.ts + entities: + - Market + - Round + - User + - Bet + abis: + - name: PredictionV3 + file: ./abis/PredictionV3.json + eventHandlers: + - event: StartRound(indexed uint256) + handler: handleStartRound + - event: LockRound(indexed uint256,indexed uint256,int256) + handler: handleLockRound + - event: EndRound(indexed uint256,indexed uint256,int256) + handler: handleEndRound + - event: BetBull(indexed address,indexed uint256,uint256) + handler: handleBetBull + - event: BetBear(indexed address,indexed uint256,uint256) + handler: handleBetBear + - event: Claim(indexed address,indexed uint256,uint256) + handler: handleClaim + - event: Pause(indexed uint256) + handler: handlePause + - event: Unpause(indexed uint256) + handler: handleUnpause + - event: RewardsCalculated(indexed uint256,uint256,uint256,uint256) + handler: handleRewardsCalculated From 08de1fceef9331af36319eddc38a9eb708cce8e9 Mon Sep 17 00:00:00 2001 From: Philip Date: Tue, 10 May 2022 10:10:52 -0300 Subject: [PATCH 2/4] chore: Update Prediction v3 address --- subgraphs/prediction/v3/subgraph.yaml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/subgraphs/prediction/v3/subgraph.yaml b/subgraphs/prediction/v3/subgraph.yaml index 21b97fbd..07763396 100644 --- a/subgraphs/prediction/v3/subgraph.yaml +++ b/subgraphs/prediction/v3/subgraph.yaml @@ -1,10 +1,5 @@ specVersion: 0.0.2 description: Pancake Prediction V3 -features: - - grafting -graft: - base: QmaHRgUYDNKEXueDTA1Ntnv4rTWsignS9LojZFwyBr2mVm - block: 17140600 repository: https://github.com/pancakeswap schema: file: ./schema.graphql @@ -13,9 +8,9 @@ dataSources: name: PredictionV3 network: bsc source: - address: '0x18B2A687610328590Bc8F2e5fEdDe3b582A49cdA' + address: "0x0da8AD335ff18020b749DE6CEB77e67F4D159F0F" abi: PredictionV3 - startBlock: 10333825 + startBlock: 17677290 mapping: kind: ethereum/events apiVersion: 0.0.4 From 1c5f512bcad46a4f129c548d69b38f0a636981d6 Mon Sep 17 00:00:00 2001 From: Philip Date: Wed, 11 May 2022 03:59:36 -0300 Subject: [PATCH 3/4] chore: Update Token to CAKE --- subgraphs/prediction/v3/mappings/index.ts | 128 +++++++++++----------- subgraphs/prediction/v3/schema.graphql | 30 ++--- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/subgraphs/prediction/v3/mappings/index.ts b/subgraphs/prediction/v3/mappings/index.ts index 985c0de8..c0d51c4e 100644 --- a/subgraphs/prediction/v3/mappings/index.ts +++ b/subgraphs/prediction/v3/mappings/index.ts @@ -35,15 +35,15 @@ export function handlePause(event: Pause): void { market.totalBets = ZERO_BI; market.totalBetsBull = ZERO_BI; market.totalBetsBear = ZERO_BI; - market.totalToken = ZERO_BD; - market.totalTokenBull = ZERO_BD; - market.totalTokenBear = ZERO_BD; - market.totalTokenTreasury = ZERO_BD; + market.totalCAKE = ZERO_BD; + market.totalCAKEBull = ZERO_BD; + market.totalCAKEBear = ZERO_BD; + market.totalCAKETreasury = ZERO_BD; market.totalBetsClaimed = ZERO_BI; - market.totalTokenClaimed = ZERO_BD; + market.totalCAKEClaimed = ZERO_BD; market.winRate = HUNDRED_BD; - market.averageToken = ZERO_BD; - market.netToken = ZERO_BD; + market.averageCAKE = ZERO_BD; + market.netCAKE = ZERO_BD; market.save(); } market.epoch = event.params.epoch.toString(); @@ -88,15 +88,15 @@ export function handleUnpause(event: Unpause): void { market.totalBets = ZERO_BI; market.totalBetsBull = ZERO_BI; market.totalBetsBear = ZERO_BI; - market.totalToken = ZERO_BD; - market.totalTokenBull = ZERO_BD; - market.totalTokenBear = ZERO_BD; - market.totalTokenTreasury = ZERO_BD; + market.totalCAKE = ZERO_BD; + market.totalCAKEBull = ZERO_BD; + market.totalCAKEBear = ZERO_BD; + market.totalCAKETreasury = ZERO_BD; market.totalBetsClaimed = ZERO_BI; - market.totalTokenClaimed = ZERO_BD; + market.totalCAKEClaimed = ZERO_BD; market.winRate = HUNDRED_BD; - market.averageToken = ZERO_BD; - market.netToken = ZERO_BD; + market.averageCAKE = ZERO_BD; + market.netCAKE = ZERO_BD; market.save(); } market.epoch = event.params.epoch.toString(); @@ -118,15 +118,15 @@ export function handleStartRound(event: StartRound): void { market.totalBets = ZERO_BI; market.totalBetsBull = ZERO_BI; market.totalBetsBear = ZERO_BI; - market.totalToken = ZERO_BD; - market.totalTokenBull = ZERO_BD; - market.totalTokenBear = ZERO_BD; - market.totalTokenTreasury = ZERO_BD; + market.totalCAKE = ZERO_BD; + market.totalCAKEBull = ZERO_BD; + market.totalCAKEBear = ZERO_BD; + market.totalCAKETreasury = ZERO_BD; market.totalBetsClaimed = ZERO_BI; - market.totalTokenClaimed = ZERO_BD; + market.totalCAKEClaimed = ZERO_BD; market.winRate = HUNDRED_BD; - market.averageToken = ZERO_BD; - market.netToken = ZERO_BD; + market.averageCAKE = ZERO_BD; + market.netCAKE = ZERO_BD; market.save(); } market.epoch = event.params.epoch.toString(); @@ -213,8 +213,8 @@ export function handleEndRound(event: EndRound): void { event.params.epoch.toString(), ]); } else { - market.totalTokenTreasury = market.totalTokenTreasury.plus(round.totalAmount); - market.netToken = market.netToken.plus(round.totalAmount); + market.totalCAKETreasury = market.totalCAKETreasury.plus(round.totalAmount); + market.netCAKE = market.netCAKE.plus(round.totalAmount); market.save(); } } else if (round.closePrice.gt(round.lockPrice as BigDecimal)) { @@ -239,11 +239,11 @@ export function handleBetBull(event: BetBull): void { } market.totalBets = market.totalBets.plus(ONE_BI); market.totalBetsBull = market.totalBetsBull.plus(ONE_BI); - market.totalToken = market.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); - market.totalTokenBull = market.totalTokenBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalCAKE = market.totalCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalCAKEBull = market.totalCAKEBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); - market.averageToken = market.totalToken.div(market.totalBets.toBigDecimal()); - market.netToken = market.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.averageCAKE = market.totalCAKE.div(market.totalBets.toBigDecimal()); + market.netCAKE = market.netCAKE.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.save(); let round = Round.load(event.params.epoch.toString()); @@ -279,14 +279,14 @@ export function handleBetBull(event: BetBull): void { user.totalBets = ZERO_BI; user.totalBetsBull = ZERO_BI; user.totalBetsBear = ZERO_BI; - user.totalToken = ZERO_BD; - user.totalTokenBull = ZERO_BD; - user.totalTokenBear = ZERO_BD; + user.totalCAKE = ZERO_BD; + user.totalCAKEBull = ZERO_BD; + user.totalCAKEBear = ZERO_BD; user.totalBetsClaimed = ZERO_BI; - user.totalTokenClaimed = ZERO_BD; + user.totalCAKEClaimed = ZERO_BD; user.winRate = HUNDRED_BD; - user.averageToken = ZERO_BD; - user.netToken = ZERO_BD; + user.averageCAKE = ZERO_BD; + user.netCAKE = ZERO_BD; market.totalUsers = market.totalUsers.plus(ONE_BI); market.save(); @@ -294,13 +294,13 @@ export function handleBetBull(event: BetBull): void { user.updatedAt = event.block.timestamp; user.totalBets = user.totalBets.plus(ONE_BI); user.totalBetsBull = user.totalBetsBull.plus(ONE_BI); - user.totalToken = user.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); - user.totalTokenBull = user.totalTokenBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalCAKE = user.totalCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalCAKEBull = user.totalCAKEBull.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); if (user.totalBets.gt(ZERO_BI)) { user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); - user.averageToken = user.totalToken.div(user.totalBets.toBigDecimal()); + user.averageCAKE = user.totalCAKE.div(user.totalBets.toBigDecimal()); } - user.netToken = user.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.netCAKE = user.netCAKE.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); user.save(); let betId = concat(event.params.sender, Bytes.fromI32(event.params.epoch.toI32())).toHex(); @@ -325,11 +325,11 @@ export function handleBetBear(event: BetBear): void { } market.totalBets = market.totalBets.plus(ONE_BI); market.totalBetsBear = market.totalBetsBear.plus(ONE_BI); - market.totalToken = market.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); - market.totalTokenBear = market.totalTokenBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalCAKE = market.totalCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalCAKEBear = market.totalCAKEBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); - market.averageToken = market.totalToken.div(market.totalBets.toBigDecimal()); - market.netToken = market.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.averageCAKE = market.totalCAKE.div(market.totalBets.toBigDecimal()); + market.netCAKE = market.netCAKE.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.save(); let round = Round.load(event.params.epoch.toString()); @@ -365,14 +365,14 @@ export function handleBetBear(event: BetBear): void { user.totalBets = ZERO_BI; user.totalBetsBull = ZERO_BI; user.totalBetsBear = ZERO_BI; - user.totalToken = ZERO_BD; - user.totalTokenBull = ZERO_BD; - user.totalTokenBear = ZERO_BD; + user.totalCAKE = ZERO_BD; + user.totalCAKEBull = ZERO_BD; + user.totalCAKEBear = ZERO_BD; user.totalBetsClaimed = ZERO_BI; - user.totalTokenClaimed = ZERO_BD; + user.totalCAKEClaimed = ZERO_BD; user.winRate = HUNDRED_BD; - user.averageToken = ZERO_BD; - user.netToken = ZERO_BD; + user.averageCAKE = ZERO_BD; + user.netCAKE = ZERO_BD; market.totalUsers = market.totalUsers.plus(ONE_BI); market.save(); @@ -380,13 +380,13 @@ export function handleBetBear(event: BetBear): void { user.updatedAt = event.block.timestamp; user.totalBets = user.totalBets.plus(ONE_BI); user.totalBetsBear = user.totalBetsBear.plus(ONE_BI); - user.totalToken = user.totalToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); - user.totalTokenBear = user.totalTokenBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalCAKE = user.totalCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalCAKEBear = user.totalCAKEBear.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); if (user.totalBets.gt(ZERO_BI)) { user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); - user.averageToken = user.totalToken.div(user.totalBets.toBigDecimal()); + user.averageCAKE = user.totalCAKE.div(user.totalBets.toBigDecimal()); } - user.netToken = user.netToken.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.netCAKE = user.netCAKE.minus(event.params.amount.divDecimal(EIGHTEEN_BD)); user.save(); let betId = concat(event.params.sender, Bytes.fromI32(event.params.epoch.toI32())).toHex(); @@ -413,9 +413,9 @@ export function handleClaim(event: Claim): void { bet.claimedAt = event.block.timestamp; bet.claimedBlock = event.block.number; bet.claimedHash = event.transaction.hash; - bet.claimedToken = event.params.amount.divDecimal(EIGHTEEN_BD); + bet.claimedCAKE = event.params.amount.divDecimal(EIGHTEEN_BD); if (bet.amount.gt(ZERO_BD)) { - bet.claimedNetToken = event.params.amount.divDecimal(EIGHTEEN_BD).minus(bet.amount); + bet.claimedNetCAKE = event.params.amount.divDecimal(EIGHTEEN_BD).minus(bet.amount); } bet.updatedAt = event.block.timestamp; bet.save(); @@ -430,22 +430,22 @@ export function handleClaim(event: Claim): void { user.totalBets = ZERO_BI; user.totalBetsBull = ZERO_BI; user.totalBetsBear = ZERO_BI; - user.totalToken = ZERO_BD; - user.totalTokenBull = ZERO_BD; - user.totalTokenBear = ZERO_BD; + user.totalCAKE = ZERO_BD; + user.totalCAKEBull = ZERO_BD; + user.totalCAKEBear = ZERO_BD; user.totalBetsClaimed = ZERO_BI; - user.totalTokenClaimed = ZERO_BD; + user.totalCAKEClaimed = ZERO_BD; user.winRate = HUNDRED_BD; - user.averageToken = ZERO_BD; - user.netToken = ZERO_BD; + user.averageCAKE = ZERO_BD; + user.netCAKE = ZERO_BD; user.save(); } user.totalBetsClaimed = user.totalBetsClaimed.plus(ONE_BI); - user.totalTokenClaimed = user.totalTokenClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.totalCAKEClaimed = user.totalCAKEClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); if (user.totalBets.gt(ZERO_BI)) { user.winRate = user.totalBetsClaimed.divDecimal(user.totalBets.toBigDecimal()).times(HUNDRED_BD); } - user.netToken = user.netToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + user.netCAKE = user.netCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); user.save(); let market = Market.load("1"); @@ -454,9 +454,9 @@ export function handleClaim(event: Claim): void { return; } market.totalBetsClaimed = market.totalBetsClaimed.plus(ONE_BI); - market.totalTokenClaimed = market.totalTokenClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.totalCAKEClaimed = market.totalCAKEClaimed.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.winRate = market.totalBetsClaimed.divDecimal(market.totalBets.toBigDecimal()).times(HUNDRED_BD); - market.netToken = market.netToken.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); + market.netCAKE = market.netCAKE.plus(event.params.amount.divDecimal(EIGHTEEN_BD)); market.save(); } @@ -468,6 +468,6 @@ export function handleRewardsCalculated(event: RewardsCalculated): void { ]); return; } - market.totalTokenTreasury = market.totalTokenTreasury.plus(event.params.treasuryAmount.divDecimal(EIGHTEEN_BD)); + market.totalCAKETreasury = market.totalCAKETreasury.plus(event.params.treasuryAmount.divDecimal(EIGHTEEN_BD)); market.save(); } diff --git a/subgraphs/prediction/v3/schema.graphql b/subgraphs/prediction/v3/schema.graphql index f6dc42e9..7337f7f8 100644 --- a/subgraphs/prediction/v3/schema.graphql +++ b/subgraphs/prediction/v3/schema.graphql @@ -17,17 +17,17 @@ type Market @entity { totalBets: BigInt! totalBetsBull: BigInt! totalBetsBear: BigInt! - totalToken: BigDecimal! - totalTokenBull: BigDecimal! - totalTokenBear: BigDecimal! - totalTokenTreasury: BigDecimal! + totalCAKE: BigDecimal! + totalCAKEBull: BigDecimal! + totalCAKEBear: BigDecimal! + totalCAKETreasury: BigDecimal! # Statistics totalBetsClaimed: BigInt! - totalTokenClaimed: BigDecimal! + totalCAKEClaimed: BigDecimal! winRate: BigDecimal! - averageToken: BigDecimal! - netToken: BigDecimal! + averageCAKE: BigDecimal! + netCAKE: BigDecimal! } type Round @entity { @@ -80,17 +80,17 @@ type User @entity { totalBets: BigInt! totalBetsBull: BigInt! totalBetsBear: BigInt! - totalToken: BigDecimal! - totalTokenBull: BigDecimal! - totalTokenBear: BigDecimal! + totalCAKE: BigDecimal! + totalCAKEBull: BigDecimal! + totalCAKEBear: BigDecimal! bets: [Bet!]! @derivedFrom(field: "user") # Statistics totalBetsClaimed: BigInt! - totalTokenClaimed: BigDecimal! + totalCAKEClaimed: BigDecimal! winRate: BigDecimal! - averageToken: BigDecimal! - netToken: BigDecimal! + averageCAKE: BigDecimal! + netCAKE: BigDecimal! } type Bet @entity { @@ -109,8 +109,8 @@ type Bet @entity { claimedAt: BigInt claimedBlock: BigInt claimedHash: Bytes - claimedToken: BigDecimal - claimedNetToken: BigDecimal + claimedCAKE: BigDecimal + claimedNetCAKE: BigDecimal createdAt: BigInt! updatedAt: BigInt! From 3710fb5f217248c2c5d10935abf6e5c68e64b431 Mon Sep 17 00:00:00 2001 From: Philip Date: Fri, 13 May 2022 09:10:09 -0300 Subject: [PATCH 4/4] chore: Update Prediction v3 contract --- .../prediction/v3/abis/PredictionV3.json | 79 +++++++++++++++++-- subgraphs/prediction/v3/subgraph.yaml | 4 +- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/subgraphs/prediction/v3/abis/PredictionV3.json b/subgraphs/prediction/v3/abis/PredictionV3.json index 62f6b366..f35fc9de 100644 --- a/subgraphs/prediction/v3/abis/PredictionV3.json +++ b/subgraphs/prediction/v3/abis/PredictionV3.json @@ -1,6 +1,11 @@ [ { "inputs": [ + { + "internalType": "contract IERC20", + "name": "_token", + "type": "address" + }, { "internalType": "address", "name": "_oracleAddress", @@ -202,6 +207,19 @@ "name": "NewBufferAndIntervalSeconds", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "operator", + "type": "address" + } + ], + "name": "NewKeeperAddress", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -458,11 +476,16 @@ "internalType": "uint256", "name": "epoch", "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" } ], "name": "betBear", "outputs": [], - "stateMutability": "payable", + "stateMutability": "nonpayable", "type": "function" }, { @@ -471,11 +494,16 @@ "internalType": "uint256", "name": "epoch", "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" } ], "name": "betBull", "outputs": [], - "stateMutability": "payable", + "stateMutability": "nonpayable", "type": "function" }, { @@ -623,7 +651,7 @@ { "components": [ { - "internalType": "enum PancakePredictionV2.Position", + "internalType": "enum PancakePredictionV3.Position", "name": "position", "type": "uint8" }, @@ -638,7 +666,7 @@ "type": "bool" } ], - "internalType": "struct PancakePredictionV2.BetInfo[]", + "internalType": "struct PancakePredictionV3.BetInfo[]", "name": "", "type": "tuple[]" }, @@ -683,6 +711,19 @@ "stateMutability": "view", "type": "function" }, + { + "inputs": [], + "name": "keeperAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { @@ -699,7 +740,7 @@ "name": "ledger", "outputs": [ { - "internalType": "enum PancakePredictionV2.Position", + "internalType": "enum PancakePredictionV3.Position", "name": "position", "type": "uint8" }, @@ -979,6 +1020,19 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [ + { + "internalType": "address", + "name": "_keeperAddress", + "type": "address" + } + ], + "name": "setKeeper", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [ { @@ -1044,6 +1098,19 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [], + "name": "token", + "outputs": [ + { + "internalType": "contract IERC20", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { @@ -1114,4 +1181,4 @@ "stateMutability": "view", "type": "function" } -] +] \ No newline at end of file diff --git a/subgraphs/prediction/v3/subgraph.yaml b/subgraphs/prediction/v3/subgraph.yaml index 07763396..cfc40f06 100644 --- a/subgraphs/prediction/v3/subgraph.yaml +++ b/subgraphs/prediction/v3/subgraph.yaml @@ -8,9 +8,9 @@ dataSources: name: PredictionV3 network: bsc source: - address: "0x0da8AD335ff18020b749DE6CEB77e67F4D159F0F" + address: "0x0E3A8078EDD2021dadcdE733C6b4a86E51EE8f07" abi: PredictionV3 - startBlock: 17677290 + startBlock: 17760782 mapping: kind: ethereum/events apiVersion: 0.0.4