Skip to content

Commit

Permalink
remove unnecesary code and improve migration
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Dec 23, 2023
1 parent 66d195f commit 6a89f44
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
48 changes: 20 additions & 28 deletions migration/1703044586989-addSolanaToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ import { NETWORK_IDS } from '../src/provider';
import { Token } from '../src/entities/token';
import seedTokens from './data/seedTokens';
import { ChainType } from '../src/types/network';
import { SOLANA_SYSTEM_ADDRESS } from '../src/utils/networks';
import { SOLANA_SYSTEM_PROGRAM } from '../src/utils/networks';

export class addSolanaToken1703044586989 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.manager.save(
Token,
seedTokens
.filter(token => token.chainType === ChainType.SOLANA)
.map(t => {
t.address = t.address;
return t;
}),
seedTokens.filter(
token =>
token.address === SOLANA_SYSTEM_PROGRAM &&
token.chainType === ChainType.SOLANA,
),
);
const tokens = await queryRunner.query(
`SELECT * FROM token WHERE "chainType" = $1 AND "address" = $2`,
[ChainType.SOLANA, SOLANA_SYSTEM_PROGRAM],
);
const tokens = await queryRunner.query(`
SELECT * FROM token
WHERE "chainType" = ${ChainType.SOLANA}
AND "address" = ${SOLANA_SYSTEM_ADDRESS}
`);
const givethOrganization = (
await queryRunner.query(`SELECT * FROM organization
WHERE label='giveth'`)
Expand All @@ -29,31 +27,25 @@ export class addSolanaToken1703044586989 implements MigrationInterface {
await queryRunner.query(`SELECT * FROM organization
WHERE label='trace'`)
)[0];

for (const token of tokens) {
await queryRunner.query(`INSERT INTO organization_tokens_token ("tokenId","organizationId") VALUES
(${token.id}, ${givethOrganization.id}),
(${token.id}, ${traceOrganization.id})
;`);
}
await queryRunner.query(
`INSERT INTO organization_tokens_token ("tokenId", "organizationId") VALUES ($1, $2), ($1, $3)`,
[tokens[0].id, givethOrganization.id, traceOrganization.id],
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
const tokens = await queryRunner.query(`
SELECT * FROM token
WHERE "chainType" = ${ChainType.SOLANA}
AND "address" = ${SOLANA_SYSTEM_ADDRESS}
`);
const tokens = await queryRunner.query(
`SELECT * FROM token WHERE "chainType" = $1 AND "address" = $2`,
[ChainType.SOLANA, SOLANA_SYSTEM_PROGRAM],
);
await queryRunner.query(
`DELETE FROM organization_tokens_token WHERE "tokenId" IN (${tokens
.map(token => token.id)
.join(',')})`,
);
await queryRunner.query(
`
DELETE from token
WHERE "networkId" = ${NETWORK_IDS.SOLANA}
`,
`DELETE FROM token WHERE "chainType" = $1 AND "address" = $2`,
[ChainType.SOLANA, SOLANA_SYSTEM_PROGRAM],
);
}
}
4 changes: 2 additions & 2 deletions migration/data/seedTokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { COINGECKO_TOKEN_IDS } from '../../src/adapters/price/CoingeckoPriceAdapter';
import { NETWORK_IDS } from '../../src/provider';
import { ChainType } from '../../src/types/network';
import { SOLANA_SYSTEM_ADDRESS } from '../../src/utils/networks';
import { SOLANA_SYSTEM_PROGRAM } from '../../src/utils/networks';

interface ITokenData {
name: string;
Expand Down Expand Up @@ -1253,7 +1253,7 @@ const seedTokens: ITokenData[] = [
{
name: 'Solana native token',
symbol: 'SOL',
address: SOLANA_SYSTEM_ADDRESS,
address: SOLANA_SYSTEM_PROGRAM,
decimals: 9,
networkId: NETWORK_IDS.SOLANA,
chainType: ChainType.SOLANA,
Expand Down
6 changes: 3 additions & 3 deletions src/services/donationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ export const updateDonationPricesAndValues = async (
} else if (token?.cryptoCompareId) {
const priceUsd = await new CryptoComparePriceAdapter().getTokenPrice({
symbol: token.cryptoCompareId,
networkId: priceChainId!,
networkId: priceChainId,
});
donation.priceUsd = toFixNumber(priceUsd, 4);
donation.valueUsd = toFixNumber(donation.amount * priceUsd, 4);
} else if (token?.coingeckoId) {
const priceUsd = await new CoingeckoPriceAdapter().getTokenPrice({
symbol: token.coingeckoId,
networkId: priceChainId!,
networkId: priceChainId,
});
donation.priceUsd = toFixNumber(priceUsd, 4);
donation.valueUsd = toFixNumber(donation.amount * priceUsd, 4);
} else {
const priceUsd = await new MonoswapPriceAdapter().getTokenPrice({
symbol: currency,
networkId: priceChainId!,
networkId: priceChainId,
});

if (priceUsd) {
Expand Down
1 change: 0 additions & 1 deletion src/services/onramper/donationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
updateUserTotalDonated,
updateUserTotalReceived,
} from '../userService';
import { ChainType } from '../../types/network';

export const createFiatDonationFromOnramper = async (
fiatTransaction: OnRamperFiatTransaction,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers } from 'ethers';
import { ChainType } from '../types/network';
import { PublicKey } from '@solana/web3.js';

export const SOLANA_SYSTEM_ADDRESS = '11111111111111111111111111111111';
export const SOLANA_SYSTEM_PROGRAM = '11111111111111111111111111111111';

export const isSolanaAddress = (address: string): boolean => {
try {
Expand Down

0 comments on commit 6a89f44

Please sign in to comment.