Skip to content
Open
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
14 changes: 7 additions & 7 deletions src/affiliateRewards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { getRanksLengh, getRank, Rank } from "./ranks";
import { log } from "@graphprotocol/graph-ts";
export const usdDecimals = new BigInt(1000000);
export const tokenDecimals = new BigInt(1 * 10 ** 18);
export class Percentage {
Expand All @@ -18,9 +17,9 @@ export class AffiliateResult {
this.usdt = usdt;
this.cpx = cpx;
}
getResString(): string {
return `usdt = ${this.usdt.toString()}; cpx = ${this.cpx.toString()}`;
}
// getResString(): string {
// return `usdt = ${this.usdt.toString()}; cpx = ${this.cpx.toString()}`;
// }
}
export function calculateReferralRewards(
usdcAmount: BigInt,
Expand All @@ -35,12 +34,13 @@ export function calculateReferralRewards(
let total: BigDecimal = new BigDecimal(usdcAmount.div(usdDecimals));
let usdRewards: BigDecimal = BigDecimal.fromString("0");
let percentages: Percentage[] = [];
log.info(`getRanksLengh: {}`, [getRanksLengh().toString()]);
for (let i = getRanksLengh() - 1; i >= 0; i++) {
let ranksLength = getRanksLengh();
if (ranksLength == 0)
return new AffiliateResult(BigDecimal.zero(), BigDecimal.zero());
for (let i = ranksLength - 1; i >= 0; i--) {
if (total == zeroBd) break;
const rank: Rank | null = getRank(i);
if (!rank) break;
log.info(`getRank: data {}, index {}`, [rank.getResString(), i.toString()]);
// Calculate the maximum amount that can be attributed to this rank
let bracketMax: BigDecimal;
// same as const bracketMax = Math.min(rank.max - rank.min + 1, total);
Expand Down
39 changes: 22 additions & 17 deletions src/ciphex-presale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
bigDecimal,
BigDecimal,
BigInt,
log,
} from "@graphprotocol/graph-ts";
import {
Bought as BoughtEvent,
Expand All @@ -24,8 +25,6 @@ import {
TotalContributions,
UserContribution,
} from "../generated/schema";
import {} from "./ranks";
import { log } from "@graphprotocol/graph-ts";
import {
calculateReferralRewards,
tokenDecimals,
Expand Down Expand Up @@ -75,7 +74,7 @@ export function handleBought(event: BoughtEvent): void {
);
user.save();

log.info("Before handling ReferralContribution:{}", []);
log.info("Before referral check", []);
// handling ReferralContribution
if (event.params.referral.notEqual(zeroAddress)) {
const referralId = event.params.referral;
Expand All @@ -87,43 +86,49 @@ export function handleBought(event: BoughtEvent): void {
referral.totalUsdRewards = BigInt.fromI32(0);
referral.totalCiphexRewards = BigInt.fromI32(0);
}
log.info("Usercpmtribution usd: {}", [usd.toString()]);
log.info("Usercpmtribution cpx: {}", [entity.ciphexAmount.toString()]);
referral.totalUsdContribution = referral.totalUsdContribution.plus(usd);
referral.totalCiphexContribution = referral.totalCiphexContribution.plus(
entity.ciphexAmount
);
log.info("Before calculateReferralRewards execution {}", []);

/* log.info("Before calculateReferralRewards", []);
// handling Referral rewards
let rewRes: AffiliateResult = calculateReferralRewards(
referral.totalUsdContribution,
referral.totalCiphexContribution
);
log.info("calculateReferralRewards {}", [rewRes.getResString()]);
let usdRewDelta: BigInt = BigInt.fromString(rewRes.usdt.toString())
log.info("After calculateReferralRewards", []);
let usdRewDelta: BigInt = BigInt.fromString(
rewRes.usdt.truncate(0).toString()
)
.times(usdDecimals)
.minus(referral.totalUsdRewards);
let cpxRewDelta: BigInt = BigInt.fromString(rewRes.cpx.toString())
let cpxRewDelta: BigInt = BigInt.fromString(
rewRes.cpx.truncate(0).toString()
)
.times(tokenDecimals)
.minus(referral.totalCiphexRewards);

referral.totalUsdRewards = BigInt.fromString(rewRes.usdt.toString());
referral.totalCiphexRewards = BigInt.fromString(rewRes.cpx.toString());
log.info("After converting res from BigDecimal to BigInt", []);
referral.totalUsdRewards = BigInt.fromString(
rewRes.usdt.truncate(0).toString()
);
referral.totalCiphexRewards = BigInt.fromString(
rewRes.cpx.truncate(0).toString()
); */
log.info("After updating total rewards", []);
let totalAffiliateRewards = new TotalAffiliateRewards(zeroAddress);
if (!totalAffiliateRewards) {
totalAffiliateRewards = new TotalAffiliateRewards(zeroAddress);
totalAffiliateRewards.totalUsdRewards = BigInt.fromI32(0);
totalAffiliateRewards.totalCiphexRewards = BigInt.fromI32(0);
}
totalAffiliateRewards.totalUsdRewards =
totalAffiliateRewards.totalUsdRewards.plus(usdRewDelta);
totalAffiliateRewards.totalCiphexRewards =
totalAffiliateRewards.totalCiphexRewards.plus(cpxRewDelta);
// totalAffiliateRewards.totalUsdRewards =
// totalAffiliateRewards.totalUsdRewards.plus(usdRewDelta);
// totalAffiliateRewards.totalCiphexRewards =
// totalAffiliateRewards.totalCiphexRewards.plus(cpxRewDelta);
referral.save();
totalAffiliateRewards.save();
}
log.info("Before handling TotalContributions", []);

// handling TotalContributions
let totalContributions = TotalContributions.load(zeroAddress);
Expand Down
23 changes: 10 additions & 13 deletions src/ranks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,29 @@ export class Rank {
this.max = BigDecimal.fromString(max);
this.rate = BigDecimal.fromString(rate);
}
getResString(): string {
return `min = ${this.min.toString()}; max = ${this.max.toString()}; rate = ${this.rate.toString()}`;
}
}

export let ranks: Rank[] = [];
// rank 1, zero means Infinity
ranks.push(new Rank("650000", "0", "0.3"));
ranks.push(new Rank("650000", "0", "300"));
// rank 2
ranks.push(new Rank("490000", "650000", "0.26"));
ranks.push(new Rank("490000", "650000", "260"));
// rank 3
ranks.push(new Rank("350000", "490000", "0.24"));
ranks.push(new Rank("350000", "490000", "240"));
// rank 4
ranks.push(new Rank("250000", "350000", "0.22"));
ranks.push(new Rank("250000", "350000", "220"));
// rank 5
ranks.push(new Rank("175000", "250000", "0.2"));
ranks.push(new Rank("175000", "250000", "200"));
// rank 6
ranks.push(new Rank("115000", "175000", "0.18"));
ranks.push(new Rank("115000", "175000", "180"));
// rank 7
ranks.push(new Rank("75000", "115000", "0.16"));
ranks.push(new Rank("75000", "115000", "160"));
// rank 8
ranks.push(new Rank("45000", "75000", "0.14"));
ranks.push(new Rank("45000", "75000", "140"));
// rank 9
ranks.push(new Rank("24000", "45000", "0.12"));
ranks.push(new Rank("24000", "45000", "120"));
// rank 10
ranks.push(new Rank("0", "24000", "0.1"));
ranks.push(new Rank("0", "24000", "100"));

export function getRanksLengh(): i32 {
return ranks.length;
Expand Down