Skip to content

Commit

Permalink
Update test code of dms-relay
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed May 23, 2024
1 parent e26fba1 commit 81b0729
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 1,380 deletions.
1 change: 1 addition & 0 deletions packages/relay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
61 changes: 24 additions & 37 deletions packages/relay/test/Approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down
58 changes: 25 additions & 33 deletions packages/relay/test/DelegatorApproval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/relay/test/ETC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
34 changes: 0 additions & 34 deletions packages/relay/test/Endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
111 changes: 49 additions & 62 deletions packages/relay/test/ForcedClose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

Expand Down
Loading

0 comments on commit 81b0729

Please sign in to comment.