Skip to content

Commit

Permalink
Merge branch 'main' into add-frontends-to-subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
edmulraney committed May 4, 2021
2 parents 15bcd15 + 2951cc7 commit 619d25f
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 166 deletions.
1 change: 1 addition & 0 deletions docs/sdk/lib-ethers.ethersliquityconnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Exposed through [ReadableEthersLiquity.connection](./lib-ethers.readableethersli
| [liquidityMiningLQTYRewardRate](./lib-ethers.ethersliquityconnection.liquiditymininglqtyrewardrate.md) | [Decimal](./lib-base.decimal.md) | Amount of LQTY collectively rewarded to stakers of the liquidity mining pool per second. |
| [provider](./lib-ethers.ethersliquityconnection.provider.md) | [EthersProvider](./lib-ethers.ethersprovider.md) | Ethers <code>Provider</code> used for connecting to the network. |
| [signer?](./lib-ethers.ethersliquityconnection.signer.md) | [EthersSigner](./lib-ethers.etherssigner.md) | <i>(Optional)</i> Ethers <code>Signer</code> used for sending transactions. |
| [startBlock](./lib-ethers.ethersliquityconnection.startblock.md) | number | Number of block in which the first Liquity contract was deployed. |
| [totalStabilityPoolLQTYReward](./lib-ethers.ethersliquityconnection.totalstabilitypoollqtyreward.md) | [Decimal](./lib-base.decimal.md) | Total amount of LQTY allocated for rewarding stability depositors. |
| [version](./lib-ethers.ethersliquityconnection.version.md) | string | Version of the Liquity contracts (Git commit hash). |
13 changes: 13 additions & 0 deletions docs/sdk/lib-ethers.ethersliquityconnection.startblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@liquity/lib-ethers](./lib-ethers.md) &gt; [EthersLiquityConnection](./lib-ethers.ethersliquityconnection.md) &gt; [startBlock](./lib-ethers.ethersliquityconnection.startblock.md)

## EthersLiquityConnection.startBlock property

Number of block in which the first Liquity contract was deployed.

<b>Signature:</b>

```typescript
readonly startBlock: number;
```
1 change: 1 addition & 0 deletions packages/lib-ethers/deployments/default/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_priceFeedIsTestnet": true,
"_uniTokenIsMock": false,
"_isDev": false,
"startBlock": 4547952,
"addresses": {
"activePool": "0x5948018DEeC14642E6127c5a3AC4081798bB73d8",
"borrowerOperations": "0xa36bA16411AF139737E8E345Cd9422a47856bECa",
Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/deployments/default/kovan.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_priceFeedIsTestnet": false,
"_uniTokenIsMock": false,
"_isDev": false,
"startBlock": 24134777,
"addresses": {
"activePool": "0x2FEB8CC8eD32117D1F39168543f29c30fdf10105",
"borrowerOperations": "0xA289111CC4b306E3F5F15c654D4c318B3dA51813",
Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/deployments/default/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_priceFeedIsTestnet": false,
"_uniTokenIsMock": false,
"_isDev": false,
"startBlock": 12178551,
"addresses": {
"activePool": "0xDf9Eb223bAFBE5c5271415C75aeCD68C21fE3D7F",
"borrowerOperations": "0x24179CD81c9e782A4096035f7eC97fB8B783e007",
Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/deployments/default/rinkeby.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_priceFeedIsTestnet": false,
"_uniTokenIsMock": false,
"_isDev": false,
"startBlock": 8341450,
"addresses": {
"activePool": "0xbA49275F8F890E7296F64b3e81F1Ada656030150",
"borrowerOperations": "0x91656701b33eca6425A239930FccAA842D0E2031",
Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/deployments/default/ropsten.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"_priceFeedIsTestnet": true,
"_uniTokenIsMock": false,
"_isDev": false,
"startBlock": 9961294,
"addresses": {
"activePool": "0x8bE79B54Bff7754B57294077c2B5017AF9f57dC2",
"borrowerOperations": "0xfe9049E677C5773dd72ac7E19c38c68aB0891744",
Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/etc/lib-ethers.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export interface EthersLiquityConnection extends EthersLiquityConnectionOptional
readonly _priceFeedIsTestnet: boolean;
readonly provider: EthersProvider;
readonly signer?: EthersSigner;
readonly startBlock: number;
readonly totalStabilityPoolLQTYReward: Decimal;
readonly version: string;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/lib-ethers/src/EthersLiquityConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export interface EthersLiquityConnection extends EthersLiquityConnectionOptional
/** Date when the Liquity contracts were deployed. */
readonly deploymentDate: Date;

/** Number of block in which the first Liquity contract was deployed. */
readonly startBlock: number;

/** Time period (in seconds) after `deploymentDate` during which redemptions are disabled. */
readonly bootstrapPeriod: number;

Expand Down
1 change: 1 addition & 0 deletions packages/lib-ethers/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export interface _LiquityDeploymentJSON {
readonly addresses: _LiquityContractAddresses;
readonly version: string;
readonly deploymentDate: number;
readonly startBlock: number;
readonly bootstrapPeriod: number;
readonly totalStabilityPoolLQTYReward: string;
readonly liquidityMiningLQTYRewardRate: string;
Expand Down
112 changes: 64 additions & 48 deletions packages/lib-ethers/utils/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export const setSilent = (s: boolean): void => {
silent = s;
};

const deployContract = async (
const deployContractAndGetBlockNumber = async (
deployer: Signer,
getContractFactory: (name: string, signer: Signer) => Promise<ContractFactory>,
contractName: string,
...args: unknown[]
) => {
): Promise<[address: string, blockNumber: number]> => {
log(`Deploying ${contractName} ...`);
const contract = await (await getContractFactory(contractName, deployer)).deploy(...args);

Expand All @@ -45,17 +45,28 @@ const deployContract = async (

log();

return contract.address;
return [contract.address, receipt.blockNumber];
};

const deployContract: (
...p: Parameters<typeof deployContractAndGetBlockNumber>
) => Promise<string> = (...p) => deployContractAndGetBlockNumber(...p).then(([a]) => a);

const deployContracts = async (
deployer: Signer,
getContractFactory: (name: string, signer: Signer) => Promise<ContractFactory>,
priceFeedIsTestnet = true,
overrides?: Overrides
): Promise<Omit<_LiquityContractAddresses, "uniToken">> => {
): Promise<[addresses: Omit<_LiquityContractAddresses, "uniToken">, startBlock: number]> => {
const [activePoolAddress, startBlock] = await deployContractAndGetBlockNumber(
deployer,
getContractFactory,
"ActivePool",
{ ...overrides }
);

const addresses = {
activePool: await deployContract(deployer, getContractFactory, "ActivePool", { ...overrides }),
activePool: activePoolAddress,
borrowerOperations: await deployContract(deployer, getContractFactory, "BorrowerOperations", {
...overrides
}),
Expand Down Expand Up @@ -95,40 +106,44 @@ const deployContracts = async (
unipool: await deployContract(deployer, getContractFactory, "Unipool", { ...overrides })
};

return {
...addresses,
lusdToken: await deployContract(
deployer,
getContractFactory,
"LUSDToken",
addresses.troveManager,
addresses.stabilityPool,
addresses.borrowerOperations,
{ ...overrides }
),
return [
{
...addresses,
lusdToken: await deployContract(
deployer,
getContractFactory,
"LUSDToken",
addresses.troveManager,
addresses.stabilityPool,
addresses.borrowerOperations,
{ ...overrides }
),

lqtyToken: await deployContract(
deployer,
getContractFactory,
"LQTYToken",
addresses.communityIssuance,
addresses.lqtyStaking,
addresses.lockupContractFactory,
Wallet.createRandom().address, // _bountyAddress (TODO: parameterize this)
addresses.unipool, // _lpRewardsAddress
Wallet.createRandom().address, // _multisigAddress (TODO: parameterize this)
{ ...overrides }
),
lqtyToken: await deployContract(
deployer,
getContractFactory,
"LQTYToken",
addresses.communityIssuance,
addresses.lqtyStaking,
addresses.lockupContractFactory,
Wallet.createRandom().address, // _bountyAddress (TODO: parameterize this)
addresses.unipool, // _lpRewardsAddress
Wallet.createRandom().address, // _multisigAddress (TODO: parameterize this)
{ ...overrides }
),

multiTroveGetter: await deployContract(
deployer,
getContractFactory,
"MultiTroveGetter",
addresses.troveManager,
addresses.sortedTroves,
{ ...overrides }
)
};
multiTroveGetter: await deployContract(
deployer,
getContractFactory,
"MultiTroveGetter",
addresses.troveManager,
addresses.sortedTroves,
{ ...overrides }
)
},

startBlock
];
};

export const deployTellorCaller = (
Expand Down Expand Up @@ -324,18 +339,19 @@ export const deployAndSetupContracts = async (
_uniTokenIsMock: !wethAddress,
_isDev,

addresses: await deployContracts(
deployer,
getContractFactory,
_priceFeedIsTestnet,
overrides
).then(async addresses => ({
...addresses,
...(await deployContracts(deployer, getContractFactory, _priceFeedIsTestnet, overrides).then(
async ([addresses, startBlock]) => ({
startBlock,

addresses: {
...addresses,

uniToken: await (wethAddress
? createUniswapV2Pair(deployer, wethAddress, addresses.lusdToken, overrides)
: deployMockUniToken(deployer, getContractFactory, overrides))
}))
uniToken: await (wethAddress
? createUniswapV2Pair(deployer, wethAddress, addresses.lusdToken, overrides)
: deployMockUniToken(deployer, getContractFactory, overrides))
}
})
))
};

const contracts = _connectToContracts(deployer, deployment);
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
/build
/generated
/subgraph.yaml
17 changes: 11 additions & 6 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
"version": "0.1.0",
"private": true,
"scripts": {
"create": "graph create liquity/liquity-protocol --node https://api.thegraph.com/deploy/",
"create-local": "graph create liquity/liquity-protocol --node http://127.0.0.1:8020",
"prepare": "graph codegen",
"create-local": "graph create liquity/liquity --node http://127.0.0.1:8020",
"prepare": "run-s prepare:*",
"prepare:manifest": "node subgraph.yaml.js",
"prepare:codegen": "graph codegen",
"prepare-local": "run-s prepare-local:*",
"prepare-local:manifest": "node subgraph.yaml.js dev",
"prepare-local:codegen": "graph codegen",
"build": "graph build",
"deploy": "graph deploy liquity/liquity-protocol --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-local": "graph deploy liquity/liquity-protocol --ipfs http://localhost:5001 --node http://127.0.0.1:8020",
"deploy": "graph deploy liquity/liquity --debug --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy-local": "graph deploy liquity/liquity --ipfs http://localhost:5001 --node http://127.0.0.1:8020",
"graph": "graph"
},
"devDependencies": {
"@graphprotocol/graph-cli": "^0.20.0",
"@graphprotocol/graph-ts": "^0.20.0"
"@graphprotocol/graph-ts": "^0.20.0",
"npm-run-all": "^4.1.5"
}
}
8 changes: 6 additions & 2 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Global @entity {
numberOfActiveLQTYStakes: Int!
totalLQTYTokensStaked: BigDecimal!

totalBorrowingFeesPaid: BigDecimal!

"Total redistributed per-stake collateral"
rawTotalRedistributedCollateral: BigInt!

Expand Down Expand Up @@ -179,6 +181,8 @@ type TroveChange implements Change @entity {
debtChange: BigDecimal!
debtAfter: BigDecimal!

borrowingFee: BigDecimal

collateralRatioBefore: BigDecimal
collateralRatioAfter: BigDecimal

Expand Down Expand Up @@ -290,8 +294,8 @@ type LqtyStakeChange implements Change @entity {
amountChange: BigDecimal!
amountAfter: BigDecimal!

issuanceGain: BigDecimal
redemptionGain: BigDecimal
issuanceGain: BigDecimal!
redemptionGain: BigDecimal!
}

type Token @entity {
Expand Down
17 changes: 15 additions & 2 deletions packages/subgraph/src/entities/Global.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Value, BigInt, Address } from "@graphprotocol/graph-ts";
import { Value, BigInt, BigDecimal, Address } from "@graphprotocol/graph-ts";

import { Global, LqtyStakeChange } from "../../generated/schema";

import { BIGINT_ZERO, DECIMAL_ZERO } from "../utils/bignumbers";
import { BIGINT_ZERO, DECIMAL_ZERO, decimalize } from "../utils/bignumbers";

const onlyGlobalId = "only";

Expand All @@ -29,6 +29,7 @@ export function getGlobal(): Global {
newGlobal.totalNumberOfLQTYStakes = 0;
newGlobal.numberOfActiveLQTYStakes = 0;
newGlobal.totalLQTYTokensStaked = DECIMAL_ZERO;
newGlobal.totalBorrowingFeesPaid = DECIMAL_ZERO;

return newGlobal;
}
Expand Down Expand Up @@ -56,6 +57,12 @@ export function getChangeSequenceNumber(): i32 {
return increaseCounter("changeCount");
}

export function getLastChangeSequenceNumber(): i32 {
let global = getGlobal();

return global.changeCount - 1;
}

export function getLiquidationSequenceNumber(): i32 {
return increaseCounter("liquidationCount");
}
Expand Down Expand Up @@ -157,3 +164,9 @@ export function handleLQTYStakeChange(
global.totalLQTYTokensStaked = global.totalLQTYTokensStaked.plus(stakeChange.amountChange);
global.save();
}

export function increaseTotalBorrowingFeesPaid(_LUSDFee: BigInt): void {
let global = getGlobal();
global.totalBorrowingFeesPaid = global.totalBorrowingFeesPaid.plus(decimalize(_LUSDFee));
global.save();
}
4 changes: 2 additions & 2 deletions packages/subgraph/src/entities/Token.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address } from "@graphprotocol/graph-ts";

import { Token } from "../../generated/schema";
import { ERC20 } from "../../generated/templates/Token/ERC20"
import { ERC20 } from "../../generated/LUSDToken/ERC20";
import { BIGINT_ZERO } from "../utils/bignumbers";

export function createToken(address: Address, name: string, symbol: string): Token {
function createToken(address: Address, name: string, symbol: string): Token {
let id = address.toHexString();
let token = new Token(id);
token.name = name;
Expand Down
15 changes: 12 additions & 3 deletions packages/subgraph/src/entities/TokenAllowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import { TokenAllowance } from "../../generated/schema";

import { getUser } from "./User";

export function getTokenAllowance(_token: Address, _owner: Address, _spender: Address): TokenAllowance {
let id = _token.toHexString() + _owner.toHexString() + _spender.toHexString();
export function getTokenAllowance(
_token: Address,
_owner: Address,
_spender: Address
): TokenAllowance {
let id = _token.toHexString() + "-" + _owner.toHexString() + "-" + _spender.toHexString();
let owner = getUser(_owner);
let spender = getUser(_spender);
let allowanceOrNull = TokenAllowance.load(id);
Expand All @@ -27,7 +31,12 @@ export function getTokenAllowance(_token: Address, _owner: Address, _spender: Ad
}
}

export function updateAllowance(_event: ethereum.Event, _owner: Address, _spender: Address, _value: BigInt): void {
export function updateAllowance(
_event: ethereum.Event,
_owner: Address,
_spender: Address,
_value: BigInt
): void {
let tokenAddress = _event.address;

let tokenAllowance = getTokenAllowance(tokenAddress, _owner, _spender);
Expand Down
Loading

0 comments on commit 619d25f

Please sign in to comment.