Skip to content

Commit

Permalink
update cauldron tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfantaholic committed Nov 29, 2023
1 parent 2ec20c9 commit 02d28ea
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 18 deletions.
2 changes: 2 additions & 0 deletions subgraphs/cauldrons/src/helpers/collateral/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './get-or-create-collateral';
export * from './get-or-create-collateral-daily-snapshot';
export * from './get-or-create-collateral-houry-snapshot';
1 change: 0 additions & 1 deletion subgraphs/cauldrons/src/helpers/updates/update-tvl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function updateTvl(block: ethereum.Block): void {

if (isDeprecated(cauldron, block)) {
cauldron.deprecated = true;
cauldron.save();
}

const marketTVL = cauldron.totalCollateralShare.times(cauldron.collateralPriceUsd);
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/cauldrons/tests/.latest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.6.0",
"timestamp": 1698919331758
"timestamp": 1701251810707
}
130 changes: 114 additions & 16 deletions subgraphs/cauldrons/tests/cauldron.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';
import { BigDecimal, BigInt, Bytes, ethereum } from '@graphprotocol/graph-ts';
import { assert, beforeEach, clearStore, createMockedFunction, describe, test } from 'matchstick-as';
import { createCauldron, getCauldron, getOrCreateCauldronDailySnapshot, getOrCreateCauldronHourySnapshot } from '../src/helpers/cauldron';
import {
Expand All @@ -18,14 +18,17 @@ import {
NON_CAULDRON_V1_MASTER_CONTRACT_ADDRESS,
COLLATERAL_ENTITY_TYPE,
CAULDRON_V1_COLLATERAL_ADDRESS,
PROTOCOL_ENTITY_TYPE,
COLLATERAL_DAILY_SNAPSHOT,
COLLATERAL_HOURY_SNAPSHOT
} from './constants';
import { handleLogAccrue, handleLogExchangeRate } from '../src/mappings/cauldron';

import { getOrCreateProtocolDailySnapshot, getOrCreateProtocolHourySnapshot } from '../src/helpers/protocol';
import { getOrCreateProtocol, getOrCreateProtocolDailySnapshot, getOrCreateProtocolHourySnapshot } from '../src/helpers/protocol';
import { createLogAccrue } from './helpers/create-log-accrue';
import { bigIntToBigDecimal } from 'misc';
import { createLogExchangeRate } from './helpers/create-log-exchange-rate';
import { getOrCreateCollateral } from '../src/helpers/collateral';
import { getOrCreateCollateral, getOrCreateCollateralDailySnapshot, getOrCreateCollateralHourySnapshot } from '../src/helpers/collateral';

describe('Cauldrons', () => {
beforeEach(() => {
Expand Down Expand Up @@ -60,14 +63,6 @@ describe('Cauldrons', () => {
createMockedFunction(CAULDRON_V1_COLLATERAL_ADDRESS, 'decimals', 'decimals():(uint8)')
.withArgs([])
.returns([ethereum.Value.fromI32(COLLATERAL_DECIMALS)]);

// createMockedFunction(SPELL_ORACLE_ADDRESS, 'peekSpot', 'peekSpot(bytes):(uint256)')
// .withArgs([ethereum.Value.fromBytes(SPELL_ORACLE_DATA)])
// .returns([ethereum.Value.fromUnsignedBigInt(SPELL_ORACLE_PRICE)]);

// createMockedFunction(CLONE_ADDRESS, 'totalBorrow', 'totalBorrow():(uint128,uint128)')
// .withArgs([])
// .returns([ethereum.Value.fromSignedBigInt(BigInt.fromString('0')), ethereum.Value.fromSignedBigInt(BigInt.fromString('0'))]);
});

describe('LogAccrue', () => {
Expand All @@ -86,6 +81,23 @@ describe('Cauldrons', () => {
const cauldronId = getCauldron(CLONE_ADDRESS.toHexString())!.id;
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'lastActive', log.block.timestamp.toString());
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'isActive', 'true');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'hourySnapshotCount', '1');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'dailySnapshotCount', '1');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'totalFeesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'interestFeesGenerated', bigIntToBigDecimal(amount).toString());
});

test('should update protocol', () => {
const amount = BigInt.fromI32(10000000);
const log = createLogAccrue(amount);

handleLogAccrue(log);

const protocolId = getOrCreateProtocol().id!;
assert.fieldEquals(PROTOCOL_ENTITY_TYPE, protocolId, 'dailySnapshotCount', '1');
assert.fieldEquals(PROTOCOL_ENTITY_TYPE, protocolId, 'hourySnapshotCount', '1');
assert.fieldEquals(PROTOCOL_ENTITY_TYPE, protocolId, 'totalFeesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(PROTOCOL_ENTITY_TYPE, protocolId, 'interestFeesGenerated', bigIntToBigDecimal(amount).toString());
});

test('should update protocol daily snapshot', () => {
Expand All @@ -98,6 +110,7 @@ describe('Cauldrons', () => {
assert.entityCount(PROTOCOL_DAILY_SNAPSHOT, 1);
assert.fieldEquals(PROTOCOL_DAILY_SNAPSHOT, protocolDailySnapshotId, 'feesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(PROTOCOL_DAILY_SNAPSHOT, protocolDailySnapshotId, 'interestFeesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(PROTOCOL_DAILY_SNAPSHOT, protocolDailySnapshotId, 'totalFeesGenerated', bigIntToBigDecimal(amount).toString());
});

test('should update protocol houry snapshot', () => {
Expand All @@ -110,6 +123,7 @@ describe('Cauldrons', () => {
assert.entityCount(PROTOCOL_HOURY_SNAPSHOT, 1);
assert.fieldEquals(PROTOCOL_HOURY_SNAPSHOT, protocolHourySnapshotId, 'feesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(PROTOCOL_HOURY_SNAPSHOT, protocolHourySnapshotId, 'interestFeesGenerated', bigIntToBigDecimal(amount).toString());
assert.fieldEquals(PROTOCOL_HOURY_SNAPSHOT, protocolHourySnapshotId, 'totalFeesGenerated', bigIntToBigDecimal(amount).toString());
});

test('should update cauldron daily snapshot', () => {
Expand Down Expand Up @@ -146,34 +160,118 @@ describe('Cauldrons', () => {
clearStore();

createCauldron(CLONE_ADDRESS, NON_CAULDRON_V1_MASTER_CONTRACT_ADDRESS, BLOCK_NUMBER, BLOCK_TIMESTAMP, NON_CAULDRON_V1_DATA);

const cauldron = getCauldron(CLONE_ADDRESS.toHexString())!;
cauldron.totalCollateralShare = BigDecimal.fromString("10000");
cauldron.save();cauldron
});

test('should update cauldron', () => {
const amount = BigInt.fromI32(10000000);
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const cauldronId = getCauldron(CLONE_ADDRESS.toHexString())!.id;
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'lastActive', log.block.timestamp.toString());
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'isActive', 'true');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'deprecated', 'false');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'exchangeRate', amount.toString());
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'collateralPriceUsd', '100000000000');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'collateralPriceUsd', '0.00057910000000000000000029296669');
assert.fieldEquals(CAULDRON_ENTITY_TYPE, cauldronId, 'totalValueLockedUsd', '5.7910000000000000000029296669');
});

test('should update cauldron daily snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const cauldron = getCauldron(CLONE_ADDRESS.toHexString())!;

const cauldronDailySnapshotId = getOrCreateCauldronDailySnapshot(cauldron, log.block).id;
assert.fieldEquals(CAULDRON_DAILY_SNAPSHOT, cauldronDailySnapshotId, 'totalValueLockedUsd', '5.7910000000000000000029296669');
});

test('should update cauldron houry snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const cauldron = getCauldron(CLONE_ADDRESS.toHexString())!;

const cauldronHourySnapshotId = getOrCreateCauldronHourySnapshot(cauldron, log.block).id;
assert.fieldEquals(CAULDRON_HOURY_SNAPSHOT, cauldronHourySnapshotId, 'totalValueLockedUsd', "5.7910000000000000000029296669");
});

test('should update collateral', () => {
const amount = BigInt.fromI32(10000000);
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const collateral = getOrCreateCollateral(NON_CAULDRON_V1_COLLATERAL_ADDRESS);
assert.fieldEquals(COLLATERAL_ENTITY_TYPE, collateral.id, 'lastPriceUsd', '100000000000');
assert.fieldEquals(COLLATERAL_ENTITY_TYPE, collateral.id, 'lastPriceUsd', '0.00057910000000000000000029296669');
assert.fieldEquals(COLLATERAL_ENTITY_TYPE, collateral.id, 'lastPriceBlockNumber', log.block.number.toString());
assert.fieldEquals(COLLATERAL_ENTITY_TYPE, collateral.id, 'lastPriceTimestamp', log.block.timestamp.toString());
});

test('should update tvl', () => {});
test('should update collateral daily snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const collateral = getOrCreateCollateral(NON_CAULDRON_V1_COLLATERAL_ADDRESS);

const collateralDailySnapshotId = getOrCreateCollateralDailySnapshot(log.block, collateral).id;
assert.entityCount(COLLATERAL_DAILY_SNAPSHOT, 1);
assert.fieldEquals(COLLATERAL_DAILY_SNAPSHOT, collateralDailySnapshotId, 'lastPriceUsd', '0.00057910000000000000000029296669');
});

test('should update collateral houry snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const collateral = getOrCreateCollateral(NON_CAULDRON_V1_COLLATERAL_ADDRESS);

const collateralHourySnapshotId = getOrCreateCollateralHourySnapshot(log.block, collateral).id;
assert.entityCount(COLLATERAL_HOURY_SNAPSHOT, 1);
assert.fieldEquals(COLLATERAL_HOURY_SNAPSHOT, collateralHourySnapshotId, 'lastPriceUsd', '0.00057910000000000000000029296669');
});

test('should update protocol', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const protocolId = getOrCreateProtocol().id!;
assert.fieldEquals(PROTOCOL_ENTITY_TYPE, protocolId, 'totalValueLockedUsd', '5.7910000000000000000029296669');
});

test('should update protocol daily snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const protocolDailySnapshotId = getOrCreateProtocolDailySnapshot(log.block).id;
assert.fieldEquals(PROTOCOL_DAILY_SNAPSHOT, protocolDailySnapshotId, 'totalValueLockedUsd', '5.7910000000000000000029296669');
});

test('should update protocol houry snapshot', () => {
const amount = BigInt.fromString("1726817475392850975651");
const log = createLogExchangeRate(amount);

handleLogExchangeRate(log);

const protocolHourySnapshotId = getOrCreateProtocolHourySnapshot(log.block).id;
assert.fieldEquals(PROTOCOL_HOURY_SNAPSHOT, protocolHourySnapshotId, 'totalValueLockedUsd', '5.7910000000000000000029296669');
});
});

describe('handleLogRepay', () => {});
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/cauldrons/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const PROTOCOL_DAILY_SNAPSHOT = 'ProtocolDailySnapshot';
export const PROTOCOL_HOURY_SNAPSHOT = 'ProtocolHourySnapshot';
export const CAULDRON_DAILY_SNAPSHOT = 'CauldronDailySnapshot';
export const CAULDRON_HOURY_SNAPSHOT = 'CauldronHourySnapshot';
export const COLLATERAL_DAILY_SNAPSHOT = 'CollateralDailySnapshot';
export const COLLATERAL_HOURY_SNAPSHOT = 'CollateralHourySnapshot';

export const MOCK_ACCOUNT = Address.fromString('0x33C52FBAB377F2C9F85DD6E678AE6BAF78A20EBE');

Expand Down

0 comments on commit 02d28ea

Please sign in to comment.