-
Notifications
You must be signed in to change notification settings - Fork 68
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
mariacarmina
wants to merge
23
commits into
release_4.0
Choose a base branch
from
escrow-contract-integration
base: release_4.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e9a663f
Created Escrow contract class.
mariacarmina c07cd43
Fix workflow.
mariacarmina 4b1c8b0
Added test with deposit and withdrw funds.
mariacarmina f755676
Use barge with escrow contracts.
mariacarmina 1b0eee1
use barge main branch.
mariacarmina 433a3de
Add docker logs.
mariacarmina 6882b67
Update contracts in ci.
mariacarmina 7822f64
Fix docker container.
mariacarmina 50c18a3
export contracts version for barge.
mariacarmina f63051d
Changed contracts version.
mariacarmina 0c9ec8c
debug.
mariacarmina 49327bc
Add more logs.
mariacarmina 5cd36a9
Fix spender.
mariacarmina b579ecd
Fix assert.
mariacarmina e49c6e4
Debug.
mariacarmina 06329f8
debug.
mariacarmina 8dfd0d6
JSON parse.
mariacarmina ca041b2
Fix extraction.
mariacarmina c75c465
Fix test.
mariacarmina 3b4cc83
Authorize test.
mariacarmina fb09c6f
fix.
mariacarmina b8e23d9
fix return type.
mariacarmina 69c7780
Merge branch 'main' into escrow-contract-integration
mariacarmina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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