Skip to content

Commit

Permalink
fixed secondary pretrades
Browse files Browse the repository at this point in the history
  • Loading branch information
kallolborah committed Aug 27, 2024
1 parent e657c25 commit 75a2348
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 55 deletions.
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type Pool @entity {

# SecondaryPool and OffchainSecondariesPool only
minOrderSize: BigInt
orderBook: Orderbook
orderBook: Bytes
issueManager: Bytes
secondaryTrades: [SecondaryTrades!] @derivedFrom(field: "pool")
secondaryPreTrades: [SecondaryPreTrades!] @derivedFrom(field: "pool")
Expand Down
70 changes: 22 additions & 48 deletions src/mappings/poolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { PrimaryIssuePool, OpenIssue, Subscription } from '../types/templates/PrimaryIssuePool/PrimaryIssuePool';
import { SecondaryIssuePool, Offer, TradeReport, OrderBook } from '../types/templates/SecondaryIssuePool/SecondaryIssuePool';
import { MarginTradingPool, MarginOffer, MarginTradeReport, MarginOrderBook } from '../types/templates/MarginTradingPool/MarginTradingPool';
import { Orderbook as OrderbookTemplate } from '../types/templates';
import { tradeExecuted } from '../types/templates/OrderBook/Orderbook';
import { OffchainSecondariesPool } from '../types/templates/OffchainSecondariesPool/OffchainSecondariesPool';
import {
Expand Down Expand Up @@ -580,23 +581,26 @@ export function handleSubscription(event: Subscription): void {

pool.security = event.params.security;
pool.currency = event.params.currency;
pool.orderBook = event.params.orderBook.toHexString();
pool.orderBook = event.params.orderBook;
pool.minOrderSize = event.params.minOrderSize;
pool.issueManager = event.params.issueManager;
log.info("Orderbook ", [event.params.orderBook.toHexString()]);
pool.save();

let orderBook = Orderbook.load(event.params.orderBook.toHexString());
if(orderBook==null){
let providerId = getPoolTokenId(event.transaction.hash.toHexString(), event.params.security);
let orderBook = new Orderbook(providerId);
orderBook.pool = poolId.toHexString();
orderBook.save();
OrderbookTemplate.create(event.params.orderBook);

let orderbook = loadSecondaryPreTrades(event.transaction.hash.toHexString(), event.params.orderBook);
if (orderbook == null) {
let providerId = getPoolTokenId(event.transaction.hash.toHexString(), event.params.orderBook);
let orderbook = new Orderbook(providerId);
orderbook.pool = poolId.toHexString();
orderbook.save();
}
else{
orderBook.pool = poolId.toHexString();
orderBook.save();
orderbook.pool = poolId.toHexString();
orderbook.save();
}

}

export function handleOrderBook(event: OrderBook): void {
Expand Down Expand Up @@ -635,62 +639,32 @@ export function handleOrderBook(event: OrderBook): void {

export function handlePreTrades(event: tradeExecuted): void {
log.info("handlePreTrades is running", []);
let pool = Pool.load(event.params.pool.toHexString());
if (pool == null) {
log.error("Pool is empty", [event.params.pool.toHexString()]);
return; // Handle error, pool not found
}

/*let poolAddress = event.params.pool;
let poolContract = SecondaryIssuePool.bind(poolAddress);
let poolIdCall = poolContract.try_getPoolId();
let poolId = poolIdCall.value;*/

let orderbook = Orderbook.load(event.address.toHexString());
if (orderbook == null) {
log.error("Orderbook is empty", [event.address.toHexString()]);
return; // Handle error, orderbook not found
}

let preTradeId = event.transaction.hash.toHex() + "-" + event.logIndex.toString();
let preTrade = new SecondaryPreTrades(preTradeId);
log.info("Pretrade data",[event.params.party.toHexString(), event.params.counterparty.toHexString()]);
preTrade.pool = pool.id;
//preTrade.pool = poolId.toHexString();
preTrade.orderbook = orderbook.id;
preTrade.party = event.params.party.toHexString();
preTrade.counterparty = event.params.counterparty.toHexString();
preTrade.executionDate = event.params.tradeToReportDate;

preTrade.save();
}

/*export function handlePreTrades(event: tradeExecuted): void {
let orderBook = event.address;

//let poolContract = SecondaryIssuePool.bind(poolAddress);
//let poolIdCall = poolContract.try_getPoolId();
//let poolId = poolIdCall.value;
let poolContract = SecondaryIssuePool.bind(event.params.pool);
let poolIdCall = poolContract.try_getPoolId();
let poolId = poolIdCall.value;

let pretrades = loadSecondaryPreTrades(event.transaction.hash.toHexString(), orderBook);
if (pretrades == null) {
let providerId = getPoolTokenId(event.transaction.hash.toHexString(), orderBook);
let pretrades = new SecondaryPreTrades(providerId);
pretrades.pool = event.params.pool.toHexString();
pretrades.pool = poolId.toHexString();
pretrades.orderbook = pretrades.id;
pretrades.executionDate = event.params.tradeToReportDate;
pretrades.party = event.params.party.toHexString();
pretrades.counterparty = event.params.counterparty.toHexString();
pretrades.save();
}
else{
pretrades.pool = event.params.pool.toHexString();
pretrades.pool = poolId.toHexString();
pretrades.orderbook = pretrades.id;
pretrades.executionDate = event.params.tradeToReportDate;
pretrades.party = event.params.party.toHexString();
pretrades.counterparty = event.params.counterparty.toHexString();
pretrades.save();
}
}*/
}

export function handleTradeReport(event: TradeReport): void {
log.info("TradeReport is running", []);
Expand Down Expand Up @@ -839,7 +813,7 @@ export function handleOffchainTradeReport(event: TradeReport): void {
pool.margin = event.params.margin;
pool.collateral = event.params.collateral;
pool.cficode = event.params.CfiCode;
pool.orderBook = event.params.orderBook.toHexString();
pool.orderBook = event.params.orderBook;
pool.minOrderSize = event.params.minOrderSize;
pool.issueManager = event.params.issueManager;

Expand Down
9 changes: 3 additions & 6 deletions src/mappings/poolFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import { ManagedPool as ManagedPoolTemplate } from '../types/templates';
import { LinearPool as LinearPoolTemplate } from '../types/templates';
import { PrimaryIssuePool as PrimaryPoolTemplate } from '../types/templates';
import { SecondaryIssuePool as SecondaryPoolTemplate } from '../types/templates';
import { Orderbook as OrderbookTemplate } from '../types/templates';
import { MarginTradingPool as MarginTradingPoolTemplate } from '../types/templates';
import { OffchainSecondariesPool as OffchainSecondaryIssuePoolTemplate } from '../types/templates';
import { Gyro2Pool as Gyro2PoolTemplate } from '../types/templates';
Expand Down Expand Up @@ -407,7 +406,7 @@ export function handleNewSecondaryPool(event: PoolCreated): void {
pool.owner = balancerManager;
pool.principalToken = security;
pool.baseToken = currency;
pool.orderBook = orderBook.toHexString();
pool.orderBook = orderBook;

let tokens = getPoolTokens(poolId);
if (tokens == null) return;
Expand All @@ -418,8 +417,6 @@ export function handleNewSecondaryPool(event: PoolCreated): void {
handleNewPoolTokens(pool, tokens);

SecondaryPoolTemplate.create(poolAddress);

OrderbookTemplate.create(poolAddress);
}

export function handleNewMarginPool(event: PoolCreated): void {
Expand Down Expand Up @@ -459,7 +456,7 @@ export function handleNewMarginPool(event: PoolCreated): void {
pool.margin = margin;
pool.collateral = collateral;
pool.baseToken = currency;
pool.orderBook = orderBook.toHexString();
pool.orderBook = orderBook;

let tokens = getPoolTokens(poolId);
if (tokens == null) return;
Expand Down Expand Up @@ -501,7 +498,7 @@ export function handleNewOffchainSecondaryPool(event: PoolCreated): void {
pool.owner = balancerManager;
pool.principalToken = security;
pool.baseToken = currency;
pool.orderBook = orderBook.toHexString();
pool.orderBook = orderBook;

let tokens = getPoolTokens(poolId);
if (tokens == null) return;
Expand Down

0 comments on commit 75a2348

Please sign in to comment.