GithubHelp home page GithubHelp logo

protofire / subgraph-devkit Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 171 KB

Happiness kit for subgraph developers

Home Page: https://protofire.github.io/subgraph-devkit/

TypeScript 100.00%
subgraph thegraph thegraphprotocol tooling toolkit typescript

subgraph-devkit's Introduction

ci

Subgraph Devkit

Happiness kit for subgraph developers

Features:

Tech Stack:

  • AssemblyScript
  • Matchstick

Installation:

yarn add @protofire/subgraph-devkit

Documentation

Oracles

import { Address } from "@graphprotocol/graph-ts";
import { oracles } from "@protofire/subgraph-devkit";

const tokenAddress = Address.fromString(
  "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"
);

// get token price in usd from ChainLink oracle
oracles.chainlink.fetchPriceUSD(tokenAddress);

// get token price in usd from YearnLens oracle
oracles.yearnLens.fetchPriceUSD(tokenAddress);

// get token price in usd from Uniswap oracle
oracles.uniswap.fetchPriceUSD(tokenAddress);

Numbers

import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { decimals, integers } from "@protofire/subgraph-devkit";

// number 0 in BigDecimal
decimals.ZERO;

// number 1 in BigDecimal
decimals.ONE;

// number 0 in BigInt
integers.ZERO;

// number 1 in BigInt
integers.ONE;

// number 10 in BigInt
integers.TEN;

// Get min/max value
const a = BigInt.fromI32(100);
const b = BigInt.fromI32(101);

integers.min(a, b); // => 100
integers.max(a, b); // => 101

// Convert BigInt into Bytes
const n = BigInt.fromI32(100);
integers.toBytes(n);

// Create BigDecimal from BigInt
const n = BigInt.fromI32(1000000);
decimals.fromBigInt(n, 6); // => 1

// Create BigDecimal from Float
decimals.fromFloat(1000000.123);

// Create BigDecimal from Integer
decimals.fromInt(9223372036854775807);

// Convert BigDecimal to BigInt
const n = BigDecimal.fromString("1234.56");
decimals.toBigInt(n); // => 123456

// Get min/max value
const a = BigDecimal.fromString("100.525678");
const b = BigDecimal.fromString("100.525679");

decimals.min(a, b); // => 100.525678
decimals.max(a, b); // => 100.525679

// MakerDAO

// Create BigDecimal from RAD BigInt
const rad = BigInt.fromString(
  "12500000000000000000000000000000000000000000000"
);
decimals.fromRad(rad); // => 12.5;

// Create BigDecimal from WAD BigInt
const wad = BigInt.fromString("12500000000000000000");
decimals.fromWad(wad); // => 12.5

// Create BigDecimal from RAY BigInt
const ray = BigInt.fromString("12500000000000000000000000000");
decimals.fromRay(ray); // => 12.5

const value = BigDecimal.fromString("12.5");

// Convert BigDecimal into RAD BigInt
decimals.toRad(value); // => 12500000000000000000000000000000000000000000000

// Convert BigDecimal into RAY BigInt
decimals.toRay(value); // => 12500000000000000000000000000

// Convert BigDecimal into WAD BigInt
decimals.toWad(value); // => 12500000000000000000

Bytes

import { bytes } from "@protofire/subgraph-devkit";

// Convert Bytes into Address
const stringAddress = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984";
const bytesAddress = Bytes.fromHexString(stringAddress);
const address = bytes.toAddress(bytesAddress);

// Convert unsigned number in Bytes into BigInt
const bytesData = Bytes.fromHexString("0x28fc");
bytes.toUnsignedBigInt(bytesData); // => 10492

// Convert signed number in Bytes into BigInt
const bytesData = Bytes.fromHexString("0xd704");
bytes.toSignedBigInt(bytesData); // => -10492

Testing

import { BigDecimal } from "@graphprotocol/graph-ts";
import { describe, test } from "matchstick-as/assembly/index";
import { tests } from "@protofire/subgraph-devkit";

describe("decimalEquals", () => {
  test("decimal equals to decimal", () => {
    const a = BigDeciaml.fromString("100");
    const b = BigDeciaml.fromString("100");

    tests.asserts.decimalEquals(a, b);
  });
});

Constants

import { constants } from "@protofire/subgraph-devkit";

// USDC contract address
constants.addresses.USDC;

// WETH contract address
constants.addresses.WETH;

// UNISWAP_V2_ROUTER_02 contract address
constants.addresses.UNISWAP_V2_ROUTER_02;

// CHAINLINK_FEED_REGISTRY contract address
constants.addresses.CHAINLINK_FEED_REGISTRY;

// YEARN_LENS_ORACLE contract address
constants.addresses.YEARN_LENS_ORACLE;

// USDC unit (BigInt): 1000000
constants.units.USDC;

// MakerDAO

// WAD unit (BigInt): 1000000000000000000
constants.units.WAD;

// RAY unit (BigInt): 1000000000000000000000000000
constants.units.RAY;

// RAD unit (BigInt): 1000000000000000000000000000000000000000000000
constants.units.RAD;

Contributing:

  • Fork the repo on GitHub
  • Clone the project to your own machine
  • Commit changes to your own branch
  • Push your work back up to your fork
  • Submit a Pull request so that we can review your changes

Running Tests

In order to be able to take advantage of matchstick testing library, we have added a dummy subgraph inside tests folder. Tests will live inside that dummy subgraph and you can run them as follows:

yarn test

subgraph-devkit's People

Contributors

llopez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

subgraph-devkit's Issues

Add network support for Oracles

Nowadays the library supports Oracles only on ethereum network. We need to scope oracles to networks. Use the right addresses and ABIs depending the chosen network

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.