Skip to content

Commit

Permalink
Merge pull request #16 from overlay-market/arthurka/bac-86-add-tests-…
Browse files Browse the repository at this point in the history
…to-overlayv1factory-handlers

`OverlayV1Factory` tests
  • Loading branch information
TomasCImach authored Aug 7, 2024
2 parents d9f8e26 + e8bdcf6 commit 8766c56
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 75 deletions.
207 changes: 207 additions & 0 deletions src/__test__/unit/market-factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import {
assert,
describe,
test,
clearStore,
beforeAll,
beforeEach,
afterEach
} from "matchstick-as/assembly/index"
import { newMockEvent } from "matchstick-as"
import { ethereum, Address, BigInt } from "@graphprotocol/graph-ts"

import {
MarketDeployed as MarketDeployedEvent,
FeeRecipientUpdated as FeeRecipientUpdatedEvent,
ParamUpdated as ParamUpdatedEvent,
EmergencyShutdown as EmergencyShutdownEvent
} from "../../../generated/OverlayV1Factory/OverlayV1Factory"
import { handleEmergencyShutdown, handleFeeRecipientUpdated, handleMarketDeployed, handleParamUpdated } from "../../mapping"
import { FACTORY_ADDRESS, PERIPHERY_ADDRESS } from "../../utils/constants"
import { setupMarketMockedFunctions } from "./shared/mockedFunctions"
import { loadFactory } from "../../utils"

export { handleMarketDeployed }

const market = Address.fromString("0x0000000000000000000000000000000000000001")
const user = Address.fromString("0x0000000000000000000000000000000000000004");
const feed = Address.fromString("0x0000000000000000000000000000000000000005");
const recipientAddress = Address.fromString("0x0000000000000000000000000000000000000006");

const factoryAddress = Address.fromString(FACTORY_ADDRESS)
const marketStateAddress = Address.fromString(PERIPHERY_ADDRESS)

// createParamUpdatedEvent attrs

// because RISK_PARAMS[5] doesn't work without million hoops
const paramNameKey = "capLeverage"
const paramNameValue: u8 = 5
const paramValue = BigInt.fromI32(5)

describe("Market Factory events", () => {
beforeAll(() => {
setupMarketMockedFunctions(factoryAddress, marketStateAddress, market)
})
describe("Market Deployed event", () => {
beforeEach(() => {
const event = createMarketDeployedEvent(factoryAddress, user, market, feed)
handleMarketDeployed(event)
})

afterEach(() => {
clearStore()
})

test("creates Market entity", () => {
assert.entityCount("Market", 1)
})

})

describe("Fee Recipient Updated event", () => {
beforeEach(() => {
// initializing factory
loadFactory(factoryAddress.toString())
// imitating an event
const event = createFeeRecipientUpdatedEvent(factoryAddress, user, recipientAddress)
handleFeeRecipientUpdated(event)
})

afterEach(() => {
clearStore()
})

test("doesn't create additional Factory entity", () => {
assert.entityCount("Factory", 1)
})

test("fee recipient is set correctly", () => {
assert.fieldEquals("Factory", factoryAddress.toHexString(), "feeRecipient", recipientAddress.toHexString())
})
})

describe("Param Updated event", () => {
beforeEach(() => {
loadFactory(factoryAddress.toString())
const event = createParamUpdatedEvent(factoryAddress, user, market, paramNameValue, paramValue)
handleParamUpdated(event)
})

afterEach(() => {
clearStore()
})

test("param value is set correctly", () => {
assert.fieldEquals("Market", market.toHexString(), paramNameKey, paramValue.toString())
})
})

describe("Emergency Shutdown event", () => {
beforeEach(() => {
loadFactory(factoryAddress.toString())
const event = createEmergencyShutdownEvent(factoryAddress, user, market)
handleEmergencyShutdown(event)
})

afterEach(() => {
clearStore()
})

test("market is marked as shut down", () => {
assert.fieldEquals("Market", market.toHexString(), "isShutdown", "true")
})
})
})

function createMarketDeployedEvent(
factory: Address,
user: Address,
market: Address,
feed: Address
): MarketDeployedEvent {
const event = changetype<MarketDeployedEvent>(newMockEvent())

event.address = factory
event.parameters = new Array()

event.parameters.push(
new ethereum.EventParam("user", ethereum.Value.fromAddress(user)),
)
event.parameters.push(
new ethereum.EventParam("market", ethereum.Value.fromAddress(market)),
)
event.parameters.push(
new ethereum.EventParam("feed", ethereum.Value.fromAddress(feed)),
)

return event
}

function createFeeRecipientUpdatedEvent(
factory: Address,
user: Address,
recipient: Address
): FeeRecipientUpdatedEvent {
const event = changetype<FeeRecipientUpdatedEvent>(newMockEvent())

event.address = factory
event.parameters = new Array()

event.parameters.push(
new ethereum.EventParam("user", ethereum.Value.fromAddress(user)),
)
event.parameters.push(
new ethereum.EventParam("recipient", ethereum.Value.fromAddress(recipient)),
)

return event
}

function createParamUpdatedEvent(
factory: Address,
user: Address,
market: Address,
name: u8,
value: BigInt
): ParamUpdatedEvent {
const event = changetype<ParamUpdatedEvent>(newMockEvent())

event.address = factory
event.parameters = new Array()

event.parameters.push(
new ethereum.EventParam("user", ethereum.Value.fromAddress(user)),
)
event.parameters.push(
new ethereum.EventParam("market", ethereum.Value.fromAddress(market)),
)
// fromI32 was chosen because `name` is uint8, int32 includes values up to uint16
event.parameters.push(
new ethereum.EventParam("name", ethereum.Value.fromI32(name)),
)
event.parameters.push(
new ethereum.EventParam("value", ethereum.Value.fromUnsignedBigInt(value)),
)

return event
}

function createEmergencyShutdownEvent(
factory: Address,
user: Address,
market: Address
): EmergencyShutdownEvent {
const event = changetype<EmergencyShutdownEvent>(newMockEvent())

event.address = factory
event.parameters = new Array()

event.parameters.push(
new ethereum.EventParam("user", ethereum.Value.fromAddress(user)),
)
event.parameters.push(
new ethereum.EventParam("market", ethereum.Value.fromAddress(market)),
)

return event
}
87 changes: 12 additions & 75 deletions src/__test__/unit/market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@ import { handleBuild, handleCacheRiskCalc, handleUpdate } from "../../mapping"
import { loadAccount } from "../../utils"
import { loadTradingMining } from "../../trading-mining"
import { loadReferralProgram, loadReferralPosition } from "../../referral"
import { PERIPHERY_ADDRESS, TRADING_MINING_ADDRESS, REFERRAL_ADDRESS } from "../../utils/constants"
import { PERIPHERY_ADDRESS, TRADING_MINING_ADDRESS, REFERRAL_ADDRESS, ADDRESS_ZERO } from "../../utils/constants"
import { setupMarketMockedFunctions, setupTradingMiningMockedFunctions } from "./shared/mockedFunctions"
import { MARKET_COLLATERAL, MARKET_PCD_HOLDER, MARKET_POSITION_ID, MARKET_SENDER } from "./shared/constants"

// Export handlers for coverage report
export { handleBuild }
export { handleBuild, handleCacheRiskCalc, handleUpdate }

const market = Address.fromString("0x0000000000000000000000000000000000000001")
const tmAddress = Address.fromString(TRADING_MINING_ADDRESS)
const referralAddress = Address.fromString(REFERRAL_ADDRESS)
const marketStateAddress = Address.fromString(PERIPHERY_ADDRESS)
const zeroAddress = Address.fromString(ADDRESS_ZERO)

// Build event parameters
const sender = Address.fromString("0x0000000000000000000000000000000000000b0b")
const pcdHolder = Address.fromString("0x000000000000000000000000000000000000ca1e")
const positionId = BigInt.fromI32(1)
const sender = MARKET_SENDER
const pcdHolder = MARKET_PCD_HOLDER
const positionId = MARKET_POSITION_ID
const collateral = MARKET_COLLATERAL

const oi = BigInt.fromI32(50)
const debt = BigInt.fromI32(20)
const isLong = true
const price = BigInt.fromI32(100)
const collateral = BigInt.fromI32(1000)
const oiAfterBuild = BigInt.fromI32(51)
const oiSharesAfterBuild = BigInt.fromI32(1)

Expand All @@ -63,75 +67,8 @@ describe("Market events", () => {

// Mock contract calls with dummy values
beforeAll(() => {
// Market contract
createMockedFunction(market, "feed", "feed():(address)")
.returns([ethereum.Value.fromAddress(Address.zero())])
createMockedFunction(market, "factory", "factory():(address)")
.returns([ethereum.Value.fromAddress(Address.zero())])
for (let i = 0; i < 15; i++) {
createMockedFunction(market, "params", "params(uint256):(uint256)")
.withArgs([ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(i))])
.returns([ethereum.Value.fromI32(1)])
}
createMockedFunction(market, "dpUpperLimit", "dpUpperLimit():(uint256)")
.returns([ethereum.Value.fromI32(1)])
createMockedFunction(market, "oiLongShares", "oiLongShares():(uint256)")
.returns([ethereum.Value.fromI32(1)])
createMockedFunction(market, "oiShortShares", "oiShortShares():(uint256)")
.returns([ethereum.Value.fromI32(1)])

// Periphery contract
createMockedFunction(Address.fromString(PERIPHERY_ADDRESS), "ois", "ois(address):(uint256,uint256)")
.withArgs([ethereum.Value.fromAddress(market)])
.returns([ethereum.Value.fromI32(1), ethereum.Value.fromI32(1)])
createMockedFunction(Address.fromString(PERIPHERY_ADDRESS), "cost", "cost(address,address,uint256):(uint256)")
.withArgs([ethereum.Value.fromAddress(market), ethereum.Value.fromAddress(sender), ethereum.Value.fromUnsignedBigInt(positionId)])
.returns([ethereum.Value.fromUnsignedBigInt(collateral)])
createMockedFunction(Address.fromString(PERIPHERY_ADDRESS), "value", "value(address,address,uint256):(uint256)")
.withArgs([ethereum.Value.fromAddress(market), ethereum.Value.fromAddress(sender), ethereum.Value.fromUnsignedBigInt(positionId)])
.returns([ethereum.Value.fromI32(1)])
createMockedFunction(Address.fromString(PERIPHERY_ADDRESS), "cost", "cost(address,address,uint256):(uint256)")
.withArgs([ethereum.Value.fromAddress(market), ethereum.Value.fromAddress(pcdHolder), ethereum.Value.fromUnsignedBigInt(positionId)])
.returns([ethereum.Value.fromUnsignedBigInt(collateral)])
createMockedFunction(Address.fromString(PERIPHERY_ADDRESS), "value", "value(address,address,uint256):(uint256)")
.withArgs([ethereum.Value.fromAddress(market), ethereum.Value.fromAddress(pcdHolder), ethereum.Value.fromUnsignedBigInt(positionId)])
.returns([ethereum.Value.fromI32(1)])

// TradingMining contract
createMockedFunction(tmAddress, "getCurrentEpoch", "getCurrentEpoch():(uint256)")
.returns([ethereum.Value.fromI32(epoch)])
createMockedFunction(tmAddress, "rewardToken1", "rewardToken1():(address)")
.returns([ethereum.Value.fromAddress(Address.zero())])
createMockedFunction(tmAddress, "rewardToken2", "rewardToken2():(address)")
.returns([ethereum.Value.fromAddress(Address.zero())])
createMockedFunction(tmAddress, "token1Percentage", "token1Percentage():(uint8)")
.returns([ethereum.Value.fromI32(0)])
createMockedFunction(tmAddress, "startTime", "startTime():(uint64)")
.returns([ethereum.Value.fromI32(0)])
createMockedFunction(tmAddress, "epochDuration", "epochDuration():(uint64)")
.returns([ethereum.Value.fromI32(0)])
createMockedFunction(tmAddress, "pcdHolderBonusPercentage", "pcdHolderBonusPercentage():(uint8)")
.returns([ethereum.Value.fromI32(0)])
createMockedFunction(tmAddress, "totalRewards", "totalRewards():(uint256)")
.returns([ethereum.Value.fromI32(0)])
createMockedFunction(tmAddress, "maxRewardPerEpochPerAddress", "maxRewardPerEpochPerAddress():(uint256)")
.returns([ethereum.Value.fromI32(0)])

// Market state contract
createMockedFunction(marketStateAddress, "marketState", "marketState(address):((uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,int256))")
.withArgs([ethereum.Value.fromAddress(market)])
.returns([ethereum.Value.fromTuple(changetype<ethereum.Tuple>([
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0)),
ethereum.Value.fromSignedBigInt(BigInt.fromI32(1))
]))])
setupMarketMockedFunctions(zeroAddress, marketStateAddress, market)
setupTradingMiningMockedFunctions(tmAddress, epoch)
})
describe("Build event", () => {

Expand Down
8 changes: 8 additions & 0 deletions src/__test__/unit/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Address, BigInt } from "@graphprotocol/graph-ts"

const MARKET_COLLATERAL = BigInt.fromI32(1000)
const MARKET_POSITION_ID = BigInt.fromI32(1)
const MARKET_PCD_HOLDER = Address.fromString("0x000000000000000000000000000000000000ca1e")
const MARKET_SENDER = Address.fromString("0x0000000000000000000000000000000000000b0b")

export { MARKET_COLLATERAL, MARKET_POSITION_ID, MARKET_PCD_HOLDER, MARKET_SENDER }
Loading

0 comments on commit 8766c56

Please sign in to comment.