diff --git a/packages/contracts/contracts/ShopCollection.sol b/packages/contracts/contracts/ShopCollection.sol index c450bfec..db3379b5 100644 --- a/packages/contracts/contracts/ShopCollection.sol +++ b/packages/contracts/contracts/ShopCollection.sol @@ -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; @@ -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); } } diff --git a/packages/contracts/deploy/bosagora_devnet/06_ledger.ts b/packages/contracts/deploy/bosagora_devnet/06_ledger.ts index 3763c3bb..fabcae2a 100644 --- a/packages/contracts/deploy/bosagora_devnet/06_ledger.ts +++ b/packages/contracts/deploy/bosagora_devnet/06_ledger.ts @@ -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"; @@ -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); diff --git a/packages/contracts/deploy/bosagora_mainnet/06_ledger.ts b/packages/contracts/deploy/bosagora_mainnet/06_ledger.ts index 0157ef4d..f693df34 100644 --- a/packages/contracts/deploy/bosagora_mainnet/06_ledger.ts +++ b/packages/contracts/deploy/bosagora_mainnet/06_ledger.ts @@ -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.`); @@ -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); diff --git a/packages/contracts/deploy/data/shops.json b/packages/contracts/deploy/data/shops.json index f89852af..858999ba 100644 --- a/packages/contracts/deploy/data/shops.json +++ b/packages/contracts/deploy/data/shops.json @@ -41,7 +41,7 @@ }, { "shopId": "0x548b7faa282b8721218962e3c1ae43608009534663de91a1548e37cc1c6980a5", - "name": "Shop-000600", + "name": "Shop-0006", "provideWaitTime": 0, "providePercent": "5", "address": "0xA9c5559da87A7511D28e87C751dAfE65374Ce59f", diff --git a/packages/faker/src/data/shops.json b/packages/faker/src/data/shops.json index f89852af..858999ba 100644 --- a/packages/faker/src/data/shops.json +++ b/packages/faker/src/data/shops.json @@ -41,7 +41,7 @@ }, { "shopId": "0x548b7faa282b8721218962e3c1ae43608009534663de91a1548e37cc1c6980a5", - "name": "Shop-000600", + "name": "Shop-0006", "provideWaitTime": 0, "providePercent": "5", "address": "0xA9c5559da87A7511D28e87C751dAfE65374Ce59f", diff --git a/packages/subgraph/generated/ShopCollection/ShopCollection.ts b/packages/subgraph/generated/ShopCollection/ShopCollection.ts index 37196135..9db06950 100644 --- a/packages/subgraph/generated/ShopCollection/ShopCollection.ts +++ b/packages/subgraph/generated/ShopCollection/ShopCollection.ts @@ -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(); } } diff --git a/packages/subgraph/generated/schema.ts b/packages/subgraph/generated/schema.ts index d82aea10..61c5ea56 100644 --- a/packages/subgraph/generated/schema.ts +++ b/packages/subgraph/generated/schema.ts @@ -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) { @@ -2475,9 +2488,9 @@ 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 { @@ -2485,32 +2498,34 @@ export class Shop extends Entity { 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(store.get_in_block("Shop", id)); + static loadInBlock(id: Bytes): Shop | null { + return changetype( + store.get_in_block("Shop", id.toHexString()) + ); } - static load(id: string): Shop | null { - return changetype(store.get("Shop", id)); + static load(id: Bytes): Shop | null { + return changetype(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 { @@ -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) { diff --git a/packages/subgraph/manifest/subgraph.placeholder.yaml b/packages/subgraph/manifest/subgraph.placeholder.yaml index 81c09abe..f5a6e3f3 100644 --- a/packages/subgraph/manifest/subgraph.placeholder.yaml +++ b/packages/subgraph/manifest/subgraph.placeholder.yaml @@ -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 diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index c170232b..b72cd3b4 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -204,7 +204,9 @@ type UserBalance @entity { enum ShopAction { ProvidedPoint, UsedPoint, - SettledPoint + SettledPoint, + OpenWithdrawnPoint, + CloseWithdrawnPoint, } type ShopTradeHistory @entity(immutable: true) { @@ -219,6 +221,7 @@ type ShopTradeHistory @entity(immutable: true) { providedPoint: BigInt! usedPoint: BigInt! settledPoint: BigInt! + withdrawnPoint: BigInt! blockNumber: BigInt! blockTimestamp: BigInt! @@ -232,7 +235,7 @@ enum ShopDataAction { } type Shop @entity { - id: ID! # shopId + id: Bytes! # shopId name: String! # string provideWaitTime: BigInt! # uint256 @@ -243,6 +246,7 @@ type Shop @entity { providedPoint: BigInt! usedPoint: BigInt! settledPoint: BigInt! + withdrawnPoint: BigInt! blockNumber: BigInt! blockTimestamp: BigInt! diff --git a/packages/subgraph/src/shop-collection.ts b/packages/subgraph/src/shop-collection.ts index 7a923ddb..2ace4da5 100644 --- a/packages/subgraph/src/shop-collection.ts +++ b/packages/subgraph/src/shop-collection.ts @@ -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; @@ -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; @@ -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"; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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;