diff --git a/src/affiliateRewards.ts b/src/affiliateRewards.ts index c04d792..2314e57 100644 --- a/src/affiliateRewards.ts +++ b/src/affiliateRewards.ts @@ -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 { @@ -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, @@ -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); diff --git a/src/ciphex-presale.ts b/src/ciphex-presale.ts index a115da1..9f643a1 100644 --- a/src/ciphex-presale.ts +++ b/src/ciphex-presale.ts @@ -3,6 +3,7 @@ import { bigDecimal, BigDecimal, BigInt, + log, } from "@graphprotocol/graph-ts"; import { Bought as BoughtEvent, @@ -24,8 +25,6 @@ import { TotalContributions, UserContribution, } from "../generated/schema"; -import {} from "./ranks"; -import { log } from "@graphprotocol/graph-ts"; import { calculateReferralRewards, tokenDecimals, @@ -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; @@ -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); diff --git a/src/ranks.ts b/src/ranks.ts index 7391521..fb055bf 100644 --- a/src/ranks.ts +++ b/src/ranks.ts @@ -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;