From 81b07297cd5bc5e3e020c7ac1708e8767ff9e8b8 Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 21 May 2024 15:55:26 +0900 Subject: [PATCH] Update test code of dms-relay --- packages/relay/package.json | 1 + packages/relay/test/Approval.test.ts | 61 +- packages/relay/test/DelegatorApproval.test.ts | 58 +- packages/relay/test/ETC.test.ts | 6 +- packages/relay/test/Endpoints.test.ts | 34 - packages/relay/test/ForcedClose.test.ts | 111 +- packages/relay/test/LoyaltyBridge.test.ts | 23 - packages/relay/test/LoyaltyExchanger.test.ts | 240 ++++ packages/relay/test/LoyaltyTransfer.test.ts | 23 - packages/relay/test/Payment.test.ts | 1103 +---------------- packages/relay/test/ShopWithdraw.test.ts | 113 +- packages/relay/test/TempararyAccount.test.ts | 15 +- 12 files changed, 408 insertions(+), 1380 deletions(-) create mode 100644 packages/relay/test/LoyaltyExchanger.test.ts diff --git a/packages/relay/package.json b/packages/relay/package.json index 1882d44c..0c24f121 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -25,6 +25,7 @@ "test:DelegatorApproval": "TESTING=true hardhat test test/DelegatorApproval.test.ts", "test:LoyaltyTransfer": "TESTING=true hardhat test test/LoyaltyTransfer.test.ts", "test:LoyaltyBridge": "TESTING=true hardhat test test/LoyaltyBridge.test.ts", + "test:LoyaltyExchanger": "TESTING=true hardhat test test/LoyaltyExchanger.test.ts", "test:Bridge": "TESTING=true hardhat test test/Bridge.test.ts", "test:DelegatedTransfer": "TESTING=true hardhat test test/DelegatedTransfer.test.ts" }, diff --git a/packages/relay/test/Approval.test.ts b/packages/relay/test/Approval.test.ts index 1f963221..4d9803a1 100644 --- a/packages/relay/test/Approval.test.ts +++ b/packages/relay/test/Approval.test.ts @@ -9,7 +9,6 @@ import { WatchScheduler } from "../src/scheduler/WatchScheduler"; import { GraphStorage } from "../src/storage/GraphStorage"; import { RelayStorage } from "../src/storage/RelayStorage"; import { - ContractLoyaltyType, ContractShopStatus, IShopData, IUserData, @@ -202,42 +201,32 @@ describe("Test of Server", function () { await fakerCallbackServer.stop(); }); - it("Transfer token", async () => { - for (const account of userData) { - await tokenContract.connect(deployments.accounts.owner).transfer(account.address, amount.value); - } - - for (const account of shopData) { - await tokenContract.connect(deployments.accounts.owner).transfer(account.address, amount.value); - } - }); - - it("Change loyalty type", async () => { - for (const user of userData) { - const nonce = await ledgerContract.nonceOf(user.address); - const signature = await ContractUtils.signLoyaltyType( - new Wallet(user.privateKey), - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: user.address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[0].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; + }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); - it("Deposit token", async () => { - const depositAmount = ContractUtils.zeroGWEI(amount.value.div(2)); for (const user of userData) { - const sender = new Wallet(user.privateKey, ethers.provider); - await tokenContract.connect(sender).approve(ledgerContract.address, depositAmount); - await ledgerContract.connect(sender).deposit(depositAmount); + expect(await ledgerContract.pointBalanceOf(user.address)).to.equal(loyaltyAmount); } }); @@ -269,7 +258,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_NEW); paymentId = response.data.data.paymentId; @@ -322,7 +310,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchase.purchaseId); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); diff --git a/packages/relay/test/DelegatorApproval.test.ts b/packages/relay/test/DelegatorApproval.test.ts index ee0b23fe..d35daa5c 100644 --- a/packages/relay/test/DelegatorApproval.test.ts +++ b/packages/relay/test/DelegatorApproval.test.ts @@ -8,13 +8,7 @@ import { Scheduler } from "../src/scheduler/Scheduler"; import { WatchScheduler } from "../src/scheduler/WatchScheduler"; import { GraphStorage } from "../src/storage/GraphStorage"; import { RelayStorage } from "../src/storage/RelayStorage"; -import { - ContractLoyaltyType, - ContractShopStatus, - LoyaltyPaymentTaskStatus, - ShopTaskStatus, - TaskResultType, -} from "../src/types"; +import { ContractShopStatus, LoyaltyPaymentTaskStatus, ShopTaskStatus, TaskResultType } from "../src/types"; import { ContractUtils, LoyaltyNetworkID } from "../src/utils/ContractUtils"; import { BIP20DelegatedTransfer, @@ -202,32 +196,32 @@ describe("Test of Delegator", function () { await fakerCallbackServer.stop(); }); - it("Change loyalty type", async () => { - for (const user of userData) { - const nonce = await ledgerContract.nonceOf(user.address); - const signature = await ContractUtils.signLoyaltyType( - new Wallet(user.privateKey), - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: user.address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[0].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; + }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); - it("Deposit token", async () => { - const depositAmount = ContractUtils.zeroGWEI(amount.value.div(2)); for (const user of userData) { - const sender = new Wallet(user.privateKey, ethers.provider); - await tokenContract.connect(sender).approve(ledgerContract.address, depositAmount); - await ledgerContract.connect(sender).deposit(depositAmount); + expect(await ledgerContract.pointBalanceOf(user.address)).to.equal(loyaltyAmount); } }); @@ -308,7 +302,6 @@ describe("Test of Delegator", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_NEW); paymentId = response.data.data.paymentId; @@ -398,7 +391,6 @@ describe("Test of Delegator", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchase.purchaseId); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); diff --git a/packages/relay/test/ETC.test.ts b/packages/relay/test/ETC.test.ts index b1e97516..494e9b56 100644 --- a/packages/relay/test/ETC.test.ts +++ b/packages/relay/test/ETC.test.ts @@ -83,9 +83,9 @@ describe("Test for ETC", function () { storage = await RelayStorage.make(config.database); const graph_sidechain = await GraphStorage.make(config.graph_sidechain); const graph_mainchain = await GraphStorage.make(config.graph_mainchain); - const contractManager = new ContractManager(config); - await contractManager.attach(); - server = new TestServer(config, contractManager, storage, graph_sidechain, graph_mainchain); + const contractManager2 = new ContractManager(config); + await contractManager2.attach(); + server = new TestServer(config, contractManager2, storage, graph_sidechain, graph_mainchain); }); before("Start TestServer", async () => { diff --git a/packages/relay/test/Endpoints.test.ts b/packages/relay/test/Endpoints.test.ts index f2c3d087..e50ebb86 100644 --- a/packages/relay/test/Endpoints.test.ts +++ b/packages/relay/test/Endpoints.test.ts @@ -206,40 +206,6 @@ describe("Test of Server", function () { await storage.dropTestDB(); }); - context("Change loyalty type", () => { - it("Check loyalty type - before", async () => { - const userIndex = 0; - const loyaltyType = await ledgerContract.loyaltyTypeOf(deployments.accounts.users[userIndex].address); - expect(loyaltyType).to.equal(0); - }); - - it("Send loyalty type", async () => { - const userIndex = 0; - const nonce = await ledgerContract.nonceOf(deployments.accounts.users[userIndex].address); - const signature = await ContractUtils.signLoyaltyType( - deployments.accounts.users[userIndex], - nonce, - contractManager.sideChainId - ); - const uri = URI(serverURL).directory("/v1/ledger/changeToLoyaltyToken"); - const url = uri.toString(); - const response = await client.post(url, { - account: deployments.accounts.users[userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - }); - - it("Check point type - after", async () => { - const userIndex = 0; - const loyaltyType = await ledgerContract.loyaltyTypeOf(deployments.accounts.users[userIndex].address); - expect(loyaltyType).to.equal(1); - }); - }); - context("Nonce", () => { it("Get Nonce of Ledger", async () => { const userIndex = 0; diff --git a/packages/relay/test/ForcedClose.test.ts b/packages/relay/test/ForcedClose.test.ts index 7c115669..acef4dca 100644 --- a/packages/relay/test/ForcedClose.test.ts +++ b/packages/relay/test/ForcedClose.test.ts @@ -10,7 +10,7 @@ import { Scheduler } from "../src/scheduler/Scheduler"; import { WatchScheduler } from "../src/scheduler/WatchScheduler"; import { GraphStorage } from "../src/storage/GraphStorage"; import { RelayStorage } from "../src/storage/RelayStorage"; -import { ContractLoyaltyType, IShopData, IUserData, LoyaltyPaymentTaskStatus } from "../src/types"; +import { IShopData, IUserData, LoyaltyPaymentTaskStatus } from "../src/types"; import { ContractUtils } from "../src/utils/ContractUtils"; import { BIP20DelegatedTransfer, @@ -205,32 +205,32 @@ describe("Test of Server", function () { } }); - it("Change loyalty type", async () => { - for (const user of userData) { - const nonce = await ledgerContract.nonceOf(user.address); - const signature = await ContractUtils.signLoyaltyType( - new Wallet(user.privateKey), - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: user.address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[0].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; + }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); - it("Deposit token", async () => { - const depositAmount = ContractUtils.zeroGWEI(amount.value.div(2)); for (const user of userData) { - const sender = new Wallet(user.privateKey, ethers.provider); - await tokenContract.connect(sender).approve(ledgerContract.address, depositAmount); - await ledgerContract.connect(sender).deposit(depositAmount); + expect(await ledgerContract.pointBalanceOf(user.address)).to.equal(loyaltyAmount); } }); @@ -262,7 +262,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_NEW); paymentId = response.data.data.paymentId; @@ -399,42 +398,32 @@ describe("Test of Server", function () { await fakerCallbackServer.stop(); }); - it("Transfer token", async () => { - for (const account of userData) { - await tokenContract.connect(deployments.accounts.owner).transfer(account.address, amount.value); - } - - for (const account of shopData) { - await tokenContract.connect(deployments.accounts.owner).transfer(account.address, amount.value); - } - }); - - it("Change loyalty type", async () => { - for (const user of userData) { - const nonce = await ledgerContract.nonceOf(user.address); - const signature = await ContractUtils.signLoyaltyType( - new Wallet(user.privateKey), - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: user.address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[0].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; + }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); - it("Deposit token", async () => { - const depositAmount = ContractUtils.zeroGWEI(amount.value.div(2)); for (const user of userData) { - const sender = new Wallet(user.privateKey, ethers.provider); - await tokenContract.connect(sender).approve(ledgerContract.address, depositAmount); - await ledgerContract.connect(sender).deposit(depositAmount); + assert.deepStrictEqual(await ledgerContract.pointBalanceOf(user.address), loyaltyAmount); } }); @@ -466,7 +455,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_NEW); paymentId = response.data.data.paymentId; @@ -514,7 +502,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchase.purchaseId); assert.deepStrictEqual(response.data.data.account, userData[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); diff --git a/packages/relay/test/LoyaltyBridge.test.ts b/packages/relay/test/LoyaltyBridge.test.ts index 0f785dcc..1696d324 100644 --- a/packages/relay/test/LoyaltyBridge.test.ts +++ b/packages/relay/test/LoyaltyBridge.test.ts @@ -95,29 +95,6 @@ describe("Test of LoyaltyBridge", function () { await storage.dropTestDB(); }); - it("Change loyalty type to 1", async () => { - for (const userIndex of [0, 1]) { - const nonce = await contractManager.sideLedgerContract.nonceOf( - deployments.accounts.users[userIndex].address - ); - const signature = await ContractUtils.signLoyaltyType( - deployments.accounts.users[userIndex], - nonce, - contractManager.sideChainId - ); - const uri = URI(serverURL).directory("/v1/ledger/changeToLoyaltyToken"); - const url = uri.toString(); - const response = await client.post(url, { - account: deployments.accounts.users[userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); - // 메인체인에서 원장 컨트랙트으로 입금 it("Deposit token by Bridge", async () => { const tokenId = ContractUtils.getTokenId( diff --git a/packages/relay/test/LoyaltyExchanger.test.ts b/packages/relay/test/LoyaltyExchanger.test.ts new file mode 100644 index 00000000..779738bc --- /dev/null +++ b/packages/relay/test/LoyaltyExchanger.test.ts @@ -0,0 +1,240 @@ +import "@nomiclabs/hardhat-ethers"; +import "@nomiclabs/hardhat-waffle"; + +import { Amount } from "../src/common/Amount"; +import { Config } from "../src/common/Config"; +import { ContractManager } from "../src/contract/ContractManager"; +import { GraphStorage } from "../src/storage/GraphStorage"; +import { RelayStorage } from "../src/storage/RelayStorage"; +import { IShopData, IUserData } from "../src/types"; +import { ContractUtils } from "../src/utils/ContractUtils"; +import { + BIP20DelegatedTransfer, + CurrencyRate, + Ledger, + LoyaltyConsumer, + LoyaltyExchanger, + LoyaltyProvider, + LoyaltyTransfer, + PhoneLinkCollection, + Shop, + Validator, +} from "../typechain-types"; +import { Deployments } from "./helper/Deployments"; +import { FakerCallbackServer } from "./helper/FakerCallbackServer"; +import { getPurchaseId, TestClient, TestServer } from "./helper/Utility"; + +import chai, { expect } from "chai"; +import { solidity } from "ethereum-waffle"; + +import fs from "fs"; +import * as path from "path"; +import { URL } from "url"; + +import { Wallet } from "ethers"; +import { ethers } from "hardhat"; + +// tslint:disable-next-line:no-var-requires +const URI = require("urijs"); + +chai.use(solidity); + +describe("Test of LoyaltyExchanger", function () { + this.timeout(1000 * 60 * 5); + const config = new Config(); + config.readFromFile(path.resolve(process.cwd(), "config", "config_test.yaml")); + const contractManager = new ContractManager(config); + const deployments = new Deployments(config); + + let validatorContract: Validator; + let tokenContract: BIP20DelegatedTransfer; + let linkContract: PhoneLinkCollection; + let currencyRateContract: CurrencyRate; + let shopContract: Shop; + let consumerContract: LoyaltyConsumer; + let providerContract: LoyaltyProvider; + let exchangerContract: LoyaltyExchanger; + let ledgerContract: Ledger; + + let client: TestClient; + let storage: RelayStorage; + let server: TestServer; + let serverURL: URL; + + let fakerCallbackServer: FakerCallbackServer; + + const userData: IUserData[] = []; + const shopData: IShopData[] = []; + + interface IPurchaseData { + purchaseId: string; + amount: number; + providePercent: number; + currency: string; + userIndex: number; + shopIndex: number; + } + + context("Test auto approval", () => { + before("Load User & Shop", async () => { + const users = JSON.parse(fs.readFileSync("./src/data/users.json", "utf8")) as IUserData[]; + const userIdx = Math.floor(Math.random() * users.length); + userData.push(users[userIdx]); + + const shops = JSON.parse(fs.readFileSync("./src/data/shops.json", "utf8")) as IShopData[]; + const shopIdx = Math.floor(Math.random() * shops.length); + shopData.push(shops[shopIdx]); + }); + + before("Transfer native token", async () => { + for (const user of userData) { + await deployments.accounts.deployer.sendTransaction({ + to: user.address, + value: Amount.make("100").value, + }); + } + for (const shop of shopData) { + await deployments.accounts.deployer.sendTransaction({ + to: shop.address, + value: Amount.make("100").value, + }); + } + }); + + before("Deploy", async () => { + deployments.setShopData( + shopData.map((m) => { + return { + shopId: m.shopId, + name: m.name, + currency: m.currency, + wallet: new Wallet(m.privateKey, ethers.provider), + }; + }) + ); + await deployments.doDeploy(); + + validatorContract = deployments.getContract("Validator") as Validator; + tokenContract = deployments.getContract("TestLYT") as BIP20DelegatedTransfer; + ledgerContract = deployments.getContract("Ledger") as Ledger; + linkContract = deployments.getContract("PhoneLinkCollection") as PhoneLinkCollection; + consumerContract = deployments.getContract("LoyaltyConsumer") as LoyaltyConsumer; + providerContract = deployments.getContract("LoyaltyProvider") as LoyaltyProvider; + exchangerContract = deployments.getContract("LoyaltyExchanger") as LoyaltyExchanger; + currencyRateContract = deployments.getContract("CurrencyRate") as CurrencyRate; + shopContract = deployments.getContract("Shop") as Shop; + }); + + before("Create Config", async () => { + config.contracts.sideChain.tokenAddress = deployments.getContractAddress("TestLYT") || ""; + config.contracts.sideChain.currencyRateAddress = deployments.getContractAddress("CurrencyRate") || ""; + config.contracts.sideChain.phoneLinkerAddress = deployments.getContractAddress("PhoneLinkCollection") || ""; + config.contracts.sideChain.ledgerAddress = deployments.getContractAddress("Ledger") || ""; + config.contracts.sideChain.shopAddress = deployments.getContractAddress("Shop") || ""; + config.contracts.sideChain.loyaltyProviderAddress = deployments.getContractAddress("LoyaltyProvider") || ""; + config.contracts.sideChain.loyaltyConsumerAddress = deployments.getContractAddress("LoyaltyConsumer") || ""; + config.contracts.sideChain.loyaltyExchangerAddress = + deployments.getContractAddress("LoyaltyExchanger") || ""; + config.contracts.sideChain.loyaltyTransferAddress = deployments.getContractAddress("LoyaltyTransfer") || ""; + config.contracts.sideChain.loyaltyBridgeAddress = deployments.getContractAddress("LoyaltyBridge") || ""; + config.contracts.sideChain.chainBridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; + + config.contracts.mainChain.tokenAddress = deployments.getContractAddress("MainChainKIOS") || ""; + config.contracts.mainChain.loyaltyBridgeAddress = + deployments.getContractAddress("MainChainLoyaltyBridge") || ""; + config.contracts.mainChain.chainBridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; + + config.relay.managerKeys = deployments.accounts.certifiers.map((m) => m.privateKey); + config.relay.relayEndpoint = `http://127.0.0.1:${config.server.port}`; + + client = new TestClient({ + headers: { + Authorization: config.relay.accessKey, + }, + }); + }); + + before("Create TestServer", async () => { + serverURL = new URL(`http://127.0.0.1:${config.server.port}`); + storage = await RelayStorage.make(config.database); + const graph_sidechain = await GraphStorage.make(config.graph_sidechain); + const graph_mainchain = await GraphStorage.make(config.graph_mainchain); + await contractManager.attach(); + server = new TestServer(config, contractManager, storage, graph_sidechain, graph_mainchain); + }); + + before("Start TestServer", async () => { + await server.start(); + }); + + after("Stop TestServer", async () => { + await server.stop(); + await storage.dropTestDB(); + }); + + before("Start CallbackServer", async () => { + fakerCallbackServer = new FakerCallbackServer(3400); + await fakerCallbackServer.start(); + }); + + after("Stop CallbackServer", async () => { + await fakerCallbackServer.stop(); + }); + + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[0].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; + }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); + + for (const user of userData) { + expect(await ledgerContract.pointBalanceOf(user.address)).to.equal(loyaltyAmount); + } + }); + + it("Exchange point to token", async () => { + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.div(100); + for (const user of userData) { + const nonce = await contractManager.sideLedgerContract.nonceOf(user.address); + const message = ContractUtils.getChangePointToTokenMessage( + user.address, + loyaltyAmount, + nonce, + contractManager.sideChainId + ); + const signature = await ContractUtils.signMessage(new Wallet(user.privateKey), message); + const response = await client.post( + URI(serverURL).directory("/v1/ledger/exchangePointToToken").toString(), + { + account: user.address, + amount: loyaltyAmount.toString(), + signature, + } + ); + + expect(response.data.code).to.equal(0); + expect(response.data.data).to.not.equal(undefined); + expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); + } + }); + }); +}); diff --git a/packages/relay/test/LoyaltyTransfer.test.ts b/packages/relay/test/LoyaltyTransfer.test.ts index d26c02f1..45841fce 100644 --- a/packages/relay/test/LoyaltyTransfer.test.ts +++ b/packages/relay/test/LoyaltyTransfer.test.ts @@ -95,29 +95,6 @@ describe("Test of LoyaltyTransfer", function () { await storage.dropTestDB(); }); - it("Change loyalty type to 1", async () => { - for (const userIndex of [0, 1]) { - const nonce = await contractManager.sideLedgerContract.nonceOf( - deployments.accounts.users[userIndex].address - ); - const signature = await ContractUtils.signLoyaltyType( - deployments.accounts.users[userIndex], - nonce, - contractManager.sideChainId - ); - const uri = URI(serverURL).directory("/v1/ledger/changeToLoyaltyToken"); - const url = uri.toString(); - const response = await client.post(url, { - account: deployments.accounts.users[userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - } - }); - it("Deposit token", async () => { for (const userIndex of [0, 1]) { const amount = Amount.make(1000, 18).value; diff --git a/packages/relay/test/Payment.test.ts b/packages/relay/test/Payment.test.ts index c93ec4af..870929d0 100644 --- a/packages/relay/test/Payment.test.ts +++ b/packages/relay/test/Payment.test.ts @@ -9,7 +9,7 @@ import { Scheduler } from "../src/scheduler/Scheduler"; import { WatchScheduler } from "../src/scheduler/WatchScheduler"; import { GraphStorage } from "../src/storage/GraphStorage"; import { RelayStorage } from "../src/storage/RelayStorage"; -import { ContractLoyaltyType, LoyaltyPaymentTaskStatus } from "../src/types"; +import { LoyaltyPaymentTaskStatus } from "../src/types"; import { ContractUtils, LoyaltyNetworkID } from "../src/utils/ContractUtils"; import { BIP20DelegatedTransfer, @@ -29,10 +29,10 @@ import { getPurchaseId, TestClient, TestServer } from "./helper/Utility"; import chai, { expect } from "chai"; import { solidity } from "ethereum-waffle"; -import * as path from "path"; -import { URL } from "url"; import * as assert from "assert"; import { BigNumber, Wallet } from "ethers"; +import * as path from "path"; +import { URL } from "url"; // tslint:disable-next-line:no-implicit-dependencies import { AddressZero } from "@ethersproject/constants"; @@ -307,16 +307,14 @@ describe("Test of Server", function () { it("Get user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); it("Endpoint POST /v1/payment/info", async () => { @@ -333,7 +331,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); assert.deepStrictEqual(response.data.data.paidPoint, Amount.make(1000).toString()); assert.deepStrictEqual(response.data.data.feePoint, Amount.make(50).toString()); @@ -360,7 +357,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -374,7 +370,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.amount, amountOfLoyalty.toString()); }); @@ -462,7 +457,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -773,16 +767,14 @@ describe("Test of Server", function () { it("Get user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); it("Endpoint POST /v1/payment/info", async () => { @@ -799,7 +791,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); assert.deepStrictEqual(response.data.data.paidPoint, Amount.make(1000).toString()); assert.deepStrictEqual(response.data.data.feePoint, Amount.make(50).toString()); @@ -826,7 +817,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -895,16 +885,14 @@ describe("Test of Server", function () { it("Check user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.sub(totalPoint).toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.sub(totalPoint).toString()); }); it("Endpoint POST /v1/payment/cancel/open", async () => { @@ -921,7 +909,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); @@ -1006,16 +993,14 @@ describe("Test of Server", function () { it("Check user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); }); }); @@ -1168,16 +1153,14 @@ describe("Test of Server", function () { it("Get user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); it("Endpoint POST /v1/payment/info", async () => { @@ -1194,7 +1177,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); assert.deepStrictEqual(response.data.data.paidPoint, Amount.make(1000).toString()); assert.deepStrictEqual(response.data.data.feePoint, Amount.make(50).toString()); @@ -1221,7 +1203,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -1290,16 +1271,14 @@ describe("Test of Server", function () { it("Check user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.sub(totalPoint).toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.sub(totalPoint).toString()); }); it("Endpoint POST /v1/payment/cancel/open", async () => { @@ -1316,7 +1295,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); @@ -1366,1037 +1344,14 @@ describe("Test of Server", function () { it("Check user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.sub(totalPoint).toString()); - }); - }); - }); - - context("Test token relay endpoints", () => { - before("Set Shop ID", async () => { - for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); - } - }); - - before("Deploy", async () => { - deployments.setShopData(shopData); - await deployments.doDeploy(); - - validatorContract = deployments.getContract("Validator") as Validator; - tokenContract = deployments.getContract("TestLYT") as BIP20DelegatedTransfer; - ledgerContract = deployments.getContract("Ledger") as Ledger; - linkContract = deployments.getContract("PhoneLinkCollection") as PhoneLinkCollection; - consumerContract = deployments.getContract("LoyaltyConsumer") as LoyaltyConsumer; - providerContract = deployments.getContract("LoyaltyProvider") as LoyaltyProvider; - exchangerContract = deployments.getContract("LoyaltyExchanger") as LoyaltyExchanger; - currencyRateContract = deployments.getContract("CurrencyRate") as CurrencyRate; - shopContract = deployments.getContract("Shop") as Shop; - }); - - before("Create Config", async () => { - config.contracts.sideChain.tokenAddress = deployments.getContractAddress("TestLYT") || ""; - config.contracts.sideChain.currencyRateAddress = deployments.getContractAddress("CurrencyRate") || ""; - config.contracts.sideChain.phoneLinkerAddress = deployments.getContractAddress("PhoneLinkCollection") || ""; - config.contracts.sideChain.ledgerAddress = deployments.getContractAddress("Ledger") || ""; - config.contracts.sideChain.shopAddress = deployments.getContractAddress("Shop") || ""; - config.contracts.sideChain.loyaltyProviderAddress = deployments.getContractAddress("LoyaltyProvider") || ""; - config.contracts.sideChain.loyaltyConsumerAddress = deployments.getContractAddress("LoyaltyConsumer") || ""; - config.contracts.sideChain.loyaltyExchangerAddress = - deployments.getContractAddress("LoyaltyExchanger") || ""; - config.contracts.sideChain.loyaltyTransferAddress = deployments.getContractAddress("LoyaltyTransfer") || ""; - config.contracts.sideChain.loyaltyBridgeAddress = deployments.getContractAddress("LoyaltyBridge") || ""; - config.contracts.sideChain.chainBridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; - - config.contracts.mainChain.tokenAddress = deployments.getContractAddress("MainChainKIOS") || ""; - config.contracts.mainChain.loyaltyBridgeAddress = - deployments.getContractAddress("MainChainLoyaltyBridge") || ""; - config.contracts.mainChain.chainBridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; - - config.relay.managerKeys = deployments.accounts.certifiers.map((m) => m.privateKey); - config.relay.callbackEndpoint = "http://127.0.0.1:3400/callback"; - config.relay.relayEndpoint = `http://127.0.0.1:${config.server.port}`; - }); - - before("Create TestServer", async () => { - serverURL = new URL(`http://127.0.0.1:${config.server.port}`); - storage = await RelayStorage.make(config.database); - const graph_sidechain = await GraphStorage.make(config.graph_sidechain); - const graph_mainchain = await GraphStorage.make(config.graph_mainchain); - - const schedulers: Scheduler[] = []; - schedulers.push(new WatchScheduler(expression)); - await contractManager.attach(); - server = new TestServer(config, contractManager, storage, graph_sidechain, graph_mainchain, schedulers); - }); - - before("Start TestServer", async () => { - await server.start(); - }); - - after("Stop TestServer", async () => { - await server.stop(); - await storage.dropTestDB(); - }); - - before("Start CallbackServer", async () => { - fakerCallbackServer = new FakerCallbackServer(3400); - await fakerCallbackServer.start(); - }); - - after("Stop CallbackServer", async () => { - await fakerCallbackServer.stop(); - }); - - context("Test of Loyalty Token", () => { - const purchase: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10000, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - - const purchaseAmount = Amount.make(purchase.amount, 18).value; - const shop = shopData[purchase.shopIndex]; - const pointAmount = ContractUtils.zeroGWEI(purchaseAmount.mul(purchase.providePercent).div(100)); - const tokenAmount = ContractUtils.zeroGWEI(pointAmount.mul(multiple).div(price)); - - const purchaseOfLoyalty: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - const amountOfLoyalty = Amount.make(purchaseOfLoyalty.amount, 18).value; - - it("Change loyalty type", async () => { - const nonce = await ledgerContract.nonceOf(users[purchase.userIndex].address); - const signature = await ContractUtils.signLoyaltyType( - users[purchase.userIndex], - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: users[purchase.userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - }); - - it("Save Purchase Data", async () => { - const phoneHash = ContractUtils.getPhoneHash(userData[purchase.userIndex].phone); - const userAccount = - userData[purchase.userIndex].address.trim() !== "" - ? userData[purchase.userIndex].address.trim() - : AddressZero; - const purchaseParam = { - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - sender: deployments.accounts.foundation.address, - }; - const purchaseMessage = ContractUtils.getPurchasesMessage( - 0, - [purchaseParam], - contractManager.sideChainId - ); - const signatures = deployments.accounts.validators.map((m) => - ContractUtils.signMessage(m, purchaseMessage) - ); - await expect(providerContract.connect(validators[0]).savePurchase(0, [purchaseParam], signatures)) - .to.emit(providerContract, "SavedPurchase") - .withNamedArgs({ - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - }) - .emit(ledgerContract, "ProvidedToken") - .withNamedArgs({ - account: userAccount, - providedToken: tokenAmount, - providedValue: pointAmount, - purchaseId: purchase.purchaseId, - shopId: shop.shopId, - }); - }); - - it("Get user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.toString()); - }); - - let paymentId: string; - it("Endpoint POST /v1/payment/new/open", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("open").toString(), - { - purchaseId: purchaseOfLoyalty.purchaseId, - amount: amountOfLoyalty.toString(), - currency: "krw", - shopId: shopData[purchaseOfLoyalty.shopIndex].shopId, - account: users[purchaseOfLoyalty.userIndex].address, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - - paymentId = response.data.data.paymentId; - }); - - it("Endpoint POST /v1/payment/item", async () => { - const response = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.paymentId, paymentId); - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - }); - - it("Endpoint POST /v1/payment/new/approval", async () => { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - const nonce = await ledgerContract.nonceOf(users[purchaseOfLoyalty.userIndex].address); - const signature = await ContractUtils.signLoyaltyNewPayment( - users[purchaseOfLoyalty.userIndex], - paymentId, - responseItem.data.data.purchaseId, - responseItem.data.data.amount, - responseItem.data.data.currency, - responseItem.data.data.shopId, - nonce, - contractManager.sideChainId - ); - - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("approval").toString(), - { - paymentId, - approval: true, - signature, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.ok(response.data.data.txHash !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX); - }); - - it("...Waiting", async () => { - const t1 = ContractUtils.getTimeStamp(); - while (true) { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - if (responseItem.data.data.paymentStatus === LoyaltyPaymentTaskStatus.REPLY_COMPLETED_NEW) break; - else if (ContractUtils.getTimeStamp() - t1 > 60) break; - await ContractUtils.delay(1000); - } - }); - - it("Endpoint POST /v1/payment/new/close", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("close").toString(), - { - confirm: true, - paymentId, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.CLOSED_NEW); - }); - }); - }); - - context("Test token relay endpoints - Cancel Confirm", () => { - before("Set Shop ID", async () => { - for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); - } - }); - - before("Deploy", async () => { - deployments.setShopData(shopData); - await deployments.doDeploy(); - - validatorContract = deployments.getContract("Validator") as Validator; - tokenContract = deployments.getContract("TestLYT") as BIP20DelegatedTransfer; - ledgerContract = deployments.getContract("Ledger") as Ledger; - linkContract = deployments.getContract("PhoneLinkCollection") as PhoneLinkCollection; - consumerContract = deployments.getContract("LoyaltyConsumer") as LoyaltyConsumer; - providerContract = deployments.getContract("LoyaltyProvider") as LoyaltyProvider; - exchangerContract = deployments.getContract("LoyaltyExchanger") as LoyaltyExchanger; - currencyRateContract = deployments.getContract("CurrencyRate") as CurrencyRate; - shopContract = deployments.getContract("Shop") as Shop; - }); - - before("Create Config", async () => { - config.contracts.sideChain.tokenAddress = deployments.getContractAddress("TestLYT") || ""; - config.contracts.sideChain.currencyRateAddress = deployments.getContractAddress("CurrencyRate") || ""; - config.contracts.sideChain.phoneLinkerAddress = deployments.getContractAddress("PhoneLinkCollection") || ""; - config.contracts.sideChain.ledgerAddress = deployments.getContractAddress("Ledger") || ""; - config.contracts.sideChain.shopAddress = deployments.getContractAddress("Shop") || ""; - config.contracts.sideChain.loyaltyProviderAddress = deployments.getContractAddress("LoyaltyProvider") || ""; - config.contracts.sideChain.loyaltyConsumerAddress = deployments.getContractAddress("LoyaltyConsumer") || ""; - config.contracts.sideChain.loyaltyExchangerAddress = - deployments.getContractAddress("LoyaltyExchanger") || ""; - config.contracts.sideChain.loyaltyTransferAddress = deployments.getContractAddress("LoyaltyTransfer") || ""; - config.contracts.sideChain.loyaltyBridgeAddress = deployments.getContractAddress("LoyaltyBridge") || ""; - config.contracts.sideChain.chainBridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; - - config.contracts.mainChain.tokenAddress = deployments.getContractAddress("MainChainKIOS") || ""; - config.contracts.mainChain.loyaltyBridgeAddress = - deployments.getContractAddress("MainChainLoyaltyBridge") || ""; - config.contracts.mainChain.chainBridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; - - config.relay.managerKeys = deployments.accounts.certifiers.map((m) => m.privateKey); - config.relay.callbackEndpoint = "http://127.0.0.1:3400/callback"; - config.relay.relayEndpoint = `http://127.0.0.1:${config.server.port}`; - }); - - before("Create TestServer", async () => { - serverURL = new URL(`http://127.0.0.1:${config.server.port}`); - storage = await RelayStorage.make(config.database); - const graph_sidechain = await GraphStorage.make(config.graph_sidechain); - const graph_mainchain = await GraphStorage.make(config.graph_mainchain); - - const schedulers: Scheduler[] = []; - schedulers.push(new WatchScheduler(expression)); - await contractManager.attach(); - server = new TestServer(config, contractManager, storage, graph_sidechain, graph_mainchain, schedulers); - }); - - before("Start TestServer", async () => { - await server.start(); - }); - - after("Stop TestServer", async () => { - await server.stop(); - await storage.dropTestDB(); - }); - - before("Start CallbackServer", async () => { - fakerCallbackServer = new FakerCallbackServer(3400); - await fakerCallbackServer.start(); - }); - - after("Stop CallbackServer", async () => { - await fakerCallbackServer.stop(); - }); - - context("Test of Loyalty Token", () => { - const purchase: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10000, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - - const purchaseAmount = Amount.make(purchase.amount, 18).value; - const shop = shopData[purchase.shopIndex]; - const pointAmount = ContractUtils.zeroGWEI(purchaseAmount.mul(purchase.providePercent).div(100)); - const tokenAmount = ContractUtils.zeroGWEI(pointAmount.mul(multiple).div(price)); - - const purchaseOfLoyalty: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - const amountOfLoyalty = Amount.make(purchaseOfLoyalty.amount, 18).value; - let totalToken: BigNumber; - - it("Change loyalty type", async () => { - const nonce = await ledgerContract.nonceOf(users[purchase.userIndex].address); - const signature = await ContractUtils.signLoyaltyType( - users[purchase.userIndex], - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: users[purchase.userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - }); - - it("Save Purchase Data", async () => { - const phoneHash = ContractUtils.getPhoneHash(userData[purchase.userIndex].phone); - const userAccount = - userData[purchase.userIndex].address.trim() !== "" - ? userData[purchase.userIndex].address.trim() - : AddressZero; - const purchaseParam = { - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - sender: deployments.accounts.foundation.address, - }; - const purchaseMessage = ContractUtils.getPurchasesMessage( - 0, - [purchaseParam], - contractManager.sideChainId - ); - const signatures = deployments.accounts.validators.map((m) => - ContractUtils.signMessage(m, purchaseMessage) - ); - await expect(providerContract.connect(validators[0]).savePurchase(0, [purchaseParam], signatures)) - .to.emit(providerContract, "SavedPurchase") - .withNamedArgs({ - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - }) - .emit(ledgerContract, "ProvidedToken") - .withNamedArgs({ - account: userAccount, - providedToken: tokenAmount, - providedValue: pointAmount, - purchaseId: purchase.purchaseId, - shopId: shop.shopId, - }); - }); - - it("Get user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.toString()); - }); - - let paymentId: string; - it("Endpoint POST /v1/payment/new/open", async () => { - const url = URI(serverURL).directory("/v1/payment/new").filename("open").toString(); - - const params = { - purchaseId: purchaseOfLoyalty.purchaseId, - amount: amountOfLoyalty.toString(), - currency: "krw", - shopId: shopData[purchaseOfLoyalty.shopIndex].shopId, - account: users[purchaseOfLoyalty.userIndex].address, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - - paymentId = response.data.data.paymentId; - }); - - it("Endpoint POST /v1/payment/new/approval", async () => { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - const nonce = await ledgerContract.nonceOf(users[purchaseOfLoyalty.userIndex].address); - const signature = await ContractUtils.signLoyaltyNewPayment( - users[purchaseOfLoyalty.userIndex], - paymentId, - responseItem.data.data.purchaseId, - responseItem.data.data.amount, - responseItem.data.data.currency, - responseItem.data.data.shopId, - nonce, - contractManager.sideChainId - ); - - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("approval").toString(), - { - paymentId, - approval: true, - signature, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.ok(response.data.data.txHash !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX); - }); - - it("...Waiting", async () => { - const t1 = ContractUtils.getTimeStamp(); - while (true) { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - if (responseItem.data.data.paymentStatus === LoyaltyPaymentTaskStatus.REPLY_COMPLETED_NEW) break; - else if (ContractUtils.getTimeStamp() - t1 > 60) break; - await ContractUtils.delay(1000); - } - }); - - it("Endpoint POST /v1/payment/new/close", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("close").toString(), - { - confirm: true, - paymentId, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.CLOSED_NEW); - totalToken = BigNumber.from(response.data.data.totalToken); - }); - - it("Waiting", async () => { - await ContractUtils.delay(2000); - }); - - it("Check user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.sub(totalToken).toString()); - }); - - it("Endpoint POST /v1/payment/cancel/open", async () => { - const url = URI(serverURL).directory("/v1/payment/cancel").filename("open").toString(); - - const params = { - paymentId, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.paymentId, paymentId); - assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); - }); - - let oldBalance: BigNumber; - let oldShopInfo: any; - it("Endpoint POST /v1/payment/cancel/approval - true", async () => { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - oldBalance = await ledgerContract.tokenBalanceOf(responseItem.data.data.account); - oldShopInfo = await shopContract.shopOf(responseItem.data.data.shopId); - - const nonce = await ledgerContract.nonceOf(shopData[purchaseOfLoyalty.shopIndex].wallet.address); - const signature = await ContractUtils.signLoyaltyCancelPayment( - shopData[purchaseOfLoyalty.shopIndex].wallet, - paymentId, - responseItem.data.data.purchaseId, - nonce, - contractManager.sideChainId - ); - - const url = URI(serverURL).directory("/v1/payment/cancel").filename("approval").toString(); - const params = { - paymentId, - approval: true, - signature, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.ok(response.data.data.txHash !== undefined); - assert.deepStrictEqual(response.data.data.paymentId, paymentId); - assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual( - response.data.data.paymentStatus, - LoyaltyPaymentTaskStatus.APPROVED_CANCEL_SENT_TX - ); - }); - - it("...Waiting", async () => { - const t1 = ContractUtils.getTimeStamp(); - while (true) { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - if (responseItem.data.data.paymentStatus === LoyaltyPaymentTaskStatus.REPLY_COMPLETED_CANCEL) break; - else if (ContractUtils.getTimeStamp() - t1 > 60) break; - await ContractUtils.delay(1000); - } - }); - - it("Endpoint POST /v1/payment/cancel/close", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/cancel").filename("close").toString(), - { - confirm: true, - paymentId, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.CLOSED_CANCEL); - - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - - const newBalance = await ledgerContract.tokenBalanceOf(users[purchaseOfLoyalty.userIndex].address); - assert.deepStrictEqual(newBalance, oldBalance.add(BigNumber.from(responseItem.data.data.totalToken))); - - const newShopInfo = await shopContract.shopOf(responseItem.data.data.shopId); - - assert.deepStrictEqual( - newShopInfo.usedAmount.toString(), - oldShopInfo.usedAmount.sub(BigNumber.from(responseItem.data.data.paidValue)).toString() - ); - }); - - it("Check user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.toString()); - }); - }); - }); - - context("Test token relay endpoints - Cancel Deny", () => { - before("Set Shop ID", async () => { - for (const elem of shopData) { - elem.shopId = ContractUtils.getShopId(elem.wallet.address, LoyaltyNetworkID.LYT); - } - }); - - before("Deploy", async () => { - deployments.setShopData(shopData); - await deployments.doDeploy(); - - validatorContract = deployments.getContract("Validator") as Validator; - tokenContract = deployments.getContract("TestLYT") as BIP20DelegatedTransfer; - ledgerContract = deployments.getContract("Ledger") as Ledger; - linkContract = deployments.getContract("PhoneLinkCollection") as PhoneLinkCollection; - consumerContract = deployments.getContract("LoyaltyConsumer") as LoyaltyConsumer; - providerContract = deployments.getContract("LoyaltyProvider") as LoyaltyProvider; - exchangerContract = deployments.getContract("LoyaltyExchanger") as LoyaltyExchanger; - currencyRateContract = deployments.getContract("CurrencyRate") as CurrencyRate; - shopContract = deployments.getContract("Shop") as Shop; - }); - - before("Create Config", async () => { - config.contracts.sideChain.tokenAddress = deployments.getContractAddress("TestLYT") || ""; - config.contracts.sideChain.currencyRateAddress = deployments.getContractAddress("CurrencyRate") || ""; - config.contracts.sideChain.phoneLinkerAddress = deployments.getContractAddress("PhoneLinkCollection") || ""; - config.contracts.sideChain.ledgerAddress = deployments.getContractAddress("Ledger") || ""; - config.contracts.sideChain.shopAddress = deployments.getContractAddress("Shop") || ""; - config.contracts.sideChain.loyaltyProviderAddress = deployments.getContractAddress("LoyaltyProvider") || ""; - config.contracts.sideChain.loyaltyConsumerAddress = deployments.getContractAddress("LoyaltyConsumer") || ""; - config.contracts.sideChain.loyaltyExchangerAddress = - deployments.getContractAddress("LoyaltyExchanger") || ""; - config.contracts.sideChain.loyaltyTransferAddress = deployments.getContractAddress("LoyaltyTransfer") || ""; - config.contracts.sideChain.loyaltyBridgeAddress = deployments.getContractAddress("LoyaltyBridge") || ""; - config.contracts.sideChain.chainBridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; - - config.contracts.mainChain.tokenAddress = deployments.getContractAddress("MainChainKIOS") || ""; - config.contracts.mainChain.loyaltyBridgeAddress = - deployments.getContractAddress("MainChainLoyaltyBridge") || ""; - config.contracts.mainChain.chainBridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; - - config.relay.managerKeys = deployments.accounts.certifiers.map((m) => m.privateKey); - config.relay.callbackEndpoint = "http://127.0.0.1:3400/callback"; - config.relay.relayEndpoint = `http://127.0.0.1:${config.server.port}`; - }); - - before("Create TestServer", async () => { - serverURL = new URL(`http://127.0.0.1:${config.server.port}`); - storage = await RelayStorage.make(config.database); - const graph_sidechain = await GraphStorage.make(config.graph_sidechain); - const graph_mainchain = await GraphStorage.make(config.graph_mainchain); - - const schedulers: Scheduler[] = []; - schedulers.push(new WatchScheduler(expression)); - await contractManager.attach(); - server = new TestServer(config, contractManager, storage, graph_sidechain, graph_mainchain, schedulers); - }); - - before("Start TestServer", async () => { - await server.start(); - }); - - after("Stop TestServer", async () => { - await server.stop(); - await storage.dropTestDB(); - }); - - before("Start CallbackServer", async () => { - fakerCallbackServer = new FakerCallbackServer(3400); - await fakerCallbackServer.start(); - }); - - after("Stop CallbackServer", async () => { - await fakerCallbackServer.stop(); - }); - - context("Test of Loyalty Point", () => { - const purchase: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10000, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - - const purchaseAmount = Amount.make(purchase.amount, 18).value; - const shop = shopData[purchase.shopIndex]; - const pointAmount = ContractUtils.zeroGWEI(purchaseAmount.mul(purchase.providePercent).div(100)); - const tokenAmount = ContractUtils.zeroGWEI(pointAmount.mul(multiple).div(price)); - - const purchaseOfLoyalty: IPurchaseData = { - purchaseId: getPurchaseId(), - amount: 10, - providePercent: 10, - currency: "krw", - shopIndex: 1, - userIndex: 0, - }; - const amountOfLoyalty = Amount.make(purchaseOfLoyalty.amount, 18).value; - let totalToken: BigNumber; - - it("Change loyalty type", async () => { - const nonce = await ledgerContract.nonceOf(users[purchase.userIndex].address); - const signature = await ContractUtils.signLoyaltyType( - users[purchase.userIndex], - nonce, - contractManager.sideChainId - ); - const url = URI(serverURL).directory("v1/ledger").filename("changeToLoyaltyToken").toString(); - const response = await client.post(url, { - account: users[purchase.userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - }); - - it("Save Purchase Data", async () => { - const phoneHash = ContractUtils.getPhoneHash(userData[purchase.userIndex].phone); - const userAccount = - userData[purchase.userIndex].address.trim() !== "" - ? userData[purchase.userIndex].address.trim() - : AddressZero; - const purchaseParam = { - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - sender: deployments.accounts.foundation.address, - }; - const purchaseMessage = ContractUtils.getPurchasesMessage( - 0, - [purchaseParam], - contractManager.sideChainId - ); - const signatures = deployments.accounts.validators.map((m) => - ContractUtils.signMessage(m, purchaseMessage) - ); - await expect(providerContract.connect(validators[0]).savePurchase(0, [purchaseParam], signatures)) - .to.emit(providerContract, "SavedPurchase") - .withNamedArgs({ - purchaseId: purchase.purchaseId, - amount: purchaseAmount, - loyalty: pointAmount, - currency: purchase.currency.toLowerCase(), - shopId: shop.shopId, - account: userAccount, - phone: phoneHash, - }) - .emit(ledgerContract, "ProvidedToken") - .withNamedArgs({ - account: userAccount, - providedToken: tokenAmount, - providedValue: pointAmount, - purchaseId: purchase.purchaseId, - shopId: shop.shopId, - }); - }); - - it("Get user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.toString()); - }); - - let paymentId: string; - it("Endpoint POST /v1/payment/new/open", async () => { - const url = URI(serverURL).directory("/v1/payment/new").filename("open").toString(); - - const params = { - purchaseId: purchaseOfLoyalty.purchaseId, - amount: amountOfLoyalty.toString(), - currency: "krw", - shopId: shopData[purchaseOfLoyalty.shopIndex].shopId, - account: users[purchaseOfLoyalty.userIndex].address, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - - paymentId = response.data.data.paymentId; - }); - - it("Endpoint POST /v1/payment/new/approval", async () => { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - const nonce = await ledgerContract.nonceOf(users[purchaseOfLoyalty.userIndex].address); - const signature = await ContractUtils.signLoyaltyNewPayment( - users[purchaseOfLoyalty.userIndex], - paymentId, - responseItem.data.data.purchaseId, - responseItem.data.data.amount, - responseItem.data.data.currency, - responseItem.data.data.shopId, - nonce, - contractManager.sideChainId - ); - - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("approval").toString(), - { - paymentId, - approval: true, - signature, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.ok(response.data.data.txHash !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.APPROVED_NEW_SENT_TX); - }); - - it("...Waiting", async () => { - const t1 = ContractUtils.getTimeStamp(); - while (true) { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - if (responseItem.data.data.paymentStatus === LoyaltyPaymentTaskStatus.REPLY_COMPLETED_NEW) break; - else if (ContractUtils.getTimeStamp() - t1 > 60) break; - await ContractUtils.delay(1000); - } - }); - - it("Waiting", async () => { - await ContractUtils.delay(2000); - }); - - it("Endpoint POST /v1/payment/new/close", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/new").filename("close").toString(), - { - confirm: true, - paymentId, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.CLOSED_NEW); - totalToken = BigNumber.from(response.data.data.totalToken); - }); - - it("Waiting", async () => { - await ContractUtils.delay(2000); - }); - - it("Check user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) - .toString(); - const response = await client.get(url); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.sub(totalToken).toString()); - }); - - it("Endpoint POST /v1/payment/cancel/open", async () => { - const url = URI(serverURL).directory("/v1/payment/cancel").filename("open").toString(); - - const params = { - paymentId, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.paymentId, paymentId); - assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); - }); - - it("Endpoint POST /v1/payment/cancel/approval - false", async () => { - const responseItem = await client.get( - URI(serverURL).directory("/v1/payment/item").addQuery("paymentId", paymentId).toString() - ); - const nonce = await ledgerContract.nonceOf(shopData[purchaseOfLoyalty.shopIndex].wallet.address); - const signature = await ContractUtils.signLoyaltyCancelPayment( - shopData[purchaseOfLoyalty.shopIndex].wallet, - paymentId, - responseItem.data.data.purchaseId, - nonce, - contractManager.sideChainId - ); - - const url = URI(serverURL).directory("/v1/payment/cancel").filename("approval").toString(); - const params = { - paymentId, - approval: false, - signature, - }; - const response = await client.post(url, params); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - - assert.deepStrictEqual(response.data.data.paymentId, paymentId); - assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); - assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.DENIED_CANCEL); - }); - - it("Endpoint POST /v1/payment/cancel/close", async () => { - const response = await client.post( - URI(serverURL).directory("/v1/payment/cancel").filename("close").toString(), - { - confirm: true, - paymentId, - } - ); - - assert.deepStrictEqual(response.data.code, 0); - assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.FAILED_CANCEL); - }); - - it("Check user's balance", async () => { - const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.TOKEN); - assert.deepStrictEqual(response.data.data.balance, tokenAmount.sub(totalToken).toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.sub(totalPoint).toString()); }); }); }); @@ -2669,16 +1624,14 @@ describe("Test of Server", function () { it("Get user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", users[purchase.userIndex].address) + .directory("/v1/ledger/balance/account") + .filename(users[purchase.userIndex].address) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); it("Endpoint POST /v1/payment/new/open", async () => { @@ -2697,7 +1650,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -2751,7 +1703,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.purchaseId, purchaseOfLoyalty.purchaseId); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.paymentStatus, LoyaltyPaymentTaskStatus.OPENED_CANCEL); }); diff --git a/packages/relay/test/ShopWithdraw.test.ts b/packages/relay/test/ShopWithdraw.test.ts index fd0a65bd..0f6fc229 100644 --- a/packages/relay/test/ShopWithdraw.test.ts +++ b/packages/relay/test/ShopWithdraw.test.ts @@ -377,7 +377,6 @@ describe("Test for Shop", () => { expect(paymentData.currency).to.deep.equal(purchase.currency); expect(paymentData.shopId).to.deep.equal(shop.shopId); expect(paymentData.account).to.deep.equal(userWallets[purchase.userIndex].address); - expect(paymentData.loyaltyType).to.deep.equal(0); expect(paymentData.paidPoint).to.deep.equal(purchaseAmount); expect(paymentData.paidValue).to.deep.equal(purchaseAmount); @@ -401,59 +400,37 @@ describe("Test for Shop", () => { }); }); - context("Change loyalty type", () => { - it("Check loyalty type - before", async () => { - const userIndex = 0; - const loyaltyType = await ledgerContract.loyaltyTypeOf(userWallets[userIndex].address); - expect(loyaltyType).to.equal(0); - }); - - it("Send loyalty type", async () => { - const userIndex = 0; - const nonce = await ledgerContract.nonceOf(userWallets[userIndex].address); - const signature = await ContractUtils.signLoyaltyType( - userWallets[userIndex], - nonce, - contractManager.sideChainId - ); - const uri = URI(serverURL).directory("/v1/ledger/changeToLoyaltyToken"); - const url = uri.toString(); - const response = await client.post(url, { - account: userWallets[userIndex].address, - signature, - }); - - expect(response.data.code).to.equal(0); - expect(response.data.data).to.not.equal(undefined); - expect(response.data.data.txHash).to.match(/^0x[A-Fa-f0-9]{64}$/i); - }); - - it("Check point type - after", async () => { - const userIndex = 0; - const loyaltyType = await ledgerContract.loyaltyTypeOf(userWallets[userIndex].address); - expect(loyaltyType).to.equal(1); - }); - }); - - context("Deposit token", () => { - it("Deposit token - Success", async () => { - const oldTokenBalance = await ledgerContract.tokenBalanceOf(userWallets[0].address); - await tokenContract.connect(userWallets[0]).approve(ledgerContract.address, amount.value); - await expect(ledgerContract.connect(userWallets[0]).deposit(amount.value)) - .to.emit(ledgerContract, "Deposited") - .withNamedArgs({ - account: userWallets[0].address, - depositedToken: amount.value, - balanceToken: oldTokenBalance.add(amount.value), - }); - expect(await ledgerContract.tokenBalanceOf(userWallets[0].address)).to.deep.equal( - oldTokenBalance.add(amount.value) - ); + it("Provide Loyalty Point - Save Purchase Data", async () => { + const phoneHash = ContractUtils.getPhoneHash(""); + const purchaseAmount = Amount.make(100_000_000, 18).value; + const loyaltyAmount = purchaseAmount.mul(10).div(100); + const purchaseParam = userData.map((m) => { + return { + purchaseId: getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "krw", + shopId: shopData[5].shopId, + account: m.address, + phone: phoneHash, + sender: deployments.accounts.foundation.address, + }; }); + const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParam, contractManager.sideChainId); + const signatures = deployments.accounts.validators.map((m) => + ContractUtils.signMessage(m, purchaseMessage) + ); + await providerContract + .connect(deployments.accounts.validators[0]) + .savePurchase(0, purchaseParam, signatures); + + for (const user of userData) { + expect(await ledgerContract.pointBalanceOf(user.address)).to.equal(loyaltyAmount); + } }); - context("Pay token", () => { - it("Pay token - Success", async () => { + context("Pay", () => { + it("Pay - Success", async () => { const purchase: IPurchaseData = { purchaseId: getPurchaseId(), amount: 500, @@ -466,10 +443,6 @@ describe("Test for Shop", () => { const nonce = await ledgerContract.nonceOf(userWallets[purchase.userIndex].address); const paymentId = ContractUtils.getPaymentId(userWallets[purchase.userIndex].address, nonce); const purchaseAmount = Amount.make(purchase.amount, 18).value; - const tokenAmount = ContractUtils.zeroGWEI(purchaseAmount.mul(multiple).div(price)); - const oldFoundationTokenBalance = await ledgerContract.tokenBalanceOf( - deployments.accounts.foundation.address - ); const shop = shopData[purchase.shopIndex]; const signature = await ContractUtils.signLoyaltyNewPayment( userWallets[purchase.userIndex], @@ -502,8 +475,7 @@ describe("Test for Shop", () => { expect(paymentData.currency).to.deep.equal(purchase.currency); expect(paymentData.shopId).to.deep.equal(shop.shopId); expect(paymentData.account).to.deep.equal(userWallets[purchase.userIndex].address); - expect(paymentData.loyaltyType).to.deep.equal(1); - expect(paymentData.paidToken).to.deep.equal(tokenAmount); + expect(paymentData.paidPoint).to.deep.equal(purchaseAmount); expect(paymentData.paidValue).to.deep.equal(purchaseAmount); await expect( @@ -516,11 +488,6 @@ describe("Test for Shop", () => { expect(shopInfo2.providedAmount).to.equal(Amount.make(100, 18).value); expect(shopInfo2.usedAmount).to.equal(Amount.make(500, 18).value); expect(shopInfo2.settledAmount).to.equal(Amount.make(400, 18).value); - - const settledToken = ContractUtils.zeroGWEI(shopInfo2.settledAmount.mul(multiple).div(price)); - expect( - (await ledgerContract.tokenBalanceOf(deployments.accounts.foundation.address)).toString() - ).to.deep.equal(oldFoundationTokenBalance.add(tokenAmount).sub(settledToken).toString()); }); }); @@ -535,11 +502,7 @@ describe("Test for Shop", () => { }); it("Get info of shop", async () => { - const url = URI(serverURL) - .directory("/v1/payment/shop") - .filename("info") - .addQuery("shopId", shopData[shopIndex].shopId) - .toString(); + const url = URI(serverURL).directory("/v1/shop/info").filename(shopData[shopIndex].shopId).toString(); const response = await client.get(url); expect(response.data.code).to.equal(0); assert.deepStrictEqual(response.data.data, { @@ -585,9 +548,8 @@ describe("Test for Shop", () => { it("Get withdrawal of shop", async () => { const url = URI(serverURL) - .directory("/v1/payment/shop") - .filename("withdrawal") - .addQuery("shopId", shopData[shopIndex].shopId) + .directory("/v1/shop/withdrawal") + .filename(shopData[shopIndex].shopId) .toString(); const response = await client.get(url); expect(response.data.code).to.equal(0); @@ -625,11 +587,7 @@ describe("Test for Shop", () => { }); it("Get info of shop", async () => { - const url = URI(serverURL) - .directory("/v1/payment/shop") - .filename("info") - .addQuery("shopId", shopData[shopIndex].shopId) - .toString(); + const url = URI(serverURL).directory("/v1/shop/info").filename(shopData[shopIndex].shopId).toString(); const response = await client.get(url); expect(response.data.code).to.equal(0); assert.deepStrictEqual(response.data.data, { @@ -648,9 +606,8 @@ describe("Test for Shop", () => { it("Get withdrawal of shop", async () => { const url = URI(serverURL) - .directory("/v1/payment/shop") - .filename("withdrawal") - .addQuery("shopId", shopData[shopIndex].shopId) + .directory("/v1/shop/withdrawal") + .filename(shopData[shopIndex].shopId) .toString(); const response = await client.get(url); expect(response.data.code).to.equal(0); diff --git a/packages/relay/test/TempararyAccount.test.ts b/packages/relay/test/TempararyAccount.test.ts index ee200947..ba52b8c0 100644 --- a/packages/relay/test/TempararyAccount.test.ts +++ b/packages/relay/test/TempararyAccount.test.ts @@ -8,7 +8,7 @@ import { Scheduler } from "../src/scheduler/Scheduler"; import { WatchScheduler } from "../src/scheduler/WatchScheduler"; import { GraphStorage } from "../src/storage/GraphStorage"; import { RelayStorage } from "../src/storage/RelayStorage"; -import { ContractLoyaltyType, LoyaltyPaymentTaskStatus } from "../src/types"; +import { LoyaltyPaymentTaskStatus } from "../src/types"; import { ContractUtils, LoyaltyNetworkID } from "../src/utils/ContractUtils"; import { BIP20DelegatedTransfer, @@ -352,16 +352,14 @@ describe("Test of Server", function () { it("Get user's balance", async () => { const url = URI(serverURL) - .directory("/v1/payment/user") - .filename("balance") - .addQuery("account", temporaryAccount) + .directory("/v1/ledger/balance/account") + .filename(temporaryAccount) .toString(); const response = await client.get(url); assert.deepStrictEqual(response.data.code, 0); assert.ok(response.data.data !== undefined); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); + assert.deepStrictEqual(response.data.data.point.balance, pointAmount.toString()); }); it("Endpoint POST /v1/payment/info", async () => { @@ -378,8 +376,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); - assert.deepStrictEqual(response.data.data.balance, pointAmount.toString()); assert.deepStrictEqual(response.data.data.paidPoint, Amount.make(1000).toString()); assert.deepStrictEqual(response.data.data.feePoint, Amount.make(50).toString()); assert.deepStrictEqual(response.data.data.totalPoint, Amount.make(1050).toString()); @@ -405,7 +401,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; }); @@ -419,7 +414,6 @@ describe("Test of Server", function () { assert.deepStrictEqual(response.data.data.paymentId, paymentId); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); assert.deepStrictEqual(response.data.data.amount, amountOfLoyalty.toString()); }); @@ -507,7 +501,6 @@ describe("Test of Server", function () { assert.ok(response.data.data !== undefined); assert.deepStrictEqual(response.data.data.account, users[purchase.userIndex].address); - assert.deepStrictEqual(response.data.data.loyaltyType, ContractLoyaltyType.POINT); paymentId = response.data.data.paymentId; });