diff --git a/contracts/BosonRouter.sol b/contracts/BosonRouter.sol index 26715241..e15cb085 100644 --- a/contracts/BosonRouter.sol +++ b/contracts/BosonRouter.sol @@ -700,6 +700,13 @@ contract BosonRouter is address tokenPriceAddress = IVoucherKernel(voucherKernel) .getVoucherPriceToken(_tokenIdSupply); + IVoucherKernel(voucherKernel).fillOrder( + _tokenIdSupply, + _issuer, + msg.sender, + TKNETH + ); + permitTransferFromAndAddEscrow( tokenPriceAddress, price, @@ -708,14 +715,7 @@ contract BosonRouter is _r, _s ); - - IVoucherKernel(voucherKernel).fillOrder( - _tokenIdSupply, - _issuer, - msg.sender, - TKNETH - ); - + //record funds in escrow ... ICashier(cashierAddress).addEscrowAmount{value: msg.value}(msg.sender); } diff --git a/test/11_gate.ts b/test/11_gate.ts index 55f5b73a..48c4e5e9 100644 --- a/test/11_gate.ts +++ b/test/11_gate.ts @@ -219,10 +219,13 @@ describe('Gate contract', async () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, gate, - conditionalOrderNftTokenID + conditionalOrderNftTokenID, + true ); const txReceipt = await txOrder.wait(); @@ -475,7 +478,13 @@ describe('Gate contract', async () => { expect(await contractGate.check(users.buyer.address, tokenId)).to.be .true; - await utils.commitToBuy(users.buyer, users.seller, tokenId); + await utils.commitToBuy( + users.buyer, + users.seller, + tokenId, + constants.product_price, + constants.buyer_deposit + ); expect(await contractGate.check(users.buyer.address, tokenId)).to.be .false; diff --git a/test/12_conditional_commit.ts b/test/12_conditional_commit.ts index 06c13585..01f2ebe7 100644 --- a/test/12_conditional_commit.ts +++ b/test/12_conditional_commit.ts @@ -1827,10 +1827,13 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.PROMISE_DEPOSITSE1, + constants.buyer_deposit, constants.QTY_10, contractGate, - 0 + 0, + true ); const txReceipt = await txOrder.wait(); @@ -1929,7 +1932,13 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm }); it('[NEGATIVE] Should not be able to request voucher twice', async () => { - await utils.commitToBuy(users.buyer, users.seller, tokenSupplyKey); + await utils.commitToBuy( + users.buyer, + users.seller, + tokenSupplyKey, + constants.product_price, + constants.buyer_deposit + ); const {txValue, DEPOSIT, PRICE} = await generateInputs( users.buyer, @@ -2000,7 +2009,9 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, users.other1, /// gate address that maps to EOA 0 @@ -2045,7 +2056,9 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, contractGate, 0 @@ -2154,10 +2167,13 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.PROMISE_DEPOSITSE1, + constants.buyer_deposit, constants.QTY_10, contractGate, - 0 + 0, + true ); const txReceipt = await txOrder.wait(); @@ -2314,7 +2330,9 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, users.other1, /// gate address that maps to EOA 0 @@ -2349,7 +2367,9 @@ describe('Create Voucher sets and commit to vouchers with token conditional comm users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, contractGate, 0 diff --git a/test/2_test_fullpath_with_permit.ts b/test/2_test_fullpath_with_permit.ts index 7d734b04..e75bba8e 100644 --- a/test/2_test_fullpath_with_permit.ts +++ b/test/2_test_fullpath_with_permit.ts @@ -16,6 +16,7 @@ import { TokenRegistry, MockERC20Permit, } from '../typechain'; +const {keccak256, solidityPack} = ethers.utils; let ERC1155ERC721_Factory: ContractFactory; let VoucherKernel_Factory: ContractFactory; @@ -35,6 +36,8 @@ let utils: Utils; let users; describe('Cashier and VoucherKernel', () => { + let promiseId: string, tokenSupplyKey: string; + before(async () => { const signers: Signer[] = await ethers.getSigners(); users = new Users(signers); @@ -47,6 +50,8 @@ describe('Cashier and VoucherKernel', () => { MockERC20Permit_Factory = await ethers.getContractFactory( 'MockERC20Permit' ); + + await setPeriods(); }); let contractERC1155ERC721: ERC1155ERC721, @@ -57,11 +62,6 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit: MockERC20Permit, contractTokenRegistry: TokenRegistry; - let tokenSupplyKey, tokenVoucherKey, tokenVoucherKey1; - - const ZERO = BN(0); - const ONE_VOUCHER = 1; - const deadline = toWei(1); let timestamp; @@ -160,10 +160,28 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit.address, contractBSNTokenDeposit.address ); + + // calculate expected tokenSupplyID for first voucher + promiseId = keccak256( + solidityPack( + ['address', 'uint256', 'uint256', 'uint256', 'address'], + [ + users.seller.address, + constants.ZERO, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + contractVoucherKernel.address, + ] + ) + ); + + // calculate expected tokenSupplyID for first voucher + const tokenIndex = constants.ONE; + const TYPE_NF_BIT = constants.ONE.shl(255); + tokenSupplyKey = TYPE_NF_BIT.or(tokenIndex.shl(128)).toString(); } describe('TOKEN SUPPLY CREATION (Voucher batch creation)', () => { - let remQty = constants.QTY_10 as number | string; const vouchersToBuy = 5; const paymentMethods = { @@ -173,12 +191,8 @@ describe('Cashier and VoucherKernel', () => { TKNTKN: 4, }; - afterEach(() => { - remQty = constants.QTY_10; - }); - describe('ETHETH', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -189,180 +203,317 @@ describe('Cashier and VoucherKernel', () => { contractCashier, contractBosonRouter ); - - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); }); - it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { - await expect( - utils.createOrder( + it('All expected events are emitted', async () => { + expect( + await utils.createOrder( users.seller, - timestamp, - timestamp + constants.ONE_MINUTE, - constants.seller_deposit, - constants.QTY_10 + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) - ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); + ) + .to.emit(contractBosonRouter, eventNames.LOG_ORDER_CREATED) + .withArgs( + tokenSupplyKey, + users.seller.address, + constants.QTY_10, + paymentMethods.ETHETH + ) + .to.emit(contractVoucherKernel, eventNames.LOG_PROMISE_CREATED) + .withArgs( + promiseId, + constants.ONE, + users.seller.address, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ZERO + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + constants.ZERO_ADDRESS, + users.seller.address, + tokenSupplyKey, + constants.QTY_10 + ); }); - it('ESCROW has correct initial balance', async () => { - const expectedBalance = BN(constants.seller_deposit).mul(BN(remQty)); - const escrowAmount = await contractCashier.getEscrowAmount( - users.seller.address - ); + describe('After creation', () => { + beforeEach(async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ); + }); - assert.isTrue( - escrowAmount.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + describe('Voucher Kernel state', () => { + it('Promise info is correct', async () => { + const promiseData = await contractVoucherKernel.getPromiseData( + promiseId + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.promiseId], + promiseId, + 'Promise Id incorrect' + ); - it('Cashier Contract has correct amount of ETH', async () => { - const expectedBalance = BN(constants.seller_deposit).mul(BN(remQty)); - const cashierBalance = await ethers.provider.getBalance( - contractCashier.address - ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.nonce].toString(), + constants.ONE.toString(), + 'Promise data field -> nonce is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validFrom].toString(), + constants.PROMISE_VALID_FROM.toString(), + 'Promise data field -> validFrom is incorrect' + ); - assert.isTrue( - BN(cashierBalance).eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validTo].toString(), + constants.PROMISE_VALID_TO.toString(), + 'Promise data field -> validTo is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.idx].toString(), + constants.ZERO.toString(), + 'Promise data field -> idx is incorrect' + ); - it('Get correct remaining qty for supply', async () => { - let remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseSeller = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); - assert.equal( - remainingQtyInContract.toString(), - remQty.toString(), - 'Remaining qty is not correct' - ); + assert.strictEqual( + promiseSeller, + users.seller.address, + 'Seller incorrect' + ); - for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, tokenSupplyKey); - remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.PROMISE_DEPOSITBU1) + ), + 'Promise buyer deposit mismatch' + ); + + const tokenNonce = await contractVoucherKernel.getTokenNonce( + users.seller.address + ); - remQty = BN(remQty).sub(1).toString(); + assert.isTrue( + tokenNonce.eq(constants.ONE.toString()), + 'Voucher kernel nonce mismatch' + ); - assert.equal( - remainingQtyInContract.toString(), - remQty, - 'Remaining qty is not correct' + assert.equal( + promiseId, + await contractVoucherKernel.getPromiseIdFromSupplyId( + tokenSupplyKey + ), + 'PromiseId mismatch' + ); + }); + + it('Should create payment method ETHETH', async () => { + expect( + await contractVoucherKernel.getVoucherPaymentMethod( + tokenSupplyKey + ) + ).to.equal( + paymentMethods.ETHETH, + 'Payment Method ETHETH not set correctly' + ); + }); + + it('Deposit and Price address should be constants.ZERO', async () => { + expect( + await contractVoucherKernel.getVoucherPriceToken(tokenSupplyKey) + ).to.equal( + constants.ZERO_ADDRESS, + 'ETHETH Method Price Token Address mismatch' + ); + + expect( + await contractVoucherKernel.getVoucherDepositToken(tokenSupplyKey) + ).to.equal( + constants.ZERO_ADDRESS, + 'ETHETH Method Deposit Token Address mismatch' + ); + }); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + tokenSupplyKey + ) + )[0]; + + assert.isTrue( + sellerERC1155ERC721Balance.eq(constants.QTY_10), + 'ERC1155ERC721 seller balance mismatch' ); - } + }); + + it('ESCROW has correct balance', async () => { + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + + expect( + await ethers.provider.getBalance(contractCashier.address) + ).to.equal(expectedBalance, 'Escrow balance is incorrect'); + expect( + await contractCashier.getEscrowAmount(users.seller.address) + ).to.equal(expectedBalance, 'Escrow stored amount is incorrect'); + }); + + it('Get correct remaining qty for supply', async () => { + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal(constants.QTY_10, 'Remaining qty is not correct'); + + for (let i = 0; i < vouchersToBuy; i++) { + await utils.commitToBuy( + users.buyer, + users.seller, + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal( + constants.QTY_10 - i - 1, + `Remaining qty is not correct [${i}]` + ); + } + }); }); - it('Should create payment method ETHETH', async () => { - timestamp = await Utils.getCurrTimestamp(); - const tokenSupplyKey = await utils.createOrder( + it('It should be possible to create Order with 0 buyer deposit', async () => { + await utils.createOrder( users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); - - const paymentMethod = await contractVoucherKernel.getVoucherPaymentMethod( - tokenSupplyKey + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + constants.QTY_10, + true ); - - const addressTokenPrice = await contractVoucherKernel.getVoucherPriceToken( + const promiseOrderData = await contractVoucherKernel.getOrderCosts( tokenSupplyKey ); - - const addressTokenDeposits = await contractVoucherKernel.getVoucherDepositToken( - tokenSupplyKey + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' ); - assert.equal( - paymentMethod.toString(), - paymentMethods.ETHETH.toString(), - 'Payment Method ETHETH not set correctly' - ); - assert.equal( - addressTokenPrice.toString(), - constants.ZERO_ADDRESS, - 'ETHETH Method Price Token Address mismatch' + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' ); - assert.equal( - addressTokenDeposits.toString(), - constants.ZERO_ADDRESS, - 'ETHETH Method Deposit Token Address mismatch' + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.ZERO) + ), + 'Promise buyer deposit mismatch' ); }); - it('[NEGATIVE] Should not create a supply if price is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect(users.seller.signer); + it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { + await expect( + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ) + ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); + }); + it('[NEGATIVE] Should not create a supply if price is above the limit', async () => { await expect( - sellerInstance.requestCreateOrderETHETH( - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.ABOVE_ETH_LIMIT, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ], - {value: txValue} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.ABOVE_ETH_LIMIT, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.ONE, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositBu is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect(users.seller.signer); - await expect( - sellerInstance.requestCreateOrderETHETH( - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.ABOVE_ETH_LIMIT, - constants.ORDER_QUANTITY1, - ], - {value: txValue} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ABOVE_ETH_LIMIT, + constants.ONE, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositSe is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect(users.seller.signer); - await expect( - sellerInstance.requestCreateOrderETHETH( - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.ABOVE_ETH_LIMIT, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ], - {value: txValue} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.PROMISE_PRICE1, + constants.ABOVE_ETH_LIMIT, + constants.PROMISE_DEPOSITBU1, + constants.ONE, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); @@ -370,7 +521,7 @@ describe('Cashier and VoucherKernel', () => { describe('[WITH PERMIT]', () => { describe('ETHTKN', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -399,173 +550,291 @@ describe('Cashier and VoucherKernel', () => { users.buyer.address, tokensToMint ); - - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); }); - it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { + it('All expected events are emitted', async () => { await expect( utils.createOrder( users.seller, - timestamp, - timestamp + constants.ONE_MINUTE, - constants.seller_deposit, - constants.QTY_10 + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) - ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); + ) + .to.emit(contractBosonRouter, eventNames.LOG_ORDER_CREATED) + .withArgs( + tokenSupplyKey, + users.seller.address, + constants.QTY_10, + paymentMethods.ETHTKN + ) + .to.emit(contractVoucherKernel, eventNames.LOG_PROMISE_CREATED) + .withArgs( + promiseId, + constants.ONE, + users.seller.address, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ZERO + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + constants.ZERO_ADDRESS, + users.seller.address, + tokenSupplyKey, + constants.QTY_10 + ); }); - it('Cashier has correct balance in Deposit Contract', async () => { - const expectedBalance = BN(constants.seller_deposit).mul( - BN(constants.QTY_10) - ); - const escrowAmount = await contractBSNTokenDeposit.balanceOf( - contractCashier.address - ); + describe('After creation', () => { + beforeEach(async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ); + }); - assert.isTrue( - escrowAmount.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + describe('Voucher Kernel state', () => { + it('Promise info is correct', async () => { + const promiseData = await contractVoucherKernel.getPromiseData( + promiseId + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.promiseId], + promiseId, + 'Promise Id incorrect' + ); - it('escrowTokens has correct balance', async () => { - const expectedBalance = BN(constants.seller_deposit).mul( - BN(constants.QTY_10) - ); - const escrowTokens = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.seller.address - ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.nonce].toString(), + constants.ONE.toString(), + 'Promise data field -> nonce is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validFrom].toString(), + constants.PROMISE_VALID_FROM.toString(), + 'Promise data field -> validFrom is incorrect' + ); - assert.isTrue( - escrowTokens.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validTo].toString(), + constants.PROMISE_VALID_TO.toString(), + 'Promise data field -> validTo is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.idx].toString(), + constants.ZERO.toString(), + 'Promise data field -> idx is incorrect' + ); - it('Get correct remaining qty for supply', async () => { - let remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); - assert.equal( - remainingQtyInContract.toString(), - remQty.toString(), - 'Remaining qty is not correct' - ); + const promiseSeller = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); - for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, tokenSupplyKey); - remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + assert.strictEqual( + promiseSeller, + users.seller.address, + 'Seller incorrect' + ); - remQty = BN(remQty).sub(1).toString(); + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.PROMISE_DEPOSITBU1) + ), + 'Promise buyer deposit mismatch' + ); - assert.equal( - remainingQtyInContract.toString(), - remQty, - 'Remaining qty is not correct' + const tokenNonce = await contractVoucherKernel.getTokenNonce( + users.seller.address + ); + + assert.isTrue( + tokenNonce.eq(constants.ONE), + 'Voucher kernel nonce mismatch' + ); + + assert.equal( + promiseId, + await contractVoucherKernel.getPromiseIdFromSupplyId( + tokenSupplyKey + ), + 'PromiseId mismatch' + ); + }); + + it('Should create payment method ETHTKN', async () => { + expect( + await contractVoucherKernel.getVoucherPaymentMethod( + tokenSupplyKey + ) + ).to.equal( + paymentMethods.ETHTKN, + 'Payment Method ETHTKN not set correctly' + ); + }); + + it('Deposit contract should be correct and Price address should be constants.ZERO', async () => { + expect( + await contractVoucherKernel.getVoucherPriceToken(tokenSupplyKey) + ).to.equal( + constants.ZERO_ADDRESS, + 'ETHTKN Method Price Token Address mismatch' + ); + + expect( + await contractVoucherKernel.getVoucherDepositToken( + tokenSupplyKey + ) + ).to.equal( + contractBSNTokenDeposit.address, + 'ETHTKN Method Deposit Token Address mismatch' + ); + }); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + tokenSupplyKey + ) + )[0]; + + assert.isTrue( + sellerERC1155ERC721Balance.eq(constants.QTY_10), + 'ERC1155ERC721 seller balance mismatch' ); - } - }); + }); - it('Should create payment method ETHTKN', async () => { - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); + it('Cashier has correct balance in Deposit Contract', async () => { + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + expect( + await contractBSNTokenDeposit.balanceOf(contractCashier.address) + ).to.equal(expectedBalance, 'Escrow amount is incorrect'); + }); - const paymentMethod = await contractVoucherKernel.getVoucherPaymentMethod( - tokenSupplyKey - ); + it('escrowTokens has correct balance', async () => { + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + const escrowTokens = await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.seller.address + ); - const addressTokenPrice = await contractVoucherKernel.getVoucherPriceToken( - tokenSupplyKey - ); + assert.isTrue( + escrowTokens.eq(expectedBalance), + 'Escrow amount is incorrect' + ); + }); - const addressTokenDeposits = await contractVoucherKernel.getVoucherDepositToken( - tokenSupplyKey - ); + it('Get correct remaining qty for supply', async () => { + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal(constants.QTY_10, 'Remaining qty is not correct'); - assert.equal( - paymentMethod.toString(), - paymentMethods.ETHTKN.toString(), - 'Payment Method ETHTKN not set correctly' - ); - assert.equal( - addressTokenPrice.toString(), - constants.ZERO_ADDRESS, - 'ETHTKN Method Price Token Address mismatch' - ); - assert.equal( - addressTokenDeposits.toString(), - contractBSNTokenDeposit.address, - 'ETHTKN Method Deposit Token Address mismatch' - ); + for (let i = 0; i < vouchersToBuy; i++) { + await utils.commitToBuy( + users.buyer, + users.seller, + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal( + constants.QTY_10 - i - 1, + `Remaining qty is not correct [${i}]` + ); + } + }); }); - it('[NEGATIVE] Should fail if token deposit contract address is not provided', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address + it('It should be possible to create Order with 0 buyer deposit', async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + constants.QTY_10, + true ); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractCashier.address, - txValue, - nonce, - deadline + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' ); - const sellerInstance = contractBosonRouter.connect( - users.seller.signer + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.ZERO) + ), + 'Promise buyer deposit mismatch' ); + }); + it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { await expect( - sellerInstance.requestCreateOrderETHTKNWithPermit( - '', - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 ) - ).to.be.reverted; + ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); }); - it('[NEGATIVE] Should revert if token deposit contract address is zero address', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); + it('[NEGATIVE] Should revert if token deposit contract address is constants.ZERO address', async () => { + const txValue = BN(constants.seller_deposit).mul(BN(constants.ONE)); const nonce = await contractBSNTokenDeposit.nonces( users.seller.address ); @@ -609,143 +878,53 @@ describe('Cashier and VoucherKernel', () => { }); it('[NEGATIVE] Should not create a supply if price is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - const deadline = toWei(1); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderETHTKNWithPermit( - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.ABOVE_ETH_LIMIT, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ABOVE_ETH_LIMIT, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositBu is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - const deadline = toWei(1); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderETHTKNWithPermit( - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.ABOVE_TOKEN_LIMIT, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ABOVE_TOKEN_LIMIT, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositSe is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - const deadline = toWei(1); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderETHTKNWithPermit( - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.ABOVE_TOKEN_LIMIT, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.ABOVE_TOKEN_LIMIT, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); }); describe('TKNETH', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -757,13 +936,9 @@ describe('Cashier and VoucherKernel', () => { contractCashier, contractBosonRouter, contractBSNTokenPrice, - '' + contractBSNTokenDeposit ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - const tokensToMint = BN(constants.product_price).mul( BN(constants.QTY_10) ); @@ -772,143 +947,265 @@ describe('Cashier and VoucherKernel', () => { users.buyer.address, tokensToMint ); - - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); }); - it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { + it('All expected events are emitted', async () => { await expect( utils.createOrder( users.seller, - timestamp, - timestamp + constants.ONE_MINUTE, - constants.seller_deposit, - constants.QTY_10 + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) - ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); + ) + .to.emit(contractBosonRouter, eventNames.LOG_ORDER_CREATED) + .withArgs( + tokenSupplyKey, + users.seller.address, + constants.QTY_10, + paymentMethods.TKNETH + ) + .to.emit(contractVoucherKernel, eventNames.LOG_PROMISE_CREATED) + .withArgs( + promiseId, + constants.ONE, + users.seller.address, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ZERO + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + constants.ZERO_ADDRESS, + users.seller.address, + tokenSupplyKey, + constants.QTY_10 + ); }); - it('ESCROW has correct balance', async () => { - const expectedBalance = BN(constants.seller_deposit).mul(BN(remQty)); - const escrowAmount = await contractCashier.getEscrowAmount( - users.seller.address - ); + describe('After creation', () => { + beforeEach(async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ); + }); - assert.isTrue( - escrowAmount.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + describe('Voucher Kernel state', () => { + it('Promise info is correct', async () => { + const promiseData = await contractVoucherKernel.getPromiseData( + promiseId + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.promiseId], + promiseId, + 'Promise Id incorrect' + ); - it('Cashier Contract has correct amount of ETH', async () => { - const expectedBalance = BN(constants.seller_deposit).mul(BN(remQty)); - const cashierBalance = await ethers.provider.getBalance( - contractCashier.address - ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.nonce].toString(), + constants.ONE.toString(), + 'Promise data field -> nonce is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validFrom].toString(), + constants.PROMISE_VALID_FROM.toString(), + 'Promise data field -> validFrom is incorrect' + ); - assert.isTrue( - BN(cashierBalance).eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validTo].toString(), + constants.PROMISE_VALID_TO.toString(), + 'Promise data field -> validTo is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.idx].toString(), + constants.ZERO.toString(), + 'Promise data field -> idx is incorrect' + ); - it('Get correct remaining qty for supply', async () => { - let remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseSeller = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); - assert.equal( - remainingQtyInContract.toString(), - remQty.toString(), - 'Remaining qty is not correct' - ); + assert.strictEqual( + promiseSeller, + users.seller.address, + 'Seller incorrect' + ); - for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, tokenSupplyKey); - remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.PROMISE_DEPOSITBU1) + ), + 'Promise buyer deposit mismatch' + ); - remQty = BN(remQty).sub(1).toString(); + const tokenNonce = await contractVoucherKernel.getTokenNonce( + users.seller.address + ); - assert.equal( - remainingQtyInContract.toString(), - remQty, - 'Remaining qty is not correct' + assert.isTrue( + tokenNonce.eq(constants.ONE.toString()), + 'Voucher kernel nonce mismatch' + ); + + assert.equal( + promiseId, + await contractVoucherKernel.getPromiseIdFromSupplyId( + tokenSupplyKey + ), + 'PromiseId mismatch' + ); + }); + + it('Should create payment method TKNETH', async () => { + expect( + await contractVoucherKernel.getVoucherPaymentMethod( + tokenSupplyKey + ) + ).to.equal( + paymentMethods.TKNETH, + 'Payment Method TKNETH not set correctly' + ); + }); + + it('Price address should be correct and Deposit should be constants.ZERO', async () => { + expect( + await contractVoucherKernel.getVoucherPriceToken(tokenSupplyKey) + ).to.equal( + contractBSNTokenPrice.address, + 'TKNETH Method Price Token Address mismatch' + ); + + expect( + await contractVoucherKernel.getVoucherDepositToken( + tokenSupplyKey + ) + ).to.equal( + constants.ZERO_ADDRESS, + 'TKNETH Method Deposit Token Address mismatch' + ); + }); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + tokenSupplyKey + ) + )[0]; + + assert.isTrue( + sellerERC1155ERC721Balance.eq(constants.QTY_10), + 'ERC1155ERC721 seller balance mismatch' ); - } - }); + }); - it('Should create payment method TKNETH', async () => { - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_1 - ); + it('ESCROW has correct balance', async () => { + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); - const paymentMethod = await contractVoucherKernel.getVoucherPaymentMethod( - tokenSupplyKey - ); + expect( + await ethers.provider.getBalance(contractCashier.address) + ).to.equal(expectedBalance, 'Escrow balance is incorrect'); + expect( + await contractCashier.getEscrowAmount(users.seller.address) + ).to.equal(expectedBalance, 'Escrow stored amount is incorrect'); + }); - const addressTokenPrice = await contractVoucherKernel.getVoucherPriceToken( - tokenSupplyKey - ); + it('Get correct remaining qty for supply', async () => { + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal(constants.QTY_10, 'Remaining qty is not correct'); - const addressTokenDeposits = await contractVoucherKernel.getVoucherDepositToken( - tokenSupplyKey - ); + for (let i = 0; i < vouchersToBuy; i++) { + await utils.commitToBuy( + users.buyer, + users.seller, + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal( + constants.QTY_10 - i - 1, + `Remaining qty is not correct [${i}]` + ); + } + }); + }); - assert.equal( - paymentMethod.toString(), - paymentMethods.TKNETH.toString(), - 'Payment Method TKNETH not set correctly' + it('It should be possible to create Order with 0 buyer deposit', async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + constants.QTY_10, + true ); - assert.equal( - addressTokenPrice.toString(), - contractBSNTokenPrice.address, - 'TKNETH Method Price Token Address mismatch' + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey ); - assert.equal( - addressTokenDeposits.toString(), - constants.ZERO_ADDRESS, - 'TKNETH Method Deposit Token Address mismatch' + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' ); - }); - - it('[NEGATIVE] Should fail if price token contract address is not provided', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const sellerInstance = contractBosonRouter.connect( - users.seller.signer + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.ZERO) + ), + 'Promise buyer deposit mismatch' ); - - await expect( - sellerInstance.requestCreateOrderTKNETH( - '', - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ], - {value: txValue.toString()} - ) - ).to.be.reverted; }); - it('[NEGATIVE] Should fail if token price contract is zero address', async () => { + it('[NEGATIVE] Should fail if token price contract is constants.ZERO address', async () => { const sellerInstance = contractBosonRouter.connect( users.seller.signer ); @@ -926,77 +1223,53 @@ describe('Cashier and VoucherKernel', () => { }); it('[NEGATIVE] Should not create a supply if price is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNETH( - contractBSNTokenPrice.address, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.ABOVE_TOKEN_LIMIT, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ], - {value: txValue.toString()} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ABOVE_TOKEN_LIMIT, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositBu is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNETH( - contractBSNTokenPrice.address, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.ABOVE_ETH_LIMIT, - constants.ORDER_QUANTITY1, - ], - {value: txValue.toString()} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ABOVE_TOKEN_LIMIT, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositSe is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNETH( - contractBSNTokenPrice.address, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.ABOVE_ETH_LIMIT, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ], - {value: txValue.toString()} + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.ABOVE_ETH_LIMIT, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); }); describe('TKNTKN', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -1011,10 +1284,6 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - const tokensToMint = BN(constants.product_price).mul( BN(constants.QTY_20) ); @@ -1034,234 +1303,291 @@ describe('Cashier and VoucherKernel', () => { users.buyer.address, tokensToMint ); - - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 - ); }); - it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { + it('All expected events are emitted', async () => { await expect( utils.createOrder( users.seller, - timestamp, - timestamp + constants.ONE_MINUTE, - constants.seller_deposit, - constants.QTY_10 + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) - ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); + ) + .to.emit(contractBosonRouter, eventNames.LOG_ORDER_CREATED) + .withArgs( + tokenSupplyKey, + users.seller.address, + constants.QTY_10, + paymentMethods.TKNTKN + ) + .to.emit(contractVoucherKernel, eventNames.LOG_PROMISE_CREATED) + .withArgs( + promiseId, + constants.ONE, + users.seller.address, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ZERO + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + constants.ZERO_ADDRESS, + users.seller.address, + tokenSupplyKey, + constants.QTY_10 + ); }); - it('Cashier has correct balance in Deposit Contract', async () => { - const expectedBalance = BN(constants.seller_deposit).mul(BN(remQty)); - const escrowAmount = await contractBSNTokenDeposit.balanceOf( - contractCashier.address - ); + describe('After creation', () => { + beforeEach(async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ); + }); - assert.isTrue( - escrowAmount.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + describe('Voucher Kernel state', () => { + it('Promise info is correct', async () => { + const promiseData = await contractVoucherKernel.getPromiseData( + promiseId + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.promiseId], + promiseId, + 'Promise Id incorrect' + ); - it('escrowTokens has correct balance', async () => { - const expectedBalance = BN(constants.seller_deposit).mul( - BN(constants.QTY_10) - ); - const escrowTokens = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.seller.address - ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.nonce].toString(), + constants.ONE.toString(), + 'Promise data field -> nonce is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validFrom].toString(), + constants.PROMISE_VALID_FROM.toString(), + 'Promise data field -> validFrom is incorrect' + ); - assert.isTrue( - escrowTokens.eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.validTo].toString(), + constants.PROMISE_VALID_TO.toString(), + 'Promise data field -> validTo is incorrect' + ); + assert.equal( + promiseData[constants.PROMISE_DATA_FIELDS.idx].toString(), + constants.ZERO.toString(), + 'Promise data field -> idx is incorrect' + ); - it('Get correct remaining qty for supply', async () => { - let remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseSeller = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); - assert.equal( - remainingQtyInContract.toString(), - remQty.toString(), - 'Remaining qty is not correct' - ); + assert.strictEqual( + promiseSeller, + users.seller.address, + 'Seller incorrect' + ); - for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, tokenSupplyKey); - remainingQtyInContract = await contractVoucherKernel.getRemQtyForSupply( - tokenSupplyKey, - users.seller.address - ); + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.PROMISE_DEPOSITBU1) + ), + 'Promise buyer deposit mismatch' + ); - remQty = BN(remQty).sub(1).toString(); + const tokenNonce = await contractVoucherKernel.getTokenNonce( + users.seller.address + ); - assert.equal( - remainingQtyInContract.toString(), - remQty, - 'Remaining qty is not correct' - ); - } + assert.isTrue( + tokenNonce.eq(constants.ONE.toString()), + 'Voucher kernel nonce mismatch' + ); - it('Create Conditional Commit', async () => { - expect( - await utils.createOrderConditional( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10, - users.seller, - 0 - ) - ).to.emit( - contractBosonRouter, - eventNames.LOG_CONDITIONAL_ORDER_CREATED - ); + assert.equal( + promiseId, + await contractVoucherKernel.getPromiseIdFromSupplyId( + tokenSupplyKey + ), + 'PromiseId mismatch' + ); + }); + + it('Should create payment method ETHETH', async () => { + expect( + await contractVoucherKernel.getVoucherPaymentMethod( + tokenSupplyKey + ) + ).to.equal( + paymentMethods.TKNTKN, + 'Payment Method TKNTKN not set correctly' + ); + }); + + it('Deposit and Price address should be correctly set', async () => { + expect( + await contractVoucherKernel.getVoucherPriceToken(tokenSupplyKey) + ).to.equal( + contractBSNTokenPrice.address, + 'TKNTKN Method Price Token Address mismatch' + ); - // .withArgs(); should calculate token supply id and compare it it - // console.log(tokenSupplyID) + expect( + await contractVoucherKernel.getVoucherDepositToken( + tokenSupplyKey + ) + ).to.equal( + contractBSNTokenDeposit.address, + 'TKNTKN Method Deposit Token Address mismatch' + ); + }); }); - }); - - it('Should create payment method TKNTKN', async () => { - tokenSupplyKey = await utils.createOrder( - users.seller, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_1 - ); - - const paymentMethod = await contractVoucherKernel.getVoucherPaymentMethod( - tokenSupplyKey - ); - const addressTokenPrice = await contractVoucherKernel.getVoucherPriceToken( - tokenSupplyKey - ); + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + tokenSupplyKey + ) + )[0]; - const addressTokenDeposits = await contractVoucherKernel.getVoucherDepositToken( - tokenSupplyKey - ); + assert.isTrue( + sellerERC1155ERC721Balance.eq(constants.QTY_10), + 'ERC1155ERC721 seller balance mismatch' + ); + }); - assert.equal( - paymentMethod.toString(), - paymentMethods.TKNTKN.toString(), - 'Payment Method TKNTKN not set correctly' - ); - assert.equal( - addressTokenPrice.toString(), - contractBSNTokenPrice.address, - 'TKNTKN Method Price Token Address mismatch' - ); - assert.equal( - addressTokenDeposits.toString(), - contractBSNTokenDeposit.address, - 'TKNTKN Method Deposit Token Address mismatch' - ); - }); + it('Cashier has correct balance in Deposit Contract', async () => { + // REWRITE + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + constants.QTY_10 + ); - it('[NEGATIVE] Should fail if token price contract address is not provided', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); + expect( + await contractBSNTokenDeposit.balanceOf(contractCashier.address) + ).to.equal(expectedBalance, 'Escrow amount is incorrect'); + }); - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractCashier.address, - txValue, - nonce, - deadline - ); + it('escrowTokens has correct balance', async () => { + const expectedBalance = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.seller.address + ) + ).to.equal(expectedBalance, 'Escrow amount is incorrect'); + }); - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); + it('Get correct remaining qty for supply', async () => { + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal(constants.QTY_10, 'Remaining qty is not correct'); - await expect( - sellerInstance.requestCreateOrderTKNTKNWithPermit( - '', - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, + for (let i = 0; i < vouchersToBuy; i++) { + await utils.commitToBuy( + users.buyer, + users.seller, + tokenSupplyKey, constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] - ) - ).to.be.reverted; + constants.PROMISE_DEPOSITBU1 + ); + expect( + await contractVoucherKernel.getRemQtyForSupply( + tokenSupplyKey, + users.seller.address + ) + ).to.equal( + constants.QTY_10 - i - 1, + `Remaining qty is not correct [${i}]` + ); + } + }); }); - it('[NEGATIVE] Should fail if token deposit contract address is not provided', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address + it('It should be possible to create Order with 0 buyer deposit', async () => { + await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + constants.QTY_10, + true ); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractCashier.address, - txValue, - nonce, - deadline + const promiseOrderData = await contractVoucherKernel.getOrderCosts( + tokenSupplyKey ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.price].eq( + BN(constants.PROMISE_PRICE1) + ), + 'Promise product price mismatch' ); - const sellerInstance = contractBosonRouter.connect( - users.seller.signer + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositSe].eq( + BN(constants.PROMISE_DEPOSITSE1) + ), + 'Promise seller deposit mismatch' + ); + assert.isTrue( + promiseOrderData[constants.PROMISE_ORDER_FIELDS.depositBu].eq( + BN(constants.ZERO) + ), + 'Promise buyer deposit mismatch' ); + }); + it('[NEGATIVE] Should revert if validTo is set below 5 minutes from now', async () => { await expect( - sellerInstance.requestCreateOrderTKNTKNWithPermit( - contractBSNTokenPrice.address, - '', - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_FROM + constants.ONE_MINUTE, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 ) - ).to.be.reverted; + ).to.be.revertedWith(revertReasons.INVALID_VALIDITY_TO); }); - it('[NEGATIVE] Should revert if token price contract address is zero address', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); + it('[NEGATIVE] Should revert if token price contract address is constants.ZERO address', async () => { + const txValue = BN(constants.seller_deposit).mul(BN(constants.ONE)); const nonce = await contractBSNTokenDeposit.nonces( users.seller.address ); @@ -1305,8 +1631,8 @@ describe('Cashier and VoucherKernel', () => { ).to.be.revertedWith(revertReasons.ZERO_ADDRESS_NOT_ALLOWED); }); - it('[NEGATIVE] Should revert if token deposit contract address is zero address', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(ONE_VOUCHER)); + it('[NEGATIVE] Should revert if token deposit contract address is constants.ZERO address', async () => { + const txValue = BN(constants.seller_deposit).mul(BN(constants.ONE)); const nonce = await contractBSNTokenDeposit.nonces( users.seller.address ); @@ -1352,136 +1678,46 @@ describe('Cashier and VoucherKernel', () => { }); it('[NEGATIVE] Should not create a supply if price is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(constants.QTY_1)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNTKNWithPermit( - contractBSNTokenPrice.address, - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.ABOVE_TOKEN_LIMIT, - constants.seller_deposit, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.ABOVE_TOKEN_LIMIT, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositBu is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(constants.QTY_1)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNTKNWithPermit( - contractBSNTokenPrice.address, - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.seller_deposit, - constants.ABOVE_TOKEN_LIMIT, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ABOVE_TOKEN_LIMIT, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); it('[NEGATIVE] Should not create a supply if depositSe is above the limit', async () => { - const txValue = BN(constants.seller_deposit).mul(BN(constants.QTY_1)); - const nonce = await contractBSNTokenDeposit.nonces( - users.seller.address - ); - - const digest = await getApprovalDigest( - contractBSNTokenDeposit, - users.seller.address, - contractBosonRouter.address, - txValue, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digest.slice(2), 'hex'), - Buffer.from(users.seller.privateKey.slice(2), 'hex') - ); - - const sellerInstance = contractBosonRouter.connect( - users.seller.signer - ); - await expect( - sellerInstance.requestCreateOrderTKNTKNWithPermit( - contractBSNTokenPrice.address, - contractBSNTokenDeposit.address, - txValue, - deadline, - v, - r, - s, - [ - constants.PROMISE_VALID_FROM, - constants.PROMISE_VALID_TO, - constants.PROMISE_PRICE1, - constants.ABOVE_TOKEN_LIMIT, - constants.PROMISE_DEPOSITBU1, - constants.ORDER_QUANTITY1, - ] + utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.ABOVE_TOKEN_LIMIT, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10, + true ) ).to.be.revertedWith(revertReasons.ABOVE_LIMIT); }); @@ -1508,12 +1744,14 @@ describe('Cashier and VoucherKernel', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.PROMISE_PRICE1, constants.seller_deposit, + constants.PROMISE_DEPOSITBU1, constants.QTY_10 ); }); - it('Should process suppy/voucher set cancellation properly', async () => { + it('Should process supply/voucher set cancellation properly', async () => { const sellerBalanceBefore = await users.seller.signer.getBalance( 'latest' ); @@ -1585,8 +1823,15 @@ describe('Cashier and VoucherKernel', () => { const ORDER_QTY = 5; let TOKEN_SUPPLY_ID; + // calculate expected tokenSupplyID for first voucher + const tokenIndex = constants.ONE; + const TYPE_NF_BIT = constants.ONE.shl(255); + tokenSupplyKey = TYPE_NF_BIT.or(tokenIndex.shl(128)).toString(); + + const voucherTokenId = BN(tokenSupplyKey).or(constants.ONE); + describe('ETHETH', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() .ETHETH() @@ -1597,42 +1842,176 @@ describe('Cashier and VoucherKernel', () => { contractBosonRouter ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_10 ); }); it('Should create order', async () => { - const txValue = BN(constants.buyer_deposit).add( - BN(constants.product_price) + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1, + true + ) + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + users.seller.address, + constants.ZERO, + tokenSupplyKey, + constants.ONE + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(constants.ZERO, users.buyer.address, voucherTokenId); + }); + + describe('After request', () => { + beforeEach(async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + }); + + it('Voucher Kernel state is correct', async () => { + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); + + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); + assert.isFalse(voucherStatus[1], 'Payment should not be released'); + assert.isFalse(voucherStatus[2], 'Deposit should not be released'); + assert.isTrue( + voucherStatus[3].eq(constants.ZERO), + 'Complaint period should not started yet' + ); + assert.isTrue( + voucherStatus[4].eq(constants.ZERO), + 'COF period should not started yet' + ); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + TOKEN_SUPPLY_ID + ) + )[0]; + + assert.isTrue( + sellerERC1155ERC721Balance.eq(constants.QTY_10 - 1), + 'Seller 1155 balance mismatch' + ); + + const buyerERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.buyer.address + ) + )[0]; + + assert.isTrue( + buyerERC721Balance.eq(constants.ONE), + 'Buyer 721 balance mismatch' + ); + + expect(await contractERC1155ERC721.ownerOf(voucherTokenId)).to.equal( + users.buyer.address, + 'Owner address mismatch' + ); + }); + + it('Cashier Contract has correct amount of funds', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + const buyerETHSent = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) + ); + const expectedBalance = sellerDeposits.add(buyerETHSent); + + expect( + await ethers.provider.getBalance(contractCashier.address) + ).to.equal(expectedBalance, 'Escrow amount is incorrect'); + }); + + it('Escrow should be updated', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + const buyerETHSent = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) + ); + + expect( + await contractCashier.getEscrowAmount(users.seller.address) + ).to.equal(sellerDeposits, 'Seller escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowAmount(users.buyer.address) + ).to.equal(buyerETHSent, 'Buyer escrow amount is incorrect'); + }); + }); + + it('It should be possible to request voucher with 0 buyer deposit', async () => { + const TOKEN_SUPPLY_ID = await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + ORDER_QTY ); - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - const txFillOrder = await buyerInstance.requestVoucherETHETH( + + await utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - { - value: txValue, - } + constants.PROMISE_PRICE1, + constants.ZERO ); - const txReceipt = await txFillOrder.wait(); + const voucherTokenId = BN(TOKEN_SUPPLY_ID).or(constants.ONE); - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_DELIVERED, - (ev) => { - assert.equal(ev._issuer, users.seller.address); - tokenVoucherKey = ev._tokenIdVoucher; - } + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); + + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' ); }); @@ -1650,87 +2029,36 @@ describe('Cashier and VoucherKernel', () => { ); await expect( - utilsTknEth.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) - ).to.be.reverted; - }); - - it('Cashier Contract has correct amount of funds', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(constants.QTY_10) - ); - const buyerETHSent = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); - const expectedBalance = sellerDeposits.add(buyerETHSent); - - const cashierBalance = await ethers.provider.getBalance( - contractCashier.address - ); - - assert.isTrue( - BN(cashierBalance).eq(expectedBalance), - 'Escrow amount is incorrect' - ); - }); - - it('Escrow should be updated', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(constants.QTY_10) - ); - const buyerETHSent = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); - - const escrowSeller = await contractCashier.getEscrowAmount( - users.seller.address - ); - const escrowBuyer = await contractCashier.getEscrowAmount( - users.buyer.address - ); - - assert.isTrue( - BN(sellerDeposits).eq(escrowSeller), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerETHSent).eq(escrowBuyer), - 'Escrow amount is incorrect' - ); + utilsTknEth.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ) + ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); }); it('[NEGATIVE] Should not create order with incorrect price', async () => { - const txValue = BN(constants.buyer_deposit).add( - BN(constants.incorrect_product_price) - ); - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherETHETH( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - { - value: txValue, - } + constants.incorrect_product_price, + constants.PROMISE_DEPOSITBU1 ) ).to.be.revertedWith(revertReasons.INVALID_FUNDS); }); it('[NEGATIVE] Should not create order with incorrect deposit', async () => { - const txValue = BN(constants.buyer_incorrect_deposit).add( - BN(constants.product_price) - ); - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherETHETH( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - { - value: txValue, - } + constants.PROMISE_PRICE1, + constants.buyer_incorrect_deposit ) ).to.be.revertedWith(revertReasons.INVALID_FUNDS); }); @@ -1738,7 +2066,7 @@ describe('Cashier and VoucherKernel', () => { describe('[WITH PERMIT]', () => { describe('ETHTKN', async () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -1753,10 +2081,10 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit ); - const tokensToMintSeller = BN(constants.seller_deposit).mul( + const tokensToMintSeller = BN(constants.PROMISE_DEPOSITSE1).mul( BN(ORDER_QTY) ); - const tokensToMintBuyer = BN(constants.buyer_deposit).mul( + const tokensToMintBuyer = BN(constants.PROMISE_DEPOSITBU1).mul( BN(ORDER_QTY) ); @@ -1769,60 +2097,210 @@ describe('Cashier and VoucherKernel', () => { tokensToMintBuyer ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, ORDER_QTY ); }); it('Should create order', async () => { - const nonce = await contractBSNTokenDeposit.nonces( - users.buyer.address + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1, + true + ) + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + users.seller.address, + constants.ZERO, + tokenSupplyKey, + constants.ONE + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(constants.ZERO, users.buyer.address, voucherTokenId) + .to.emit(contractBSNTokenDeposit, eventNames.TRANSFER) + .withArgs( + users.buyer.address, + contractCashier.address, + constants.PROMISE_DEPOSITBU1 + ); + }); + + describe('After request', () => { + beforeEach(async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + }); + + it('Voucher Kernel state is correct', async () => { + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); + + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); + assert.isFalse(voucherStatus[1], 'Payment should not be released'); + assert.isFalse(voucherStatus[2], 'Deposit should not be released'); + assert.isTrue( + voucherStatus[3].eq(constants.ZERO), + 'Complaint period should not started yet' + ); + assert.isTrue( + voucherStatus[4].eq(constants.ZERO), + 'COF period should not started yet' + ); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + TOKEN_SUPPLY_ID + ) + )[0]; + + assert.isTrue( + sellerERC1155ERC721Balance.eq(ORDER_QTY - 1), + 'Seller 1155 balance mismatch' + ); + + const buyerERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.buyer.address + ) + )[0]; + + assert.isTrue( + buyerERC721Balance.eq(constants.ONE), + 'Buyer 721 balance mismatch' + ); + + expect( + await contractERC1155ERC721.ownerOf(voucherTokenId) + ).to.equal(users.buyer.address, 'Owner address mismatch'); + }); + + it('Cashier Contract has correct amount of funds', async () => { + const expectedETHBalance = BN(constants.PROMISE_PRICE1); + const sellerTokenDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const expectedTokenBalance = BN(constants.PROMISE_DEPOSITBU1).add( + sellerTokenDeposits + ); + + expect( + await ethers.provider.getBalance(contractCashier.address) + ).to.equal(expectedETHBalance, 'Escrow amount is incorrect'); + + expect( + await contractBSNTokenDeposit.balanceOf(contractCashier.address) + ).to.equal(expectedTokenBalance, 'Escrow amount is incorrect'); + }); + + it('Escrows should be updated', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const buyerETHSent = BN(constants.PROMISE_PRICE1); + const buyerTKNSent = BN(constants.PROMISE_DEPOSITBU1); + + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.seller.address + ) + ).to.equal(sellerDeposits, 'Escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowAmount(users.buyer.address) + ).to.equal(buyerETHSent, 'Escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.buyer.address + ) + ).to.equal(buyerTKNSent, 'Escrow amount is incorrect'); + }); + }); + + it('It should be possible to request voucher with 0 buyer deposit', async () => { + const tokensToMintSeller = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) ); - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractBosonRouter.address, - constants.buyer_deposit, - nonce, - deadline + await contractBSNTokenDeposit.mint( + users.seller.address, + tokensToMintSeller ); - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') + const TOKEN_SUPPLY_ID = await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + ORDER_QTY ); - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - - const txFillOrder = await buyerInstance.requestVoucherETHTKNWithPermit( + await utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - constants.buyer_deposit, - deadline, - v, - r, - s, - {value: constants.product_price} + constants.PROMISE_PRICE1, + constants.ZERO ); - const txReceipt = await txFillOrder.wait(); + const voucherTokenId = BN(TOKEN_SUPPLY_ID).or(constants.ONE); - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_DELIVERED, - (ev) => { - assert.equal(ev._issuer, users.seller.address); - tokenVoucherKey = ev._tokenIdVoucher; - } + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); + + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' ); }); @@ -1840,144 +2318,43 @@ describe('Cashier and VoucherKernel', () => { ); await expect( - utilsEthEth.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utilsEthEth.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ) ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); }); - it('Cashier Contract has correct amount of funds', async () => { - const expectedETHBalance = BN(constants.product_price); - const cashierETHBalance = await ethers.provider.getBalance( - contractCashier.address - ); - - const cashierDepositTokenBalance = await contractBSNTokenDeposit.balanceOf( - contractCashier.address - ); - const sellerTokenDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const expectedTokenBalance = BN(constants.buyer_deposit).add( - sellerTokenDeposits - ); - - assert.isTrue( - BN(cashierETHBalance).eq(expectedETHBalance), - 'Escrow amount is incorrect' - ); - assert.isTrue( - expectedTokenBalance.eq(cashierDepositTokenBalance), - 'Escrow amount is incorrect' - ); - }); - - it('Escrows should be updated', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const buyerETHSent = BN(constants.product_price); - const buyerTKNSent = BN(constants.buyer_deposit); - - const escrowSellerTkn = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.seller.address - ); - const escrowBuyerEth = await contractCashier.getEscrowAmount( - users.buyer.address - ); - const escrowBuyerTkn = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.buyer.address - ); - - assert.isTrue( - BN(sellerDeposits).eq(escrowSellerTkn), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerETHSent).eq(escrowBuyerEth), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerTKNSent).eq(escrowBuyerTkn), - 'Escrow amount is incorrect' - ); - }); - it('[NEGATIVE] Should not create order with incorrect price', async () => { - const nonce = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractCashier.address, - constants.buyer_deposit, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherETHTKNWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - constants.buyer_deposit, - deadline, - v, - r, - s, - { - value: constants.incorrect_product_price, - } + constants.incorrect_product_price, + constants.PROMISE_DEPOSITBU1 ) ).to.be.revertedWith(revertReasons.INCORRECT_PRICE); }); it('[NEGATIVE] Should not create order with incorrect deposit', async () => { - const nonce = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractCashier.address, - constants.buyer_deposit, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherETHTKNWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - constants.buyer_incorrect_deposit, - deadline, - v, - r, - s, - {value: constants.product_price} + constants.PROMISE_PRICE1, + constants.buyer_incorrect_deposit ) ).to.be.revertedWith(revertReasons.INCORRECT_DEPOSIT); }); }); describe('TKNTKN', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -2012,92 +2389,205 @@ describe('Cashier and VoucherKernel', () => { tokensToMintBuyer ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, ORDER_QTY ); }); it('Should create order', async () => { - const nonce1 = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const tokensToSend = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1, + true + ) + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + users.seller.address, + constants.ZERO, + tokenSupplyKey, + constants.ONE + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(constants.ZERO, users.buyer.address, voucherTokenId) + .to.emit(contractBSNTokenDeposit, eventNames.TRANSFER) + .withArgs( + users.buyer.address, + contractCashier.address, + constants.PROMISE_DEPOSITBU1 + ) + .to.emit(contractBSNTokenPrice, eventNames.TRANSFER) + .withArgs( + users.buyer.address, + contractCashier.address, + constants.PROMISE_PRICE1 + ); + }); - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractBosonRouter.address, - constants.buyer_deposit, - nonce1, - deadline - ); + describe('After request', () => { + beforeEach(async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + }); - const VRS_DEPOSIT = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); + it('Voucher Kernel state is correct', async () => { + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); - const vDeposit = VRS_DEPOSIT.v; - const rDeposit = VRS_DEPOSIT.r; - const sDeposit = VRS_DEPOSIT.s; + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations - const nonce2 = await contractBSNTokenPrice.nonces( - users.buyer.address - ); + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); + assert.isFalse(voucherStatus[1], 'Payment should not be released'); + assert.isFalse(voucherStatus[2], 'Deposit should not be released'); + assert.isTrue( + voucherStatus[3].eq(constants.ZERO), + 'Complaint period should not started yet' + ); + assert.isTrue( + voucherStatus[4].eq(constants.ZERO), + 'COF period should not started yet' + ); + }); - const digestPrice = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractBosonRouter.address, - constants.product_price, - nonce2, - deadline - ); + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + TOKEN_SUPPLY_ID + ) + )[0]; - const VRS_PRICE = ecsign( - Buffer.from(digestPrice.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); + assert.isTrue( + sellerERC1155ERC721Balance.eq(ORDER_QTY - 1), + 'Seller 1155 balance mismatch' + ); - const vPrice = VRS_PRICE.v; - const rPrice = VRS_PRICE.r; - const sPrice = VRS_PRICE.s; + const buyerERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.buyer.address + ) + )[0]; - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); + assert.isTrue( + buyerERC721Balance.eq(constants.ONE), + 'Buyer 721 balance mismatch' + ); - const txFillOrder = await buyerInstance.requestVoucherTKNTKNWithPermit( + expect( + await contractERC1155ERC721.ownerOf(voucherTokenId) + ).to.equal(users.buyer.address, 'Owner address mismatch'); + }); + + it('Cashier Contract has correct amount of funds', async () => { + const sellerDeposit = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const expectedDepositBalance = BN(constants.PROMISE_DEPOSITBU1).add( + sellerDeposit + ); + + expect( + await contractBSNTokenPrice.balanceOf(contractCashier.address) + ).to.be.equal( + constants.PROMISE_PRICE1, + 'Escrow amount is incorrect' + ); + + expect( + await contractBSNTokenDeposit.balanceOf(contractCashier.address) + ).to.be.equal(expectedDepositBalance, 'Escrow amount is incorrect'); + }); + + it('Escrows should be updated', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const buyerTknPriceSent = BN(constants.PROMISE_PRICE1); + const buyerTknDepositSent = BN(constants.PROMISE_DEPOSITBU1); + + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.seller.address + ) + ).to.be.equal(sellerDeposits, 'Escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenPrice.address, + users.buyer.address + ) + ).to.be.equal(buyerTknPriceSent, 'Escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.buyer.address + ) + ).to.be.equal(buyerTknDepositSent, 'Escrow amount is incorrect'); + }); + }); + + it('It should be possible to request voucher with 0 buyer deposit', async () => { + const TOKEN_SUPPLY_ID = await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + ORDER_QTY + ); + + await utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - tokensToSend, - deadline, - vPrice, - rPrice, - sPrice, - vDeposit, - rDeposit, - sDeposit + constants.PROMISE_PRICE1, + constants.ZERO ); - const txReceipt = await txFillOrder.wait(); + const voucherTokenId = BN(TOKEN_SUPPLY_ID).or(constants.ONE); - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_DELIVERED, - (ev) => { - assert.equal(ev._issuer, users.seller.address); - tokenVoucherKey = ev._tokenIdVoucher; - } + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); + + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' ); }); @@ -2115,196 +2605,36 @@ describe('Cashier and VoucherKernel', () => { ); await expect( - utilsEthTkn.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utilsEthTkn.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ) ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); }); - it('Cashier Contract has correct amount of funds', async () => { - const cashierPriceTokenBalance = await contractBSNTokenPrice.balanceOf( - contractCashier.address - ); - const cashierDepositTokenBalance = await contractBSNTokenDeposit.balanceOf( - contractCashier.address - ); - const sellerDeposit = BN(constants.seller_deposit).mul(BN(ORDER_QTY)); - const expectedDepositBalance = BN(constants.buyer_deposit).add( - sellerDeposit - ); - - assert.isTrue( - BN(cashierPriceTokenBalance).eq(BN(constants.product_price)), - 'Escrow amount is incorrect' - ); - assert.isTrue( - BN(cashierDepositTokenBalance).eq(expectedDepositBalance), - 'Escrow amount is incorrect' - ); - }); - - it('Escrows should be updated', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const buyerTknPriceSent = BN(constants.product_price); - const buyerTknDepositSent = BN(constants.buyer_deposit); - - const escrowSellerTknDeposit = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.seller.address - ); - const escrowBuyerTknPrice = await contractCashier.getEscrowTokensAmount( - contractBSNTokenPrice.address, - users.buyer.address - ); - const escrowBuyerTknDeposit = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.buyer.address - ); - - assert.isTrue( - BN(sellerDeposits).eq(escrowSellerTknDeposit), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerTknPriceSent).eq(escrowBuyerTknPrice), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerTknDepositSent).eq(escrowBuyerTknDeposit), - 'Escrow amount is incorrect' - ); - }); - it('[NEGATIVE] Should not create order with incorrect price', async () => { - const nonce1 = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const tokensToSend = BN(constants.incorrect_product_price).add( - BN(constants.buyer_deposit) - ); - - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractCashier.address, - constants.buyer_deposit, - nonce1, - deadline - ); - - const VRS_DEPOSIT = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const vDeposit = VRS_DEPOSIT.v; - const rDeposit = VRS_DEPOSIT.r; - const sDeposit = VRS_DEPOSIT.s; - - const nonce2 = await contractBSNTokenPrice.nonces( - users.buyer.address - ); - - const digestPrice = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractCashier.address, - constants.product_price, - nonce2, - deadline - ); - - const VRS_PRICE = ecsign( - Buffer.from(digestPrice.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const vPrice = VRS_PRICE.v; - const rPrice = VRS_PRICE.r; - const sPrice = VRS_PRICE.s; - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherTKNTKNWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - tokensToSend, - deadline, - vPrice, - rPrice, - sPrice, - vDeposit, - rDeposit, - sDeposit + constants.incorrect_product_price, + constants.PROMISE_DEPOSITBU1 ) ).to.be.revertedWith(revertReasons.INVALID_FUNDS); }); it('[NEGATIVE] Should not create order with incorrect deposit', async () => { - const nonce1 = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const tokensToSend = BN(constants.product_price).add( - BN(constants.buyer_incorrect_deposit) - ); - - const digestDeposit = await getApprovalDigest( - contractBSNTokenDeposit, - users.buyer.address, - contractCashier.address, - constants.buyer_deposit, - nonce1, - deadline - ); - - const VRS_DEPOSIT = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const vDeposit = VRS_DEPOSIT.v; - const rDeposit = VRS_DEPOSIT.r; - const sDeposit = VRS_DEPOSIT.s; - - const nonce2 = await contractBSNTokenPrice.nonces( - users.buyer.address - ); - - const digestPrice = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractCashier.address, - constants.product_price, - nonce2, - deadline - ); - - const VRS_PRICE = ecsign( - Buffer.from(digestPrice.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const vPrice = VRS_PRICE.v; - const rPrice = VRS_PRICE.r; - const sPrice = VRS_PRICE.s; - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherTKNTKNWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - tokensToSend, - deadline, - vPrice, - rPrice, - sPrice, - vDeposit, - rDeposit, - sDeposit + constants.PROMISE_PRICE1, + constants.buyer_incorrect_deposit ) ).to.be.revertedWith(revertReasons.INVALID_FUNDS); }); @@ -2318,7 +2648,7 @@ describe('Cashier and VoucherKernel', () => { BN(ORDER_QTY) ); - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -2342,70 +2672,190 @@ describe('Cashier and VoucherKernel', () => { tokensToMintBuyer ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, ORDER_QTY ); }); it('Should create voucher', async () => { - const nonce = await utils.contractBSNTokenSame.nonces( - users.buyer.address - ); - const tokensToSend = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1, + true + ) + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + users.seller.address, + constants.ZERO, + tokenSupplyKey, + constants.ONE + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(constants.ZERO, users.buyer.address, voucherTokenId) + .to.emit(contractBSNTokenPrice, eventNames.TRANSFER) + .withArgs( + users.buyer.address, + contractCashier.address, + BN(constants.PROMISE_PRICE1).add(constants.PROMISE_DEPOSITBU1) + ); + }); - const digestTokens = await getApprovalDigest( - utils.contractBSNTokenSame, - users.buyer.address, - contractBosonRouter.address, - tokensToSend, - nonce, - deadline - ); + describe('After request', () => { + beforeEach(async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + }); - const VRS_TOKENS = ecsign( - Buffer.from(digestTokens.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); + it('Voucher Kernel state is correct', async () => { + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); - const v = VRS_TOKENS.v; - const r = VRS_TOKENS.r; - const s = VRS_TOKENS.s; + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); + assert.isFalse(voucherStatus[1], 'Payment should not be released'); + assert.isFalse(voucherStatus[2], 'Deposit should not be released'); + assert.isTrue( + voucherStatus[3].eq(constants.ZERO), + 'Complaint period should not started yet' + ); + assert.isTrue( + voucherStatus[4].eq(constants.ZERO), + 'COF period should not started yet' + ); + }); + + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + TOKEN_SUPPLY_ID + ) + )[0]; - const txFillOrder = await buyerInstance.requestVoucherTKNTKNSameWithPermit( + assert.isTrue( + sellerERC1155ERC721Balance.eq(ORDER_QTY - 1), + 'Seller 1155 balance mismatch' + ); + + const buyerERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.buyer.address + ) + )[0]; + + assert.isTrue( + buyerERC721Balance.eq(constants.ONE), + 'Buyer 721 balance mismatch' + ); + + expect( + await contractERC1155ERC721.ownerOf(voucherTokenId) + ).to.equal(users.buyer.address, 'Owner address mismatch'); + }); + + it('Cashier Contract has correct amount of funds', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const buyerTokensSent = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) + ); + const expectedDepositBalance = buyerTokensSent.add(sellerDeposits); + + expect( + await utils.contractBSNTokenSame.balanceOf( + contractCashier.address + ) + ).to.equal(expectedDepositBalance, 'Cashier amount is incorrect'); + }); + + it('Escrows should be updated', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const buyerTknSent = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) + ); + + expect( + await contractCashier.getEscrowTokensAmount( + utils.contractBSNTokenSame.address, + users.seller.address + ) + ).to.equal(sellerDeposits, 'Escrow amount is incorrect'); + + expect( + await contractCashier.getEscrowTokensAmount( + utils.contractBSNTokenSame.address, + users.buyer.address + ) + ).to.equal(buyerTknSent, 'Escrow amount is incorrect'); + }); + }); + + it('It should be possible to request voucher with 0 buyer deposit', async () => { + const TOKEN_SUPPLY_ID = await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + ORDER_QTY + ); + + await utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - tokensToSend, - deadline, - v, - r, - s + constants.PROMISE_PRICE1, + constants.ZERO ); - const txReceipt = await txFillOrder.wait(); + const voucherTokenId = BN(TOKEN_SUPPLY_ID).or(constants.ONE); - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_DELIVERED, - (ev) => { - assert.equal(ev._issuer, users.seller.address); - tokenVoucherKey1 = ev._tokenIdVoucher; - } + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId ); - assert.isDefined(tokenVoucherKey1.toString()); + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); }); it('[NEGATIVE] Should not create order from a wrong payment type', async () => { @@ -2422,132 +2872,37 @@ describe('Cashier and VoucherKernel', () => { ); await expect( - utilsEthEth.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utilsEthEth.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ) ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); }); - it('Cashier Contract has correct amount of funds', async () => { - const cashierTokenBalanceSame = await utils.contractBSNTokenSame.balanceOf( - contractCashier.address - ); - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const buyerTokensSent = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); - const expectedDepositBalance = buyerTokensSent.add(sellerDeposits); - - assert.isTrue( - BN(cashierTokenBalanceSame).eq(expectedDepositBalance), - 'Cashier amount is incorrect' - ); - }); - - it('Escrows should be updated', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const buyerTknSent = BN(constants.product_price).add( - BN(constants.buyer_deposit) - ); - - const escrowSellerTknDeposit = await contractCashier.getEscrowTokensAmount( - utils.contractBSNTokenSame.address, - users.seller.address - ); - const escrowBuyerTkn = await contractCashier.getEscrowTokensAmount( - utils.contractBSNTokenSame.address, - users.buyer.address - ); - - assert.isTrue( - BN(sellerDeposits).eq(escrowSellerTknDeposit), - 'Escrow amount is incorrect' - ); - - assert.isTrue( - BN(buyerTknSent).eq(escrowBuyerTkn), - 'Escrow amount is incorrect' - ); - }); - it('[NEGATIVE] Should not create order with incorrect price', async () => { - const nonce = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const incorrectTokensToSign = BN( - constants.incorrect_product_price - ).add(BN(constants.buyer_deposit)); - const digestTokens = await getApprovalDigest( - utils.contractBSNTokenSame, - users.buyer.address, - contractCashier.address, - incorrectTokensToSign, - nonce, - deadline - ); - - const VRS_TOKENS = ecsign( - Buffer.from(digestTokens.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const v = VRS_TOKENS.v; - const r = VRS_TOKENS.r; - const s = VRS_TOKENS.s; - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherTKNTKNSameWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - incorrectTokensToSign, - deadline, - v, - r, - s - ) - ).to.be.revertedWith(revertReasons.INVALID_FUNDS); - }); - - it('[NEGATIVE] Should not create order with incorrect deposit', async () => { - const nonce = await contractBSNTokenDeposit.nonces( - users.buyer.address - ); - const incorrectTokensToSign = BN(constants.product_price).add( - BN(constants.buyer_incorrect_deposit) - ); - const digestTokens = await getApprovalDigest( - utils.contractBSNTokenSame, - users.buyer.address, - contractCashier.address, - incorrectTokensToSign, - nonce, - deadline - ); - - const VRS_TOKENS = ecsign( - Buffer.from(digestTokens.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const v = VRS_TOKENS.v; - const r = VRS_TOKENS.r; - const s = VRS_TOKENS.s; - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); + constants.incorrect_product_price, + constants.PROMISE_DEPOSITBU1 + ) + ).to.be.revertedWith(revertReasons.INVALID_FUNDS); + }); + it('[NEGATIVE] Should not create order with incorrect deposit', async () => { await expect( - buyerInstance.requestVoucherTKNTKNSameWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - incorrectTokensToSign, - deadline, - v, - r, - s + + constants.PROMISE_PRICE1, + constants.buyer_incorrect_deposit ) ).to.be.revertedWith(revertReasons.INVALID_FUNDS); }); @@ -2583,15 +2938,18 @@ describe('Cashier and VoucherKernel', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, - ORDER_QTY + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + ORDER_QTY, + false ); const nonce = await utils.contractBSNTokenSame.nonces( users.buyer.address ); - const tokensToSend = BN(constants.product_price).add( - BN(constants.buyer_deposit) + const tokensToSend = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) ); const digestTokens = await getApprovalDigest( @@ -2629,7 +2987,7 @@ describe('Cashier and VoucherKernel', () => { }); describe('TKNETH', () => { - before(async () => { + beforeEach(async () => { await deployContracts(); utils = await UtilsBuilder.create() @@ -2644,7 +3002,7 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit ); - const tokensToMintBuyer = BN(constants.product_price).mul( + const tokensToMintBuyer = BN(constants.PROMISE_PRICE1).mul( BN(ORDER_QTY) ); @@ -2653,209 +3011,234 @@ describe('Cashier and VoucherKernel', () => { tokensToMintBuyer ); - timestamp = await Utils.getCurrTimestamp(); - constants.PROMISE_VALID_FROM = timestamp; - constants.PROMISE_VALID_TO = timestamp + 2 * constants.SECONDS_IN_DAY; - TOKEN_SUPPLY_ID = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, ORDER_QTY ); }); it('Should create order', async () => { - const nonce = await contractBSNTokenPrice.nonces(users.buyer.address); + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1, + true + ) + ) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_DELIVERED) + .withArgs( + tokenSupplyKey, + voucherTokenId, + users.seller.address, + users.buyer.address, + promiseId + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER_SINGLE) + .withArgs( + contractVoucherKernel.address, + users.seller.address, + constants.ZERO, + tokenSupplyKey, + constants.ONE + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(constants.ZERO, users.buyer.address, voucherTokenId) + .to.emit(contractBSNTokenPrice, eventNames.TRANSFER) + .withArgs( + users.buyer.address, + contractCashier.address, + constants.PROMISE_PRICE1 + ); + }); - const digestDeposit = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractBosonRouter.address, - constants.product_price, - nonce, - deadline - ); + describe('After request', () => { + beforeEach(async () => { + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ); + }); - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); + it('Voucher Kernel state is correct', async () => { + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId + ); - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations - const txFillOrder = await buyerInstance.requestVoucherTKNETHWithPermit( - TOKEN_SUPPLY_ID, - users.seller.address, - constants.product_price, - deadline, - v, - r, - s, - {value: constants.buyer_deposit} - ); + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' + ); + assert.isFalse(voucherStatus[1], 'Payment should not be released'); + assert.isFalse(voucherStatus[2], 'Deposit should not be released'); + assert.isTrue( + voucherStatus[3].eq(constants.ZERO), + 'Complaint period should not started yet' + ); + assert.isTrue( + voucherStatus[4].eq(constants.ZERO), + 'COF period should not started yet' + ); + }); - const txReceipt = await txFillOrder.wait(); + it('ERC1155ERC721 state is correct', async () => { + const sellerERC1155ERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.seller.address, + TOKEN_SUPPLY_ID + ) + )[0]; - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_DELIVERED, - (ev) => { - assert.equal(ev._issuer, users.seller.address); - tokenVoucherKey = ev._tokenIdVoucher; - } - ); + assert.isTrue( + sellerERC1155ERC721Balance.eq(ORDER_QTY - 1), + 'Seller 1155 balance mismatch' + ); - assert.isDefined(tokenVoucherKey.toString()); - }); + const buyerERC721Balance = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.buyer.address + ) + )[0]; - it('[NEGATIVE] Should not create order from a wrong payment type', async () => { - const utilsEthEth = await UtilsBuilder.create() - .ERC20withPermit() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter, - contractBSNTokenPrice, - contractBSNTokenDeposit + assert.isTrue( + buyerERC721Balance.eq(constants.ONE), + 'Buyer 721 balance mismatch' ); - await expect( - utilsEthEth.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) - ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); - }); - - it('Cashier Contract has correct amount of funds', async () => { - const cashierDepositETH = await ethers.provider.getBalance( - contractCashier.address - ); - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); - const expectedDepositBalance = BN(constants.buyer_deposit).add( - sellerDeposits - ); + expect( + await contractERC1155ERC721.ownerOf(voucherTokenId) + ).to.equal(users.buyer.address, 'Owner address mismatch'); + }); - const cashierPriceTokenBalance = await contractBSNTokenPrice.balanceOf( - contractCashier.address - ); + it('Cashier Contract has correct amount of funds', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); + const expectedDepositBalance = BN(constants.PROMISE_DEPOSITBU1).add( + sellerDeposits + ); - assert.isTrue( - BN(cashierDepositETH).eq(expectedDepositBalance), - 'Cashier amount is incorrect' - ); - assert.isTrue( - BN(cashierPriceTokenBalance).eq(BN(constants.product_price)), - 'Cashier amount is incorrect' - ); - }); + expect( + await ethers.provider.getBalance(contractCashier.address) + ).to.equal(expectedDepositBalance, 'Cashier amount is incorrect'); - it('Escrow should be updated', async () => { - const sellerDeposits = BN(constants.seller_deposit).mul( - BN(ORDER_QTY) - ); + expect( + await contractBSNTokenPrice.balanceOf(contractCashier.address) + ).to.equal(constants.PROMISE_PRICE1, 'Cashier amount is incorrect'); + }); - const buyerTknSent = BN(constants.product_price); - const buyerEthSent = BN(constants.buyer_deposit); + it('Escrow should be updated', async () => { + const sellerDeposits = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(ORDER_QTY) + ); - const escrowSeller = await contractCashier.getEscrowAmount( - users.seller.address - ); - const escrowBuyerEth = await contractCashier.getEscrowAmount( - users.buyer.address - ); - const escrowBuyerTkn = await contractCashier.getEscrowTokensAmount( - contractBSNTokenPrice.address, - users.buyer.address - ); + expect( + await contractCashier.getEscrowAmount(users.seller.address) + ).to.equal(sellerDeposits, 'Escrow amount is incorrect'); - assert.isTrue( - BN(sellerDeposits).eq(escrowSeller), - 'Escrow amount is incorrect' - ); + expect( + await contractCashier.getEscrowTokensAmount( + contractBSNTokenPrice.address, + users.buyer.address + ) + ).to.equal(constants.PROMISE_PRICE1, 'Escrow amount is incorrect'); + }); + }); - assert.isTrue( - BN(buyerEthSent).eq(escrowBuyerEth), - 'Escrow amount is incorrect' + it('It should be possible to request voucher with 0 buyer deposit', async () => { + const TOKEN_SUPPLY_ID = await utils.createOrder( + users.seller, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.ZERO, + ORDER_QTY ); - assert.isTrue( - BN(buyerTknSent).eq(escrowBuyerTkn), - 'Escrow amount is incorrect' + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.ZERO ); - }); - it('[NEGATIVE] Should not create order with incorrect deposit', async () => { - const nonce = await contractBSNTokenPrice.nonces(users.buyer.address); + const voucherTokenId = BN(TOKEN_SUPPLY_ID).or(constants.ONE); - const digestDeposit = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractCashier.address, - constants.product_price, - nonce, - deadline + const voucherStatus = await contractVoucherKernel.getVoucherStatus( + voucherTokenId ); - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') + const expectedStatus = constants.ZERO.or(constants.ONE.shl(7)); // as per contract implementations + + assert.equal( + voucherStatus[0], + expectedStatus.toNumber(), + 'Wrong status' ); + }); - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); + it('[NEGATIVE] Should not create order from a wrong payment type', async () => { + const utilsEthEth = await UtilsBuilder.create() + .ERC20withPermit() + .ETHETH() + .buildAsync( + contractERC1155ERC721, + contractVoucherKernel, + contractCashier, + contractBosonRouter, + contractBSNTokenPrice, + contractBSNTokenDeposit + ); await expect( - buyerInstance.requestVoucherTKNETHWithPermit( + utilsEthEth.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, - constants.product_price, - deadline, - v, - r, - s, - { - value: constants.buyer_incorrect_deposit, - } + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ) - ).to.be.revertedWith(revertReasons.INCORRECT_DEPOSIT); + ).to.be.revertedWith(revertReasons.INCORRECT_PAYMENT_METHOD); }); it('[NEGATIVE] Should not create order with incorrect price', async () => { - const nonce = await contractBSNTokenPrice.nonces(users.buyer.address); - - const digestDeposit = await getApprovalDigest( - contractBSNTokenPrice, - users.buyer.address, - contractCashier.address, - constants.product_price, - nonce, - deadline - ); - - const {v, r, s} = ecsign( - Buffer.from(digestDeposit.slice(2), 'hex'), - Buffer.from(users.buyer.privateKey.slice(2), 'hex') - ); - - const buyerInstance = contractBosonRouter.connect(users.buyer.signer); - await expect( - buyerInstance.requestVoucherTKNETHWithPermit( + utils.commitToBuy( + users.buyer, + users.seller, TOKEN_SUPPLY_ID, - users.seller.address, constants.incorrect_product_price, - deadline, - v, - r, - s, - {value: constants.buyer_deposit} + constants.PROMISE_DEPOSITBU1 ) ).to.be.revertedWith(revertReasons.INCORRECT_PRICE); }); + + it('[NEGATIVE] Should not create order with incorrect deposit', async () => { + await expect( + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.buyer_incorrect_deposit + ) + ).to.be.revertedWith(revertReasons.INCORRECT_DEPOSIT); + }); }); }); @@ -2884,7 +3267,9 @@ describe('Cashier and VoucherKernel', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, constants.seller_deposit, + constants.PROMISE_DEPOSITBU1, constants.QTY_10 ); }); @@ -2895,7 +3280,13 @@ describe('Cashier and VoucherKernel', () => { ); await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 + ) ).to.be.revertedWith(revertReasons.OFFER_EXPIRED); }); @@ -2903,7 +3294,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds( constants.PROMISE_VALID_TO + cancelPeriod + complainPeriod @@ -2918,7 +3311,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.cancel(voucherID, users.seller.signer); @@ -2934,7 +3329,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds( @@ -2950,7 +3347,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds( @@ -2966,7 +3365,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -2983,7 +3384,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -3000,11 +3403,13 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - await utils.cancel(voucherID, users.seller.signer), - await advanceTimeSeconds(complainPeriod + constants.ONE_MINUTE); + await utils.cancel(voucherID, users.seller.signer); + await advanceTimeSeconds(complainPeriod + constants.ONE_MINUTE); await expect( utils.complain(voucherID, users.buyer.signer) @@ -3015,12 +3420,14 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - await utils.complain(voucherID, users.buyer.signer), - await advanceTimeSeconds(cancelPeriod + constants.ONE_MINUTE); + await utils.complain(voucherID, users.buyer.signer); + await advanceTimeSeconds(cancelPeriod + constants.ONE_MINUTE); await expect( utils.cancel(voucherID, users.seller.signer) @@ -3031,7 +3438,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.buyer.signer); @@ -3049,7 +3458,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.buyer.signer); @@ -3067,7 +3478,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.buyer.signer); @@ -3084,7 +3497,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.buyer.signer); @@ -3104,7 +3519,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds(ONE_WEEK); @@ -3144,7 +3561,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds(ONE_WEEK); @@ -3184,7 +3603,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds(ONE_WEEK); @@ -3236,7 +3657,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await advanceTimeSeconds(ONE_WEEK); @@ -3283,11 +3706,13 @@ describe('Cashier and VoucherKernel', () => { }); describe('TOKEN SUPPLY TRANSFER', () => { - let actualOldOwnerBalanceFromEscrow = BN(0); - let actualNewOwnerBalanceFromEscrow = BN(0); - let expectedBalanceInEscrow = BN(0); + const paymentType = { + PAYMENT: 0, + DEPOSIT_SELLER: 1, + DEPOSIT_BUYER: 2, + }; - afterEach(() => { + beforeEach(() => { distributedAmounts = { buyerAmount: BN(0), sellerAmount: BN(0), @@ -3295,30 +3720,82 @@ describe('Cashier and VoucherKernel', () => { }; }); - describe('Common transfer', () => { - beforeEach(async () => { - await deployContracts(); - utils = await UtilsBuilder.create() - .ETHETH() - .buildAsync( - contractERC1155ERC721, - contractVoucherKernel, - contractCashier, - contractBosonRouter - ); + describe('Common transfer', () => { + beforeEach(async () => { + await deployContracts(); + await setPeriods(); + utils = await UtilsBuilder.create() + .ETHETH() + .buildAsync( + contractERC1155ERC721, + contractVoucherKernel, + contractCashier, + contractBosonRouter + ); + + tokenSupplyKey = await utils.createOrder( + users.other1, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, + constants.QTY_10 + ); + }); + + it('Should transfer voucher supply', async () => { + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), + 'New owner balance from escrow does not match' + ); + + // balances before + const user1BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; - const timestamp = await Utils.getCurrTimestamp(); + const user2BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; - tokenSupplyKey = await utils.createOrder( - users.other1, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, - constants.QTY_10 + assert.equal( + user1BalanceBeforeTransfer, + constants.QTY_10, + 'User1 before balance mismatch' + ); + assert.equal( + user2BalanceBeforeTransfer, + 0, + 'User2 before balance mismatch' ); - }); - it('Should transfer voucher supply', async () => { const transferTx = await utils.safeTransfer1155( users.other1.address, users.other2.address, @@ -3341,9 +3818,74 @@ describe('Cashier and VoucherKernel', () => { assert.isTrue(ev._value.eq(constants.QTY_10)); } ); + + // balances after + const user1BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceAfterTransfer, + 0, + 'User1 after balance mismatch' + ); + assert.equal( + user2BalanceAfterTransfer, + constants.QTY_10, + 'User2 after balance mismatch' + ); + + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'New owner balance from escrow does not match' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); }); it('Should transfer voucher supply to self and balance should be the same', async () => { + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(constants.QTY_10) + ); + + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + const balanceBeforeTransfer = ( await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( users.other1.address, @@ -3366,8 +3908,9 @@ describe('Cashier and VoucherKernel', () => { ) )[0]; - assert.isTrue( - balanceBeforeTransfer.eq(balanceAfterTransfer), + assert.equal( + balanceBeforeTransfer.toString(), + balanceAfterTransfer.toString(), 'Balance mismatch!' ); @@ -3384,6 +3927,20 @@ describe('Cashier and VoucherKernel', () => { assert.equal(ev._value.toString(), constants.QTY_10); } ); + + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 after - holder mismatch'); }); it('[NEGATIVE] Should revert if owner tries to transfer voucher supply partially', async () => { @@ -3410,107 +3967,259 @@ describe('Cashier and VoucherKernel', () => { ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_1155); }); - it('Should transfer batch voucher supply', async () => { - const transferTx = await utils.safeBatchTransfer1155( - users.other1.address, - users.other2.address, - [tokenSupplyKey], - [constants.QTY_10], - users.other1.signer - ); + describe('Batch transfers', () => { + let tokenSupplyKey2; + let tokenSupplyBatch; + const batchQuantities = [BN(constants.QTY_10), BN(constants.QTY_20)]; - const txReceipt = await transferTx.wait(); + beforeEach(async () => { + tokenSupplyKey2 = await utils.createOrder( + users.other1, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE2, + constants.PROMISE_DEPOSITBU1, + constants.QTY_20 + ); - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER_BATCH, - (ev) => { - assert.equal(ev._from, users.other1.address); - assert.equal(ev._to, users.other2.address); - assert.equal( - JSON.stringify(ev._ids), - JSON.stringify([BN(tokenSupplyKey)]) - ); - assert.equal( - JSON.stringify(ev._values), - JSON.stringify([BN(constants.QTY_10)]) - ); - } - ); - }); + tokenSupplyBatch = [BN(tokenSupplyKey), BN(tokenSupplyKey2)]; + }); - it('Should transfer batch voucher supply to self and balance should be the same', async () => { - const balanceBeforeTransfer = ( - await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( - users.other1.address, - tokenSupplyKey - ) - )[0]; + it('Should transfer batch voucher supply', async () => { + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey2) + ).to.equal(users.other1.address, 'Supply 2 before - holder mismatch'); + + // balances before + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address + ); - const transferTx = await utils.safeBatchTransfer1155( - users.other1.address, - users.other1.address, - [tokenSupplyKey], - [constants.QTY_10], - users.other1.signer - ); + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1) + .mul(BN(constants.QTY_10)) + .add(BN(constants.PROMISE_DEPOSITSE2).mul(BN(constants.QTY_20))); - const balanceAfterTransfer = ( - await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( - users.other1.address, - tokenSupplyKey - ) - )[0]; + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), + 'New owner balance from escrow does not match' + ); - assert.isTrue( - balanceBeforeTransfer.eq(balanceAfterTransfer), - 'Balance mismatch!' - ); + const user1BalanceBeforeTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other1.address, users.other1.address], + tokenSupplyBatch + ); - const txReceipt = await transferTx.wait(); + const user2BalanceBeforeTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other2.address, users.other2.address], + tokenSupplyBatch + ); - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER_BATCH, - (ev) => { - assert.equal(ev._from, users.other1.address); - assert.equal(ev._to, users.other1.address); - assert.equal( - JSON.stringify(ev._ids), - JSON.stringify([BN(tokenSupplyKey)]) - ); - assert.equal( - JSON.stringify(ev._values), - JSON.stringify([BN(constants.QTY_10)]) - ); - } - ); - }); + assert.equal( + JSON.stringify(user1BalanceBeforeTransfer), + JSON.stringify(batchQuantities), + 'User1 before balance mismatch' + ); + assert.equal( + JSON.stringify(user2BalanceBeforeTransfer), + JSON.stringify([constants.ZERO, constants.ZERO]), + 'User2 before balance mismatch' + ); - it('[NEGATIVE] Should revert if owner tries to transfer voucher supply batch partially', async () => { - await expect( - utils.safeBatchTransfer1155( + const transferTx = await utils.safeBatchTransfer1155( users.other1.address, users.other2.address, - [tokenSupplyKey], - [constants.QTY_1], + tokenSupplyBatch, + batchQuantities.map((q) => q.toString()), users.other1.signer - ) - ).to.be.revertedWith(revertReasons.INVALID_QUANTITY); - }); + ); - it('[NEGATIVE] Should revert if Attacker tries to transfer batch voucher supply', async () => { - await expect( - utils.safeBatchTransfer1155( + const txReceipt = await transferTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal(ev._from, users.other1.address); + assert.equal(ev._to, users.other2.address); + assert.equal( + JSON.stringify(ev._ids), + JSON.stringify(tokenSupplyBatch) + ); + assert.equal( + JSON.stringify(ev._values), + JSON.stringify(batchQuantities) + ); + } + ); + + // balances after + const user1BalanceAfterTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other1.address, users.other1.address], + tokenSupplyBatch + ); + + const user2BalanceAfterTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other2.address, users.other2.address], + tokenSupplyBatch + ); + + assert.equal( + JSON.stringify(user1BalanceAfterTransfer), + JSON.stringify([constants.ZERO, constants.ZERO]), + 'User1 after balance mismatch' + ); + assert.equal( + JSON.stringify(user2BalanceAfterTransfer), + JSON.stringify(batchQuantities), + 'User2 after balance mismatch' + ); + + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'New owner balance from escrow does not match' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey2) + ).to.equal(users.other2.address, 'Supply 2 after - holder mismatch'); + }); + + it('Should transfer batch voucher supply to self and balance should be the same', async () => { + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey2) + ).to.equal(users.other1.address, 'Supply 2 before - holder mismatch'); + + // balances before + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1) + .mul(BN(constants.QTY_10)) + .add(BN(constants.PROMISE_DEPOSITSE2).mul(BN(constants.QTY_20))); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + + const balanceBeforeTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other1.address, users.other1.address], + tokenSupplyBatch + ); + + const transferTx = await utils.safeBatchTransfer1155( users.other1.address, - users.other2.address, - [tokenSupplyKey], - [constants.QTY_10], - users.attacker.signer - ) - ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_BATCH_1155); + users.other1.address, + tokenSupplyBatch, + batchQuantities.map((q) => q.toString()), + users.other1.signer + ); + + const balanceAfterTransfer = await contractERC1155ERC721.balanceOfBatch( + [users.other1.address, users.other1.address], + tokenSupplyBatch + ); + + assert.equal( + JSON.stringify(balanceBeforeTransfer), + JSON.stringify(balanceAfterTransfer), + 'Balance mismatch!' + ); + + const txReceipt = await transferTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + ERC1155ERC721_Factory, + eventNames.TRANSFER_BATCH, + (ev) => { + assert.equal(ev._from, users.other1.address); + assert.equal(ev._to, users.other1.address); + assert.equal( + JSON.stringify(ev._ids), + JSON.stringify(tokenSupplyBatch) + ); + assert.equal( + JSON.stringify(ev._values), + JSON.stringify(batchQuantities) + ); + } + ); + + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 after - holder mismatch'); + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey2) + ).to.equal(users.other1.address, 'Supply 2 after - holder mismatch'); + }); + + it('[NEGATIVE] Should revert if owner tries to transfer voucher supply batch partially', async () => { + await expect( + utils.safeBatchTransfer1155( + users.other1.address, + users.other2.address, + tokenSupplyBatch, + [constants.QTY_10, constants.QTY_10], + users.other1.signer + ) + ).to.be.revertedWith(revertReasons.INVALID_QUANTITY); + }); + + it('[NEGATIVE] Should revert if Attacker tries to transfer batch voucher supply', async () => { + await expect( + utils.safeBatchTransfer1155( + users.other1.address, + users.other2.address, + tokenSupplyBatch, + batchQuantities.map((q) => q.toString()), + users.attacker.signer + ) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_TRANSFER_BATCH_1155); + }); }); }); @@ -3532,20 +4241,27 @@ describe('Cashier and VoucherKernel', () => { users.other1, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_1 ); }); it('Should update escrow amounts after transfer', async () => { - expectedBalanceInEscrow = BN(constants.seller_deposit).mul( + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( BN(constants.QTY_1) ); - actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other1.address ); - actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other2.address ); @@ -3554,7 +4270,7 @@ describe('Cashier and VoucherKernel', () => { 'Old owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrow.eq(ZERO), + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -3564,30 +4280,47 @@ describe('Cashier and VoucherKernel', () => { tokenSupplyKey, constants.QTY_1, users.other1.signer - ), - (actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( - users.other1.address - )); + ); + + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other2.address ); assert.isTrue( - actualOldOwnerBalanceFromEscrow.eq(ZERO), + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), 'New owner balance from escrow does not match' ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerAmount = BN(constants.buyer_deposit); // 0.04 - const expectedSellerAmount = BN(constants.seller_deposit).add( - BN(constants.product_price) - ); // 0.35 - const expectedEscrowAmount = BN(0); // 0 + // Check supply holder before + let supplyHolder = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); + + assert.equal( + supplyHolder, + users.other1.address, + 'Supply holder mismatch' + ); + + const expectedBuyerAmount = BN(constants.PROMISE_DEPOSITBU1); + const expectedSellerAmount = BN(constants.PROMISE_DEPOSITSE1).add( + BN(constants.PROMISE_PRICE1) + ); + const expectedEscrowAmount = BN(0); await utils.safeTransfer1155( users.other1.address, @@ -3600,7 +4333,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -3615,11 +4350,89 @@ describe('Cashier and VoucherKernel', () => { const txReceipt = await withdrawTx.wait(); + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_WITHDRAWAL, + (ev) => { + expect(ev._payee).to.be.oneOf( + [users.other2.address, users.buyer.address], + 'Incorrect Payee' + ); + switch (ev._payee) { + case users.other2.address: + expect(ev._payment.toNumber()).to.be.oneOf([ + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + ]); + break; + case users.buyer.address: + assert.equal(ev._payment, constants.PROMISE_DEPOSITBU1); + break; + } + } + ); + eventUtils.assertEventEmitted( txReceipt, Cashier_Factory, eventNames.LOG_AMOUNT_DISTRIBUTION, (ev) => { + expect(ev._type).to.be.oneOf(Object.values(paymentType)); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong seller deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITSE1, + 'Wrong seller deposit amount' + ); + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.buyer.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + utils.calcTotalAmountToRecipients( ev, distributedAmounts, @@ -3642,6 +4455,16 @@ describe('Cashier and VoucherKernel', () => { distributedAmounts.escrowAmount.eq(expectedEscrowAmount), 'Escrow Amount is not as expected' ); + + supplyHolder = await contractVoucherKernel.getSupplyHolder( + tokenSupplyKey + ); + + assert.equal( + supplyHolder, + users.other2.address, + 'Supply holder mismatch' + ); }); it('New owner should be able to COF', async () => { @@ -3656,22 +4479,16 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - const cofTx = await utils.cancel(voucherID, users.other2.signer); - const txReceipt = await cofTx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_FAULT_CANCEL, - (ev) => { - assert.isTrue(ev._tokenIdVoucher.eq(voucherID)); - } - ); + expect(await utils.cancel(voucherID, users.other2.signer)) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_FAULT_CANCEL) + .withArgs(voucherID); }); it('[NEGATIVE] Old owner should not be able to COF', async () => { @@ -3686,7 +4503,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -3699,15 +4518,6 @@ describe('Cashier and VoucherKernel', () => { describe('[WITH PERMIT]', () => { describe('ETHTKN', () => { - let balanceBuyerFromDeposits = BN(0); - - let balanceSellerFromDeposits = BN(0); - - let escrowBalanceFromDeposits = BN(0); - - const cashierPaymentLeft = BN(0); - let cashierDepositLeft = BN(0); - beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -3724,9 +4534,7 @@ describe('Cashier and VoucherKernel', () => { contractBSNTokenDeposit ); - const timestamp = await Utils.getCurrTimestamp(); - - const tokensToMint = BN(constants.seller_deposit).mul( + const tokensToMint = BN(constants.PROMISE_DEPOSITSE1).mul( BN(constants.QTY_1) ); @@ -3738,43 +4546,36 @@ describe('Cashier and VoucherKernel', () => { await utils.mintTokens( 'contractBSNTokenDeposit', users.buyer.address, - constants.buyer_deposit + constants.PROMISE_DEPOSITBU1.toString() ); tokenSupplyKey = await utils.createOrder( users.other1, - timestamp, - timestamp + constants.SECONDS_IN_DAY, - constants.seller_deposit, + constants.PROMISE_VALID_FROM, + constants.PROMISE_VALID_TO, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_1 ); }); - async function getBalancesDepositToken() { - balanceBuyerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.buyer.address - ); - balanceSellerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.other2.address - ); - escrowBalanceFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.deployer.address - ); - cashierDepositLeft = await utils.contractBSNTokenDeposit.balanceOf( - utils.contractCashier.address - ); - } - it('Should update escrow amounts after transfer', async () => { - expectedBalanceInEscrow = BN(constants.seller_deposit).mul( + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + // balances before + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( BN(constants.QTY_1) ); - actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other1.address ); - actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other2.address ); @@ -3784,40 +4585,97 @@ describe('Cashier and VoucherKernel', () => { 'Old owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrow.eq(ZERO), + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), 'New owner balance from escrow does not match' ); + const user1BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceBeforeTransfer, + constants.QTY_1, + 'User1 before balance mismatch' + ); + assert.equal( + user2BalanceBeforeTransfer, + 0, + 'User2 before balance mismatch' + ); + await utils.safeTransfer1155( users.other1.address, users.other2.address, tokenSupplyKey, constants.QTY_1, users.other1.signer - ), - (actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.other1.address - )); + ); + + // balances after + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.other1.address + ); actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other2.address ); + const user1BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceAfterTransfer, + 0, + 'User1 after balance mismatch' + ); + assert.equal( + user2BalanceAfterTransfer, + constants.QTY_1, + 'User2 after balance mismatch' + ); + assert.isTrue( - actualOldOwnerBalanceFromEscrow.eq(ZERO), + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), 'New owner balance from escrow does not match' ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerDeposit = BN(constants.buyer_deposit); // 0.04 - const expectedSellerPrice = BN(constants.product_price); //// 0.3 - const expectedSellerDeposit = BN(constants.seller_deposit); // 0.05 + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1); + const expectedSellerPrice = BN(constants.PROMISE_PRICE1); + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1); const expectedEscrowAmountDeposit = BN(0); await utils.safeTransfer1155( @@ -3831,7 +4689,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -3844,9 +4704,8 @@ describe('Cashier and VoucherKernel', () => { users.deployer.signer ); - await getBalancesDepositToken(); - const txReceipt = await withdrawTx.wait(); + eventUtils.assertEventEmitted( txReceipt, Cashier_Factory, @@ -3857,6 +4716,92 @@ describe('Cashier and VoucherKernel', () => { } ); + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + (ev) => { + expect(ev._type).to.be.oneOf(Object.values(paymentType)); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong seller deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITSE1, + 'Wrong seller deposit amount' + ); + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.buyer.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + + utils.calcTotalAmountToRecipients( + ev, + distributedAmounts, + '_to', + users.buyer.address, + users.other2.address + ); + } + ); + + const balanceBuyerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( + users.buyer.address + ); + const balanceSellerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( + users.other2.address + ); + const escrowBalanceFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( + users.deployer.address + ); + const cashierDepositLeft = await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ); + const cashierPaymentLeft = await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ); + //Deposits assert.isTrue( balanceBuyerFromDeposits.eq(expectedBuyerDeposit), @@ -3894,12 +4839,16 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - await utils.cancel(voucherID, users.other2.signer); + expect(await utils.cancel(voucherID, users.other2.signer)) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_FAULT_CANCEL) + .withArgs(voucherID); }); it('[NEGATIVE] Old owner should not be able to COF', async () => { @@ -3914,7 +4863,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -3926,18 +4877,6 @@ describe('Cashier and VoucherKernel', () => { }); describe('TKNTKN', () => { - let balanceBuyerFromPayment = BN(0); - let balanceBuyerFromDeposits = BN(0); - - let balanceSellerFromPayment = BN(0); - let balanceSellerFromDeposits = BN(0); - - let escrowBalanceFromPayment = BN(0); - let escrowBalanceFromDeposits = BN(0); - - let cashierPaymentLeft = BN(0); - let cashierDepositLeft = BN(0); - beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -3955,7 +4894,9 @@ describe('Cashier and VoucherKernel', () => { ); const supplyQty = 1; - const tokensToMint = BN(constants.seller_deposit).mul(BN(supplyQty)); + const tokensToMint = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(supplyQty) + ); await utils.mintTokens( 'contractBSNTokenDeposit', @@ -3965,73 +4906,76 @@ describe('Cashier and VoucherKernel', () => { await utils.mintTokens( 'contractBSNTokenPrice', users.buyer.address, - constants.product_price + BN(constants.PROMISE_PRICE1) ); await utils.mintTokens( 'contractBSNTokenDeposit', users.buyer.address, - constants.buyer_deposit + BN(constants.PROMISE_DEPOSITBU1) ); tokenSupplyKey = await utils.createOrder( users.other1, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, supplyQty ); }); - async function getBalancesFromPiceTokenAndDepositToken() { - balanceBuyerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.buyer.address - ); - balanceBuyerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.buyer.address - ); - - balanceSellerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.other2.address - ); - balanceSellerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.other2.address - ); - - escrowBalanceFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.deployer.address - ); - escrowBalanceFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.deployer.address - ); - - cashierPaymentLeft = await utils.contractBSNTokenPrice.balanceOf( - utils.contractCashier.address - ); - cashierDepositLeft = await utils.contractBSNTokenDeposit.balanceOf( - utils.contractCashier.address - ); - } - it('Should update escrow amounts after transfer', async () => { - expectedBalanceInEscrow = BN(constants.seller_deposit).mul( + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + // balances before transfer + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( BN(constants.QTY_1) ); - actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other1.address ); - actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other2.address ); + const user1BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceBeforeTransfer, + constants.QTY_1, + 'User1 before balance mismatch' + ); + assert.equal( + user2BalanceBeforeTransfer, + 0, + 'User2 before balance mismatch' + ); + assert.isTrue( actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), 'Old owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrow.eq(ZERO), + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -4041,31 +4985,63 @@ describe('Cashier and VoucherKernel', () => { tokenSupplyKey, constants.QTY_1, users.other1.signer - ), - (actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( - contractBSNTokenDeposit.address, - users.other1.address - )); + ); + + // balances after + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( + contractBSNTokenDeposit.address, + users.other1.address + ); actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other2.address ); assert.isTrue( - actualOldOwnerBalanceFromEscrow.eq(ZERO), + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), 'New owner balance from escrow does not match' ); + + const user1BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceAfterTransfer, + 0, + 'User1 after balance mismatch' + ); + assert.equal( + user2BalanceAfterTransfer, + constants.QTY_1, + 'User2 after balance mismatch' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { const expectedBuyerPrice = BN(0); - const expectedBuyerDeposit = BN(constants.buyer_deposit); // 0.04 - const expectedSellerPrice = BN(constants.product_price); //// 0.3 - const expectedSellerDeposit = BN(constants.seller_deposit); // 0.05 + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1); + const expectedSellerPrice = BN(constants.PROMISE_PRICE1); + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1); const expectedEscrowAmountDeposit = BN(0); const expectedEscrowAmountPrice = BN(0); @@ -4080,7 +5056,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -4088,47 +5066,141 @@ describe('Cashier and VoucherKernel', () => { await advanceTimeSeconds(60); await utils.finalize(voucherID, users.deployer.signer); - await utils.withdraw(voucherID, users.deployer.signer); + const withdrawTx = await utils.withdraw( + voucherID, + users.deployer.signer + ); + + const txReceipt = await withdrawTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + (ev) => { + expect(ev._type).to.be.oneOf(Object.values(paymentType)); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong seller deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITSE1, + 'Wrong seller deposit amount' + ); + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.buyer.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } - await getBalancesFromPiceTokenAndDepositToken(); + utils.calcTotalAmountToRecipients( + ev, + distributedAmounts, + '_to', + users.buyer.address, + users.other2.address + ); + } + ); //Payments - assert.isTrue( - balanceBuyerFromPayment.eq(expectedBuyerPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.buyer.address) + ).to.equal( + expectedBuyerPrice, 'Buyer did not get expected tokens from PriceTokenContract' ); - assert.isTrue( - balanceSellerFromPayment.eq(expectedSellerPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.other2.address) + ).to.equal( + expectedSellerPrice, 'Seller did not get expected tokens from PriceTokenContract' ); - assert.isTrue( - escrowBalanceFromPayment.eq(expectedEscrowAmountPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.deployer.address) + ).to.equal( + expectedEscrowAmountPrice, 'Escrow did not get expected tokens from PriceTokenContract' ); - //Deposits - assert.isTrue( - balanceBuyerFromDeposits.eq(expectedBuyerDeposit), + // //Deposits + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.buyer.address) + ).to.equal( + expectedBuyerDeposit, 'Buyer did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - balanceSellerFromDeposits.eq(expectedSellerDeposit), + + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.other2.address) + ).to.equal( + expectedSellerDeposit, 'Seller did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - escrowBalanceFromDeposits.eq(expectedEscrowAmountDeposit), + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + users.deployer.address + ) + ).to.equal( + expectedEscrowAmountDeposit, 'Escrow did not get expected tokens from DepositTokenContract' ); //Cashier Should be Empty - assert.isTrue( - cashierPaymentLeft.eq(ZERO), - 'Cashier Contract is not empty' - ); - assert.isTrue( - cashierDepositLeft.eq(ZERO), - 'Cashier Contract is not empty' - ); + expect( + await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); }); it('New owner should be able to COF', async () => { @@ -4143,22 +5215,16 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - const cofTx = await utils.cancel(voucherID, users.other2.signer); - const txReceipt = await cofTx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_FAULT_CANCEL, - (ev) => { - assert.isTrue(ev._tokenIdVoucher.eq(voucherID)); - } - ); + expect(await utils.cancel(voucherID, users.other2.signer)) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_FAULT_CANCEL) + .withArgs(voucherID); }); it('[NEGATIVE] Old owner should not be able to COF', async () => { @@ -4173,7 +5239,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -4185,13 +5253,6 @@ describe('Cashier and VoucherKernel', () => { }); describe('TKNETH', () => { - let balanceBuyerFromPayment = BN(0); - let balanceSellerFromPayment = BN(0); - let escrowBalanceFromPayment = BN(0); - - let cashierPaymentLeft = BN(0); - const cashierDepositLeft = BN(0); - beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -4205,48 +5266,41 @@ describe('Cashier and VoucherKernel', () => { contractCashier, contractBosonRouter, contractBSNTokenPrice, - '' + contractBSNTokenDeposit ); await utils.mintTokens( 'contractBSNTokenPrice', users.buyer.address, - constants.product_price + BN(constants.PROMISE_PRICE1) ); tokenSupplyKey = await utils.createOrder( users.other1, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_1 ); }); - async function getBalancesPriceToken() { - balanceBuyerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.buyer.address - ); - balanceSellerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.other2.address - ); - escrowBalanceFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.deployer.address - ); - cashierPaymentLeft = await utils.contractBSNTokenPrice.balanceOf( - utils.contractCashier.address - ); - } - it('Should update escrow amounts after transfer', async () => { - expectedBalanceInEscrow = BN(constants.seller_deposit).mul( + // Check supply holder before + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other1.address, 'Supply 1 before - holder mismatch'); + + // balances before + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITSE1).mul( BN(constants.QTY_1) ); - actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other1.address ); - actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other2.address ); @@ -4255,10 +5309,35 @@ describe('Cashier and VoucherKernel', () => { 'Old owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrow.eq(ZERO), + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), 'New owner balance from escrow does not match' ); + const user1BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceBeforeTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceBeforeTransfer, + constants.QTY_1, + 'User1 before balance mismatch' + ); + assert.equal( + user2BalanceBeforeTransfer, + 0, + 'User2 before balance mismatch' + ); + await utils.safeTransfer1155( users.other1.address, users.other2.address, @@ -4267,6 +5346,7 @@ describe('Cashier and VoucherKernel', () => { users.other1.signer ); + // balances after actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( users.other1.address ); @@ -4275,21 +5355,51 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualOldOwnerBalanceFromEscrow.eq(ZERO), + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), 'New owner balance from escrow does not match' ); + + const user1BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other1.address, + tokenSupplyKey + ) + )[0]; + + const user2BalanceAfterTransfer = ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf1155]( + users.other2.address, + tokenSupplyKey + ) + )[0]; + + assert.equal( + user1BalanceAfterTransfer, + 0, + 'User1 after balance mismatch' + ); + assert.equal( + user2BalanceAfterTransfer, + constants.QTY_1, + 'User2 after balance mismatch' + ); + + // Check supply holder after + expect( + await contractVoucherKernel.getSupplyHolder(tokenSupplyKey) + ).to.equal(users.other2.address, 'Supply 1 after - holder mismatch'); }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { const expectedBuyerPrice = BN(0); - const expectedSellerPrice = BN(constants.product_price); // 0.3 + const expectedSellerPrice = BN(constants.PROMISE_PRICE1); const expectedEscrowPrice = BN(0); - const expectedBuyerDeposit = BN(constants.buyer_deposit); // 0.04 - const expectedSellerDeposit = BN(constants.seller_deposit); // 0.05 + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1); + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1); const expectedEscrowAmountDeposit = BN(0); await utils.safeTransfer1155( @@ -4303,7 +5413,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -4315,20 +5427,25 @@ describe('Cashier and VoucherKernel', () => { users.deployer.signer ); - await getBalancesPriceToken(); - // Payments in TKN - // Payment should have been sent to seller - assert.isTrue( - balanceBuyerFromPayment.eq(expectedBuyerPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.buyer.address) + ).to.equal( + expectedBuyerPrice, 'Buyer did not get expected tokens from PaymentTokenContract' ); - assert.isTrue( - balanceSellerFromPayment.eq(expectedSellerPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.other2.address) + ).to.equal( + expectedSellerPrice, 'Seller did not get expected tokens from PaymentTokenContract' ); - assert.isTrue( - escrowBalanceFromPayment.eq(expectedEscrowPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.deployer.address) + ).to.equal( + expectedEscrowPrice, 'Escrow did not get expected tokens from PaymentTokenContract' ); @@ -4349,6 +5466,68 @@ describe('Cashier and VoucherKernel', () => { } ); + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + (ev) => { + expect(ev._type).to.be.oneOf(Object.values(paymentType)); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong seller deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITSE1, + 'Wrong seller deposit amount' + ); + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.buyer.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + } + ); + //Deposits in ETH assert.isTrue( distributedAmounts.buyerAmount.eq(expectedBuyerDeposit), @@ -4364,14 +5543,17 @@ describe('Cashier and VoucherKernel', () => { ); //Cashier Should be Empty - assert.isTrue( - cashierPaymentLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); - assert.isTrue( - cashierDepositLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); + expect( + await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); }); it('New owner should be able to COF', async () => { @@ -4386,22 +5568,16 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); - const cofTx = await utils.cancel(voucherID, users.other2.signer); - const txReceipt = await cofTx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - VoucherKernel_Factory, - eventNames.LOG_VOUCHER_FAULT_CANCEL, - (ev) => { - assert.isTrue(ev._tokenIdVoucher.eq(voucherID)); - } - ); + expect(await utils.cancel(voucherID, users.other2.signer)) + .to.emit(contractVoucherKernel, eventNames.LOG_VOUCHER_FAULT_CANCEL) + .withArgs(voucherID); }); it('[NEGATIVE] Old owner should not be able to COF', async () => { @@ -4416,7 +5592,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.buyer, users.other2, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.redeem(voucherID, users.buyer.signer); @@ -4430,26 +5608,23 @@ describe('Cashier and VoucherKernel', () => { }); describe('VOUCHER TRANSFER', () => { - let actualOldOwnerBalanceFromEscrowEth = BN(0); - let actualOldOwnerBalanceFromEscrowTkn = BN(0); - let actualNewOwnerBalanceFromEscrowEth = BN(0); - let actualNewOwnerBalanceFromEscrowTkn = BN(0); + const paymentType = { + PAYMENT: 0, + DEPOSIT_SELLER: 1, + DEPOSIT_BUYER: 2, + }; - afterEach(() => { + beforeEach(() => { distributedAmounts = { buyerAmount: BN(0), sellerAmount: BN(0), escrowAmount: BN(0), }; - - actualOldOwnerBalanceFromEscrowEth = BN(0); - actualOldOwnerBalanceFromEscrowTkn = BN(0); - actualNewOwnerBalanceFromEscrowEth = BN(0); - actualNewOwnerBalanceFromEscrowTkn = BN(0); }); describe('Common transfer', () => { - before(async () => { + let voucherID; + beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -4466,58 +5641,145 @@ describe('Cashier and VoucherKernel', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_10 ); - }); - it('Should transfer a voucher', async () => { - const voucherID = await utils.commitToBuy( + voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); + }); - const transferTx = await utils.safeTransfer721( - users.other1.address, - users.other2.address, - voucherID, - users.other1.signer + it('Should transfer a voucher', async () => { + // balances before + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITBU1).add( + constants.PROMISE_PRICE1 ); - const txReceipt = await transferTx.wait(); + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + let actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address + ); - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER, - (ev) => { - assert.equal(ev._from, users.other1.address); - assert.equal(ev._to, users.other2.address); - assert.equal(ev._tokenId.toString(), voucherID); - } + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(constants.ZERO), + 'New owner balance from escrow does not match' + ); + + expect( + ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.other1.address + ) + )[0] + ).to.equal(1, 'User1 before balance mismatch'); + + expect( + ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.other2.address + ) + )[0] + ).to.equal(0, 'User2 before balance mismatch'); + + expect( + await utils.safeTransfer721( + users.other1.address, + users.other2.address, + voucherID, + users.other1.signer + ) + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(users.other1.address, users.other2.address, voucherID); + + // balances after + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + actualNewOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other2.address ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(constants.ZERO), + 'Old owner balance from escrow does not match' + ); + assert.isTrue( + actualNewOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'New owner balance from escrow does not match' + ); + + expect( + ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.other1.address + ) + )[0] + ).to.equal(0, 'User1 after balance mismatch'); + + expect( + ( + await contractERC1155ERC721.functions[fnSignatures.balanceOf721]( + users.other2.address + ) + )[0] + ).to.equal(1, 'User2 after balance mismatch'); }); it('Should transfer voucher to self and balance should be the same', async () => { - const balanceOf = - contractERC1155ERC721.functions[fnSignatures.balanceOf721]; + // balances before + const expectedBalanceInEscrow = BN(constants.PROMISE_DEPOSITBU1).add( + constants.PROMISE_PRICE1 + ); - const voucherID = await utils.commitToBuy( - users.other1, - users.seller, - tokenSupplyKey + let actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' ); + const balanceOf = + contractERC1155ERC721.functions[fnSignatures.balanceOf721]; + const balanceBeforeTransfer = ( await balanceOf(users.other1.address) )[0]; - const transferTx = await utils.safeTransfer721( - users.other1.address, - users.other1.address, - voucherID, - users.other1.signer + expect( + await utils.safeTransfer721( + users.other1.address, + users.other1.address, + voucherID, + users.other1.signer + ) + ) + .to.emit(contractERC1155ERC721, eventNames.TRANSFER) + .withArgs(users.other1.address, users.other1.address, voucherID); + + // balances after + actualOldOwnerBalanceFromEscrow = await contractCashier.getEscrowAmount( + users.other1.address + ); + + assert.isTrue( + actualOldOwnerBalanceFromEscrow.eq(expectedBalanceInEscrow), + 'Old owner balance from escrow does not match' ); const balanceAfterTransfer = (await balanceOf(users.other1.address))[0]; @@ -4526,19 +5788,6 @@ describe('Cashier and VoucherKernel', () => { balanceBeforeTransfer.eq(balanceAfterTransfer), 'Balance mismatch!' ); - - const txReceipt = await transferTx.wait(); - - eventUtils.assertEventEmitted( - txReceipt, - ERC1155ERC721_Factory, - eventNames.TRANSFER, - (ev) => { - assert.equal(ev._from, users.other1.address); - assert.equal(ev._to, users.other1.address); - assert.equal(ev._tokenId.toString(), voucherID); - } - ); }); }); @@ -4560,25 +5809,30 @@ describe('Cashier and VoucherKernel', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_10 ); }); it('Should update escrow amounts after transfer', async () => { - const expectedBalanceInEscrow = BN(constants.product_price).add( - BN(constants.buyer_deposit) + const expectedBalanceInEscrow = BN(constants.PROMISE_PRICE1).add( + BN(constants.PROMISE_DEPOSITBU1) ); + const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); - actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other1.address ); - actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other2.address ); @@ -4587,7 +5841,7 @@ describe('Cashier and VoucherKernel', () => { 'Old owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrowEth.eq(ZERO), + actualNewOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -4606,7 +5860,7 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualOldOwnerBalanceFromEscrowEth.eq(ZERO), + actualOldOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( @@ -4616,16 +5870,22 @@ describe('Cashier and VoucherKernel', () => { }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerAmount = BN(constants.buyer_deposit) - .add(BN(constants.product_price)) - .add(BN(constants.seller_deposit).div(BN(2))); // 0.3 + 0.04 + 0.025 - const expectedSellerAmount = BN(constants.seller_deposit).div(BN(4)); // 0.0125 - const expectedEscrowAmount = BN(constants.seller_deposit).div(BN(4)); // 0.0125 + const expectedBuyerAmount = BN(constants.PROMISE_DEPOSITBU1) + .add(BN(constants.PROMISE_PRICE1)) + .add(BN(constants.PROMISE_DEPOSITSE1).div(BN(2))); + const expectedSellerAmount = BN(constants.PROMISE_DEPOSITSE1).div( + BN(4) + ); + const expectedEscrowAmount = BN(constants.PROMISE_DEPOSITSE1).div( + BN(4) + ); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -4647,11 +5907,137 @@ describe('Cashier and VoucherKernel', () => { const txReceipt = await withdrawTx.wait(); + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_WITHDRAWAL, + (ev) => { + expect(ev._payee).to.be.oneOf( + [ + users.other2.address, + users.seller.address, + users.deployer.address, + ], + 'Incorrect Payee' + ); + switch (ev._payee) { + case users.other2.address: + expect(ev._payment.toNumber()).to.be.oneOf([ + expectedBuyerAmount.sub(constants.PROMISE_PRICE1).toNumber(), + constants.PROMISE_PRICE1, + ]); + break; + case users.seller.address: + expect(ev._payment.toNumber()).to.be.oneOf([ + BN(constants.PROMISE_DEPOSITSE1).div(4).toNumber(), + constants.PROMISE_DEPOSITBU1, + ]); + break; + case users.deployer.address: + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toNumber() + ); + break; + } + } + ); + eventUtils.assertEventEmitted( txReceipt, Cashier_Factory, eventNames.LOG_AMOUNT_DISTRIBUTION, (ev) => { + expect(ev._type).to.be.oneOf( + Object.values(paymentType), + 'Wrong payment type' + ); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + expect(ev._to).to.be.oneOf( + [ + users.seller.address, + users.deployer.address, + users.other2.address, + ], + 'Unexpected recipient' + ); + + switch (ev._to) { + case users.other2.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(2).toString(), + 'Wrong payment amount' + ); + break; + case users.seller.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.deployer.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + } + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + utils.calcTotalAmountToRecipients( ev, distributedAmounts, @@ -4680,7 +6066,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.other1.signer); @@ -4704,7 +6092,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -4727,44 +6117,24 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await expect( utils.safeTransfer721( - users.other1.address, - users.other2.address, - voucherID, - users.attacker.signer - ) - ).to.be.revertedWith(revertReasons.NOT_OWNER_NOR_APPROVED); - }); - }); - - describe('[WITH PERMIT]', () => { - describe('ETHTKN', () => { - let balanceBuyerFromDeposits = BN(0); - let balanceSellerFromDeposits = BN(0); - let escrowBalanceFromDeposits = BN(0); - - let cashierPaymentLeft = BN(0); - let cashierDepositLeft = BN(0); - - async function getBalancesDepositToken() { - balanceBuyerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.other2.address - ); - balanceSellerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.seller.address - ); - escrowBalanceFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.deployer.address - ); - cashierDepositLeft = await utils.contractBSNTokenDeposit.balanceOf( - utils.contractCashier.address - ); - } + users.other1.address, + users.other2.address, + voucherID, + users.attacker.signer + ) + ).to.be.revertedWith(revertReasons.NOT_OWNER_NOR_APPROVED); + }); + }); + describe('[WITH PERMIT]', () => { + describe('ETHTKN', () => { beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -4782,7 +6152,9 @@ describe('Cashier and VoucherKernel', () => { ); const supplyQty = 1; - const tokensToMint = BN(constants.seller_deposit).mul(BN(supplyQty)); + const tokensToMint = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(supplyQty) + ); await utils.mintTokens( 'contractBSNTokenDeposit', @@ -4792,62 +6164,54 @@ describe('Cashier and VoucherKernel', () => { await utils.mintTokens( 'contractBSNTokenDeposit', users.other1.address, - constants.buyer_deposit + BN(constants.PROMISE_DEPOSITBU1) ); tokenSupplyKey = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, supplyQty ); }); - afterEach(async () => { + beforeEach(async () => { distributedAmounts = { buyerAmount: BN(0), sellerAmount: BN(0), escrowAmount: BN(0), }; - - balanceBuyerFromDeposits = BN(0); - balanceSellerFromDeposits = BN(0); - escrowBalanceFromDeposits = BN(0); - - cashierPaymentLeft = BN(0); - cashierDepositLeft = BN(0); - - const isPaused = await contractCashier.paused(); - if (isPaused) { - await contractCashier.unpause(); - } }); it('Should update escrow amounts after transfer', async () => { - const expectedBalanceInEscrowEth = BN(constants.product_price); - const expectedBalanceInEscrowTkn = BN(constants.buyer_deposit); + const expectedBalanceInEscrowEth = BN(constants.PROMISE_PRICE1); + const expectedBalanceInEscrowTkn = BN(constants.PROMISE_DEPOSITBU1); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); - actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other1.address ); - actualOldOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( + let actualOldOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other1.address ); - actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other2.address ); - actualNewOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( + let actualNewOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, users.other2.address ); @@ -4863,12 +6227,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualNewOwnerBalanceFromEscrowEth.eq(ZERO), + actualNewOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'New owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrowTkn.eq(ZERO), + actualNewOwnerBalanceFromEscrowTkn.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -4898,12 +6262,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualOldOwnerBalanceFromEscrowEth.eq(ZERO), + actualOldOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( - actualOldOwnerBalanceFromEscrowTkn.eq(ZERO), + actualOldOwnerBalanceFromEscrowTkn.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); @@ -4919,19 +6283,23 @@ describe('Cashier and VoucherKernel', () => { }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerPrice = BN(constants.product_price); // 0.3 - const expectedBuyerDeposit = BN(constants.buyer_deposit).add( - BN(constants.seller_deposit).div(BN(2)) - ); // 0.065 - const expectedSellerDeposit = BN(constants.seller_deposit).div(BN(4)); // 0.0125 - const expectedEscrowAmountDeposit = BN(constants.seller_deposit).div( + const expectedBuyerPrice = BN(constants.PROMISE_PRICE1); + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1).add( + BN(constants.PROMISE_DEPOSITSE1).div(BN(2)) + ); + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1).div( BN(4) - ); // 0.0125 + ); + const expectedEscrowAmountDeposit = BN( + constants.PROMISE_DEPOSITSE1 + ).div(BN(4)); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -4951,10 +6319,151 @@ describe('Cashier and VoucherKernel', () => { users.deployer.signer ); - await getBalancesDepositToken(); + const txReceipt = await withdrawTx.wait(); + + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_WITHDRAWAL, + (ev) => { + expect(ev._payee).to.be.oneOf( + [ + users.other2.address, + users.seller.address, + users.deployer.address, + ], + 'Incorrect Payee' + ); + switch (ev._payee) { + case users.other2.address: + expect(ev._payment.toNumber()).to.be.oneOf([ + constants.PROMISE_PRICE1, + ]); + break; + case users.seller.address: + expect(ev._payment.toNumber()).to.be.oneOf([ + BN(constants.PROMISE_DEPOSITSE1).div(4).toNumber(), + constants.PROMISE_DEPOSITBU1, + ]); + break; + case users.deployer.address: + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toNumber() + ); + break; + } + } + ); + + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + + (ev) => { + expect(ev._type).to.be.oneOf( + Object.values(paymentType), + 'Wrong payment type' + ); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + expect(ev._to).to.be.oneOf( + [ + users.seller.address, + users.deployer.address, + users.other2.address, + ], + 'Unexpected recipient' + ); + + switch (ev._to) { + case users.other2.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(2).toString(), + 'Wrong payment amount' + ); + break; + case users.seller.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.deployer.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + } + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + + utils.calcTotalAmountToRecipients( + ev, + distributedAmounts, + '_to', + users.buyer.address, + users.other2.address + ); + } + ); // Payment should have been returned to buyer - const txReceipt = await withdrawTx.wait(); + // const txReceipt = await withdrawTx.wait(); eventUtils.assertEventEmitted( txReceipt, Cashier_Factory, @@ -4966,35 +6475,48 @@ describe('Cashier and VoucherKernel', () => { ); //Deposits - assert.isTrue( - balanceBuyerFromDeposits.eq(expectedBuyerDeposit), + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.other2.address) + ).to.equal( + expectedBuyerDeposit, 'NewVoucherOwner did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - balanceSellerFromDeposits.eq(expectedSellerDeposit), + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.seller.address) + ).to.equal( + expectedSellerDeposit, 'Seller did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - escrowBalanceFromDeposits.eq(expectedEscrowAmountDeposit), + expect( + await utils.contractBSNTokenDeposit.balanceOf( + users.deployer.address + ) + ).to.equal( + expectedEscrowAmountDeposit, 'Escrow did not get expected tokens from DepositTokenContract' ); //Cashier Should be Empty - assert.isTrue( - cashierPaymentLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); - assert.isTrue( - cashierDepositLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); + expect( + await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); }); it('[NEGATIVE] Should not transfer a voucher if payments / deposits are released', async () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.other1.signer); @@ -5018,7 +6540,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -5041,7 +6565,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await expect( @@ -5056,48 +6582,6 @@ describe('Cashier and VoucherKernel', () => { }); describe('TKNTKN', () => { - let balanceBuyerFromPayment = BN(0); - let balanceBuyerFromDeposits = BN(0); - - let balanceSellerFromPayment = BN(0); - let balanceSellerFromDeposits = BN(0); - - let escrowBalanceFromPayment = BN(0); - let escrowBalanceFromDeposits = BN(0); - - let cashierPaymentLeft = BN(0); - let cashierDepositLeft = BN(0); - - async function getBalancesFromPiceTokenAndDepositToken() { - balanceBuyerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.other2.address - ); - balanceBuyerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.other2.address - ); - - balanceSellerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.seller.address - ); - balanceSellerFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.seller.address - ); - - escrowBalanceFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.deployer.address - ); - escrowBalanceFromDeposits = await utils.contractBSNTokenDeposit.balanceOf( - users.deployer.address - ); - - cashierPaymentLeft = await utils.contractBSNTokenPrice.balanceOf( - utils.contractCashier.address - ); - cashierDepositLeft = await utils.contractBSNTokenDeposit.balanceOf( - utils.contractCashier.address - ); - } - beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -5115,7 +6599,9 @@ describe('Cashier and VoucherKernel', () => { ); const supplyQty = 1; - const tokensToMint = BN(constants.seller_deposit).mul(BN(supplyQty)); + const tokensToMint = BN(constants.PROMISE_DEPOSITSE1).mul( + BN(supplyQty) + ); await utils.mintTokens( 'contractBSNTokenDeposit', @@ -5137,18 +6623,24 @@ describe('Cashier and VoucherKernel', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, supplyQty ); }); it('Should update escrow amounts after transfer', async () => { - const expectedBalanceInEscrowTknPrice = BN(constants.product_price); - const expectedBalanceInEscrowTknDeposit = BN(constants.buyer_deposit); + const expectedBalanceInEscrowTknPrice = BN(constants.PROMISE_PRICE1); + const expectedBalanceInEscrowTknDeposit = BN( + constants.PROMISE_DEPOSITBU1 + ); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); let actualOldOwnerBalanceFromEscrowTknPrice = await contractCashier.getEscrowTokensAmount( @@ -5186,12 +6678,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualNewOwnerBalanceFromEscrowTknPrice.eq(ZERO), + actualNewOwnerBalanceFromEscrowTknPrice.eq(constants.ZERO), 'New owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrowTknDeposit.eq(ZERO), + actualNewOwnerBalanceFromEscrowTknDeposit.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -5200,11 +6692,12 @@ describe('Cashier and VoucherKernel', () => { users.other2.address, voucherID, users.other1.signer - ), - (actualOldOwnerBalanceFromEscrowTknPrice = await contractCashier.getEscrowTokensAmount( - contractBSNTokenPrice.address, - users.other1.address - )); + ); + + actualOldOwnerBalanceFromEscrowTknPrice = await contractCashier.getEscrowTokensAmount( + contractBSNTokenPrice.address, + users.other1.address + ); actualOldOwnerBalanceFromEscrowTknDeposit = await contractCashier.getEscrowTokensAmount( contractBSNTokenDeposit.address, @@ -5222,12 +6715,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualOldOwnerBalanceFromEscrowTknPrice.eq(ZERO), + actualOldOwnerBalanceFromEscrowTknPrice.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( - actualOldOwnerBalanceFromEscrowTknDeposit.eq(ZERO), + actualOldOwnerBalanceFromEscrowTknDeposit.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); @@ -5247,21 +6740,25 @@ describe('Cashier and VoucherKernel', () => { }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerPrice = BN(constants.product_price); // 0.3 - const expectedBuyerDeposit = BN(constants.buyer_deposit).add( - BN(constants.seller_deposit).div(BN(2)) - ); // 0.065 + const expectedBuyerPrice = BN(constants.PROMISE_PRICE1); + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1).add( + BN(constants.PROMISE_DEPOSITSE1).div(BN(2)) + ); const expectedSellerPrice = BN(0); - const expectedSellerDeposit = BN(constants.seller_deposit).div(BN(4)); // 0.0125 - const expectedEscrowAmountDeposit = BN(constants.seller_deposit).div( + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1).div( BN(4) - ); // 0.0125 + ); + const expectedEscrowAmountDeposit = BN( + constants.PROMISE_DEPOSITSE1 + ).div(BN(4)); const expectedEscrowAmountPrice = BN(0); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -5276,54 +6773,186 @@ describe('Cashier and VoucherKernel', () => { await utils.cancel(voucherID, users.seller.signer); await utils.finalize(voucherID, users.deployer.signer); - await utils.withdraw(voucherID, users.deployer.signer); + // await utils.withdraw(voucherID, users.deployer.signer); + const withdrawTx = await utils.withdraw( + voucherID, + users.deployer.signer + ); + + const txReceipt = await withdrawTx.wait(); - await getBalancesFromPiceTokenAndDepositToken(); + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + (ev) => { + expect(ev._type).to.be.oneOf( + Object.values(paymentType), + 'Wrong payment type' + ); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + expect(ev._to).to.be.oneOf( + [ + users.seller.address, + users.deployer.address, + users.other2.address, + ], + 'Unexpected recipient' + ); + + switch (ev._to) { + case users.other2.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(2).toString(), + 'Wrong payment amount' + ); + break; + case users.seller.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.deployer: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + } + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + + utils.calcTotalAmountToRecipients( + ev, + distributedAmounts, + '_to', + users.buyer.address, + users.other2.address + ); + } + ); //Payments - assert.isTrue( - balanceBuyerFromPayment.eq(expectedBuyerPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.other2.address) + ).to.equal( + expectedBuyerPrice, 'Buyer did not get expected tokens from PriceTokenContract' ); - assert.isTrue( - balanceSellerFromPayment.eq(expectedSellerPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.seller.address) + ).to.equal( + expectedSellerPrice, 'Seller did not get expected tokens from PriceTokenContract' ); - assert.isTrue( - escrowBalanceFromPayment.eq(expectedEscrowAmountPrice), + + expect( + await utils.contractBSNTokenPrice.balanceOf(users.deployer.address) + ).to.equal( + expectedEscrowAmountPrice, 'Escrow did not get expected tokens from PriceTokenContract' ); //Deposits - assert.isTrue( - balanceBuyerFromDeposits.eq(expectedBuyerDeposit), + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.other2.address) + ).to.equal( + expectedBuyerDeposit, 'Buyer did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - balanceSellerFromDeposits.eq(expectedSellerDeposit), + + expect( + await utils.contractBSNTokenDeposit.balanceOf(users.seller.address) + ).to.equal( + expectedSellerDeposit, 'Seller did not get expected tokens from DepositTokenContract' ); - assert.isTrue( - escrowBalanceFromDeposits.eq(expectedEscrowAmountDeposit), + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + users.deployer.address + ) + ).to.equal( + expectedEscrowAmountDeposit, 'Buyer did not get expected tokens from DepositTokenContract' ); //Cashier Should be Empty - assert.isTrue( - cashierPaymentLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); - assert.isTrue( - cashierDepositLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); + expect( + await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); }); it('[NEGATIVE] Should not transfer a voucher if payments / deposits are released', async () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.other1.signer); @@ -5347,7 +6976,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -5370,7 +7001,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await expect( @@ -5385,13 +7018,6 @@ describe('Cashier and VoucherKernel', () => { }); describe('TKNETH', () => { - let balanceBuyerFromPayment = BN(0); - let balanceSellerFromPayment = BN(0); - let escrowBalanceFromPayment = BN(0); - - let cashierPaymentLeft = BN(0); - const cashierDepositLeft = BN(0); - beforeEach(async () => { await deployContracts(); await setPeriods(); @@ -5405,62 +7031,51 @@ describe('Cashier and VoucherKernel', () => { contractCashier, contractBosonRouter, contractBSNTokenPrice, - '' + contractBSNTokenDeposit ); await utils.mintTokens( 'contractBSNTokenPrice', users.other1.address, - constants.product_price + BN(constants.PROMISE_PRICE1) ); tokenSupplyKey = await utils.createOrder( users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, - constants.seller_deposit, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITSE1, + constants.PROMISE_DEPOSITBU1, constants.QTY_1 ); }); - async function getBalancesPriceToken() { - balanceBuyerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.other2.address - ); - balanceSellerFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.seller.address - ); - escrowBalanceFromPayment = await utils.contractBSNTokenPrice.balanceOf( - users.deployer.address - ); - cashierPaymentLeft = await utils.contractBSNTokenPrice.balanceOf( - utils.contractCashier.address - ); - } - it('Should update escrow amounts after transfer', async () => { - const expectedBalanceInEscrowEth = BN(constants.buyer_deposit); - const expectedBalanceInEscrowTkn = BN(constants.product_price); + const expectedBalanceInEscrowEth = BN(constants.PROMISE_DEPOSITBU1); + const expectedBalanceInEscrowTkn = BN(constants.PROMISE_PRICE1); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); - actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other1.address ); - actualOldOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( + let actualOldOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( contractBSNTokenPrice.address, users.other1.address ); - actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + let actualNewOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( users.other2.address ); - actualNewOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( + let actualNewOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( contractBSNTokenPrice.address, users.other2.address ); @@ -5476,12 +7091,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualNewOwnerBalanceFromEscrowEth.eq(ZERO), + actualNewOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'New owner balance from escrow does not match' ); assert.isTrue( - actualNewOwnerBalanceFromEscrowTkn.eq(ZERO), + actualNewOwnerBalanceFromEscrowTkn.eq(constants.ZERO), 'New owner balance from escrow does not match' ); @@ -5490,10 +7105,11 @@ describe('Cashier and VoucherKernel', () => { users.other2.address, voucherID, users.other1.signer - ), - (actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( - users.other1.address - )); + ); + + actualOldOwnerBalanceFromEscrowEth = await contractCashier.getEscrowAmount( + users.other1.address + ); actualOldOwnerBalanceFromEscrowTkn = await contractCashier.getEscrowTokensAmount( contractBSNTokenPrice.address, @@ -5510,12 +7126,12 @@ describe('Cashier and VoucherKernel', () => { ); assert.isTrue( - actualOldOwnerBalanceFromEscrowEth.eq(ZERO), + actualOldOwnerBalanceFromEscrowEth.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); assert.isTrue( - actualOldOwnerBalanceFromEscrowTkn.eq(ZERO), + actualOldOwnerBalanceFromEscrowTkn.eq(constants.ZERO), 'Old owner balance from escrow does not match' ); @@ -5531,21 +7147,25 @@ describe('Cashier and VoucherKernel', () => { }); it('Should finalize 1 voucher to ensure payments are sent to the new owner', async () => { - const expectedBuyerPrice = BN(constants.product_price); // 0.3 + const expectedBuyerPrice = BN(constants.PROMISE_PRICE1); const expectedSellerPrice = BN(0); const expectedEscrowPrice = BN(0); - const expectedBuyerDeposit = BN(constants.buyer_deposit).add( - BN(constants.seller_deposit).div(BN(2)) - ); // 0.065 - const expectedSellerDeposit = BN(constants.seller_deposit).div(BN(4)); // 0.0125 - const expectedEscrowAmountDeposit = BN(constants.seller_deposit).div( + const expectedBuyerDeposit = BN(constants.PROMISE_DEPOSITBU1).add( + BN(constants.PROMISE_DEPOSITSE1).div(BN(2)) + ); + const expectedSellerDeposit = BN(constants.PROMISE_DEPOSITSE1).div( BN(4) - ); // 0.0125 + ); + const expectedEscrowAmountDeposit = BN( + constants.PROMISE_DEPOSITSE1 + ).div(BN(4)); const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -5553,8 +7173,8 @@ describe('Cashier and VoucherKernel', () => { users.other2.address, voucherID, users.other1.signer - ), - await utils.refund(voucherID, users.other2.signer); + ); + await utils.refund(voucherID, users.other2.signer); await utils.complain(voucherID, users.other2.signer); await utils.cancel(voucherID, users.seller.signer); await utils.finalize(voucherID, users.deployer.signer); @@ -5564,25 +7184,166 @@ describe('Cashier and VoucherKernel', () => { users.deployer.signer ); - await getBalancesPriceToken(); + const txReceipt = await withdrawTx.wait(); // Payments in TKN // Payment should have been returned to buyer - assert.isTrue( - balanceBuyerFromPayment.eq(expectedBuyerPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.other2.address) + ).to.equal( + expectedBuyerPrice, 'Buyer did not get expected tokens from PaymentTokenContract' ); - assert.isTrue( - balanceSellerFromPayment.eq(expectedSellerPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.seller.address) + ).to.equal( + expectedSellerPrice, 'Seller did not get expected tokens from PaymentTokenContract' ); - assert.isTrue( - escrowBalanceFromPayment.eq(expectedEscrowPrice), + expect( + await utils.contractBSNTokenPrice.balanceOf(users.deployer.address) + ).to.equal( + expectedEscrowPrice, 'Escrow did not get expected tokens from PaymentTokenContract' ); - const txReceipt = await withdrawTx.wait(); - //Deposits in ETH + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_WITHDRAWAL, + (ev) => { + expect(ev._payee).to.be.oneOf( + [ + users.other2.address, + users.seller.address, + users.deployer.address, + ], + 'Incorrect Payee' + ); + switch (ev._payee) { + case users.other2.address: + assert.equal( + ev._payment.toString(), + expectedBuyerDeposit.toString(), + 'Wrong payment amount' + ); + break; + case users.seller.address: + assert.equal( + ev._payment.toString(), + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.deployer.address: + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + } + } + ); + + // //Deposits in ETH + eventUtils.assertEventEmitted( + txReceipt, + Cashier_Factory, + eventNames.LOG_AMOUNT_DISTRIBUTION, + (ev) => { + expect(ev._type).to.be.oneOf( + Object.values(paymentType), + 'Wrong payment type' + ); + switch (ev._type) { + case paymentType.PAYMENT: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong payment recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_PRICE1, + 'Wrong payment amount' + ); + break; + case paymentType.DEPOSIT_SELLER: + expect(ev._to).to.be.oneOf( + [ + users.seller.address, + users.deployer.address, + users.other2.address, + ], + 'Unexpected recipient' + ); + + switch (ev._to) { + case users.seller.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.deployer.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(4).toString(), + 'Wrong payment amount' + ); + break; + case users.other2.address: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._payment, + BN(constants.PROMISE_DEPOSITSE1).div(2).toString(), + 'Wrong payment amount' + ); + break; + } + break; + case paymentType.DEPOSIT_BUYER: + assert.equal( + ev._tokenIdVoucher.toString(), + voucherID.toString(), + 'Wrong token id voucher' + ); + assert.equal( + ev._to, + users.other2.address, + 'Wrong buyer deposit recipient' + ); + assert.equal( + ev._payment, + constants.PROMISE_DEPOSITBU1, + 'Wrong buyer deposit amount' + ); + break; + } + } + ); + eventUtils.assertEventEmitted( txReceipt, Cashier_Factory, @@ -5612,21 +7373,26 @@ describe('Cashier and VoucherKernel', () => { ); //Cashier Should be Empty - assert.isTrue( - cashierPaymentLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); - assert.isTrue( - cashierDepositLeft.eq(BN(0)), - 'Cashier Contract is not empty' - ); + expect( + await utils.contractBSNTokenPrice.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); + + expect( + await utils.contractBSNTokenDeposit.balanceOf( + utils.contractCashier.address + ) + ).to.equal(constants.ZERO, 'Cashier Contract is not empty'); }); it('[NEGATIVE] Should not transfer a voucher if payments / deposits are released', async () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.refund(voucherID, users.other1.signer); @@ -5650,7 +7416,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await utils.safeTransfer721( @@ -5658,10 +7426,11 @@ describe('Cashier and VoucherKernel', () => { users.other2.address, voucherID, users.other1.signer - ), - await expect( - utils.redeem(voucherID, users.other1.signer) - ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VOUCHER_OWNER); + ); + + await expect( + utils.redeem(voucherID, users.other1.signer) + ).to.be.revertedWith(revertReasons.UNAUTHORIZED_VOUCHER_OWNER); await expect( utils.refund(voucherID, users.other1.signer) @@ -5672,7 +7441,9 @@ describe('Cashier and VoucherKernel', () => { const voucherID = await utils.commitToBuy( users.other1, users.seller, - tokenSupplyKey + tokenSupplyKey, + constants.PROMISE_PRICE1, + constants.PROMISE_DEPOSITBU1 ); await expect( diff --git a/test/3_withdrawals.ts b/test/3_withdrawals.ts index cf78aa8e..dc8626fe 100644 --- a/test/3_withdrawals.ts +++ b/test/3_withdrawals.ts @@ -258,7 +258,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_15 ); }); @@ -273,7 +275,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -326,7 +330,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -378,7 +384,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -432,7 +440,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -485,7 +495,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -538,7 +550,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -591,7 +605,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -642,7 +658,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -698,7 +716,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -755,7 +775,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -811,7 +833,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -935,7 +959,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, supplyQty ); }); @@ -966,7 +992,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1046,7 +1074,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -1125,7 +1155,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1204,7 +1236,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1284,7 +1318,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1359,7 +1395,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -1437,7 +1475,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -1512,7 +1552,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -1589,7 +1631,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -1670,7 +1714,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -1751,7 +1797,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -1884,7 +1932,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, supplyQty ); }); @@ -1908,7 +1958,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -1970,7 +2022,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -2032,7 +2086,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2094,7 +2150,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2157,7 +2215,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2215,7 +2275,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -2276,7 +2338,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -2333,7 +2397,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -2393,7 +2459,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -2457,7 +2525,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -2521,7 +2591,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -2634,7 +2706,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, supplyQty ); }); @@ -2660,7 +2734,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2735,7 +2811,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -2809,7 +2887,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2883,7 +2963,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -2958,7 +3040,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -3028,7 +3112,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -3101,7 +3187,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -3171,7 +3259,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -3243,7 +3333,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -3319,7 +3411,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -3395,7 +3489,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -3512,7 +3608,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); }); @@ -3521,7 +3619,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -3617,7 +3717,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -3712,7 +3814,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -3807,7 +3911,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -3903,7 +4009,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -3994,7 +4102,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.cancel(voucherID, users.seller.signer); @@ -4088,7 +4198,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -4179,7 +4291,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -4272,7 +4386,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.complain(voucherID, users.buyer.signer); @@ -4369,7 +4485,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); await utils.cancel(voucherID, users.seller.signer); @@ -4466,7 +4584,9 @@ describe('Cashier withdrawals ', () => { const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(voucherID, users.buyer.signer); @@ -4598,12 +4718,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < voucherToBuyBeforeBurn; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); remQty--; } }); @@ -4674,7 +4802,13 @@ describe('Cashier withdrawals ', () => { it('[NEGATIVE] Buyer should not be able to commit to buy anything from the burnt supply', async () => { await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.OFFER_EMPTY); }); @@ -4741,12 +4875,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < voucherToBuyBeforeBurn; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); remQty--; } }); @@ -4835,7 +4977,13 @@ describe('Cashier withdrawals ', () => { it('[NEGATIVE] Buyer should not be able to commit to buy anything from the burnt supply', async () => { await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.OFFER_EMPTY); }); @@ -4900,12 +5048,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < voucherToBuyBeforeBurn; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); remQty--; } }); @@ -4994,7 +5150,13 @@ describe('Cashier withdrawals ', () => { it('[NEGATIVE] Buyer should not be able to commit to buy anything from the burnt supply', async () => { await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.OFFER_EMPTY); }); @@ -5052,12 +5214,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < voucherToBuyBeforeBurn; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); remQty--; } }); @@ -5131,7 +5301,13 @@ describe('Cashier withdrawals ', () => { it('[NEGATIVE] Buyer should not be able to commit to buy anything from the burnt supply', async () => { await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.OFFER_EMPTY); }); @@ -5181,7 +5357,9 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -5221,12 +5399,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); } await contractBosonRouter.pause(); @@ -5388,12 +5574,20 @@ describe('Cashier withdrawals ', () => { users.seller, constants.PROMISE_VALID_FROM, constants.PROMISE_VALID_TO, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); for (let i = 0; i < vouchersToBuy; i++) { - await utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID); + await utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ); } await contractBosonRouter.pause(); diff --git a/test/4_pausing_contracts.ts b/test/4_pausing_contracts.ts index abae989c..621c3f45 100644 --- a/test/4_pausing_contracts.ts +++ b/test/4_pausing_contracts.ts @@ -226,7 +226,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ) ).to.be.revertedWith(revertReasons.PAUSED); @@ -237,7 +239,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); @@ -249,14 +253,22 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); await contractBosonRouter.pause(); await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.PAUSED); }); }); @@ -302,7 +314,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ) ).to.be.revertedWith(revertReasons.PAUSED); @@ -313,7 +327,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); @@ -325,14 +341,22 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); await contractBosonRouter.pause(); await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.PAUSED); }); }); @@ -368,7 +392,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -381,7 +407,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ) ).to.be.revertedWith(revertReasons.PAUSED); @@ -392,7 +420,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); @@ -404,14 +434,22 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); await contractBosonRouter.pause(); await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.PAUSED); }); }); @@ -463,7 +501,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ) ).to.be.revertedWith(revertReasons.PAUSED); @@ -474,7 +514,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); @@ -486,14 +528,22 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); await contractBosonRouter.pause(); await expect( - utils.commitToBuy(users.buyer, users.seller, TOKEN_SUPPLY_ID) + utils.commitToBuy( + users.buyer, + users.seller, + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit + ) ).to.be.revertedWith(revertReasons.PAUSED); }); }); @@ -558,7 +608,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -567,7 +619,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -581,7 +635,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(VOUCHER_ID, users.buyer.signer); @@ -597,7 +653,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -611,7 +669,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(VOUCHER_ID, users.buyer.signer); @@ -660,7 +720,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -669,7 +731,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -683,7 +747,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(VOUCHER_ID, users.buyer.signer); @@ -699,7 +765,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -713,7 +781,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(VOUCHER_ID, users.buyer.signer); @@ -756,7 +826,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -765,7 +837,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -779,7 +853,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(VOUCHER_ID, users.buyer.signer); @@ -795,7 +871,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -809,7 +887,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(VOUCHER_ID, users.buyer.signer); @@ -862,7 +942,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -871,7 +953,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -885,7 +969,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(VOUCHER_ID, users.buyer.signer); @@ -901,7 +987,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -915,7 +1003,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(VOUCHER_ID, users.buyer.signer); @@ -963,7 +1053,9 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -972,7 +1064,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -986,7 +1080,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(VOUCHER_ID, users.buyer.signer); @@ -1002,7 +1098,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractBosonRouter.pause(); @@ -1016,7 +1114,9 @@ describe('Cashier && VK', () => { VOUCHER_ID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.redeem(VOUCHER_ID, users.buyer.signer); @@ -1108,14 +1208,18 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1168,14 +1272,18 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1221,14 +1329,18 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); @@ -1287,14 +1399,18 @@ describe('Cashier && VK', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_1 ); const voucherID = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await utils.refund(voucherID, users.buyer.signer); diff --git a/test/6_erc1155721.ts b/test/6_erc1155721.ts index ef97f959..1a704bdd 100644 --- a/test/6_erc1155721.ts +++ b/test/6_erc1155721.ts @@ -213,13 +213,17 @@ describe('ERC1155ERC721', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10, true ); + const txReceipt = await txFillOrder.wait(); + eventUtils.assertEventEmitted( - txFillOrder, + txReceipt, ERC1155ERC721_Factory, eventNames.TRANSFER_SINGLE, (ev) => { @@ -249,11 +253,15 @@ describe('ERC1155ERC721', () => { users.buyer, users.seller, TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit, true ); + const txReceipt = await commitTx.wait(); + eventUtils.assertEventEmitted( - commitTx, + txReceipt, ERC1155ERC721_Factory, eventNames.TRANSFER_SINGLE, (ev) => { @@ -274,7 +282,7 @@ describe('ERC1155ERC721', () => { ); eventUtils.assertEventEmitted( - commitTx, + txReceipt, ERC1155ERC721_Factory, eventNames.TRANSFER, (ev) => { @@ -293,7 +301,9 @@ describe('ERC1155ERC721', () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); const owner721Instance = contractERC1155ERC721.connect( @@ -333,7 +343,9 @@ describe('ERC1155ERC721', () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); const attackerInstance = contractERC1155ERC721.connect( @@ -349,7 +361,9 @@ describe('ERC1155ERC721', () => { const token721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); const attackerInstance = contractERC1155ERC721.connect( @@ -381,7 +395,9 @@ describe('ERC1155ERC721', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -499,7 +515,9 @@ describe('ERC1155ERC721', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); }); @@ -526,7 +544,9 @@ describe('ERC1155ERC721', () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await expect( @@ -543,7 +563,9 @@ describe('ERC1155ERC721', () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await expect( @@ -560,7 +582,9 @@ describe('ERC1155ERC721', () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await expect( @@ -577,7 +601,9 @@ describe('ERC1155ERC721', () => { const erc721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await expect( @@ -617,14 +643,18 @@ describe('ERC1155ERC721', () => { users.seller, timestamp, timestamp + constants.SECONDS_IN_DAY, + constants.product_price, constants.seller_deposit, + constants.buyer_deposit, constants.QTY_10 ); erc721 = await utils.commitToBuy( users.buyer, users.seller, - TOKEN_SUPPLY_ID + TOKEN_SUPPLY_ID, + constants.product_price, + constants.buyer_deposit ); await contractERC1155ERC721._setMetadataBase(metadataBase); diff --git a/testHelpers/constants.ts b/testHelpers/constants.ts index bbc1e87f..54e04947 100644 --- a/testHelpers/constants.ts +++ b/testHelpers/constants.ts @@ -32,7 +32,7 @@ let PROMISE_VALID_FROM: number; // evaluated based on the current block timestam let PROMISE_VALID_TO: number; // evaluated based on the PROMISE_VALID_FROM + 2 * SECONDS_IN_DAY const PROMISE_PRICE1 = 10; const PROMISE_PRICE2 = 21; -const PROMISE_DEPOSITSE1 = 1; +const PROMISE_DEPOSITSE1 = 1000000; const PROMISE_DEPOSITSE2 = 5; const PROMISE_DEPOSITBU1 = 1; const PROMISE_DEPOSITBU2 = 2; diff --git a/testHelpers/utils.ts b/testHelpers/utils.ts index e9e52779..197f997c 100644 --- a/testHelpers/utils.ts +++ b/testHelpers/utils.ts @@ -3,7 +3,6 @@ import {ecsign} from 'ethereumjs-util'; import {BigNumber, Contract, ContractTransaction, Signer} from 'ethers'; import {Account, DistributionAmounts, DistributionEvent} from './types'; -import constants from './constants'; import * as events from './events'; import fnSignatures from './functionSignatures'; import {toWei, getApprovalDigest} from '../testHelpers/permitUtils'; @@ -28,18 +27,36 @@ import { } from '../typechain'; class Utils { - createOrder: (seller, from, to, sellerDeposit, qty, returnTx?) => any; + createOrder: ( + seller, + from, + to, + promisePrice, + sellerDeposit, + buyerDeposit, + qty, + returnTx? + ) => any; createOrderConditional: ( seller, from, to, + promisePrice, sellerDeposit, + buyerDeposit, qty, gateContract, nftTokenID, returnTx? ) => any; - commitToBuy: (buyer, seller, tokenSupplyId, returnTx?) => any; + commitToBuy: ( + buyer, + seller, + tokenSupplyId, + promisePrice, + buyerDeposit, + returnTx? + ) => any; factories?: { ERC1155ERC721: ERC1155ERC721__factory | any; VoucherKernel: VoucherKernel__factory | any; @@ -82,7 +99,9 @@ class Utils { seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, + buyerDeposit: number | string, qty: number | string, returnTx = false ): Promise { @@ -92,19 +111,15 @@ class Utils { seller.signer ) as BosonRouter; const txOrder = await sellerInstance.requestCreateOrderETHETH( - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], { value: txValue, } ); + if (returnTx) return txOrder; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await txOrder.wait(); let eventArgs; @@ -115,14 +130,16 @@ class Utils { (e) => (eventArgs = e) ); - return returnTx ? txReceipt : eventArgs._tokenIdSupply.toString(); + return eventArgs._tokenIdSupply.toString(); } async requestCreateOrderETHTKNSameWithPermit( seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, + buyerDeposit: number | string, qty: number | string, returnTx = false ): Promise { @@ -156,19 +173,15 @@ class Utils { v, r, s, - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], { from: seller.address, } ); + if (returnTx) return txOrder; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await txOrder.wait(); let eventArgs; @@ -179,16 +192,19 @@ class Utils { (e) => (eventArgs = e) ); - return returnTx ? txReceipt : eventArgs._tokenIdSupply.toString(); + return eventArgs._tokenIdSupply.toString(); } async requestCreateOrderTKNTKNWithPermit( seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, - qty: number | string - ): Promise { + buyerDeposit: number | string, + qty: number | string, + returnTx = false + ): Promise { const txValue = BN(sellerDeposit).mul(BN(qty)); const nonce = await this.contractBSNTokenDeposit.nonces(seller.address); @@ -218,19 +234,15 @@ class Utils { v, r, s, - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], { from: seller.address, } ); + if (returnTx) return txOrder; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await txOrder.wait(); let eventArgs; @@ -248,11 +260,14 @@ class Utils { seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, + buyerDeposit: number | string, qty: number | string, gateContract: Account, - nftTokenId: number | string | null - ): Promise { + nftTokenId: number | string | null, + returnTx = false + ): Promise { const txValue = BN(sellerDeposit).mul(BN(qty)); const nonce = await this.contractBSNTokenDeposit.nonces(seller.address); @@ -274,8 +289,7 @@ class Utils { const sellerInstance = this.contractBSNRouter.connect( seller.signer ) as BosonRouter; - return sellerInstance.requestCreateOrderTKNTKNWithPermitConditional( - // const txOrder = await sellerInstance.requestCreateOrderTKNTKNWithPermitConditional( + const txOrder = await sellerInstance.requestCreateOrderTKNTKNWithPermitConditional( this.contractBSNTokenPrice.address, this.contractBSNTokenDeposit.address, txValue, @@ -283,14 +297,7 @@ class Utils { v, r, s, - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], gateContract.address, nftTokenId || '0', { @@ -298,24 +305,28 @@ class Utils { } ); - // const txReceipt = await txOrder.wait(); - // let eventArgs; + if (returnTx) return txOrder; - // events.assertEventEmitted( - // txReceipt, - // this.factories.BosonRouter, - // events.eventNames.LOG_ORDER_CREATED, - // (e) => (eventArgs = e) - // ); + const txReceipt = await txOrder.wait(); + let eventArgs; - // return eventArgs._tokenIdSupply.toString(); + events.assertEventEmitted( + txReceipt, + this.factories.BosonRouter, + events.eventNames.LOG_ORDER_CREATED, + (e) => (eventArgs = e) + ); + + return eventArgs._tokenIdSupply.toString(); } async requestCreateOrderETHTKNWithPermit( seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, + buyerDeposit: number | string, qty: number | string, returnTx = false ): Promise { @@ -346,19 +357,15 @@ class Utils { v, r, s, - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], { from: seller.address, } ); + if (returnTx) return txOrder; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await txOrder.wait(); let eventArgs; @@ -369,14 +376,16 @@ class Utils { (e) => (eventArgs = e) ); - return returnTx ? txReceipt : eventArgs._tokenIdSupply.toString(); + return eventArgs._tokenIdSupply.toString(); } async requestCreateOrderTKNETH( seller: Account, from: number, to: number, + promisePrice: number | string | BigNumber, sellerDeposit: number | string, + buyerDeposit: number | string, qty: number | string, returnTx = false ): Promise { @@ -387,19 +396,15 @@ class Utils { ) as BosonRouter; const txOrder = await sellerInstance.requestCreateOrderTKNETH( this.contractBSNTokenPrice.address, - [ - from, - to, - constants.product_price, - sellerDeposit, - constants.buyer_deposit, - qty, - ], + [from, to, promisePrice, sellerDeposit, buyerDeposit, qty], { value: txValue, } ); + if (returnTx) return txOrder; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await txOrder.wait(); let eventArgs; @@ -410,24 +415,25 @@ class Utils { (e) => (eventArgs = e) ); - return returnTx ? txReceipt : eventArgs._tokenIdSupply.toString(); + return eventArgs._tokenIdSupply.toString(); } async commitToBuyTKNTKNWithPermit( buyer: Account, seller: Account, - tokenSupplyId: string - ): Promise { - const txValue = BN(constants.buyer_deposit).add( - BN(constants.product_price) - ); + tokenSupplyId: string, + promisePrice: number | string | BigNumber, + buyerDeposit: number | string, + returnTx = false + ): Promise { + const txValue = BN(buyerDeposit).add(BN(promisePrice)); const nonce1 = await this.contractBSNTokenDeposit.nonces(buyer.address); const digestDeposit = await getApprovalDigest( this.contractBSNTokenDeposit, buyer.address, this.contractBSNRouter.address, - constants.buyer_deposit, + buyerDeposit, nonce1, this.deadline ); @@ -447,7 +453,7 @@ class Utils { this.contractBSNTokenPrice, buyer.address, this.contractBSNRouter.address, - constants.product_price, + promisePrice, nonce2, this.deadline ); @@ -477,6 +483,9 @@ class Utils { sDeposit ); + if (returnTx) return commitTx; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await commitTx.wait(); let eventArgs; @@ -493,11 +502,12 @@ class Utils { async commitToBuyTKNTKNSameWithPermit( buyer: Account, seller: Account, - tokenSupplyId: string - ): Promise { - const txValue = BN(constants.buyer_deposit).add( - BN(constants.product_price) - ); + tokenSupplyId: string, + promisePrice: number | string | BigNumber, + buyerDeposit: number | string, + returnTx = false + ): Promise { + const txValue = BN(buyerDeposit).add(BN(promisePrice)); const nonce = await this.contractBSNTokenSame.nonces(buyer.address); const digestTxValue = await getApprovalDigest( @@ -531,6 +541,9 @@ class Utils { s ); + if (returnTx) return commitTx; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await commitTx.wait(); let eventArgs; @@ -547,15 +560,18 @@ class Utils { async commitToBuyETHTKNWithPermit( buyer: Account, seller: Account, - tokenSupplyId: string - ): Promise { + tokenSupplyId: string, + promisePrice: number | string | BigNumber, + buyerDeposit: number | string, + returnTx = false + ): Promise { const nonce1 = await this.contractBSNTokenDeposit.nonces(buyer.address); const digestDeposit = await getApprovalDigest( this.contractBSNTokenDeposit, buyer.address, this.contractBSNRouter.address, - constants.buyer_deposit, + buyerDeposit, nonce1, this.deadline ); @@ -568,18 +584,21 @@ class Utils { const buyerInstance = this.contractBSNRouter.connect( buyer.signer ) as BosonRouter; - const txOrder = await buyerInstance.requestVoucherETHTKNWithPermit( + const commitTx = await buyerInstance.requestVoucherETHTKNWithPermit( tokenSupplyId, seller.address, - constants.buyer_deposit, + buyerDeposit, this.deadline, v, r, s, - {value: constants.product_price.toString()} + {value: promisePrice.toString()} ); - const txReceipt = await txOrder.wait(); + if (returnTx) return commitTx; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. + const txReceipt = await commitTx.wait(); let eventArgs; events.assertEventEmitted( @@ -596,11 +615,11 @@ class Utils { buyer: Account, seller: Account, tokenSupplyId: string, + promisePrice: number | string | BigNumber, + buyerDeposit: number | string, returnTx = false ): Promise { - const txValue = BN(constants.buyer_deposit).add( - BN(constants.product_price) - ); + const txValue = BN(buyerDeposit).add(BN(promisePrice)); // TODO MAKE PARAMETERS const buyerInstance = this.contractBSNRouter.connect( buyer.signer @@ -613,6 +632,9 @@ class Utils { } ); + if (returnTx) return commitTx; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. const txReceipt = await commitTx.wait(); let eventArgs; @@ -623,21 +645,24 @@ class Utils { (e) => (eventArgs = e) ); - return returnTx ? txReceipt : eventArgs._tokenIdVoucher; + return eventArgs._tokenIdVoucher; } async commitToBuyTKNETHWithPermit( buyer: Account, seller: Account, - tokenSupplyId: string - ): Promise { + tokenSupplyId: string, + promisePrice: number | string | BigNumber, + buyerDeposit: number | string, + returnTx = false + ): Promise { const nonce1 = await this.contractBSNTokenPrice.nonces(buyer.address); const digestDeposit = await getApprovalDigest( this.contractBSNTokenPrice, buyer.address, this.contractBSNRouter.address, - constants.product_price, + promisePrice, nonce1, this.deadline ); @@ -650,18 +675,21 @@ class Utils { const buyerInstance = this.contractBSNRouter.connect( buyer.signer ) as BosonRouter; - const txOrder = await buyerInstance.requestVoucherTKNETHWithPermit( + const commitTx = await buyerInstance.requestVoucherTKNETHWithPermit( tokenSupplyId, seller.address, - constants.product_price, + promisePrice, this.deadline, v, r, s, - {value: constants.buyer_deposit} + {value: buyerDeposit} ); - const txReceipt = await txOrder.wait(); + if (returnTx) return commitTx; + + // only needed when needed to get _tokenIdSupply. Not really checking anything. + const txReceipt = await commitTx.wait(); let eventArgs; events.assertEventEmitted( @@ -719,10 +747,6 @@ class Utils { const deployerInstance = this.contractCashier.connect(deployer) as Cashier; const tx = await deployerInstance.withdraw(voucherID); - const receipt = await tx.wait(); - - console.log('GAS USED: ', receipt.gasUsed.toString()); - return tx; }