Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Relay] Network provider must be specified during callback #381

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/relay/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ LOYALTY_CONSUMER_CONTRACT_ADDRESS=0xb9CE93eeb00a74Deeec319cBCD37938b5aAE6314
LOYALTY_EXCHANGER_CONTRACT_ADDRESS=0x1ae7b2564Ae7a7Fb1E7976F3F71cAF43ef49ac6F
LOYALTY_TRANSFER_CONTRACT_ADDRESS=0xcAE5A9f266991dcEdc62bb9291f15E112f212820
SIDE_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS=0xd73e6a2f2e47236F1Ff737E72497f598652122F9
SIDE_CHAIN_BRIDGE_CONTRACT_ADDRESS=0xd73e6a2f2e47236F1Ff737E72497f598652122F9
SIDE_CHAIN_BRIDGE_CONTRACT_ADDRESS=0x02aAcAC866385A86ddA9a3C1FB7923c4A0433B31

MAIN_CHAIN_TOKEN_CONTRACT_ADDRESS=0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022
MAIN_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS=0xd73e6a2f2e47236F1Ff737E72497f598652122F9
MAIN_CHAIN_BRIDGE_CONTRACT_ADDRESS=0xd73e6a2f2e47236F1Ff737E72497f598652122F9
MAIN_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS=0x4Ca91738C7cD24895467c6d550D96BE8dC4b33AA
MAIN_CHAIN_BRIDGE_CONTRACT_ADDRESS=0x979a62090BDCff36F2e140F6915fbAdA7510cb6a

REPORT_GAS=true
16 changes: 16 additions & 0 deletions packages/relay/scripts/balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BOACoin } from "../src/common/Amount";

import "@nomiclabs/hardhat-ethers";
import * as hre from "hardhat";

async function main() {
const provider = hre.ethers.provider;
const account = "0x9E8549cc1B5b9036AC410Ed11966BB3c6B94A77d";
const balance = await provider.getBalance(account);
console.log(`${account} : ${new BOACoin(balance).toBOAString()}`);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
2 changes: 1 addition & 1 deletion packages/relay/src/routers/BridgeRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class BridgeRouter {
return res.status(200).json(ResponseMessage.getErrorMessage("2001", { validation: errors.array() }));
}

const signerItem = await this.getRelaySigner();
const signerItem = await this.getRelaySigner(this.contractManager.sideChainProvider);
try {
const account: string = String(req.body.account).trim();
const amount: BigNumber = BigNumber.from(req.body.amount);
Expand Down
2 changes: 2 additions & 0 deletions packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class DefaultRouter {
}

private async callback(req: express.Request, res: express.Response) {
console.log("----- CALL BACK -----");
console.log(JSON.stringify(req.body));
console.log("----- CALL BACK -----");
res.status(200).json(this.makeResponseData(0, { message: "OK" }, undefined));
}

Expand Down
12 changes: 9 additions & 3 deletions packages/relay/src/routers/ShopRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import * as hre from "hardhat";

// tslint:disable-next-line:no-implicit-dependencies
import { AddressZero } from "@ethersproject/constants";
import { Metrics } from "../metrics/Metrics";
import { ContractManager } from "../contract/ContractManager";
import { Metrics } from "../metrics/Metrics";

export class ShopRouter {
private web_service: WebService;
Expand Down Expand Up @@ -608,7 +608,9 @@ export class ShopRouter {

if (mobileData !== undefined) {
/// 상점주에게 메세지 발송
let title, shopLabel, currencyLabel: string;
let title: string;
let shopLabel: string;
let currencyLabel: string;
if (mobileData.language === "kr") {
title = "상점 정보 변경 요청";
shopLabel = "상점이름";
Expand Down Expand Up @@ -873,7 +875,11 @@ export class ShopRouter {

if (mobileData !== undefined) {
/// 사용자에게 메세지 statusLabel
let title, shopLabel, statusLabel, activeLabel, inactiveLabel: string;
let title: string;
let shopLabel: string;
let statusLabel: string;
let activeLabel: string;
let inactiveLabel: string;
if (mobileData.language === "kr") {
title = "상점 상태 변경 요청";
shopLabel = "상점이름";
Expand Down
115 changes: 56 additions & 59 deletions packages/relay/src/scheduler/WatchScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Scheduler } from "./Scheduler";

// tslint:disable-next-line:no-implicit-dependencies
import { ContractTransaction } from "@ethersproject/contracts";
import { BigNumber } from "ethers";
import { BigNumber, ethers } from "ethers";

/**
* Creates blocks at regular intervals and stores them in IPFS and databases.
Expand Down Expand Up @@ -71,8 +71,9 @@ export class WatchScheduler extends Scheduler {
}
}

private async getRelaySigner(): Promise<ISignerItem> {
return this.signers.getSigner();
private async getRelaySigner(provider?: ethers.providers.Provider): Promise<ISignerItem> {
if (provider === undefined) provider = this.contractManager.sideChainProvider;
return this.signers.getSigner(provider);
}

private releaseRelaySigner(signer: ISignerItem) {
Expand Down Expand Up @@ -122,36 +123,34 @@ export class WatchScheduler extends Scheduler {
const contract = this.contractManager.sideLoyaltyConsumerContract;
const signerItem = await this.getRelaySigner();
try {
if (signerItem.signer.provider) {
const contractTx = (await signerItem.signer.provider.getTransaction(
payment.openNewTxId
)) as ContractTransaction;
const event = await this.waitPaymentLoyalty(contract, contractTx);
if (event !== undefined) {
this.updateEvent(event, payment);
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_NEW_CONFIRMED_TX;
await this.storage.updatePayment(payment);
}
const contractTx = (await this.contractManager.sideChainProvider.getTransaction(
payment.openNewTxId
)) as ContractTransaction;
const event = await this.waitPaymentLoyalty(contract, contractTx);
if (event !== undefined) {
this.updateEvent(event, payment);
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_NEW_CONFIRMED_TX;
await this.storage.updatePayment(payment);
}

await this.sendPaymentResult(
TaskResultType.NEW,
TaskResultCode.SUCCESS,
"Success",
this.getCallBackResponse(payment)
);
await this.sendPaymentResult(
TaskResultType.NEW,
TaskResultCode.SUCCESS,
"Success",
this.getCallBackResponse(payment)
);

if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.REPLY_COMPLETED_NEW;
await this.storage.updatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
} else {
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_NEW_REVERTED_TX;
await this.storage.forcedUpdatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.REPLY_COMPLETED_NEW;
await this.storage.updatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
} else {
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_NEW_REVERTED_TX;
await this.storage.forcedUpdatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
}
} catch (error) {
Expand All @@ -167,36 +166,34 @@ export class WatchScheduler extends Scheduler {
const contract = this.contractManager.sideLoyaltyConsumerContract;
const signerItem = await this.getRelaySigner();
try {
if (signerItem.signer.provider) {
const contractTx = (await signerItem.signer.provider.getTransaction(
payment.openCancelTxId
)) as ContractTransaction;
const event = await this.waitPaymentLoyalty(contract, contractTx);
if (event !== undefined) {
this.updateEvent(event, payment);
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_CANCEL_CONFIRMED_TX;
await this.storage.updatePayment(payment);
}
const contractTx = (await this.contractManager.sideChainProvider.getTransaction(
payment.openCancelTxId
)) as ContractTransaction;
const event = await this.waitPaymentLoyalty(contract, contractTx);
if (event !== undefined) {
this.updateEvent(event, payment);
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_CANCEL_CONFIRMED_TX;
await this.storage.updatePayment(payment);
}

await this.sendPaymentResult(
TaskResultType.CANCEL,
TaskResultCode.SUCCESS,
"Success",
this.getCallBackResponse(payment)
);
await this.sendPaymentResult(
TaskResultType.CANCEL,
TaskResultCode.SUCCESS,
"Success",
this.getCallBackResponse(payment)
);

if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.REPLY_COMPLETED_CANCEL;
await this.storage.updatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
} else {
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_CANCEL_REVERTED_TX;
await this.storage.forcedUpdatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.REPLY_COMPLETED_CANCEL;
await this.storage.updatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
} else {
const item = await this.storage.getPayment(payment.paymentId);
if (item !== undefined && item.paymentStatus === LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX) {
payment.paymentStatus = LoyaltyPaymentTaskStatus.APPROVED_CANCEL_REVERTED_TX;
await this.storage.forcedUpdatePaymentStatus(payment.paymentId, payment.paymentStatus);
}
}
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions packages/relay/test/helper/FakerCallbackServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export class FakerCallbackServer {
}

private async callback(req: express.Request, res: express.Response) {
console.log("----- CALL BACK -----");
console.log(JSON.stringify(req.body));
console.log("----- CALL BACK -----");
this.responseData.push(req.body);
res.status(200).json(this.makeResponseData(0, { message: "OK" }, undefined));
}
Expand Down
Loading