Skip to content

Commit

Permalink
[SubGraph] Apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Oct 12, 2023
1 parent ee25bcc commit ade21bb
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 35 deletions.
4 changes: 2 additions & 2 deletions packages/contracts/contracts/ShopCollection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract ShopCollection {
event IncreasedSettledPoint(bytes32 shopId, uint256 increase, uint256 total, string purchaseId);

event OpenedWithdrawal(bytes32 shopId, uint256 amount, address account);
event ClosedWithdrawal(bytes32 shopId, uint256 amount, address account);
event ClosedWithdrawal(bytes32 shopId, uint256 amount, uint256 total, address account);

address public ledgerAddress;
address public deployer;
Expand Down Expand Up @@ -248,6 +248,6 @@ contract ShopCollection {
shops[_shopId].withdrawData.status = WithdrawStatus.CLOSE;
shops[_shopId].withdrawnPoint += shop.withdrawData.amount;

emit ClosedWithdrawal(_shopId, shops[_shopId].withdrawData.amount, msg.sender);
emit ClosedWithdrawal(_shopId, shops[_shopId].withdrawData.amount, shops[_shopId].withdrawnPoint, msg.sender);
}
}
8 changes: 2 additions & 6 deletions packages/contracts/deploy/bosagora_devnet/06_ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Amount } from "../../src/utils/Amount";
import { ContractUtils } from "../../src/utils/ContractUtils";
import { Ledger, PhoneLinkCollection, ShopCollection, Token } from "../../typechain-types";
import {
getContractAddress,
getEmailLinkCollectionContractAddress,
getPhoneLinkCollectionContractAddress,
} from "../helpers";
import { getContractAddress, getPhoneLinkCollectionContractAddress } from "../helpers";

import { Wallet } from "ethers";

Expand All @@ -26,7 +22,7 @@ const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { deployer, foundation, settlements, fee, validator1, linkValidator1, linkValidator2, linkValidator3 } =
await getNamedAccounts();

const linkCollectionContractAddress = await getEmailLinkCollectionContractAddress("EmailLinkCollection", hre);
const linkCollectionContractAddress = await getPhoneLinkCollectionContractAddress("PhoneLinkCollection", hre);
const tokenContractAddress = await getContractAddress("Token", hre);
const validatorContractAddress = await getContractAddress("ValidatorCollection", hre);
const currencyRateContractAddress = await getContractAddress("CurrencyRate", hre);
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/deploy/bosagora_mainnet/06_ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeployFunction } from "hardhat-deploy/types";
// tslint:disable-next-line:no-submodule-imports
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Ledger, ShopCollection, Token } from "../../typechain-types";
import { getContractAddress, getEmailLinkCollectionContractAddress } from "../helpers";
import { getContractAddress, getPhoneLinkCollectionContractAddress } from "../helpers";

const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
console.log(`\nDeploying Ledger.`);
Expand All @@ -14,7 +14,7 @@ const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { deploy } = deployments;
const { deployer, foundation, settlements } = await getNamedAccounts();

const linkCollectionContractAddress = await getEmailLinkCollectionContractAddress("EmailLinkCollection", hre);
const linkCollectionContractAddress = await getPhoneLinkCollectionContractAddress("PhoneLinkCollection", hre);
const tokenContractAddress = await getContractAddress("Token", hre);
const validatorContractAddress = await getContractAddress("ValidatorCollection", hre);
const currencyRateContractAddress = await getContractAddress("CurrencyRate", hre);
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/deploy/data/shops.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
{
"shopId": "0x548b7faa282b8721218962e3c1ae43608009534663de91a1548e37cc1c6980a5",
"name": "Shop-000600",
"name": "Shop-0006",
"provideWaitTime": 0,
"providePercent": "5",
"address": "0xA9c5559da87A7511D28e87C751dAfE65374Ce59f",
Expand Down
2 changes: 1 addition & 1 deletion packages/faker/src/data/shops.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
{
"shopId": "0x548b7faa282b8721218962e3c1ae43608009534663de91a1548e37cc1c6980a5",
"name": "Shop-000600",
"name": "Shop-0006",
"provideWaitTime": 0,
"providePercent": "5",
"address": "0xA9c5559da87A7511D28e87C751dAfE65374Ce59f",
Expand Down
6 changes: 5 additions & 1 deletion packages/subgraph/generated/ShopCollection/ShopCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ export class ClosedWithdrawal__Params {
return this._event.parameters[1].value.toBigInt();
}

get total(): BigInt {
return this._event.parameters[2].value.toBigInt();
}

get account(): Address {
return this._event.parameters[2].value.toAddress();
return this._event.parameters[3].value.toAddress();
}
}

Expand Down
54 changes: 41 additions & 13 deletions packages/subgraph/generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,19 @@ export class ShopTradeHistory extends Entity {
this.set("settledPoint", Value.fromBigInt(value));
}

get withdrawnPoint(): BigInt {
let value = this.get("withdrawnPoint");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}

set withdrawnPoint(value: BigInt) {
this.set("withdrawnPoint", Value.fromBigInt(value));
}

get blockNumber(): BigInt {
let value = this.get("blockNumber");
if (!value || value.kind == ValueKind.NULL) {
Expand Down Expand Up @@ -2475,42 +2488,44 @@ export class ShopTradeHistory extends Entity {
}

export class Shop extends Entity {
constructor(id: string) {
constructor(id: Bytes) {
super();
this.set("id", Value.fromString(id));
this.set("id", Value.fromBytes(id));
}

save(): void {
let id = this.get("id");
assert(id != null, "Cannot save Shop entity without an ID");
if (id) {
assert(
id.kind == ValueKind.STRING,
`Entities of type Shop must have an ID of type String but the id '${id.displayData()}' is of type ${id.displayKind()}`
id.kind == ValueKind.BYTES,
`Entities of type Shop must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`
);
store.set("Shop", id.toString(), this);
store.set("Shop", id.toBytes().toHexString(), this);
}
}

static loadInBlock(id: string): Shop | null {
return changetype<Shop | null>(store.get_in_block("Shop", id));
static loadInBlock(id: Bytes): Shop | null {
return changetype<Shop | null>(
store.get_in_block("Shop", id.toHexString())
);
}

static load(id: string): Shop | null {
return changetype<Shop | null>(store.get("Shop", id));
static load(id: Bytes): Shop | null {
return changetype<Shop | null>(store.get("Shop", id.toHexString()));
}

get id(): string {
get id(): Bytes {
let value = this.get("id");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toString();
return value.toBytes();
}
}

set id(value: string) {
this.set("id", Value.fromString(value));
set id(value: Bytes) {
this.set("id", Value.fromBytes(value));
}

get name(): string {
Expand Down Expand Up @@ -2617,6 +2632,19 @@ export class Shop extends Entity {
this.set("settledPoint", Value.fromBigInt(value));
}

get withdrawnPoint(): BigInt {
let value = this.get("withdrawnPoint");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}

set withdrawnPoint(value: BigInt) {
this.set("withdrawnPoint", Value.fromBigInt(value));
}

get blockNumber(): BigInt {
let value = this.get("blockNumber");
if (!value || value.kind == ValueKind.NULL) {
Expand Down
2 changes: 1 addition & 1 deletion packages/subgraph/manifest/subgraph.placeholder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dataSources:
eventHandlers:
- event: AddedShop(bytes32,string,uint256,uint256,address)
handler: handleAddedShop
- event: ClosedWithdrawal(bytes32,uint256,address)
- event: ClosedWithdrawal(bytes32,uint256,uint256,address)
handler: handleClosedWithdrawal
- event: IncreasedProvidedPoint(bytes32,uint256,uint256,string)
handler: handleIncreasedProvidedPoint
Expand Down
8 changes: 6 additions & 2 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ type UserBalance @entity {
enum ShopAction {
ProvidedPoint,
UsedPoint,
SettledPoint
SettledPoint,
OpenWithdrawnPoint,
CloseWithdrawnPoint,
}

type ShopTradeHistory @entity(immutable: true) {
Expand All @@ -219,6 +221,7 @@ type ShopTradeHistory @entity(immutable: true) {
providedPoint: BigInt!
usedPoint: BigInt!
settledPoint: BigInt!
withdrawnPoint: BigInt!

blockNumber: BigInt!
blockTimestamp: BigInt!
Expand All @@ -232,7 +235,7 @@ enum ShopDataAction {
}

type Shop @entity {
id: ID! # shopId
id: Bytes! # shopId

name: String! # string
provideWaitTime: BigInt! # uint256
Expand All @@ -243,6 +246,7 @@ type Shop @entity {
providedPoint: BigInt!
usedPoint: BigInt!
settledPoint: BigInt!
withdrawnPoint: BigInt!

blockNumber: BigInt!
blockTimestamp: BigInt!
Expand Down
83 changes: 77 additions & 6 deletions packages/subgraph/src/shop-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
IncreasedUsedPoint as IncreasedUsedPointEvent,
RemovedShop as RemovedShopEvent,
UpdatedShop as UpdatedShopEvent,
ClosedWithdrawal as ClosedWithdrawalEvent,
OpenedWithdrawal as OpenedWithdrawalEvent,
} from "../generated/ShopCollection/ShopCollection";
import { Shop, ShopTradeHistory } from "../generated/schema";
import { BigInt } from "@graphprotocol/graph-ts";
import { AmountUnit } from "./utils";

export function handleAddedShop(event: AddedShopEvent): void {
let entity = new Shop(event.params.shopId.toString());
let entity = new Shop(event.params.shopId);

entity.name = event.params.name;
entity.provideWaitTime = event.params.provideWaitTime;
Expand All @@ -22,6 +24,7 @@ export function handleAddedShop(event: AddedShopEvent): void {
entity.providedPoint = BigInt.fromI32(0);
entity.usedPoint = BigInt.fromI32(0);
entity.settledPoint = BigInt.fromI32(0);
entity.withdrawnPoint = BigInt.fromI32(0);

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
Expand All @@ -31,7 +34,7 @@ export function handleAddedShop(event: AddedShopEvent): void {
}

export function handleRemovedShop(event: RemovedShopEvent): void {
let shopEntity = Shop.load(event.params.shopId.toString());
let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
shopEntity.action = "Remove";

Expand All @@ -43,7 +46,7 @@ export function handleRemovedShop(event: RemovedShopEvent): void {
}

export function handleUpdatedShop(event: UpdatedShopEvent): void {
let shopEntity = Shop.load(event.params.shopId.toString());
let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
shopEntity.name = event.params.name;
shopEntity.provideWaitTime = event.params.provideWaitTime;
Expand All @@ -65,10 +68,11 @@ export function handleIncreasedSettledPoint(event: IncreasedSettledPointEvent):
entity.increase = event.params.increase.div(AmountUnit);
entity.settledPoint = event.params.total.div(AmountUnit);

let shopEntity = Shop.load(event.params.shopId.toString());
let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
entity.providedPoint = shopEntity.providedPoint;
entity.usedPoint = shopEntity.usedPoint;
entity.withdrawnPoint = shopEntity.withdrawnPoint;
shopEntity.settledPoint = entity.settledPoint;
shopEntity.blockNumber = event.block.number;
shopEntity.blockTimestamp = event.block.timestamp;
Expand All @@ -77,6 +81,7 @@ export function handleIncreasedSettledPoint(event: IncreasedSettledPointEvent):
} else {
entity.providedPoint = BigInt.fromI32(0);
entity.usedPoint = BigInt.fromI32(0);
entity.withdrawnPoint = BigInt.fromI32(0);
}

entity.blockNumber = event.block.number;
Expand All @@ -94,10 +99,11 @@ export function handleIncreasedProvidedPoint(event: IncreasedProvidedPointEvent)
entity.increase = event.params.increase.div(AmountUnit);
entity.providedPoint = event.params.total.div(AmountUnit);

let shopEntity = Shop.load(event.params.shopId.toString());
let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
entity.usedPoint = shopEntity.usedPoint;
entity.settledPoint = shopEntity.settledPoint;
entity.withdrawnPoint = shopEntity.withdrawnPoint;
shopEntity.providedPoint = entity.providedPoint;
shopEntity.blockNumber = event.block.number;
shopEntity.blockTimestamp = event.block.timestamp;
Expand All @@ -106,6 +112,7 @@ export function handleIncreasedProvidedPoint(event: IncreasedProvidedPointEvent)
} else {
entity.usedPoint = BigInt.fromI32(0);
entity.settledPoint = BigInt.fromI32(0);
entity.withdrawnPoint = BigInt.fromI32(0);
}

entity.blockNumber = event.block.number;
Expand All @@ -123,10 +130,11 @@ export function handleIncreasedUsedPoint(event: IncreasedUsedPointEvent): void {
entity.increase = event.params.increase.div(AmountUnit);
entity.usedPoint = event.params.total.div(AmountUnit);

let shopEntity = Shop.load(event.params.shopId.toString());
let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
entity.providedPoint = shopEntity.providedPoint;
entity.settledPoint = shopEntity.settledPoint;
entity.withdrawnPoint = shopEntity.withdrawnPoint;
shopEntity.usedPoint = entity.usedPoint;
shopEntity.blockNumber = event.block.number;
shopEntity.blockTimestamp = event.block.timestamp;
Expand All @@ -135,6 +143,69 @@ export function handleIncreasedUsedPoint(event: IncreasedUsedPointEvent): void {
} else {
entity.providedPoint = BigInt.fromI32(0);
entity.settledPoint = BigInt.fromI32(0);
entity.withdrawnPoint = BigInt.fromI32(0);
}

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

export function handleOpenedWithdrawal(event: OpenedWithdrawalEvent): void {
let entity = new ShopTradeHistory(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.action = "OpenWithdrawnPoint";
entity.shopId = event.params.shopId;
entity.purchaseId = "";
entity.increase = event.params.amount.div(AmountUnit);

let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
entity.providedPoint = shopEntity.providedPoint;
entity.usedPoint = shopEntity.usedPoint;
entity.settledPoint = shopEntity.settledPoint;
entity.withdrawnPoint = shopEntity.withdrawnPoint;
shopEntity.blockNumber = event.block.number;
shopEntity.blockTimestamp = event.block.timestamp;
shopEntity.transactionHash = event.transaction.hash;
shopEntity.save();
} else {
entity.providedPoint = BigInt.fromI32(0);
entity.settledPoint = BigInt.fromI32(0);
entity.usedPoint = BigInt.fromI32(0);
entity.withdrawnPoint = BigInt.fromI32(0);
}

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

export function handleClosedWithdrawal(event: ClosedWithdrawalEvent): void {
let entity = new ShopTradeHistory(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.action = "CloseWithdrawnPoint";
entity.shopId = event.params.shopId;
entity.purchaseId = "";
entity.increase = event.params.amount.div(AmountUnit);
entity.withdrawnPoint = event.params.total.div(AmountUnit);

let shopEntity = Shop.load(event.params.shopId);
if (shopEntity !== null) {
entity.providedPoint = shopEntity.providedPoint;
entity.settledPoint = shopEntity.settledPoint;
entity.usedPoint = shopEntity.usedPoint;
shopEntity.withdrawnPoint = entity.withdrawnPoint;
shopEntity.blockNumber = event.block.number;
shopEntity.blockTimestamp = event.block.timestamp;
shopEntity.transactionHash = event.transaction.hash;
shopEntity.save();
} else {
entity.providedPoint = BigInt.fromI32(0);
entity.settledPoint = BigInt.fromI32(0);
entity.usedPoint = BigInt.fromI32(0);
}

entity.blockNumber = event.block.number;
Expand Down

0 comments on commit ade21bb

Please sign in to comment.