From e9a663fd3be45fd3ea268ec9c072765fa403aee2 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Mon, 3 Feb 2025 22:48:45 +0200 Subject: [PATCH 01/22] Created Escrow contract class. --- package-lock.json | 14 ++-- package.json | 2 +- src/contracts/Escrow.ts | 165 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 src/contracts/Escrow.ts diff --git a/package-lock.json b/package-lock.json index d47a17ccd..50cffc911 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@oasisprotocol/sapphire-paratime": "^1.3.2", - "@oceanprotocol/contracts": "^2.2.0", + "@oceanprotocol/contracts": "^2.3.0-next.1", "cross-fetch": "^4.0.0", "crypto-js": "^4.1.1", "decimal.js": "^10.4.1", @@ -3125,9 +3125,9 @@ } }, "node_modules/@oceanprotocol/contracts": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-2.2.0.tgz", - "integrity": "sha512-QhewXdtTebycRSZEdkAdvJkSMMnGZyxldlw2eX4VOJto8wymyNdxuhjL/tiaZ5xO7SS5BqURricx9170hfh2kQ==", + "version": "2.3.0-next.1", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-2.3.0-next.1.tgz", + "integrity": "sha512-PbVKDAtY/EL7Cg7Q6zJT26umeBt3e3PPywgqc5TEsmbKWN/A61dtrdiP6tvmOKf/hZATy9YwEaJMPpuESaYZNQ==", "license": "Apache-2.0" }, "node_modules/@octokit/auth-token": { @@ -20143,9 +20143,9 @@ } }, "@oceanprotocol/contracts": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-2.2.0.tgz", - "integrity": "sha512-QhewXdtTebycRSZEdkAdvJkSMMnGZyxldlw2eX4VOJto8wymyNdxuhjL/tiaZ5xO7SS5BqURricx9170hfh2kQ==" + "version": "2.3.0-next.1", + "resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-2.3.0-next.1.tgz", + "integrity": "sha512-PbVKDAtY/EL7Cg7Q6zJT26umeBt3e3PPywgqc5TEsmbKWN/A61dtrdiP6tvmOKf/hZATy9YwEaJMPpuESaYZNQ==" }, "@octokit/auth-token": { "version": "3.0.3", diff --git a/package.json b/package.json index 1c4a66f8a..bcbf35b46 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ }, "dependencies": { "@oasisprotocol/sapphire-paratime": "^1.3.2", - "@oceanprotocol/contracts": "^2.2.0", + "@oceanprotocol/contracts": "^2.3.0-next.1", "cross-fetch": "^4.0.0", "crypto-js": "^4.1.1", "decimal.js": "^10.4.1", diff --git a/src/contracts/Escrow.ts b/src/contracts/Escrow.ts new file mode 100644 index 000000000..f4e1991c7 --- /dev/null +++ b/src/contracts/Escrow.ts @@ -0,0 +1,165 @@ +import { Signer } from 'ethers' +import Escrow from '@oceanprotocol/contracts/artifacts/contracts/escrow/Escrow.sol/Escrow.json' +import { amountToUnits, sendTx } from '../utils/ContractUtils' +import { AbiItem, ReceiptOrEstimate } from '../@types' +import { Config } from '../config' +import { SmartContractWithAddress } from './SmartContractWithAddress' + +export class EscrowContract extends SmartContractWithAddress { + public abiEnterprise: AbiItem[] + + getDefaultAbi() { + return Escrow.abi as AbiItem[] + } + + /** + * Instantiate AccessList class + * @param {string} address The contract address. + * @param {Signer} signer The signer object. + * @param {string | number} [network] Network id or name + * @param {Config} [config] The configuration object. + * @param {AbiItem[]} [abi] ABI array of the smart contract + * @param {AbiItem[]} abiEnterprise Enterprise ABI array of the smart contract + */ + constructor( + address: string, + signer: Signer, + network?: string | number, + config?: Config, + abi?: AbiItem[] + ) { + super(address, signer, network, config, abi) + this.abi = abi || this.getDefaultAbi() + } + + /** + * Get Funds + * @return {Promise} Funds + */ + public async getFunds(token: string): Promise { + return await this.contract.getFunds(token) + } + + /** + * Get User Funds + * @return {Promise} User funds + */ + public async getUserFunds(payer: string, token: string): Promise { + return await this.contract.getUserFunds(payer, token) + } + + /** + * Get Locks + * @return {Promise<[]>} Locks + */ + public async getLocks(token: string, payer: string, payee: string): Promise<[]> { + return await this.contract.getLocks(token, payer, payee) + } + + /** + * Get Authorizations + * @return {Promise<[]>} Authorizations + */ + public async getAuthorizations( + token: string, + payer: string, + payee: string + ): Promise<[]> { + return await this.contract.getAuthorizations(token, payer, payee) + } + + /** + * Deposit funds + * @param {String} token Token address + * @param {String} amount tokenURI + * @param {Boolean} estimateGas if True, return gas estimate + * @return {Promise} returns the transaction receipt or the estimateGas value + */ + public async deposit( + token: string, + amount: string, + estimateGas?: G + ): Promise> { + const amountParsed = amountToUnits(null, null, amount, 18) + const estGas = await this.contract.estimateGas.deposit(token, amountParsed) + if (estimateGas) return >estGas + const trxReceipt = await sendTx( + estGas, + this.getSignerAccordingSdk(), + this.config?.gasFeeMultiplier, + this.contract.deposit, + token, + amountParsed + ) + return >trxReceipt + } + + /** + * Withdraw funds + * @param {String} token Token address + * @param {String} amount tokenURI + * @param {Boolean} estimateGas if True, return gas estimate + * @return {Promise} returns the transaction receipt or the estimateGas value + */ + public async withdraw( + token: string, + amount: string, + estimateGas?: G + ): Promise> { + const amountParsed = amountToUnits(null, null, amount, 18) + const estGas = await this.contract.estimateGas.withdraw(token, amountParsed) + if (estimateGas) return >estGas + const trxReceipt = await sendTx( + estGas, + this.getSignerAccordingSdk(), + this.config?.gasFeeMultiplier, + this.contract.withdraw, + token, + amountParsed + ) + return >trxReceipt + } + + /** + * Authorize locks + * @param {String} token Token address + * @param {String} payee, + * @param {String} maxLockedAmount, + * @param {String} maxLockSeconds, + * @param {String} maxLockCounts, + * @param {Boolean} estimateGas if True, return gas estimate + * @return {Promise} returns the transaction receipt or the estimateGas value + */ + public async authorize( + token: string, + payee: string, + maxLockedAmount: string, + maxLockSeconds: string, + maxLockCounts: string, + estimateGas?: G + ): Promise> { + const maxLockedAmountParsed = amountToUnits(null, null, maxLockedAmount, 18) + const maxLockSecondsParsed = amountToUnits(null, null, maxLockSeconds, 18) + const maxLockCountsParsed = amountToUnits(null, null, maxLockCounts, 18) + const estGas = await this.contract.estimateGas.withdraw( + token, + payee, + maxLockedAmountParsed, + maxLockSecondsParsed, + maxLockCountsParsed + ) + if (estimateGas) return >estGas + const trxReceipt = await sendTx( + estGas, + this.getSignerAccordingSdk(), + this.config?.gasFeeMultiplier, + this.contract.authorize, + token, + payee, + maxLockedAmountParsed, + maxLockSecondsParsed, + maxLockCountsParsed + ) + return >trxReceipt + } +} From c07cd43f78c6364936f77b841841f5275fa2cf62 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Tue, 4 Feb 2025 21:43:25 +0200 Subject: [PATCH 02/22] Fix workflow. --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 179f031f4..8664b8006 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -119,12 +119,12 @@ jobs: - run: npm run build:metadata - name: Delete default runner images run: | - docker image rm node:18 - docker image rm node:18-alpine - docker image rm node:20 - docker image rm debian:10 - docker image rm debian:11 - docker image rm moby/buildkit:latest + docker image rm -f node:18 + docker image rm -f node:18-alpine + docker image rm -f node:20 + docker image rm -f debian:10 + docker image rm -f debian:11 + docker image rm -f moby/buildkit:latest - name: Wait for contracts deployment and C2D cluster to be ready working-directory: ${{ github.workspace }}/barge run: | From 4b1c8b0ea244a965fdd5ae6d7483a3d8b0980256 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Wed, 19 Feb 2025 00:21:24 +0200 Subject: [PATCH 03/22] Added test with deposit and withdrw funds. --- test/unit/Escrow.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/unit/Escrow.test.ts diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts new file mode 100644 index 000000000..f99e99581 --- /dev/null +++ b/test/unit/Escrow.test.ts @@ -0,0 +1,48 @@ +import { assert } from 'chai' +import { provider, getAddresses } from '../config' +import { Signer } from 'ethers' + +import { Datatoken, amountToUnits } from '../../src/' +import { EscrowContract } from '../../src/contracts/Escrow' + +describe('Escrow payments flow', () => { + let factoryOwner: Signer + let user1: Signer + let user2: Signer + let Escrow: EscrowContract + let datatoken: Datatoken + let addresses + let OCEAN + + before(async () => { + factoryOwner = (await provider.getSigner(0)) as Signer + user1 = (await provider.getSigner(3)) as Signer + user2 = (await provider.getSigner(4)) as Signer + + addresses = await getAddresses() + OCEAN = addresses.Ocean + }) + + it('should initialize Escrow class', async () => { + Escrow = new EscrowContract(addresses.Escrow, user2, await user2.getChainId()) + assert(Escrow !== null) + }) + + it('User2 makes a deposit in Escrow', async () => { + datatoken = new Datatoken(factoryOwner, await factoryOwner.getChainId()) + await datatoken.approve(OCEAN, await user2.getAddress(), '1000') + await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') + assert((await datatoken.balance(OCEAN, await user2.getAddress())) !== '1000') + await datatoken.approve(addresses.Escrow, await user2.getAddress(), '1000') + await Escrow.deposit(OCEAN, '100') + const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) + assert(funds.available === amountToUnits(null, null, '100', 18)) + }) + + it('Withdraws funds', async () => { + const tx = await Escrow.withdraw(OCEAN, '50') + assert(tx, 'failed to withdraw half of available tokens') + const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) + assert(funds.available === amountToUnits(null, null, '50', 18)) + }) +}) From f755676fa71d6657e4293931b0e8332b7deea988 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Wed, 19 Feb 2025 19:08:00 +0200 Subject: [PATCH 04/22] Use barge with escrow contracts. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8664b8006..fb444aaf2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,7 +55,7 @@ jobs: with: repository: 'oceanprotocol/barge' path: 'barge' - + ref: escrow-contracts - name: Run Ganache with Barge working-directory: ${{ github.workspace }}/barge run: | From 1b0eee12ac5e5b1679818796c3652e849780e926 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 20 Feb 2025 14:36:40 +0200 Subject: [PATCH 05/22] use barge main branch. --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fb444aaf2..f0493d172 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,7 +55,6 @@ jobs: with: repository: 'oceanprotocol/barge' path: 'barge' - ref: escrow-contracts - name: Run Ganache with Barge working-directory: ${{ github.workspace }}/barge run: | From 433a3dec2adf2deec3bd48990b3667ae2d717e12 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 20 Feb 2025 16:20:04 +0200 Subject: [PATCH 06/22] Add docker logs. --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0493d172..b7f4bba31 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,9 @@ jobs: ls -la "$HOME/.ocean/ocean-contracts/artifacts/" - run: npm run build:metadata - run: npm run test:unit:cover + - name: docker logs for unit tests + run: docker logs ocean_aquarius_1 && docker logs ocean_provider_1 && docker logs ocean_provider2_1 && docker logs ocean_computetodata_1 && docker logs ocean-ocean-contracts-1 + if: ${{ failure() }} - uses: actions/upload-artifact@v4 with: name: coverage-unit @@ -137,7 +140,7 @@ jobs: env: PROVIDER_URL: 'http://172.15.0.4:8030' - name: docker logs - run: docker logs ocean_aquarius_1 && docker logs ocean_provider_1 && docker logs ocean_provider2_1 && docker logs ocean_computetodata_1 + run: docker logs ocean_aquarius_1 && docker logs ocean_provider_1 && docker logs ocean_provider2_1 && docker logs ocean_computetodata_1 && docker logs ocean-ocean-contracts-1 if: ${{ failure() }} - name: Upload coverage uses: actions/upload-artifact@v4 From 6882b678b55749dc9caa330e68c80cc441b54370 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 20 Feb 2025 16:21:05 +0200 Subject: [PATCH 07/22] Update contracts in ci. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b7f4bba31..18d19261c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,7 +71,7 @@ jobs: - run: npm run build:metadata - run: npm run test:unit:cover - name: docker logs for unit tests - run: docker logs ocean_aquarius_1 && docker logs ocean_provider_1 && docker logs ocean_provider2_1 && docker logs ocean_computetodata_1 && docker logs ocean-ocean-contracts-1 + run: docker logs ocean-ocean-contracts-1 if: ${{ failure() }} - uses: actions/upload-artifact@v4 with: From 7822f6490ddfa11f5e8f8f1543b2e6001969445f Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 20 Feb 2025 17:04:29 +0200 Subject: [PATCH 08/22] Fix docker container. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18d19261c..a372c6bb3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,7 +71,7 @@ jobs: - run: npm run build:metadata - run: npm run test:unit:cover - name: docker logs for unit tests - run: docker logs ocean-ocean-contracts-1 + run: docker logs ocean-contracts-1 if: ${{ failure() }} - uses: actions/upload-artifact@v4 with: From 50c18a313b6277fd91a004f7a70298fe0ab03401 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Thu, 20 Feb 2025 17:57:50 +0200 Subject: [PATCH 09/22] export contracts version for barge. --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a372c6bb3..7098c2566 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,6 +58,7 @@ jobs: - name: Run Ganache with Barge working-directory: ${{ github.workspace }}/barge run: | + export CONTRACTS_VERSION=v2.3.0-next.1 bash -x start_ocean.sh --no-aquarius --no-elasticsearch --no-provider --no-dashboard 2>&1 > start_ocean.log & - run: npm ci - name: Wait for contracts deployment From f63051dd8f678735565e9bea80a38cea4bedd72f Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 20:18:45 +0200 Subject: [PATCH 10/22] Changed contracts version. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7098c2566..292dbaaf3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: - name: Run Ganache with Barge working-directory: ${{ github.workspace }}/barge run: | - export CONTRACTS_VERSION=v2.3.0-next.1 + export CONTRACTS_VERSION=escrow bash -x start_ocean.sh --no-aquarius --no-elasticsearch --no-provider --no-dashboard 2>&1 > start_ocean.log & - run: npm ci - name: Wait for contracts deployment From 0c9ec8cec79bb3d1d512257a7b1ef1986beb6bfb Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 20:38:43 +0200 Subject: [PATCH 11/22] debug. --- .github/workflows/ci.yml | 3 --- test/unit/Escrow.test.ts | 11 +++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 292dbaaf3..6b611176f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,9 +71,6 @@ jobs: ls -la "$HOME/.ocean/ocean-contracts/artifacts/" - run: npm run build:metadata - run: npm run test:unit:cover - - name: docker logs for unit tests - run: docker logs ocean-contracts-1 - if: ${{ failure() }} - uses: actions/upload-artifact@v4 with: name: coverage-unit diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index f99e99581..893550582 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -29,9 +29,16 @@ describe('Escrow payments flow', () => { }) it('User2 makes a deposit in Escrow', async () => { - datatoken = new Datatoken(factoryOwner, await factoryOwner.getChainId()) + datatoken = new Datatoken(user2, await user2.getChainId()) + console.log( + `(await datatoken.balance(OCEAN, await user2.getAddress())): ${await datatoken.balance( + OCEAN, + await user2.getAddress() + )}` + ) await datatoken.approve(OCEAN, await user2.getAddress(), '1000') - await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') + const tx = await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') + console.log(`tx: ${tx}`) assert((await datatoken.balance(OCEAN, await user2.getAddress())) !== '1000') await datatoken.approve(addresses.Escrow, await user2.getAddress(), '1000') await Escrow.deposit(OCEAN, '100') From 49327bc9d41f14b726aa2402965224590028ac49 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 20:52:11 +0200 Subject: [PATCH 12/22] Add more logs. --- test/unit/Escrow.test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 893550582..234eb61ce 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -30,19 +30,19 @@ describe('Escrow payments flow', () => { it('User2 makes a deposit in Escrow', async () => { datatoken = new Datatoken(user2, await user2.getChainId()) - console.log( - `(await datatoken.balance(OCEAN, await user2.getAddress())): ${await datatoken.balance( - OCEAN, - await user2.getAddress() - )}` - ) + const initialBalance = await datatoken.balance(OCEAN, await user2.getAddress()) await datatoken.approve(OCEAN, await user2.getAddress(), '1000') const tx = await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') - console.log(`tx: ${tx}`) - assert((await datatoken.balance(OCEAN, await user2.getAddress())) !== '1000') + console.log(`tx: ${JSON.stringify(tx)}`) + assert( + (await datatoken.balance(OCEAN, await user2.getAddress())) !== + `${initialBalance + 1000}` + ) await datatoken.approve(addresses.Escrow, await user2.getAddress(), '1000') - await Escrow.deposit(OCEAN, '100') + const tx2 = await Escrow.deposit(OCEAN, '100') + console.log(`tx2: ${JSON.stringify(tx2)}`) const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) + console.log(`funds: ${JSON.stringify(funds)}`) assert(funds.available === amountToUnits(null, null, '100', 18)) }) From 5cd36a93e6cc4c4890853a14f1510d4602efa2b6 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 21:09:17 +0200 Subject: [PATCH 13/22] Fix spender. --- test/unit/Escrow.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 234eb61ce..2f7a083f6 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -32,13 +32,12 @@ describe('Escrow payments flow', () => { datatoken = new Datatoken(user2, await user2.getChainId()) const initialBalance = await datatoken.balance(OCEAN, await user2.getAddress()) await datatoken.approve(OCEAN, await user2.getAddress(), '1000') - const tx = await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') - console.log(`tx: ${JSON.stringify(tx)}`) + await datatoken.transfer(OCEAN, await user2.getAddress(), '1000') assert( (await datatoken.balance(OCEAN, await user2.getAddress())) !== `${initialBalance + 1000}` ) - await datatoken.approve(addresses.Escrow, await user2.getAddress(), '1000') + await datatoken.approve(OCEAN, addresses.Escrow, '1000') const tx2 = await Escrow.deposit(OCEAN, '100') console.log(`tx2: ${JSON.stringify(tx2)}`) const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) From b579ecde488fd4e6b0d49ccf1b2493de5313e2ad Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 21:28:13 +0200 Subject: [PATCH 14/22] Fix assert. --- test/unit/Escrow.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 2f7a083f6..761fa4661 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -1,6 +1,6 @@ import { assert } from 'chai' import { provider, getAddresses } from '../config' -import { Signer } from 'ethers' +import { BigNumber, Signer } from 'ethers' import { Datatoken, amountToUnits } from '../../src/' import { EscrowContract } from '../../src/contracts/Escrow' @@ -42,7 +42,8 @@ describe('Escrow payments flow', () => { console.log(`tx2: ${JSON.stringify(tx2)}`) const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) console.log(`funds: ${JSON.stringify(funds)}`) - assert(funds.available === amountToUnits(null, null, '100', 18)) + const available = BigNumber.from(funds[0].hex) + assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) it('Withdraws funds', async () => { From e49c6e43798887c1758486897388500faa79c6cb Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:28:28 +0200 Subject: [PATCH 15/22] Debug. --- test/unit/Escrow.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 761fa4661..f3a441581 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -42,7 +42,8 @@ describe('Escrow payments flow', () => { console.log(`tx2: ${JSON.stringify(tx2)}`) const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) console.log(`funds: ${JSON.stringify(funds)}`) - const available = BigNumber.from(funds[0].hex) + const available = BigNumber.from(funds[0].hex.toHexString()) + console.log(`available: ${available}`) assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) From 06329f8c50eb657de4bc575c2804cf5f0ad04282 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:30:24 +0200 Subject: [PATCH 16/22] debug. --- test/unit/Escrow.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index f3a441581..0af70ca12 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -41,8 +41,8 @@ describe('Escrow payments flow', () => { const tx2 = await Escrow.deposit(OCEAN, '100') console.log(`tx2: ${JSON.stringify(tx2)}`) const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) - console.log(`funds: ${JSON.stringify(funds)}`) - const available = BigNumber.from(funds[0].hex.toHexString()) + console.log(`funds: ${JSON.stringify(funds[0])}`) + const available = BigNumber.from(funds[0].hex) console.log(`available: ${available}`) assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) From 8dfd0d6dc01add6c5b399ceee6ecdf7780f6b5a6 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:33:22 +0200 Subject: [PATCH 17/22] JSON parse. --- test/unit/Escrow.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 0af70ca12..d91cf931d 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -38,11 +38,10 @@ describe('Escrow payments flow', () => { `${initialBalance + 1000}` ) await datatoken.approve(OCEAN, addresses.Escrow, '1000') - const tx2 = await Escrow.deposit(OCEAN, '100') - console.log(`tx2: ${JSON.stringify(tx2)}`) + await Escrow.deposit(OCEAN, '100') const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) console.log(`funds: ${JSON.stringify(funds[0])}`) - const available = BigNumber.from(funds[0].hex) + const available = BigNumber.from(JSON.parse(funds[0]).hex) console.log(`available: ${available}`) assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) From ca041b293120b8f2f205c3d41001debfe5f093e3 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:39:31 +0200 Subject: [PATCH 18/22] Fix extraction. --- test/unit/Escrow.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index d91cf931d..09fc9e002 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -40,8 +40,7 @@ describe('Escrow payments flow', () => { await datatoken.approve(OCEAN, addresses.Escrow, '1000') await Escrow.deposit(OCEAN, '100') const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) - console.log(`funds: ${JSON.stringify(funds[0])}`) - const available = BigNumber.from(JSON.parse(funds[0]).hex) + const available = BigNumber.from(funds[0]) console.log(`available: ${available}`) assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) From c75c465b914f1399c260d2caba2017e6a19557b6 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:45:11 +0200 Subject: [PATCH 19/22] Fix test. --- test/unit/Escrow.test.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 09fc9e002..bbe2d4881 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -6,8 +6,6 @@ import { Datatoken, amountToUnits } from '../../src/' import { EscrowContract } from '../../src/contracts/Escrow' describe('Escrow payments flow', () => { - let factoryOwner: Signer - let user1: Signer let user2: Signer let Escrow: EscrowContract let datatoken: Datatoken @@ -15,8 +13,6 @@ describe('Escrow payments flow', () => { let OCEAN before(async () => { - factoryOwner = (await provider.getSigner(0)) as Signer - user1 = (await provider.getSigner(3)) as Signer user2 = (await provider.getSigner(4)) as Signer addresses = await getAddresses() @@ -41,7 +37,6 @@ describe('Escrow payments flow', () => { await Escrow.deposit(OCEAN, '100') const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) const available = BigNumber.from(funds[0]) - console.log(`available: ${available}`) assert(available.toString() === (await amountToUnits(null, null, '100', 18))) }) @@ -49,6 +44,7 @@ describe('Escrow payments flow', () => { const tx = await Escrow.withdraw(OCEAN, '50') assert(tx, 'failed to withdraw half of available tokens') const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN) - assert(funds.available === amountToUnits(null, null, '50', 18)) + const available = BigNumber.from(funds[0]) + assert(available.toString() === (await amountToUnits(null, null, '50', 18))) }) }) From 3b4cc83ff4585f56cdd5fd7f79b01d435fd2fb84 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 22:53:03 +0200 Subject: [PATCH 20/22] Authorize test. --- test/unit/Escrow.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index bbe2d4881..3c32e6b53 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -6,6 +6,7 @@ import { Datatoken, amountToUnits } from '../../src/' import { EscrowContract } from '../../src/contracts/Escrow' describe('Escrow payments flow', () => { + let user1: Signer let user2: Signer let Escrow: EscrowContract let datatoken: Datatoken @@ -13,6 +14,7 @@ describe('Escrow payments flow', () => { let OCEAN before(async () => { + user1 = (await provider.getSigner(3)) as Signer user2 = (await provider.getSigner(4)) as Signer addresses = await getAddresses() @@ -47,4 +49,16 @@ describe('Escrow payments flow', () => { const available = BigNumber.from(funds[0]) assert(available.toString() === (await amountToUnits(null, null, '50', 18))) }) + + it('Authorize user1', async () => { + const tx = await Escrow.authorize(OCEAN, await user1.getAddress(), '20', '100', '3') + assert(tx, 'failed to authorize user1') + const auths = await Escrow.getAuthorizations( + OCEAN, + await user2.getAddress(), + await user1.getAddress() + ) + console.log(`auths: ${JSON.stringify(auths)}`) + // assert(auths[0] === await user1.getAddress(), ) + }) }) From fb09c6f516abf46655b2509d67faf3e421348ec2 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 23:03:08 +0200 Subject: [PATCH 21/22] fix. --- src/contracts/Escrow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contracts/Escrow.ts b/src/contracts/Escrow.ts index f4e1991c7..9379f5a29 100644 --- a/src/contracts/Escrow.ts +++ b/src/contracts/Escrow.ts @@ -141,7 +141,7 @@ export class EscrowContract extends SmartContractWithAddress { const maxLockedAmountParsed = amountToUnits(null, null, maxLockedAmount, 18) const maxLockSecondsParsed = amountToUnits(null, null, maxLockSeconds, 18) const maxLockCountsParsed = amountToUnits(null, null, maxLockCounts, 18) - const estGas = await this.contract.estimateGas.withdraw( + const estGas = await this.contract.estimateGas.authorize( token, payee, maxLockedAmountParsed, From b8e23d9307e5e1480871dbde34c24da6461bcdd3 Mon Sep 17 00:00:00 2001 From: mariacarmina Date: Fri, 21 Feb 2025 23:11:07 +0200 Subject: [PATCH 22/22] fix return type. --- src/contracts/Escrow.ts | 4 ++-- test/unit/Escrow.test.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/contracts/Escrow.ts b/src/contracts/Escrow.ts index 9379f5a29..21e838934 100644 --- a/src/contracts/Escrow.ts +++ b/src/contracts/Escrow.ts @@ -52,7 +52,7 @@ export class EscrowContract extends SmartContractWithAddress { * Get Locks * @return {Promise<[]>} Locks */ - public async getLocks(token: string, payer: string, payee: string): Promise<[]> { + public async getLocks(token: string, payer: string, payee: string): Promise { return await this.contract.getLocks(token, payer, payee) } @@ -64,7 +64,7 @@ export class EscrowContract extends SmartContractWithAddress { token: string, payer: string, payee: string - ): Promise<[]> { + ): Promise { return await this.contract.getAuthorizations(token, payer, payee) } diff --git a/test/unit/Escrow.test.ts b/test/unit/Escrow.test.ts index 3c32e6b53..654de5564 100644 --- a/test/unit/Escrow.test.ts +++ b/test/unit/Escrow.test.ts @@ -58,7 +58,6 @@ describe('Escrow payments flow', () => { await user2.getAddress(), await user1.getAddress() ) - console.log(`auths: ${JSON.stringify(auths)}`) - // assert(auths[0] === await user1.getAddress(), ) + assert(auths[0][0] === (await user1.getAddress()), 'payee address not present') }) })