Skip to content

Commit

Permalink
Merge pull request #1417 from connext/1407-negative-balance
Browse files Browse the repository at this point in the history
only update if not reconciled
  • Loading branch information
Rahul Sethuram authored Jun 14, 2022
2 parents bbf108c + d7d500e commit 714e2ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
49 changes: 27 additions & 22 deletions packages/deployments/subgraph/src/amarok-runtime-staging/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,33 +239,36 @@ export function handleXCalled(event: XCalled): void {
* @param event - The contract event used to update the subgraph
*/
export function handleExecuted(event: Executed): void {
const num = event.params.args.routers.length;
const amount = event.params.args.amount;
const routers: string[] = [];
for (let i = 0; i < num; i++) {
const param = event.params.args.routers[i].toHex();
let router = Router.load(param);
if (router == null) {
// TODO: Shouldn't we be throwing an error here? How did a transfer get made with a non-existent
// router?
router = new Router(param);
router.isActive = true;
router.save();
}

routers.push(router.id);

// Update router's liquidity
const assetBalance = getOrCreateAssetBalance(event.params.args.local, event.params.args.routers[i]);
assetBalance.amount = assetBalance.amount.minus(amount.div(BigInt.fromI32(num)));
assetBalance.save();
}

// Load transfer details
let transfer = DestinationTransfer.load(event.params.transferId.toHexString());
if (transfer == null) {
transfer = new DestinationTransfer(event.params.transferId.toHexString());
}

const num = event.params.args.routers.length;
const amount = event.params.args.amount;
const routers: string[] = [];
if (transfer.status != "Reconciled") {
for (let i = 0; i < num; i++) {
const param = event.params.args.routers[i].toHex();
let router = Router.load(param);
if (router == null) {
// TODO: Shouldn't we be throwing an error here? How did a transfer get made with a non-existent
// router?
router = new Router(param);
router.isActive = true;
router.save();
}

routers.push(router.id);

// Update router's liquidity
const assetBalance = getOrCreateAssetBalance(event.params.args.local, event.params.args.routers[i]);
assetBalance.amount = assetBalance.amount.minus(amount.div(BigInt.fromI32(num)));
assetBalance.save();
}
} // otherwise no routers used

// Meta
transfer.chainId = getChainId();
transfer.transferId = event.params.transferId;
Expand Down Expand Up @@ -369,9 +372,11 @@ export function handleReconciled(event: Reconciled): void {
transfer.save();
}

// eslint-disable-next-line @typescript-eslint/ban-types
function getChainId(): BigInt {
// try to get chainId from the mapping
let network = dataSource.network();
// eslint-disable-next-line @typescript-eslint/ban-types
let chainId: BigInt;
if (network == "mainnet") {
chainId = BigInt.fromI32(1);
Expand Down
46 changes: 26 additions & 20 deletions packages/deployments/subgraph/src/amarok-runtime-v0/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,32 +239,36 @@ export function handleXCalled(event: XCalled): void {
* @param event - The contract event used to update the subgraph
*/
export function handleExecuted(event: Executed): void {
// Load transfer details
let transfer = DestinationTransfer.load(event.params.transferId.toHexString());
if (transfer == null) {
transfer = new DestinationTransfer(event.params.transferId.toHexString());
}

const num = event.params.args.routers.length;
const amount = event.params.args.amount;
const routers: string[] = [];
for (let i = 0; i < num; i++) {
const param = event.params.args.routers[i].toHex();
let router = Router.load(param);
if (router == null) {
// TODO: Shouldn't we be throwing an error here? How did a transfer get made with a non-existent
// router?
router = new Router(param);
router.isActive = true;
router.save();
}

routers.push(router.id);
if (transfer.status != "Reconciled") {
for (let i = 0; i < num; i++) {
const param = event.params.args.routers[i].toHex();
let router = Router.load(param);
if (router == null) {
// TODO: Shouldn't we be throwing an error here? How did a transfer get made with a non-existent
// router?
router = new Router(param);
router.isActive = true;
router.save();
}

// Update router's liquidity
const assetBalance = getOrCreateAssetBalance(event.params.args.local, event.params.args.routers[i]);
assetBalance.amount = assetBalance.amount.minus(amount.div(BigInt.fromI32(num)));
assetBalance.save();
}
routers.push(router.id);

let transfer = DestinationTransfer.load(event.params.transferId.toHexString());
if (transfer == null) {
transfer = new DestinationTransfer(event.params.transferId.toHexString());
}
// Update router's liquidity
const assetBalance = getOrCreateAssetBalance(event.params.args.local, event.params.args.routers[i]);
assetBalance.amount = assetBalance.amount.minus(amount.div(BigInt.fromI32(num)));
assetBalance.save();
}
} // otherwise no routers used

// Meta
transfer.chainId = getChainId();
Expand Down Expand Up @@ -369,9 +373,11 @@ export function handleReconciled(event: Reconciled): void {
transfer.save();
}

// eslint-disable-next-line @typescript-eslint/ban-types
function getChainId(): BigInt {
// try to get chainId from the mapping
let network = dataSource.network();
// eslint-disable-next-line @typescript-eslint/ban-types
let chainId: BigInt;
if (network == "mainnet") {
chainId = BigInt.fromI32(1);
Expand Down

0 comments on commit 714e2ef

Please sign in to comment.