Developers Docs
Search
⌃K

SimpleFi Subgraph Mapping Code Library

While developing subgraphs our team realized that there are a lot of common functionality that is used to manipulate entities of our common schema. While factoring out these common functions in mapping code we also found out that we can develop an API to manipulate our common schema entities so that developers don't need to worry about maintaining common entities data integrity. This gave birth to our subgraph mapping code library. This library currently has only two files -
  1. 1.
  2. 2.
    common.ts

Contants.ts

This files defines constants used to populate Enum values in schema entities.
If you add a new value to Enum in schema then you also need to add it in constants.ts so that you can use it in mapping code and avoid common errors caused by typing mistakes.

Common.ts

This file implements high level functions to manipulate common schema entities. You can find reference doc for these function at <add link here>
We strongly recommend using these functions to populate common entities to avoid data integrity errors. You will find that these functions helpful in reducing your development time and code complexity. Below is description of functions exported by this library -

getOrCreateAccount(address) ⇒ *

Fetch account entity, or create it if it doens't exist. Account can be either EOA or contract.
Kind: global function Returns: * - {Account} Account entity
Param
Type
Description
address
Address
Address of the account to load/create

getOrCreateERC20Token(event, address) ⇒ *

Fetch token entity, or create it if not existing, for ERC20 token. Token name, symbol and decimals are fetched by contract calls.
Kind: global function Returns: * - {Token} Token entity
Param
Type
Description
event
ethereum.Event
Event with block info for this token
address
Address
Address of the ERC20 token

getOrCreateMarket(event, address, protocolName, protocolType, inputTokens, outputToken, rewardTokens) ⇒ *

Fetch market entity, or create it if it doesn't exist.
Kind: global function Returns: * - {Market} Market entity
Param
Type
Description
event
ethereum.Event
Event contains block info
address
Address
Address of the market
protocolName
string
Name of the protocol based on ProtocolName enum
protocolType
string
Type of the protocol based on ProtocolType enum
inputTokens
Array.<Token>
List of tokens that can be deposited in this market as investment
outputToken
Token
Token that is minted by the market to track the position of a user in the market (e.g. an LP token)
rewardTokens
Array.<Token>
List of reward tokens given out by protocol as incentives

updateMarket(event, market, inputTokenBalances, outputTokenTotalSupply) ⇒ *

Update market with new input token balances and new supply of output token. Before updating market create market snapshot and store it.
Kind: global function Returns: * - {MarketSnapshot} Market snapshot entity
Param
Type
Description
event
ethereum.Event
Event which triggered the change
market
Market
Market to be updated
inputTokenBalances
Balances of the input tokens that can be redeemed by sending the outputTokenTotalSupply back to the market.
outputTokenTotalSupply
BigInt
Total supply of output token

investInMarket(event, account, market, outputTokenAmount, inputTokenAmounts, rewardTokenAmounts, outputTokenBalance, inputTokenBalances, rewardTokenBalances, transferredFrom) ⇒ *

Store transaction and update user's position when user has invested in market (or received market output token). Before transaction is stored and position updated, snapshots of market and position are created for historical tracking. If new balance of user's market output tokens is 0, position is closed.
Kind: global function Returns: * - {Position} User's updated position in the market
Param
Type
Description
event
ethereum.Event
Event emitted after user's investment
account
Account
Investor's account
market
Market
Market in which user invested
outputTokenAmount
BigInt
Amount of output token minted for user
inputTokenAmounts
Amounts of input tokens that are deposited by user in this transaction
rewardTokenAmounts
Amounts of reward tokens that are claimed by user in this transaction
outputTokenBalance
BigInt
User's latest balance of the market's output token
inputTokenBalances
Balances of the input tokens that can be redeemed by sending the outputTokenBalance back to the market
rewardTokenBalances
Amounts of market's reward tokens claimable by user (not counting already claimed tokens)
transferredFrom
string | null
Null if investment was made by user; or address of sender in case when market ouput tokens were transferred to user

redeemFromMarket(event, account, market, outputTokenAmount, inputTokenAmounts, rewardTokenAmounts, outputTokenBalance, inputTokenBalances, rewardTokenBalances, transferredTo) ⇒ *

Store transaction and update user's position when user has withdrawn tokens from market (or sent out market output token). Before transaction is stored and position updated, snapshots of market and position are created for historical tracking. If new balance of user's market output tokens is 0, position is closed.
Kind: global function Returns: * - {Position} User's updated position in the market
Param
Type
Description
event
ethereum.Event
Event emitted after user's withdrawal
account
Account
Investor's account
market
Market
Market from which user withdrew
outputTokenAmount
BigInt
Amount of output token transferred by user back to market or burned by market from user's balance
inputTokenAmounts
Amounts of input tokens that are received by user in this transaction
rewardTokenAmounts
Amounts of reward tokens that are claimed by user in this transaction
outputTokenBalance
BigInt
User's latest balance of the market's output token
inputTokenBalances
Balances of the input tokens that can be redeemed by sending the outputTokenBalance back to the market
rewardTokenBalances
Amounts of market's reward tokens claimable by user (not counting already claimed tokens)
transferredTo
string | null
Null if withdrawal was made by user; or address of receiver in case when user sent out marker output tokens

borrowFromMarket(event, account, market, outputTokenAmount, inputTokenAmounts, rewardTokenAmounts, outputTokenBalance, inputTokenBalances, rewardTokenBalances) ⇒ *

Store transaction and update user's position when user has borrowed from market. Before transaction is stored and position updated, snapshots of market and position are created for historical tracking.
Kind: global function Returns: * - {Position} User's updated position in the market
Param
Type
Description
event
ethereum.Event
Event emitted after user's borrowing
account
Account
Investor's account
market
Market
Market from which user borrowed
outputTokenAmount
BigInt
Change in user's output token balance as part of this transaction
inputTokenAmounts
Amounts of input tokens borrowed by user in this transaction
rewardTokenAmounts
Amounts of reward tokens that are claimed by user in this transaction
outputTokenBalance
BigInt
Latest user's balance of the market's output token
inputTokenBalances
Balances of the input tokens that can be redeemed by sending the outputTokenBalance back to the market
rewardTokenBalances
Amounts of market's reward tokens claimable by user (not counting already claimed tokens)

repayToMarket(event, account, market, outputTokenAmount, inputTokenAmounts, rewardTokenAmounts, outputTokenBalance, inputTokenBalances, rewardTokenBalances) ⇒ *

Store transaction and update user's position when user has repayed debt to market. Before transaction is stored and position updated, snapshots of market and position are created for historical tracking.
Kind: global function Returns: * - {Position} User's updated position in the market
Param
Type
Description
event
ethereum.Event
Event emitted after user's repayment
account
Account
Investor's account
market
Market
Market to which user repayed
outputTokenAmount
BigInt
Change in user's output token balance as part of this transaction
inputTokenAmounts
Amounts of input tokens repayed by user in this transaction
rewardTokenAmounts
Amounts of reward tokens that are claimed by user in this transaction
outputTokenBalance
BigInt
Latest user's balance of the market's output token
inputTokenBalances
Balances of the input tokens that can be redeemed by sending the outputTokenBalance back to the market
rewardTokenBalances
Amounts of market's reward tokens claimable by user (not counting already claimed tokens)