Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End to end test #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- subgraph-additions
pull_request:

jobs:
Expand Down Expand Up @@ -31,4 +32,5 @@ jobs:

- run: yarn
- run: yarn codegen
- run: yarn build
- run: yarn build
- run: yarn test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ src/types/
.DS_STORE
.idea
yarn-error.log
test/data
todo
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ codingnirvana/uniswap-v3-subgraph",
"create-local": "graph create --node http://localhost:8020/ codingnirvana/uniswap-v3-subgraph",
"remove-local": "graph remove --node http://localhost:8020/ codingnirvana/uniswap-v3-subgraph",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 codingnirvana/uniswap-v3-subgraph"
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 codingnirvana/uniswap-v3-subgraph",
"test": "./test/runner/test.sh"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.20.1",
"@graphprotocol/graph-ts": "git://github.com/graphprotocol/graph-ts#2f628dfbce14adc40bab35d66604c4c7a2e6b46c",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"apollo-fetch": "^0.7.0",
"eslint": "^6.2.2",
"eslint-config-prettier": "^6.1.0",
"prettier": "^1.18.2",
"typescript": "^3.5.2"
"typescript": "^3.5.2"
},
"devDependencies": {
"@types/chai": "^4.2.19",
"@types/mocha": "^8.2.2",
"chai": "^4.3.4",
"mocha": "^9.0.1",
"ts-mocha": "^8.0.0"
}
}
2 changes: 1 addition & 1 deletion subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dataSources:
name: UniswapV3Staker
network: mainnet
source:
address: "0x1F98431c8aD98523631AE4a59f267346ea31F984"
address: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"
abi: UniswapV3Staker
mapping:
kind: ethereum/events
Expand Down
90 changes: 90 additions & 0 deletions test/IncentiveEvent.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { createApolloFetch } from "apollo-fetch";
import { expect } from "chai";

let graphNodeIP = "127.0.0.1";
const fetchSubgraph = createApolloFetch({
uri: `http://${graphNodeIP}:8000/subgraphs/name/codingnirvana/uniswap-v3-subgraph`,
});

const poolAddr = "0x1fa8dda81477a5b6fa1b2e149e93ed9c7928992f"
const refundeeAddr = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"
const rewardTokenAddr = "0x610178da211fef7d417bc0e6fed39f05609ad788"
const reward = 1000;

describe("Check records", function () {
it("should equal number of events fired", async function () {
let subgraphData = await fetchSubgraph({
query: `{
incentives {
id
}
}`,
});

expect(subgraphData.data.incentives.length).to.eq(1);
});

it("should have matching reward token address used while creation", async function () {
let subgraphData = await fetchSubgraph({
query: `{
incentives {
id
rewardToken
}
}`,
});


for (let idx = 0; idx < subgraphData.data.incentives.length; idx++) {
expect(subgraphData.data.incentives[idx].rewardToken).to.eq(rewardTokenAddr);
}
});

it("should have matching pool address used while creation", async function () {
let subgraphData = await fetchSubgraph({
query: `{
incentives {
id
pool
}
}`,
});


for (let idx = 0; idx < subgraphData.data.incentives.length; idx++) {
expect(subgraphData.data.incentives[idx].pool).to.eq(poolAddr);
}
});

it("should have matching refundee address used while creation", async function () {
let subgraphData = await fetchSubgraph({
query: `{
incentives {
id
refundee
}
}`,
});


for (let idx = 0; idx < subgraphData.data.incentives.length; idx++) {
expect(subgraphData.data.incentives[idx].refundee).to.eq(refundeeAddr);
}
});

it("should have matching reward used while creation", async function () {
let subgraphData = await fetchSubgraph({
query: `{
incentives {
id
reward
}
}`,
});


for (let idx = 0; idx < subgraphData.data.incentives.length; idx++) {
expect(subgraphData.data.incentives[idx].reward).equal(reward.toString());
}
});
});
14 changes: 14 additions & 0 deletions test/runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:14

COPY ./scripts /app/scripts
COPY ./setup-contracts /app/setup-contracts
COPY ./setup-env.sh /app/

WORKDIR /app/
RUN git clone "https://github.com/Uniswap/uniswap-v3-staker.git"

WORKDIR uniswap-v3-staker
RUN yarn install
RUN yarn add uniswap-v3-deploy-plugin

CMD /app/setup-env.sh
45 changes: 45 additions & 0 deletions test/runner/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3'
services:
subgraphtest:
container_name: uniswap-staker-deployment
build:
context: .
dockerfile: Dockerfile
ports:
- "8545:8545"
graph-node:
image: graphprotocol/graph-node
ports:
- '8000:8000'
- '8001:8001'
- '8020:8020'
- '8030:8030'
- '8040:8040'
depends_on:
- ipfs
- postgres
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'mainnet:http://host.docker.internal:8545'
GRAPH_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.4.23
ports:
- '5001:5001'
volumes:
- ./data/ipfs:/data/ipfs
postgres:
image: postgres
ports:
- '5432:5432'
command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
environment:
POSTGRES_USER: graph-node
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
volumes:
- ./data/postgres:/var/lib/postgresql/data
23 changes: 23 additions & 0 deletions test/runner/scripts/deploy/staker/staker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
async function main() {
const [deployer] = await ethers.getSigners();
console.log(`Deploying contracts with account address: ${deployer.address}`);

const factoryContractAddress = process.env.FACTORY_CONTRACT_ADDR
const positionManagerContractAddress = process.env.POSITION_MANAGER_CONTRACT_ADDR

const UniswapV3Staker = await ethers.getContractFactory("UniswapV3Staker");
const uniswapV3Staker = await UniswapV3Staker.deploy(
factoryContractAddress,
positionManagerContractAddress,
1000,
110000
);

console.log(`UniswapV3Staker Address: ${uniswapV3Staker.address}`);
}

main()
.then(() => console.log("Exited Properly"))
.catch(error => {
console.log(error);
});
32 changes: 32 additions & 0 deletions test/runner/scripts/deploy/v3core/pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
async function main() {
const [deployer] = await ethers.getSigners();
const factoryContractAddress = process.env.FACTORY_CONTRACT_ADDR
console.log(`Factory Contract Address - ${factoryContractAddress}`)

const uniswapV3FactoryContractInstance = await ethers.getContractAt("IUniswapV3Factory", factoryContractAddress);
console.log(`Contract Address: ${factoryContractAddress}`);

const Token = await ethers.getContractFactory("Token");
const token1 = await Token.deploy("Token1", "TK1");
const token2 = await Token.deploy("Token2", "TK2");

console.log(`Token1 Contract Address: ${token1.address}`);
console.log(`Token2 Contract Address: ${token2.address}`);

const fees = 500;

const tx = await uniswapV3FactoryContractInstance.createPool(
token1.address,
token2.address,
fees
);

const receipt = await tx.wait();
const event = receipt.events?.filter((x) => {return x.event == "PoolCreated"})[0];

console.log(`Pool address: ${event.args.pool}`);
}

main()
.then(() => console.log("Successfull"))
.catch(error => console.log(error));
65 changes: 65 additions & 0 deletions test/runner/scripts/events/incentive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
async function fireIncentiveCreated(uniswapContract, incentiveKey) {
const txn = await uniswapContract.createIncentive(
incentiveKey,
1000
);
const receipt = await txn.wait();
const event = receipt.events?.filter((x) => { return x.event == "IncentiveCreated" })[0];

return event;
}

async function fireIncentiveEnded(uniswapContract, incentiveKey) {
const txn = await uniswapContract.endIncentive(
incentiveKey
);
const receipt = await txn.wait();
const event = receipt.events?.filter((x) => { return x.event == "IncentiveEnded" })[0];

return event;
}

async function main() {
const delay = ms => new Promise(res => setTimeout(res, ms));

const [deployer] = await ethers.getSigners();

const uniswapStakerAddress = process.env.STAKER_CONTRACT_ADDR;
const uniswapContract = await ethers.getContractAt("IUniswapV3Staker", uniswapStakerAddress);

const Token = await ethers.getContractFactory("Token");
const rewardToken = await Token.deploy("Token3", "RT");
console.log(`Reward Token Address: ${rewardToken.address}`);

const poolAddress = process.env.POOL_CONTRACT_ADDR;

const block = await ethers.provider.getBlock('latest');

const startTime = block.timestamp + 1;
const endTime = startTime + 2;

const incentiveKey = [
rewardToken.address,
poolAddress,
startTime,
endTime,
deployer.address
];

const txn = await uniswapContract.createIncentive(
incentiveKey,
1000
);
const receipt = await txn.wait();
const event = receipt.events?.filter((x) => { return x.event == "IncentiveCreated" })[0];
console.log("IncentiveCreated Event Fired");

await delay(5000);

const incentiveEndedEvent = await fireIncentiveEnded(uniswapContract, incentiveKey);
console.log("IncentiveEnded Event Fired");
}

main()
.then(() => process.exit(0))
.catch(error => console.log(error));
22 changes: 22 additions & 0 deletions test/runner/setup-contracts/Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.7.6;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
constructor (string memory name, string memory symbol) ERC20(name, symbol) {
_mint(msg.sender, 100 * 10 ** uint(decimals()));
}

function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
return true;
}

function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
return true;
}
}
29 changes: 29 additions & 0 deletions test/runner/setup-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

cp -r ../scripts ./
cp -r ../setup-contracts ./contracts

sed -i '1s/^/import "uniswap-v3-deploy-plugin";\n/' hardhat.config.ts

npx hardhat node &
sleep 15

UNISWAP_CONTRACT_ADDRS=`npx hardhat deploy-uniswap --network localhost | grep "factory\|positionManager" | awk '{print $4}'`
export FACTORY_CONTRACT_ADDR=`echo $UNISWAP_CONTRACT_ADDRS | awk '{split($0,a," "); print a[1]}'`
export POSITION_MANAGER_CONTRACT_ADDR=`echo $UNISWAP_CONTRACT_ADDRS | awk '{split($0,a," "); print a[2]}'`

export POOL_CONTRACT_ADDR=`npx hardhat run scripts/deploy/v3core/pool.js --network localhost | grep 'Pool address' | awk '{print $3}'`
export STAKER_CONTRACT_ADDR=`npx hardhat run scripts/deploy/staker/staker.js --network localhost | grep 'UniswapV3Staker Address' | awk '{print $3}'`

echo "Uniswap Staker Address: $STAKER_CONTRACT_ADDR"
npx hardhat run scripts/events/incentive.js --network localhost

while sleep 60; do
ps aux |grep node |grep -q -v grep
PROCESS_1_STATUS=$?

if [ $PROCESS_1_STATUS -ne 0 ]; then
echo "Process has already exited."
exit 1
fi
done
Loading