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

Feature/affliate program #225

Closed
wants to merge 10 commits into from
Closed
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
495 changes: 495 additions & 0 deletions subgraphs/affiliate-program/abis/AffiliateProgram.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions subgraphs/affiliate-program/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "affiliate-program",
"license": "UNLICENSED",
"scripts": {
"auth": "graph auth --product hosted-service SUBGRAPH_KEY",
"codegen": "graph codegen subgraph.yaml",
"build": "graph build subgraph.yaml",
"deploy": "graph deploy --product hosted-service pancakeswap/affiliate-program"
}
}
13 changes: 13 additions & 0 deletions subgraphs/affiliate-program/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum RequestType {
AFFILIATE
USER
}
type ClaimRecord @entity {
id: ID!
hash: Bytes! # transaction hash
type: RequestType!
nonce: BigInt! # uint256
claimer: Bytes! # address
amount: BigInt! # uint256
claimTime: BigInt! # uint256
}
17 changes: 17 additions & 0 deletions subgraphs/affiliate-program/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Claim } from "../generated/AffiliateProgram/AffiliateProgram";
import { ClaimRecord } from "../generated/schema";

export function handleClaim(event: Claim): void {
let claimType = event.params.claimType == 0 ? "AFFILIATE" : "USER";
let entity = new ClaimRecord(
event.params.claimer.toHexString().concat("-").concat(claimType).concat("-").concat(event.params.nonce.toString())
);
entity.nonce = event.params.nonce;
entity.claimer = event.params.claimer;
entity.type = claimType;
entity.amount = event.params.amount;
entity.hash = event.transaction.hash;
entity.claimTime = event.block.timestamp;

entity.save();
}
24 changes: 24 additions & 0 deletions subgraphs/affiliate-program/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
specVersion: 0.0.2
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: AffiliateProgram
network: bsc
source:
address: "0x92C73D90F709DFf7e5E7307e8F2EE20e39396b12"
abi: AffiliateProgram
startBlock: 28916072
mapping:
kind: ethereum/events
apiVersion: 0.0.2
language: wasm/assemblyscript
entities:
- Claim
abis:
- name: AffiliateProgram
file: ./abis/AffiliateProgram.json
eventHandlers:
- event: Claim(uint256,address,uint8,uint256)
handler: handleClaim
file: ./src/mapping.ts
40 changes: 34 additions & 6 deletions subgraphs/exchange-eth/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
} from "../generated/schema";
import { Burn, Mint, Swap, Sync, Transfer } from "../generated/templates/Pair/Pair";
import { updatePairDayData, updatePairHourData, updatePancakeDayData, updateTokenDayData } from "./dayUpdates";
import { findEthPerToken, getETHPriceInUSD, getTrackedLiquidityUSD, getTrackedVolumeUSD } from "./pricing";
import {
findEthPerToken,
getETHPriceInUSD,
getTrackedFeeVolumeUSD,
getTrackedLiquidityUSD,
getTrackedVolumeUSD,
} from "./pricing";
import { ADDRESS_ZERO, BI_18, convertTokenToDecimal, FACTORY_ADDRESS, ONE_BI, ZERO_BD } from "./utils";

function isCompleteMint(mintId: string): boolean {
Expand Down Expand Up @@ -361,13 +367,27 @@ export function handleSwap(event: Swap): void {
// BNB/USD prices
let bundle = Bundle.load("1");

// get total amounts of derived USD and ETH for tracking
let derivedAmountBNB = token1.derivedETH
.times(amount1Total)
.plus(token0.derivedETH.times(amount0Total))
.div(BigDecimal.fromString("2"));
let derivedToken0AmountETH = token0.derivedETH.times(amount0Total);
let derivedToken1AmountETH = token1.derivedETH.times(amount1Total);

// get total amounts of derived USD and BNB for tracking
let derivedAmountBNB = derivedToken1AmountETH.plus(derivedToken0AmountETH).div(BigDecimal.fromString("2"));
let derivedAmountUSD = derivedAmountBNB.times(bundle.ethPrice);

// get swap fee amount of derived USD and BNB for tracking
let derivedFeeAmountBNB: BigDecimal;
if (
derivedToken0AmountETH.equals(BigDecimal.fromString("0")) ||
derivedToken1AmountETH.equals(BigDecimal.fromString("0"))
) {
derivedFeeAmountBNB = ZERO_BD;
} else if (derivedToken0AmountETH.ge(derivedToken1AmountETH)) {
derivedFeeAmountBNB = derivedToken0AmountETH.minus(derivedToken1AmountETH);
} else {
derivedFeeAmountBNB = derivedToken1AmountETH.minus(derivedToken0AmountETH);
}
let derivedFeeAmountUSD = derivedFeeAmountBNB.times(bundle.ethPrice);

// only accounts for volume through white listed tokens
let trackedAmountUSD = getTrackedVolumeUSD(
bundle as Bundle,
Expand All @@ -376,6 +396,13 @@ export function handleSwap(event: Swap): void {
amount1Total,
token1 as Token
);
let trackedFeeAmountUSD = getTrackedFeeVolumeUSD(
bundle as Bundle,
amount0Total,
token0 as Token,
amount1Total,
token1 as Token
);

let trackedAmountBNB: BigDecimal;
if (bundle.ethPrice.equals(ZERO_BD)) {
Expand Down Expand Up @@ -448,6 +475,7 @@ export function handleSwap(event: Swap): void {
swap.logIndex = event.logIndex;
// use the tracked amount if we have it
swap.amountUSD = trackedAmountUSD === ZERO_BD ? derivedAmountUSD : trackedAmountUSD;
swap.amountFeeUSD = trackedFeeAmountUSD === ZERO_BD ? derivedFeeAmountUSD : trackedFeeAmountUSD;
swap.save();

// update the transaction
Expand Down
30 changes: 30 additions & 0 deletions subgraphs/exchange-eth/mappings/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ export function getTrackedVolumeUSD(
return ZERO_BD;
}

/**
* Accepts tokens and amounts, return tracked fee amount based on token whitelist
* If both are, return the difference between the token amounts
* If not, return 0
*/
export function getTrackedFeeVolumeUSD(
bundle: Bundle,
tokenAmount0: BigDecimal,
token0: Token,
tokenAmount1: BigDecimal,
token1: Token
): BigDecimal {
let price0 = token0.derivedETH.times(bundle.ethPrice);
let price1 = token1.derivedETH.times(bundle.ethPrice);

// both are whitelist tokens, take average of both amounts
if (WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
let tokenAmount0USD = tokenAmount0.times(price0);
let tokenAmount1USD = tokenAmount1.times(price1);
if (tokenAmount0USD.ge(tokenAmount1USD)) {
return tokenAmount0USD.minus(tokenAmount1USD);
} else {
return tokenAmount1USD.minus(tokenAmount0USD);
}
}

// neither token is on white list, tracked volume is 0
return ZERO_BD;
}

/**
* Accepts tokens and amounts, return tracked amount based on token whitelist
* If one token on whitelist, return amount in that token converted to USD * 2.
Expand Down
1 change: 1 addition & 0 deletions subgraphs/exchange-eth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type Swap @entity {

# derived info
amountUSD: BigDecimal!
amountFeeUSD: BigDecimal!
}

type PancakeDayData @entity {
Expand Down
32 changes: 27 additions & 5 deletions subgraphs/exchange-stableswap/mappings/services/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { BigDecimal, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts";
import { Bundle, Pair, Swap, Token, Transaction } from "../../generated/schema";
import { BIG_DECIMAL_ZERO, BIG_INT_ONE, convertTokenToDecimal } from "../utils";
import { getTrackedVolumeUSD } from "../utils/pricing";
import { getTrackedFeeVolumeUSD, getTrackedVolumeUSD } from "../utils/pricing";
import { getOrCreateFactory } from "../utils/data";
import { updatePairDayData, updatePairHourData, updatePancakeDayData, updateTokenDayData } from "./dayUpdates";

Expand Down Expand Up @@ -31,13 +31,27 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
// BNB/USD prices
let bundle = Bundle.load("1");

let derivedToken0AmountBNB = token0.derivedBNB.times(amount0Total);
let derivedToken1AmountBNB = token1.derivedBNB.times(amount1Total);

// get total amounts of derived USD and BNB for tracking
let derivedAmountBNB = token1.derivedBNB
.times(amount1Total)
.plus(token0.derivedBNB.times(amount0Total))
.div(BigDecimal.fromString("2"));
let derivedAmountBNB = derivedToken1AmountBNB.plus(derivedToken0AmountBNB).div(BigDecimal.fromString("2"));
let derivedAmountUSD = derivedAmountBNB.times(bundle.bnbPrice);

// get swap fee amount of derived USD and BNB for tracking
let derivedFeeAmountBNB: BigDecimal;
if (
derivedToken0AmountBNB.equals(BigDecimal.fromString("0")) ||
derivedToken1AmountBNB.equals(BigDecimal.fromString("0"))
) {
derivedFeeAmountBNB = BIG_DECIMAL_ZERO;
} else if (derivedToken0AmountBNB.ge(derivedToken1AmountBNB)) {
derivedFeeAmountBNB = derivedToken0AmountBNB.minus(derivedToken1AmountBNB);
} else {
derivedFeeAmountBNB = derivedToken1AmountBNB.minus(derivedToken0AmountBNB);
}
let derivedFeeAmountUSD = derivedFeeAmountBNB.times(bundle.bnbPrice);

// only accounts for volume through white listed tokens
let trackedAmountUSD = getTrackedVolumeUSD(
bundle as Bundle,
Expand All @@ -46,6 +60,13 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
amount1Total,
token1 as Token
);
let trackedFeeAmountUSD = getTrackedFeeVolumeUSD(
bundle as Bundle,
amount0Total,
token0 as Token,
amount1Total,
token1 as Token
);

let price0 = token0.derivedBNB.times(bundle.bnbPrice);
let price1 = token1.derivedBNB.times(bundle.bnbPrice);
Expand Down Expand Up @@ -120,6 +141,7 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
swap.logIndex = event.logIndex;
// use the tracked amount if we have it
swap.amountUSD = trackedAmountUSD === BIG_DECIMAL_ZERO ? derivedAmountUSD : trackedAmountUSD;
swap.amountFeeUSD = trackedFeeAmountUSD === BIG_DECIMAL_ZERO ? derivedFeeAmountUSD : trackedFeeAmountUSD;
swap.save();

// update the transaction
Expand Down
30 changes: 30 additions & 0 deletions subgraphs/exchange-stableswap/mappings/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,36 @@ export function getTrackedVolumeUSD(
return BIG_DECIMAL_ZERO;
}

/**
* Accepts tokens and amounts, return tracked fee amount based on token whitelist
* If both are, return the difference between the token amounts
* If not, return 0
*/
export function getTrackedFeeVolumeUSD(
bundle: Bundle,
tokenAmount0: BigDecimal,
token0: Token,
tokenAmount1: BigDecimal,
token1: Token
): BigDecimal {
let price0 = token0.derivedBNB.times(bundle.bnbPrice);
let price1 = token1.derivedBNB.times(bundle.bnbPrice);

// both are whitelist tokens, take average of both amounts
if (WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
let tokenAmount0USD = tokenAmount0.times(price0);
let tokenAmount1USD = tokenAmount1.times(price1);
if (tokenAmount0USD.ge(tokenAmount1USD)) {
return tokenAmount0USD.minus(tokenAmount1USD);
} else {
return tokenAmount1USD.minus(tokenAmount0USD);
}
}

// neither token is on white list, tracked volume is 0
return BIG_DECIMAL_ZERO;
}

/**
* Accepts tokens and amounts, return tracked amount based on token whitelist
* If one token on whitelist, return amount in that token converted to USD * 2.
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/exchange-stableswap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"auth": "graph auth --product hosted-service SUBGRAPH_KEY",
"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/exchange-stableswap subgraph.yaml"
"deploy": "graph deploy --product hosted-service pancakeswap/exchange-stableswap"
}
}
1 change: 1 addition & 0 deletions subgraphs/exchange-stableswap/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type Swap @entity {

# derived info
amountUSD: BigDecimal!
amountFeeUSD: BigDecimal!
}

type PancakeDayData @entity {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/exchange-stableswap/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/pancakeswap
schema:
file: ./schema.graphql
graft:
base: QmZMeDY8PjpwRQKxUEN7ZXeJPX5BpnER8MDN5zCfyWNAFR
base: Qme4U6by9yqGwTPKRaPvbgo7Ar9CwYFaTjmy9z6uVSjxvL
block: 25535458 # for previous exiting factory
dataSources:
- kind: ethereum/contract
Expand Down
38 changes: 33 additions & 5 deletions subgraphs/exchange/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
} from "../generated/schema";
import { Mint, Burn, Swap, Transfer, Sync } from "../generated/templates/Pair/Pair";
import { updatePairDayData, updateTokenDayData, updatePancakeDayData, updatePairHourData } from "./dayUpdates";
import { getBnbPriceInUSD, findBnbPerToken, getTrackedVolumeUSD, getTrackedLiquidityUSD } from "./pricing";
import {
getBnbPriceInUSD,
findBnbPerToken,
getTrackedVolumeUSD,
getTrackedLiquidityUSD,
getTrackedFeeVolumeUSD,
} from "./pricing";
import { convertTokenToDecimal, ADDRESS_ZERO, FACTORY_ADDRESS, ONE_BI, ZERO_BD, BI_18 } from "./utils";

function isCompleteMint(mintId: string): boolean {
Expand Down Expand Up @@ -353,13 +359,27 @@ export function handleSwap(event: Swap): void {
// BNB/USD prices
let bundle = Bundle.load("1");

let derivedToken0AmountBNB = token0.derivedBNB.times(amount0Total);
let derivedToken1AmountBNB = token1.derivedBNB.times(amount1Total);

// get total amounts of derived USD and BNB for tracking
let derivedAmountBNB = token1.derivedBNB
.times(amount1Total)
.plus(token0.derivedBNB.times(amount0Total))
.div(BigDecimal.fromString("2"));
let derivedAmountBNB = derivedToken1AmountBNB.plus(derivedToken0AmountBNB).div(BigDecimal.fromString("2"));
let derivedAmountUSD = derivedAmountBNB.times(bundle.bnbPrice);

// get swap fee amount of derived USD and BNB for tracking
let derivedFeeAmountBNB: BigDecimal;
if (
derivedToken0AmountBNB.equals(BigDecimal.fromString("0")) ||
derivedToken1AmountBNB.equals(BigDecimal.fromString("0"))
) {
derivedFeeAmountBNB = ZERO_BD;
} else if (derivedToken0AmountBNB.ge(derivedToken1AmountBNB)) {
derivedFeeAmountBNB = derivedToken0AmountBNB.minus(derivedToken1AmountBNB);
} else {
derivedFeeAmountBNB = derivedToken1AmountBNB.minus(derivedToken0AmountBNB);
}
let derivedFeeAmountUSD = derivedFeeAmountBNB.times(bundle.bnbPrice);

// only accounts for volume through white listed tokens
let trackedAmountUSD = getTrackedVolumeUSD(
bundle as Bundle,
Expand All @@ -368,6 +388,13 @@ export function handleSwap(event: Swap): void {
amount1Total,
token1 as Token
);
let trackedFeeAmountUSD = getTrackedFeeVolumeUSD(
bundle as Bundle,
amount0Total,
token0 as Token,
amount1Total,
token1 as Token
);

let trackedAmountBNB: BigDecimal;
if (bundle.bnbPrice.equals(ZERO_BD)) {
Expand Down Expand Up @@ -438,6 +465,7 @@ export function handleSwap(event: Swap): void {
swap.logIndex = event.logIndex;
// use the tracked amount if we have it
swap.amountUSD = trackedAmountUSD === ZERO_BD ? derivedAmountUSD : trackedAmountUSD;
swap.amountFeeUSD = trackedFeeAmountUSD === ZERO_BD ? derivedFeeAmountUSD : trackedFeeAmountUSD;
swap.save();

// update the transaction
Expand Down
Loading