Skip to content

Commit

Permalink
Add getAllALEPHxOutflows and getAllALEPHxInflows
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Jan 24, 2024
1 parent f3c3d76 commit f533a07
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
95 changes: 94 additions & 1 deletion src/accounts/superfluid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Framework, SuperToken } from "@superfluid-finance/sdk-core";
import { AvalancheAccount } from "./avalanche";
import { ChainData, ChangeRpcParam, decToHex, JsonRPCWallet, RpcId } from "./providers/JsonRPCWallet";
import { BigNumber, ethers, providers } from "ethers";
import { ALEPH_SUPERFLUID_FUJI_TESTNET, ALEPH_SUPERFLUID_MAINNET } from "../global";
import {
ALEPH_SUPERFLUID_FUJI_TESTNET,
ALEPH_SUPERFLUID_MAINNET,
SUPERFLUID_FUJI_TESTNET_SUBGRAPH_URL,
SUPERFLUID_MAINNET_SUBGRAPH_URL,
} from "../global";
import { Decimal } from "decimal.js";

/**
Expand Down Expand Up @@ -109,6 +114,94 @@ export class SuperfluidAccount extends AvalancheAccount {
return this.flowRateToAlephPerHour(flow);
}

public async getAllALEPHxOutflows(): Promise<Record<string, Decimal>> {
if (!this.wallet) throw Error("PublicKey Error: No providers are set up");
if (!this.framework || !this.alephx) throw new Error("SuperfluidAccount not initialized");
// make a graphql query to Superfluid Subgraph
const query = `
query {
accounts(where: {outflows_: {sender: ${this.address}}}) {
id
outflows(where: {currentFlowRate_not: "0"}) {
sender {
id
}
receiver {
id
}
createdAtTimestamp
currentFlowRate
}
}
}
`;
const data = await this.querySubgraph(query);
const accounts = data.data.accounts;
const outflows: Record<string, Decimal> = {};
for (const account of accounts) {
for (const outflow of account.outflows) {
const flowRate = this.flowRateToAlephPerHour(outflow.currentFlowRate);
if (outflow.sender.id === this.address) {
outflows[outflow.sender.id] = flowRate;
}
}
}
return outflows;
}

private async querySubgraph(query: string) {
const response = await fetch(await this.getSubgraphUrl(), {
method: "POST",
body: JSON.stringify({ query }),
});
return await response.json();
}

public async getAllALEPHxInflows(): Promise<Record<string, Decimal>> {
if (!this.wallet) throw Error("PublicKey Error: No providers are set up");
if (!this.framework || !this.alephx) throw new Error("SuperfluidAccount not initialized");
// make a graphql query to Superfluid Subgraph
const query = `
query {
accounts(where: {inflows_: {receiver: ${this.address}}}) {
id
inflows(where: {currentFlowRate_not: "0"}) {
sender {
id
}
receiver {
id
}
createdAtTimestamp
currentFlowRate
}
}
}
`;
const data = await this.querySubgraph(query);
const accounts = data.data.accounts;
const inflows: Record<string, Decimal> = {};
for (const account of accounts) {
for (const inflow of account.inflows) {
const flowRate = this.flowRateToAlephPerHour(inflow.currentFlowRate);
if (inflow.receiver.id === this.address) {
inflows[inflow.sender.id] = flowRate;
}
}
}
return inflows;
}

private async getSubgraphUrl() {
if (ChainData[RpcId.AVAX_TESTNET].chainId === decToHex(await this.getChainId())) {
return SUPERFLUID_FUJI_TESTNET_SUBGRAPH_URL;
} else if (ChainData[RpcId.AVAX].chainId === decToHex(await this.getChainId())) {
return SUPERFLUID_MAINNET_SUBGRAPH_URL;
} else {
throw new Error(`ChainID ${await this.getChainId()} not supported`);
}
}

/**
* Increase the ALEPHx flow rate to a given receiver. Creates a new flow if none exists.
* @param receiver The receiver address.
Expand Down
2 changes: 2 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const DEFAULT_API_V2 = "https://api2.aleph.im";
export const DEFAULT_API_WS_V2 = "ws://api2.aleph.im";
export const ALEPH_SUPERFLUID_FUJI_TESTNET = "0x1290248E01ED2F9f863A9752A8aAD396ef3a1B00";
export const ALEPH_SUPERFLUID_MAINNET = "0xc0Fbc4967259786C743361a5885ef49380473dCF";
export const SUPERFLUID_FUJI_TESTNET_SUBGRAPH_URL = "https://avalanche-fuji.subgraph.x.superfluid.dev/";
export const SUPERFLUID_MAINNET_SUBGRAPH_URL = "https://avalanche-c.subgraph.x.superfluid.dev/";

0 comments on commit f533a07

Please sign in to comment.