Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escrow contract integration #1910

Open
wants to merge 23 commits into
base: release_4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ jobs:
with:
repository: 'oceanprotocol/barge'
path: 'barge'

- name: Run Ganache with Barge
working-directory: ${{ github.workspace }}/barge
run: |
export CONTRACTS_VERSION=escrow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export CONTRACTS_VERSION=escrow
env:
CONTRACTS_VERSION: escrow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also check the conflicts

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
Expand Down Expand Up @@ -138,7 +138,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
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
165 changes: 165 additions & 0 deletions src/contracts/Escrow.ts
Original file line number Diff line number Diff line change
@@ -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<any>} Funds
*/
public async getFunds(token: string): Promise<any> {
return await this.contract.getFunds(token)
}

/**
* Get User Funds
* @return {Promise<any>} User funds
*/
public async getUserFunds(payer: string, token: string): Promise<any> {
return await this.contract.getUserFunds(payer, token)
}

/**
* Get Locks
* @return {Promise<[]>} Locks
*/
public async getLocks(token: string, payer: string, payee: string): Promise<any[]> {
return await this.contract.getLocks(token, payer, payee)
}

/**
* Get Authorizations
* @return {Promise<[]>} Authorizations
*/
public async getAuthorizations(
token: string,
payer: string,
payee: string
): Promise<any[]> {
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<ReceiptOrEstimate>} returns the transaction receipt or the estimateGas value
*/
public async deposit<G extends boolean = false>(
token: string,
amount: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const amountParsed = amountToUnits(null, null, amount, 18)
const estGas = await this.contract.estimateGas.deposit(token, amountParsed)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.getSignerAccordingSdk(),
this.config?.gasFeeMultiplier,
this.contract.deposit,
token,
amountParsed
)
return <ReceiptOrEstimate<G>>trxReceipt
}

/**
* Withdraw funds
* @param {String} token Token address
* @param {String} amount tokenURI
* @param {Boolean} estimateGas if True, return gas estimate
* @return {Promise<ReceiptOrEstimate>} returns the transaction receipt or the estimateGas value
*/
public async withdraw<G extends boolean = false>(
token: string,
amount: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const amountParsed = amountToUnits(null, null, amount, 18)
const estGas = await this.contract.estimateGas.withdraw(token, amountParsed)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.getSignerAccordingSdk(),
this.config?.gasFeeMultiplier,
this.contract.withdraw,
token,
amountParsed
)
return <ReceiptOrEstimate<G>>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<ReceiptOrEstimate>} returns the transaction receipt or the estimateGas value
*/
public async authorize<G extends boolean = false>(
token: string,
payee: string,
maxLockedAmount: string,
maxLockSeconds: string,
maxLockCounts: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
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.authorize(
token,
payee,
maxLockedAmountParsed,
maxLockSecondsParsed,
maxLockCountsParsed
)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
const trxReceipt = await sendTx(
estGas,
this.getSignerAccordingSdk(),
this.config?.gasFeeMultiplier,
this.contract.authorize,
token,
payee,
maxLockedAmountParsed,
maxLockSecondsParsed,
maxLockCountsParsed
)
return <ReceiptOrEstimate<G>>trxReceipt
}
}
63 changes: 63 additions & 0 deletions test/unit/Escrow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { assert } from 'chai'
import { provider, getAddresses } from '../config'
import { BigNumber, Signer } from 'ethers'

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
let addresses
let OCEAN

before(async () => {
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(user2, await user2.getChainId())
const initialBalance = await datatoken.balance(OCEAN, await user2.getAddress())
await datatoken.approve(OCEAN, await user2.getAddress(), '1000')
await datatoken.transfer(OCEAN, await user2.getAddress(), '1000')
assert(
(await datatoken.balance(OCEAN, await user2.getAddress())) !==
`${initialBalance + 1000}`
)
await datatoken.approve(OCEAN, addresses.Escrow, '1000')
await Escrow.deposit(OCEAN, '100')
const funds = await Escrow.getUserFunds(await user2.getAddress(), OCEAN)
const available = BigNumber.from(funds[0])
assert(available.toString() === (await 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)
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()
)
assert(auths[0][0] === (await user1.getAddress()), 'payee address not present')
})
})