Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed May 2, 2024
1 parent 9d8a918 commit 1169952
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"buildonly": "graph build",
"deploy:alchemy": "graph deploy --node https://subgraphs.alchemy.com/api/subgraphs/deploy --ipfs https://ipfs.satsuma.xyz",
"codegen": "graph codegen --output-dir src/types/",
"test": "graph test",
"create-local": "graph create davekaj/uniswap --node http://127.0.0.1:8020",
"deploy-local": "graph deploy davekaj/uniswap --debug --ipfs http://localhost:5001 --node http://127.0.0.1:8020",
"deploy": "graph deploy ianlapham/uniswap-v2-dev --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/ --debug",
Expand All @@ -23,6 +24,7 @@
"@uniswap/eslint-config": "^1.2.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^6.1.0",
"matchstick-as": "^0.6.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"typescript": "^3.5.2"
Expand Down
10 changes: 7 additions & 3 deletions src/mappings/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Address, BigDecimal, BigInt } from '@graphprotocol/graph-ts/index'
import { Bundle, Pair, Token } from '../types/schema'
import { ADDRESS_ZERO, factoryContract, ONE_BD, UNTRACKED_PAIRS, ZERO_BD } from './helpers'

const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const USDC_WETH_PAIR = '0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc' // created 10008355
export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
export const USDC_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
export const USDC_WETH_PAIR = '0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc' // created 10008355
const DAI_WETH_PAIR = '0xa478c2975ab1ea89e8196811f51a7b7ade33eb11' // created block 10042267
const USDT_WETH_PAIR = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852' // created block 10093341

Expand Down Expand Up @@ -150,7 +151,10 @@ export function getTrackedVolumeUSD(

// both are whitelist tokens, take average of both amounts
if (WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
return tokenAmount0.times(price0).plus(tokenAmount1.times(price1)).div(BigDecimal.fromString('2'))
return tokenAmount0
.times(price0)
.plus(tokenAmount1.times(price1))
.div(BigDecimal.fromString('2'))
}

// take full value of the whitelisted token amount
Expand Down
Binary file added tests/.bin/handlenewpair.wasm
Binary file not shown.
Binary file added tests/.bin/mint.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/.latest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "0.6.0",
"timestamp": 1714590102821
}
37 changes: 37 additions & 0 deletions tests/handleNewPair.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { assert, test, newMockEvent, dataSourceMock } from 'matchstick-as/assembly/index'
import { describe, test } from 'matchstick-as/assembly/index'
import { log } from 'matchstick-as/assembly/log'
import { PairCreated } from '../src/types/Factory/Factory'
import { Address, ethereum } from '@graphprotocol/graph-ts'
import { USDC_ADDRESS, USDC_WETH_PAIR, WETH_ADDRESS } from '../src/mappings/pricing'
import { handleNewPair } from '../src/mappings/factory'
import { FACTORY_ADDRESS } from '../src/mappings/helpers'

describe('handleNewPair', () => {
test('success - what', () => {
log.success('HERE', [])
const mockEvent = newMockEvent()
const token0Address = Address.fromString(USDC_ADDRESS)
const token1Address = Address.fromString(WETH_ADDRESS)
const pairAddress = Address.fromString(USDC_WETH_PAIR)
const parameters = [
new ethereum.EventParam('token0', ethereum.Value.fromAddress(token0Address)),
new ethereum.EventParam('token1', ethereum.Value.fromAddress(token1Address)),
new ethereum.EventParam('pair', ethereum.Value.fromAddress(pairAddress)),
]
const pairCreatedEvent = new PairCreated(
mockEvent.address,
mockEvent.logIndex,
mockEvent.transactionLogIndex,
mockEvent.logType,
mockEvent.block,
mockEvent.transaction,
parameters,
mockEvent.receipt,
)

handleNewPair(pairCreatedEvent)

// assert.fieldEquals('UniswapFactory', FACTORY_ADDRESS, 'pairCount', '3', 'Fail')
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3721,6 +3721,13 @@ make-error@^1.1.1:
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

matchstick-as@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/matchstick-as/-/matchstick-as-0.6.0.tgz#c65296b1f51b1014d605c52067d9b5321ea630e8"
integrity sha512-E36fWsC1AbCkBFt05VsDDRoFvGSdcZg6oZJrtIe/YDBbuFh8SKbR5FcoqDhNWqSN+F7bN/iS2u8Md0SM+4pUpw==
dependencies:
wabt "1.0.24"

md5.js@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
Expand Down Expand Up @@ -5349,6 +5356,11 @@ [email protected]:
core-util-is "1.0.2"
extsprintf "^1.2.0"

[email protected]:
version "1.0.24"
resolved "https://registry.yarnpkg.com/wabt/-/wabt-1.0.24.tgz#c02e0b5b4503b94feaf4a30a426ef01c1bea7c6c"
integrity sha512-8l7sIOd3i5GWfTWciPL0+ff/FK/deVK2Q6FN+MPz4vfUcD78i2M/49XJTwF6aml91uIiuXJEsLKWMB2cw/mtKg==

wcwidth@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"
Expand Down

0 comments on commit 1169952

Please sign in to comment.