-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add mpeth prices fetch and token * add env var to tests * add tests for fetching price method (check status)
- Loading branch information
Showing
11 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { NETWORK_IDS } from '../src/provider'; | ||
import { ChainType } from '../src/types/network'; | ||
import { Token } from '../src/entities/token'; | ||
|
||
const mpEthTokens = [ | ||
{ | ||
name: 'mpETH', | ||
symbol: 'mpETH', | ||
address: '0x48afbbd342f64ef8a9ab1c143719b63c2ad81710', | ||
decimals: 18, | ||
isGivbackEligible: true, | ||
networkId: NETWORK_IDS.MAIN_NET, | ||
chainType: ChainType.EVM, | ||
}, | ||
{ | ||
name: 'mpETH', | ||
symbol: 'mpETH', | ||
address: '0x819845b60a192167ed1139040b4f8eca31834f27', | ||
mainnetAddress: '0x48afbbd342f64ef8a9ab1c143719b63c2ad81710', | ||
decimals: 18, | ||
isGivbackEligible: true, | ||
networkId: NETWORK_IDS.OPTIMISTIC, | ||
chainType: ChainType.EVM, | ||
}, | ||
]; | ||
|
||
export class addmpEthToDatabaseTokens1706820821887 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.manager.save(Token, mpEthTokens); | ||
|
||
const tokens = await queryRunner.query(` | ||
SELECT * FROM token | ||
WHERE | ||
("address" = '0x48afbbd342f64ef8a9ab1c143719b63c2ad81710' AND "networkId" = ${NETWORK_IDS.MAIN_NET}) OR | ||
("address" = '0x819845b60a192167ed1139040b4f8eca31834f27' AND "networkId" = ${NETWORK_IDS.OPTIMISTIC}) | ||
`); | ||
const givethOrganization = ( | ||
await queryRunner.query(`SELECT * FROM organization | ||
WHERE label='giveth'`) | ||
)[0]; | ||
|
||
const traceOrganization = ( | ||
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}) | ||
;`); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
const tokens = await queryRunner.query(` | ||
SELECT * FROM token | ||
WHERE | ||
("address" = '0x48afbbd342f64ef8a9ab1c143719b63c2ad81710' AND "networkId" = ${NETWORK_IDS.MAIN_NET}) OR | ||
("address" = '0x819845b60a192167ed1139040b4f8eca31834f27' AND "networkId" = ${NETWORK_IDS.OPTIMISTIC}) | ||
`); | ||
await queryRunner.query( | ||
`DELETE FROM organization_tokens_token WHERE "tokenId" IN (${tokens | ||
.map(token => token.id) | ||
.join(',')})`, | ||
); | ||
|
||
await queryRunner.query( | ||
`DELETE FROM token WHERE "id" IN (${tokens | ||
.map(token => token.id) | ||
.join(',')})`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { assert, expect } from 'chai'; | ||
import { fetchMpEthPrice } from './mpEthPriceService'; | ||
|
||
describe('fetchMpEthPrice test cases', fetchMpEthPriceTestCases); | ||
|
||
function fetchMpEthPriceTestCases() { | ||
it('should fetch the price from velodrome subgraph for mpeth', async () => { | ||
const mpEthPrice = await fetchMpEthPrice(); | ||
assert.isOk(mpEthPrice); | ||
expect(mpEthPrice).to.gt(0); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { logger } from '../utils/logger'; | ||
import Axios, { AxiosResponse } from 'axios'; | ||
import axiosRetry from 'axios-retry'; | ||
|
||
const mpEthSubgraphUrl = process.env.MPETH_GRAPHQL_PRICES_URL as string; | ||
|
||
// Maximum timeout of axios. | ||
const axiosTimeout = 20000; | ||
|
||
const query = { | ||
query: ` | ||
{ | ||
tokens(where:{id:"0x819845b60a192167ed1139040b4f8eca31834f27"}) { | ||
id | ||
name | ||
symbol | ||
decimals | ||
lastPriceUSD | ||
} | ||
} | ||
`, | ||
}; | ||
|
||
export const fetchMpEthPrice = async () => { | ||
try { | ||
const result = await Axios.post(mpEthSubgraphUrl, query, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
timeout: axiosTimeout, | ||
}); | ||
return Number(result.data.data.tokens[0].lastPriceUSD); | ||
} catch (e) { | ||
logger.error('fetching Giv Price fetchGivPrice() err', e); | ||
throw e; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters