"use strict";
/*
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("../environmentMock");
const RPCTransactionAdapter_1 = require("../../../src/port/out/rpc/RPCTransactionAdapter");
const TransferRequest_1 = __importDefault(require("../../../src/port/in/request/security/operations/transfer/TransferRequest"));
const RedeemRequest_1 = __importDefault(require("../../../src/port/in/request/security/operations/redeem/RedeemRequest"));
const Injectable_1 = __importDefault(require("../../../src/core/injectable/Injectable"));
const MirrorNodeAdapter_1 = require("../../../src/port/out/mirror/MirrorNodeAdapter");
const config_1 = require("../../config");
const NetworkService_1 = __importDefault(require("../../../src/app/service/network/NetworkService"));
const RPCQueryAdapter_1 = require("../../../src/port/out/rpc/RPCQueryAdapter");
const ethers_1 = require("ethers");
const GetSecurityDetailsRequest_1 = __importDefault(require("../../../src/port/in/request/security/GetSecurityDetailsRequest"));
const SecurityRole_1 = require("../../../src/domain/context/security/SecurityRole");
const SecurityControlListType_1 = require("../../../src/domain/context/security/SecurityControlListType");
const RegulationType_1 = require("../../../src/domain/context/factory/RegulationType");
const Account_1 = __importDefault(require("../../../src/domain/context/account/Account"));
const js_sha3_1 = require("js-sha3");
const Constants_1 = require("../../../src/core/Constants");
const verifiableCredentials_1 = __importDefault(require("../../utils/verifiableCredentials"));
const Clearing_1 = require("../../../src/domain/context/security/Clearing");
const BigDecimal_1 = __importDefault(require("../../../src/domain/context/shared/BigDecimal"));
const SecurityDate_1 = require("../../../src/domain/context/shared/SecurityDate");
const in_1 = require("../../../src/port/in");
in_1.SDK.log = { level: 'ERROR', transports: new in_1.LoggerTransports.Console() };
const decimals = 0;
const name = 'TEST_SECURITY_TOKEN';
const symbol = 'TEST';
const isin = 'ABCDE123456Z';
const type = 'EQUITY';
const votingRight = true;
const informationRight = false;
const liquidationRight = true;
const subscriptionRight = false;
const conversionRight = true;
const redemptionRight = false;
const putRight = true;
const dividendRight = 1;
const currency = '0x345678';
const numberOfShares = 0;
const nominalValue = 1000;
const nominalValueDecimals = 3;
const regulationType = RegulationType_1.RegulationType.REG_D;
const regulationSubType = RegulationType_1.RegulationSubType.B_506;
const countries = 'AF,HG,BN';
const info = 'Anything';
const configId = '0x0000000000000000000000000000000000000000000000000000000000000000';
const configVersion = 1;
const mirrorNode = {
    name: 'testmirrorNode',
    baseUrl: 'https://testnet.mirrornode.hedera.com/api/v1/',
};
const rpcNode = {
    name: 'testrpcNode',
    baseUrl: 'http://127.0.0.1:7546/api',
};
let th;
let mirrorNodeAdapter;
describe('🧪 Security tests', () => {
    let ns;
    let rpcQueryAdapter;
    let equity;
    const url = 'http://127.0.0.1:7546';
    const customHttpProvider = new ethers_1.ethers.providers.JsonRpcProvider(url);
    beforeAll(async () => {
        mirrorNodeAdapter = Injectable_1.default.resolve(MirrorNodeAdapter_1.MirrorNodeAdapter);
        mirrorNodeAdapter.set(mirrorNode);
        th = Injectable_1.default.resolve(RPCTransactionAdapter_1.RPCTransactionAdapter);
        ns = Injectable_1.default.resolve(NetworkService_1.default);
        rpcQueryAdapter = Injectable_1.default.resolve(RPCQueryAdapter_1.RPCQueryAdapter);
        rpcQueryAdapter.init();
        ns.environment = 'testnet';
        ns.configuration = {
            factoryAddress: config_1.FACTORY_ADDRESS,
            resolverAddress: config_1.RESOLVER_ADDRESS,
        };
        ns.mirrorNode = mirrorNode;
        ns.rpcNode = rpcNode;
        await th.init(true);
        const account = new Account_1.default({
            id: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
            evmAddress: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress,
            alias: config_1.CLIENT_ACCOUNT_ECDSA.alias,
            privateKey: config_1.CLIENT_ACCOUNT_ECDSA.privateKey,
            publicKey: config_1.CLIENT_ACCOUNT_ECDSA.publicKey,
        });
        await th.register(account, true);
        th.setSignerOrProvider(new ethers_1.Wallet(config_1.CLIENT_ACCOUNT_ECDSA.privateKey?.key ?? '', customHttpProvider));
        const requestST = new in_1.CreateEquityRequest({
            name: name,
            symbol: symbol,
            isin: isin,
            decimals: decimals,
            isWhiteList: false,
            erc20VotesActivated: false,
            isControllable: true,
            arePartitionsProtected: false,
            clearingActive: false,
            internalKycActivated: true,
            isMultiPartition: false,
            diamondOwnerAccount: config_1.CLIENT_ACCOUNT_ECDSA.id.toString(),
            votingRight: votingRight,
            informationRight: informationRight,
            liquidationRight: liquidationRight,
            subscriptionRight: subscriptionRight,
            conversionRight: conversionRight,
            redemptionRight: redemptionRight,
            putRight: putRight,
            dividendRight: dividendRight,
            currency: currency,
            numberOfShares: numberOfShares.toString(),
            nominalValue: nominalValue.toString(),
            nominalValueDecimals: nominalValueDecimals,
            regulationType: RegulationType_1.CastRegulationType.toNumber(regulationType),
            regulationSubType: RegulationType_1.CastRegulationSubType.toNumber(regulationSubType),
            isCountryControlListWhiteList: true,
            countries: countries,
            info: info,
            configId: configId,
            configVersion: configVersion,
        });
        equity = (await in_1.Equity.create(requestST)).security;
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._ISSUER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CONTROLLER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._PAUSER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CONTROLLIST_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CORPORATEACTIONS_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._DOCUMENTER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._SNAPSHOT_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._LOCKER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._CAP_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._PROTECTED_PARTITION_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._SSI_MANAGER_ROLE,
        }));
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: SecurityRole_1.SecurityRole._KYC_ROLE,
        }));
        await in_1.SsiManagement.addIssuer(new in_1.AddIssuerRequest({
            securityId: equity.evmDiamondAddress,
            issuerId: config_1.CLIENT_EVM_ADDRESS_ECDSA_1_CORRECT,
        }));
        await in_1.Kyc.grantKyc(new in_1.GrantKycRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            vcBase64: await (0, verifiableCredentials_1.default)(config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString()),
        }));
        await in_1.Kyc.grantKyc(new in_1.GrantKycRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            vcBase64: await (0, verifiableCredentials_1.default)(config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString()),
        }));
        await in_1.Security.setMaxSupply(new in_1.SetMaxSupplyRequest({
            securityId: equity.evmDiamondAddress,
            maxSupply: '1000000',
        }));
    }, 900000);
    afterAll(async () => {
        await in_1.SsiManagement.removeIssuer(new in_1.AddIssuerRequest({
            securityId: equity.evmDiamondAddress,
            issuerId: config_1.CLIENT_EVM_ADDRESS_ECDSA_1_CORRECT,
        }));
    });
    it('Get security', async () => {
        const equityInfo = await in_1.Security.getInfo(new GetSecurityDetailsRequest_1.default({
            securityId: equity.evmDiamondAddress,
        }));
        expect(equityInfo.name).toEqual(name);
        expect(equityInfo.symbol).toEqual(symbol);
        expect(equityInfo.isin).toEqual(isin);
        expect(equityInfo.type).toEqual(type);
        expect(equityInfo.decimals).toEqual(decimals);
        expect(equityInfo.isWhiteList).toEqual(false);
        expect(equityInfo.isControllable).toEqual(true);
        expect(equityInfo.isMultiPartition).toEqual(false);
        expect(equityInfo.totalSupply).toEqual('0');
        expect(equityInfo.diamondAddress).toEqual(equity.diamondAddress.toString());
        expect(equityInfo.evmDiamondAddress.toString().toUpperCase()).toEqual(equity.evmDiamondAddress.toString().toUpperCase());
        expect(equityInfo.paused).toEqual(false);
        expect(equityInfo.regulation?.type).toEqual(regulationType);
        expect(equityInfo.regulation?.subType).toEqual(regulationSubType);
        expect(equityInfo.isCountryControlListWhiteList).toEqual(true);
        expect(equityInfo.countries).toEqual(countries);
        expect(equityInfo.info).toEqual(info);
    }, 600000);
    it('Control List Add & Remove', async () => {
        let membersCount = await in_1.Security.getControlListCount(new in_1.GetControlListCountRequest({
            securityId: equity.evmDiamondAddress,
        }));
        expect(membersCount).toBe(0);
        let members = await in_1.Security.getControlListMembers(new in_1.GetControlListMembersRequest({
            securityId: equity.evmDiamondAddress,
            start: 0,
            end: membersCount,
        }));
        expect(members).toStrictEqual([]);
        expect(await in_1.Security.isAccountInControlList(new in_1.ControlListRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).toBe(false);
        await in_1.Security.addToControlList(new in_1.ControlListRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
        expect(await in_1.Security.isAccountInControlList(new in_1.ControlListRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).toBe(true);
        membersCount = await in_1.Security.getControlListCount(new in_1.GetControlListCountRequest({
            securityId: equity.evmDiamondAddress,
        }));
        expect(membersCount).toBe(1);
        members = await in_1.Security.getControlListMembers(new in_1.GetControlListMembersRequest({
            securityId: equity.evmDiamondAddress,
            start: 0,
            end: membersCount,
        }));
        expect(members).toContain(config_1.CLIENT_ACCOUNT_ECDSA_A.id.value);
        await in_1.Security.removeFromControlList(new in_1.ControlListRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
        expect(await in_1.Security.isAccountInControlList(new in_1.ControlListRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).toBe(false);
    }, 600000);
    it('Issue and Redeem', async () => {
        const issuedAmount = '10';
        await in_1.Security.issue(new in_1.IssueRequest({
            amount: issuedAmount,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            securityId: equity.evmDiamondAddress,
        }));
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual(issuedAmount);
        await in_1.Security.redeem(new RedeemRequest_1.default({
            securityId: equity.evmDiamondAddress,
            amount: issuedAmount,
        }));
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual('0');
    }, 600000);
    it('Transfer and Controller Redeem', async () => {
        const issuedAmount = '10';
        const transferredAmount = '1';
        await in_1.Security.issue(new in_1.IssueRequest({
            amount: issuedAmount,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            securityId: equity.evmDiamondAddress,
        }));
        expect((await in_1.Security.transfer(new TransferRequest_1.default({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: transferredAmount,
        }))).payload).toBe(true);
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+issuedAmount - +transferredAmount).toString());
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual(transferredAmount);
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: transferredAmount,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: (+issuedAmount - +transferredAmount).toString(),
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }));
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual('0');
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual('0');
    }, 600000);
    it('TransferAndLock and release', async () => {
        const issuedAmount = '10';
        const transferredAndLockedAmount = '2';
        const expirationTimeStamp = '9991976120';
        await in_1.Security.issue(new in_1.IssueRequest({
            amount: issuedAmount,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            securityId: equity.evmDiamondAddress,
        }));
        expect((await in_1.Security.transferAndLock(new in_1.TransferAndLockRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: transferredAndLockedAmount,
            expirationDate: expirationTimeStamp,
        }))).payload).toBe(1);
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+issuedAmount - +transferredAndLockedAmount).toString());
        expect((await in_1.Security.getLockedBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual(transferredAndLockedAmount);
        const lockCount = await in_1.Security.getLockCount(new in_1.GetLockCountRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
        expect(lockCount).toEqual(1);
        // check locks id
        const locksId = await in_1.Security.getLocksId(new in_1.GetLocksIdRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            start: 0,
            end: 1,
        }));
        expect(locksId.length).toEqual(1);
        expect(locksId[0]).toEqual('1');
        expect((await in_1.Security.release(new in_1.ReleaseRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            lockId: 1,
        }))).payload).toBe(true);
        expect((await in_1.Security.getLockedBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual('0');
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual(transferredAndLockedAmount);
        await in_1.Security.redeem(new RedeemRequest_1.default({
            securityId: equity.evmDiamondAddress,
            amount: (+issuedAmount - +transferredAndLockedAmount).toString(),
        }));
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: transferredAndLockedAmount,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
    }, 600000);
    it('Force transfer securities', async () => {
        const issueAmount = '100';
        const forceTransferAmount = '50';
        // issue securities in redeemed account
        await in_1.Security.issue(new in_1.IssueRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: issueAmount,
        }));
        // do force transfer
        expect((await in_1.Security.controllerTransfer(new in_1.ForceTransferRequest({
            securityId: equity.evmDiamondAddress,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            amount: forceTransferAmount,
        }))).payload).toBe(true);
        // check if transfer origin account has correct balance securities
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual((+issueAmount - +forceTransferAmount).toString());
        // check if transfer origin account has correct balance securities
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+forceTransferAmount).toString());
        await in_1.Security.redeem(new RedeemRequest_1.default({
            securityId: equity.evmDiamondAddress,
            amount: forceTransferAmount,
        }));
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: (+issueAmount - +forceTransferAmount).toString(),
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }));
    }, 600000);
    it('Pause and UnPause a security', async () => {
        expect((await in_1.Security.pause(new in_1.PauseRequest({
            securityId: equity.evmDiamondAddress,
        }))).payload).toBe(true);
        expect(await in_1.Security.isPaused(new in_1.PauseRequest({
            securityId: equity.evmDiamondAddress,
        }))).toBe(true);
        await in_1.Security.unpause(new in_1.PauseRequest({
            securityId: equity.evmDiamondAddress,
        }));
        expect(await in_1.Security.isPaused(new in_1.PauseRequest({
            securityId: equity.evmDiamondAddress,
        }))).toBe(false);
    }, 120000);
    it('Get the security control list type', async () => {
        expect(await in_1.Security.getControlListType(new in_1.GetControlListTypeRequest({
            securityId: equity.evmDiamondAddress,
        }))).toBe(SecurityControlListType_1.SecurityControlListType.BLACKLIST);
    });
    it('Hold balance and Release', async () => {
        const issuedAmount = '10';
        const heldAmount = '2';
        const expirationTimeStamp = '9991976120';
        await in_1.Security.issue(new in_1.IssueRequest({
            amount: issuedAmount,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            securityId: equity.evmDiamondAddress,
        }));
        expect((await in_1.Security.createHoldByPartition(new in_1.CreateHoldByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: Constants_1._PARTITION_ID_1,
            amount: heldAmount,
            escrowId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            expirationDate: expirationTimeStamp,
        }))).payload).toBe(1);
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+issuedAmount - +heldAmount).toString());
        expect(await in_1.Security.getHeldAmountFor(new in_1.GetHeldAmountForRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).toEqual(heldAmount);
        const holdCount = await in_1.Security.getHoldCountForByPartition(new in_1.GetHoldCountForByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            partitionId: Constants_1._PARTITION_ID_1,
        }));
        expect(holdCount).toEqual(1);
        // check hold id
        const holdId = await in_1.Security.getHoldsIdForByPartition(new in_1.GetHoldsIdForByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            partitionId: Constants_1._PARTITION_ID_1,
            start: 0,
            end: 1,
        }));
        expect(holdId.length).toEqual(1);
        expect(holdId[0]).toEqual(1);
        expect((await in_1.Security.releaseHoldByPartition(new in_1.ReleaseHoldByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            holdId: 1,
            partitionId: Constants_1._PARTITION_ID_1,
            amount: heldAmount,
        }))).payload).toBe(true);
        expect(await in_1.Security.getHeldAmountFor(new in_1.GetHeldAmountForRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).toEqual('0');
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+issuedAmount).toString());
        await in_1.Security.redeem(new RedeemRequest_1.default({
            securityId: equity.evmDiamondAddress,
            amount: heldAmount,
        }));
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: (+issuedAmount - +heldAmount).toString(),
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }));
    }, 600000);
    it('Protect and UnProtect a security', async () => {
        expect((await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }))).payload).toBe(true);
        expect(await in_1.Security.arePartitionsProtected(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }))).toBe(true);
        await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }));
        expect(await in_1.Security.arePartitionsProtected(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }))).toBe(false);
    }, 120000);
    it('Protected transfer and redeem securities', async () => {
        const issueAmount = '100';
        const protectedTransferAmount = '50';
        const protectedRedeemAmount = '5';
        const protectedTransferAndLockAmount = '1';
        const partitionBytes32 = '0x0000000000000000000000000000000000000000000000000000000000000001';
        await in_1.Security.issue(new in_1.IssueRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: issueAmount,
        }));
        await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }));
        const encodedValue = ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [SecurityRole_1.SecurityRole._PROTECTED_PARTITIONS_PARTICIPANT_ROLE, partitionBytes32]);
        const hash = (0, js_sha3_1.keccak256)(encodedValue);
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: '0x' + hash,
        }));
        expect((await in_1.Security.protectedTransferFromByPartition(new in_1.ProtectedTransferFromByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: partitionBytes32,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            amount: protectedTransferAmount,
            deadline: '9999999999',
            nounce: 1,
            signature: 'vvvv',
        }))).payload).toBe(true);
        expect((await in_1.Security.protectedRedeemFromByPartition(new in_1.ProtectedRedeemFromByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: partitionBytes32,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: protectedRedeemAmount,
            deadline: '9999999999',
            nounce: 2,
            signature: 'vvvv',
        }))).payload).toBe(true);
        expect((await in_1.Security.protectedTransferAndLockByPartition(new in_1.ProtectedTransferAndLockByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: partitionBytes32,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            expirationDate: '9999999999',
            amount: protectedTransferAndLockAmount,
            deadline: '9999999999',
            nounce: 3,
            signature: 'vvvv',
        }))).payload).toBe(1);
        // check if transfer origin account has correct balance securities
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value).toEqual((+issueAmount -
            +protectedTransferAmount -
            +protectedRedeemAmount -
            +protectedTransferAndLockAmount).toString());
        // check if transfer origin account has correct balance securities
        expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value).toEqual((+protectedTransferAmount).toString());
        await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }));
    }, 600000);
    it('Protected hold securities', async () => {
        const issueAmount = BigInt(100);
        const protectedHoldAmount = BigInt(1);
        const partitionBytes32 = '0x0000000000000000000000000000000000000000000000000000000000000001';
        await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }));
        const balanceECDSA_A = BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value);
        const balanceECDSA = BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value);
        await in_1.Security.issue(new in_1.IssueRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            amount: issueAmount.toString(),
        }));
        const encodedValue = ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [SecurityRole_1.SecurityRole._PROTECTED_PARTITIONS_PARTICIPANT_ROLE, partitionBytes32]);
        const hash = (0, js_sha3_1.keccak256)(encodedValue);
        await in_1.Role.grantRole(new in_1.RoleRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            role: '0x' + hash,
        }));
        expect((await in_1.Security.protectedCreateHoldByPartition(new in_1.ProtectedCreateHoldByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: partitionBytes32,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            expirationDate: '9999999999',
            amount: protectedHoldAmount.toString(),
            deadline: '9999999999',
            nonce: 3,
            signature: 'vvvv',
            escrowId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).payload).toBe(1);
        expect(BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
        }))).value)).toEqual(balanceECDSA_A + issueAmount - protectedHoldAmount);
        expect((await in_1.Security.executeHoldByPartition(new in_1.ExecuteHoldByPartitionRequest({
            securityId: equity.evmDiamondAddress,
            partitionId: partitionBytes32,
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            amount: protectedHoldAmount.toString(),
            holdId: 1,
        }))).payload).toBe(true);
        expect(BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
            securityId: equity.evmDiamondAddress,
            targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }))).value)).toEqual(balanceECDSA + protectedHoldAmount);
        await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
            securityId: equity.evmDiamondAddress,
        }));
        await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
            securityId: equity.evmDiamondAddress,
            amount: (balanceECDSA + protectedHoldAmount).toString(),
            sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
        }));
    }, 600000);
    describe('Clearing tests', () => {
        beforeAll(async () => {
            await in_1.Role.grantRole(new in_1.RoleRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                role: SecurityRole_1.SecurityRole._CLEARING_ROLE,
            }));
            await in_1.Role.grantRole(new in_1.RoleRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                role: SecurityRole_1.SecurityRole._CLEARING_VALIDATOR_ROLE,
            }));
        });
        beforeEach(async () => {
            await in_1.Security.activateClearing(new in_1.ActivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
        });
        afterEach(async () => {
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
        });
        it('Clearing Create Hold', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            const date = new Date(BigDecimal_1.default.fromString(expirationTimeStamp).toBigNumber().toNumber() *
                SecurityDate_1.ONE_THOUSAND);
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.clearingCreateHoldByPartition(new in_1.ClearingCreateHoldByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                amount: clearedAmount,
                escrowId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingExpirationDate: expirationTimeStamp,
                holdExpirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearing = await in_1.Security.getClearingCreateHoldForByPartition(new in_1.GetClearingCreateHoldForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingId: 1,
            }));
            expect(clearing.id).toEqual(1);
            expect(clearing.amount).toEqual(clearedAmount);
            expect(clearing.expirationDate).toEqual(date);
            expect(clearing.data).toEqual('0x');
            expect(clearing.operatorData).toEqual('0x');
            expect(clearing.holdEscrowId).toEqual(config_1.CLIENT_ACCOUNT_ECDSA_A.id.toString());
            expect(clearing.holdExpirationDate).toEqual(date);
            expect(clearing.holdTo).toEqual(config_1.CLIENT_ACCOUNT_ECDSA.id.toString());
            expect(clearing.holdData).toEqual('0x');
            const clearingHoldCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.HoldCreation,
            }));
            expect(clearingHoldCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.HoldCreation,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.reclaimClearingOperationByPartition(new in_1.ReclaimClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Clearing Create Redeem', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            const date = new Date(BigDecimal_1.default.fromString(expirationTimeStamp).toBigNumber().toNumber() *
                SecurityDate_1.ONE_THOUSAND);
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.clearingRedeemByPartition(new in_1.ClearingRedeemByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                amount: clearedAmount,
                expirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearing = await in_1.Security.getClearingRedeemForByPartition(new in_1.GetClearingRedeemForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingId: 1,
            }));
            expect(clearing.id).toEqual(1);
            expect(clearing.amount).toEqual(clearedAmount);
            expect(clearing.expirationDate).toEqual(date);
            expect(clearing.data).toEqual('0x');
            expect(clearing.operatorData).toEqual('0x');
            const clearingRedeemCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }));
            expect(clearingRedeemCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Clearing Create Transfer', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            const date = new Date(BigDecimal_1.default.fromString(expirationTimeStamp).toBigNumber().toNumber() *
                SecurityDate_1.ONE_THOUSAND);
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.clearingTransferByPartition(new in_1.ClearingTransferByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                amount: clearedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                expirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearing = await in_1.Security.getClearingTransferForByPartition(new in_1.GetClearingTransferForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingId: 1,
            }));
            expect(clearing.id).toEqual(1);
            expect(clearing.amount).toEqual(clearedAmount);
            expect(clearing.expirationDate).toEqual(date);
            expect(clearing.destination).toEqual(config_1.CLIENT_ACCOUNT_ECDSA.id.toString());
            expect(clearing.data).toEqual('0x');
            expect(clearing.operatorData).toEqual('0x');
            const clearingTransferCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Transfer,
            }));
            expect(clearingTransferCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Transfer,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.approveClearingOperationByPartition(new in_1.ApproveClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Transfer,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Operator clearing Create Transfer', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.operatorClearingTransferByPartition(new in_1.OperatorClearingTransferByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                amount: clearedAmount,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                expirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearingTransferCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Transfer,
            }));
            expect(clearingTransferCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Transfer,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Operator Clearing Create Redeem', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.operatorClearingRedeemByPartition(new in_1.OperatorClearingRedeemByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                amount: clearedAmount,
                expirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearingRedeemCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }));
            expect(clearingRedeemCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Operator Clearing Create Hold', async () => {
            const issuedAmount = '10';
            const clearedAmount = '2';
            const expirationTimeStamp = '9991976120';
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.issue(new in_1.IssueRequest({
                amount: issuedAmount,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                securityId: equity.evmDiamondAddress,
            }));
            expect((await in_1.Security.operatorClearingCreateHoldByPartition(new in_1.OperatorClearingCreateHoldByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                amount: clearedAmount,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                escrowId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingExpirationDate: expirationTimeStamp,
                holdExpirationDate: expirationTimeStamp,
            }))).payload).toBe(1);
            expect((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value).toEqual((+issuedAmount - +clearedAmount).toString());
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(+clearedAmount);
            const clearingHoldCount = await in_1.Security.getClearingCountForByPartition(new in_1.GetClearingCountForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.HoldCreation,
            }));
            expect(clearingHoldCount).toEqual(1);
            const clearingId = await in_1.Security.getClearingsIdForByPartition(new in_1.GetClearingsIdForByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                partitionId: Constants_1._PARTITION_ID_1,
                clearingOperationType: Clearing_1.ClearingOperationType.HoldCreation,
                start: 0,
                end: 1,
            }));
            expect(clearingId.length).toEqual(1);
            expect(clearingId[0]).toEqual(1);
            expect((await in_1.Security.reclaimClearingOperationByPartition(new in_1.ReclaimClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: (+issuedAmount).toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }));
        }, 600000);
        it('Protected Clearing Hold securities', async () => {
            const issueAmount = BigInt(100);
            const protectedClearingAmount = BigInt(1);
            const partitionBytes32 = '0x0000000000000000000000000000000000000000000000000000000000000001';
            await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            const balanceECDSA_A = BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }))).value);
            await in_1.Security.issue(new in_1.IssueRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                amount: issueAmount.toString(),
            }));
            const encodedValue = ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [SecurityRole_1.SecurityRole._PROTECTED_PARTITIONS_PARTICIPANT_ROLE, partitionBytes32]);
            const hash = (0, js_sha3_1.keccak256)(encodedValue);
            await in_1.Role.grantRole(new in_1.RoleRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                role: '0x' + hash,
            }));
            expect((await in_1.Security.protectedClearingCreateHoldByPartition(new in_1.ProtectedClearingCreateHoldByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: partitionBytes32,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                holdExpirationDate: '9999999999',
                clearingExpirationDate: '9999999999',
                amount: protectedClearingAmount.toString(),
                deadline: '9999999999',
                nonce: 3,
                signature: 'vvvv',
                escrowId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }))).payload).toBe(1);
            expect(BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }))).value)).toEqual(balanceECDSA_A + issueAmount - protectedClearingAmount);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: issueAmount.toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
            }));
        }, 600000);
        it('Protected Clearing Redeem securities', async () => {
            const issueAmount = BigInt(100);
            const protectedClearingAmount = BigInt(1);
            const partitionBytes32 = '0x0000000000000000000000000000000000000000000000000000000000000001';
            await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            const balanceECDSA = BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value);
            await in_1.Security.issue(new in_1.IssueRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                amount: issueAmount.toString(),
            }));
            const encodedValue = ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [SecurityRole_1.SecurityRole._PROTECTED_PARTITIONS_PARTICIPANT_ROLE, partitionBytes32]);
            const hash = (0, js_sha3_1.keccak256)(encodedValue);
            await in_1.Role.grantRole(new in_1.RoleRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                role: '0x' + hash,
            }));
            expect((await in_1.Security.protectedClearingRedeemByPartition(new in_1.ProtectedClearingRedeemByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: partitionBytes32,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                expirationDate: '9999999999',
                amount: protectedClearingAmount.toString(),
                deadline: '9999999999',
                nonce: 3,
                signature: 'vvvv',
            }))).payload).toBe(1);
            expect(BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value)).toEqual(balanceECDSA + issueAmount - protectedClearingAmount);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: issueAmount.toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
        it('Protected Clearing Transfer securities', async () => {
            const issueAmount = BigInt(100);
            const protectedClearingAmount = BigInt(1);
            const partitionBytes32 = '0x0000000000000000000000000000000000000000000000000000000000000001';
            await in_1.Security.protectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            const balanceECDSA = BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value);
            await in_1.Security.issue(new in_1.IssueRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                amount: issueAmount.toString(),
            }));
            const encodedValue = ethers_1.ethers.utils.defaultAbiCoder.encode(['bytes32', 'bytes32'], [SecurityRole_1.SecurityRole._PROTECTED_PARTITIONS_PARTICIPANT_ROLE, partitionBytes32]);
            const hash = (0, js_sha3_1.keccak256)(encodedValue);
            await in_1.Role.grantRole(new in_1.RoleRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                role: '0x' + hash,
            }));
            expect((await in_1.Security.protectedClearingTransferByPartition(new in_1.ProtectedClearingTransferByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: partitionBytes32,
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                targetId: config_1.CLIENT_ACCOUNT_ECDSA_A.evmAddress.toString(),
                expirationDate: '9999999999',
                amount: protectedClearingAmount.toString(),
                deadline: '9999999999',
                nonce: 3,
                signature: 'vvvv',
            }))).payload).toBe(1);
            expect(BigInt((await in_1.Security.getBalanceOf(new in_1.GetAccountBalanceRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).value)).toEqual(balanceECDSA + issueAmount - protectedClearingAmount);
            expect((await in_1.Security.cancelClearingOperationByPartition(new in_1.CancelClearingOperationByPartitionRequest({
                securityId: equity.evmDiamondAddress,
                partitionId: Constants_1._PARTITION_ID_1,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
                clearingId: 1,
                clearingOperationType: Clearing_1.ClearingOperationType.Redeem,
            }))).payload).toBe(true);
            expect(await in_1.Security.getClearedAmountFor(new in_1.GetClearedAmountForRequest({
                securityId: equity.evmDiamondAddress,
                targetId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }))).toEqual(0);
            await in_1.Security.unprotectPartitions(new in_1.PartitionsProtectedRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.deactivateClearing(new in_1.DeactivateClearingRequest({
                securityId: equity.evmDiamondAddress,
            }));
            await in_1.Security.controllerRedeem(new in_1.ForceRedeemRequest({
                securityId: equity.evmDiamondAddress,
                amount: issueAmount.toString(),
                sourceId: config_1.CLIENT_ACCOUNT_ECDSA.evmAddress.toString(),
            }));
        }, 600000);
    });
});
