Skip to content

Commit

Permalink
Use the graph to get vault and token addresses (#2)
Browse files Browse the repository at this point in the history
* use the graph for vault addresses

* implement subgraph for getting vault addresses

* rename addresses.js -> networks.js

* use the graph to get token pair address

* remove pair retreived from ABI call in favor of vault.pair.id

* remove block-height parameter from subgraph query to avoid failing aurora test

* remove block-height parameter from query request
  • Loading branch information
mrod502 authored Jul 1, 2022
1 parent cc0791b commit a283ea6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
6 changes: 0 additions & 6 deletions projects/rift-finance/addresses.js

This file was deleted.

116 changes: 55 additions & 61 deletions projects/rift-finance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const sdk = require("@defillama/sdk");
const { getBlock } = require("../helper/getBlock");
const { unwrapUniswapLPs } = require("../helper/unwrapLPs");
const { getChainTransform } = require("../helper/portedTokens");
const { request, gql } = require("graphql-request");

const ADDRESSES = require("./addresses");
const NETWORKS = require("./networks");

const riftVaultAbi = require("./abis/riftVaultAbi");
const masterChefAbi = require("./abis/masterChefAbi");
Expand All @@ -13,107 +14,93 @@ function getAbi(obj, fnName) {
return obj.find((x) => x.name === fnName);
}

function addressToId(address) {
return address.toLowerCase();
}

async function getChainBalances(timestamp, chainBlocks, chain) {
const block = await getBlock(timestamp, chain, chainBlocks);
const balances = {};
const transform = await getChainTransform(chain);

const { core, startBlock } = ADDRESSES[chain];

// Get all registered vaults by searching events on core address.
const vaultRegisteredEvents = (
await sdk.api.util.getLogs({
chain,
keys: [],
toBlock: block,
fromBlock: startBlock,
target: core,
topic: "VaultRegistered(address,address)",
})
).output;

const vaults = vaultRegisteredEvents.map((log) => `0x${log.topics[1].substring(26)}`);

for (const vault of vaults) {
// Get addresses.
const token0 = (
await sdk.api.abi.call({
abi: getAbi(riftVaultAbi, "token0"),
chain,
target: vault,
block,
})
).output;

const token1 = (
await sdk.api.abi.call({
chain,
block,
target: vault,
abi: getAbi(riftVaultAbi, "token1"),
})
).output;

const pair = (
await sdk.api.abi.call({
chain,
block,
target: vault,
abi: getAbi(riftVaultAbi, "pair"),
})
).output;

const { coreAddress, graphUrl } = NETWORKS[chain];

const query = gql`
query get_vaults($coreAddr: String) {
core(id: $coreAddr) {
vaults {
id
type
token0 {
id
}
token1 {
id
}
pair {
id
}
}
}
}
`;

const queryResult = await request(graphUrl, query, {
coreAddr: addressToId(coreAddress),
});

for (const vault of queryResult.core.vaults) {
// Get balances.
const token0Bal = (
await sdk.api.abi.call({
chain,
block,
target: token0,
target: vault.token0.id,
abi: "erc20:balanceOf",
params: [vault],
params: [vault.id],
})
).output;

const token1Bal = (
await sdk.api.abi.call({
chain,
block,
target: token1,
target: vault.token1.id,
abi: "erc20:balanceOf",
params: [vault],
params: [vault.id],
})
).output;

const pairBal = (
await sdk.api.abi.call({
chain,
block,
target: pair,
target: vault.pair.id,
abi: "erc20:balanceOf",
params: [vault],
params: [vault.id],
})
).output;

// Add token balances.
sdk.util.sumSingleBalance(balances, transform(token0), token0Bal);
sdk.util.sumSingleBalance(balances, transform(token1), token1Bal);
sdk.util.sumSingleBalance(balances, transform(vault.token0.id), token0Bal);
sdk.util.sumSingleBalance(balances, transform(vault.token1.id), token1Bal);

// Unwrap and add pair balances.
await unwrapUniswapLPs(
balances,
[{ balance: pairBal, token: pair }],
[{ balance: pairBal, token: vault.pair.id }],
block,
chain,
transform
);

// Look for MasterChef balance.
try {
if (vault.type === "SUSHI_SWAP") {
const rewarder = (
await sdk.api.abi.call({
chain,
block,
target: vault,
target: vault.id,
abi: getAbi(riftVaultAbi, "rewarder"),
params: [],
})
Expand All @@ -123,7 +110,7 @@ async function getChainBalances(timestamp, chainBlocks, chain) {
await sdk.api.abi.call({
chain,
block,
target: vault,
target: vault.id,
abi: getAbi(riftVaultAbi, "pid"),
params: [],
})
Expand All @@ -135,19 +122,19 @@ async function getChainBalances(timestamp, chainBlocks, chain) {
block,
target: rewarder,
abi: getAbi(masterChefAbi, "userInfo"),
params: [pid, vault],
params: [pid, vault.id],
})
).output;

// Unwrap and add MasterChef balances.
await unwrapUniswapLPs(
balances,
[{ balance: masterChefBal, token: pair }],
[{ balance: masterChefBal, token: vault.pair.id }],
block,
chain,
transform
);
} catch (e) {}
}
}

return balances;
Expand All @@ -157,10 +144,17 @@ async function aurora(timestamp, block, chainBlocks) {
return getChainBalances(timestamp, chainBlocks, "aurora");
}

async function ethereum(timestamp, block, chainBlocks) {
return getChainBalances(timestamp, chainBlocks, "ethereum");
}

module.exports = {
timetravel: true,
misrepresentedTokens: false,
aurora: {
tvl: aurora,
},
ethereum: {
tvl: ethereum,
},
};
14 changes: 14 additions & 0 deletions projects/rift-finance/networks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
aurora: {
coreAddress: "0x40A01A4064b690cA33FA52d315ec02015eF5287E",
startBlock: 58983267,
graphUrl:
"https://api.thegraph.com/subgraphs/name/recursive-research/rift-subgraph-aurora",
},
ethereum: {
coreAddress: "0x5D7e616B2c0bf268494A482e315a60814F97dBC8",
startBlock: 14845882,
graphUrl:
"https://api.thegraph.com/subgraphs/name/recursive-research/rift-subgraph",
},
};

0 comments on commit a283ea6

Please sign in to comment.