Skip to content

Commit

Permalink
Merge pull request #1150 from Giveth/get_giv_price_from_givback_calcu…
Browse files Browse the repository at this point in the history
…lation_instead_of_monoswap

Get giv price from givback-calculation instead of monoswap
  • Loading branch information
mohammadranjbarz authored Oct 8, 2023
2 parents 7edd1f4 + be80bd7 commit ea97ab5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions config/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ REDIS_PORT=6379
REDIS_PASSWORD=

GIVETH_TRACE_BASE_URL=https://feathers.develop.giveth.io
GIVETH_GIV_PRICES_URL=https://givback.develop.giveth.io/givPrice

PINATA_GATEWAY_ADDRESS=https://giveth.mypinata.cloud

Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ function createDonationTestCases() {
assert.isTrue(donation?.isTokenEligibleForGivback);
});

it('should fill usd value of when creating donation', async () => {
it('should fill usd value of when creating GIV donation', async () => {
const project = await saveProjectDirectlyToDb(createProjectData());
const user = await User.create({
walletAddress: generateRandomEtheriumAddress(),
Expand Down
42 changes: 26 additions & 16 deletions src/services/donationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { getTransactionInfoFromNetwork } from './transactionService';
import { findProjectById } from '../repositories/projectRepository';
import { convertExponentialNumber } from '../utils/utils';
import { fetchGivHistoricPrice } from './givPriceService';
import { fetchGivHistoricPrice, fetchGivPrice } from './givPriceService';
import {
findDonationById,
findStableCoinDonationsWithoutPrice,
Expand Down Expand Up @@ -41,18 +41,28 @@ export const updateDonationPricesAndValues = async (
amount: string | number,
) => {
try {
const tokenPrices = await getMonoSwapTokenPrices(
currency,
baseTokens,
Number(priceChainId),
);
if (currency === 'GIV') {
const { givPriceInEth, ethPriceInUsd, givPriceInUsd } =
await fetchGivPrice();

donation.priceEth = toFixNumber(ethPriceInUsd, 7);
donation.priceUsd = toFixNumber(givPriceInUsd, 4);
donation.valueUsd = toFixNumber(donation.amount * givPriceInUsd, 4);
donation.valueEth = toFixNumber(donation.amount * givPriceInEth, 7);
} else {
const tokenPrices = await getMonoSwapTokenPrices(
currency,
baseTokens,
Number(priceChainId),
);

if (tokenPrices.length !== 0) {
donation.priceUsd = Number(tokenPrices[0]);
donation.priceEth = Number(tokenPrices[1]);
if (tokenPrices.length !== 0) {
donation.priceUsd = Number(tokenPrices[0]);
donation.priceEth = Number(tokenPrices[1]);

donation.valueUsd = Number(amount) * donation.priceUsd;
donation.valueEth = Number(amount) * donation.priceEth;
donation.valueUsd = Number(amount) * donation.priceUsd;
donation.valueEth = Number(amount) * donation.priceEth;
}
}
} catch (e) {
logger.error('Error in getting price from monoswap', {
Expand Down Expand Up @@ -233,18 +243,18 @@ export const updateOldGivDonationsPrice = async () => {
...givHistoricPrices,
valueEth: toFixNumber(
donation.amount * givHistoricPrices.givPriceInEth,
6,
7,
),
});
donation.priceEth = toFixNumber(givHistoricPrices.ethPriceInUsd, 6);
donation.priceUsd = toFixNumber(givHistoricPrices.givPriceInUsd, 3);
donation.priceEth = toFixNumber(givHistoricPrices.ethPriceInUsd, 7);
donation.priceUsd = toFixNumber(givHistoricPrices.givPriceInUsd, 4);
donation.valueUsd = toFixNumber(
donation.amount * givHistoricPrices.givPriceInUsd,
3,
4,
);
donation.valueEth = toFixNumber(
donation.amount * givHistoricPrices.givPriceInEth,
6,
7,
);
await donation.save();
await updateTotalDonationsOfProject(donation.projectId);
Expand Down
17 changes: 16 additions & 1 deletion src/services/givPriceService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Axios, { AxiosResponse } from 'axios';
import axiosRetry from 'axios-retry';
import config from '../config';
import { logger } from '../utils/logger';
import { NETWORK_IDS } from '../provider';

Expand Down Expand Up @@ -51,3 +50,19 @@ export const fetchGivHistoricPrice = async (
throw e;
}
};

export const fetchGivPrice = async (): Promise<GivPricesResponse> => {
try {
/**
* @see {@link https://givback.develop.giveth.io/api-docs/#/default/get_givPrice}
*/
const result = await Axios.get(givPricesUrl, {
headers: { accept: 'application/json' },
timeout: axiosTimeout,
});
return result.data;
} catch (e) {
logger.error('fetching Giv Price fetchGivPrice() err', e);
throw e;
}
};

0 comments on commit ea97ab5

Please sign in to comment.