-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed tests MarketJob 🎉 🎉 && fixed withdraw using transfer instead of…
… send
- Loading branch information
Showing
3 changed files
with
154 additions
and
45 deletions.
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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ pragma solidity ^0.4.18; | |
|
||
import './MarketJob.sol'; | ||
|
||
|
||
contract MarketJobFactory { | ||
|
||
function create( | ||
|
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 |
---|---|---|
@@ -1,81 +1,191 @@ | ||
const Market = artifacts.require('market/MarketJob.sol') | ||
const AGIToken = artifacts.require('tokens/SingularityNetTokenMock.sol') | ||
|
||
contract('Market Job', function ([payer,firstAgent,secondAgent,thirdAgent]) { | ||
|
||
let marketJob | ||
let token | ||
const amounts = [30, 40, 30] | ||
|
||
beforeEach(async () => { | ||
token = await AGIToken.new(payer,100) | ||
marketJob = await Market.new( | ||
contract('Market Job', function ([payer, firstAgent, secondAgent, thirdAgent]) { | ||
|
||
const amounts = [new web3.BigNumber(300), new web3.BigNumber(300), new web3.BigNumber(400)] | ||
|
||
beforeEach(async () => { | ||
|
||
}) | ||
|
||
|
||
|
||
it('DEPOSIT # only the payer can deposit AGI token', async () => { | ||
|
||
const token = await AGIToken.new(payer, 1000) | ||
const marketJob = await Market.new( | ||
[firstAgent, secondAgent, thirdAgent], // agents | ||
amounts, //amounts | ||
[101, 102, 103], // services id | ||
token.address, //token address | ||
payer, // payer address | ||
"0x0" // first bytes packet | ||
) | ||
}) | ||
|
||
|
||
"0x0", // first bytes packet | ||
{ | ||
from: firstAgent | ||
} | ||
|
||
it('only the payer can deposit AGI token', async () => { | ||
// console.log(await token.balanceOf(payer)) | ||
const amount = new web3.BigNumber(1000) | ||
|
||
const watch = token.Approval() | ||
|
||
) | ||
const escrow = marketJob.address | ||
// console.log(await token.balanceOf(payer)) | ||
const amount = new web3.BigNumber(1000) | ||
const watch = token.Approval() | ||
//APPROVE | ||
await token.approve(escrow, amount, { from: payer }) | ||
const allowance = await token.allowance.call(payer, escrow) | ||
|
||
await token.approve(marketJob.address, amount, {from: payer}) | ||
const allowance = await token.allowance.call(marketJob.address,payer) | ||
|
||
|
||
assert.equal(watch.get()[0].args.owner, payer) | ||
assert.equal(watch.get()[0].args.spender, marketJob.address) | ||
assert.equal(watch.get()[0].args.spender, escrow) | ||
|
||
//console.log(allowance.toNumber()) | ||
|
||
await marketJob.deposit(amount) | ||
//DEPOSIT | ||
const result = await marketJob.deposit(amount, { from: payer }) | ||
|
||
assert.strictEqual( | ||
(await token.balanceOf.call(marketJob.address)).toNumber(), | ||
(await token.balanceOf.call(escrow)).toNumber(), | ||
amount.toNumber() | ||
); | ||
|
||
assert.equal(result.logs[0].event, 'Deposited', 'Amount was not deposited') | ||
assert.equal(result.logs[0].event, 'Deposited', 'Amount was not deposited') | ||
}) | ||
|
||
it('COMPLETION # only the the master agent can set the job as completed and trigger an event', async () => { | ||
it('COMPLETION # only the the master agent can set the job as completed', async () => { | ||
const hash = '0x01' | ||
|
||
const result = await marketJob.setJobCompleted(hash) | ||
const token = await AGIToken.new(payer, 1000) | ||
const marketJob = await Market.new( | ||
[firstAgent, secondAgent, thirdAgent], // agents | ||
amounts, //amounts | ||
[101, 102, 103], // services id | ||
token.address, //token address | ||
payer, // payer address | ||
"0x0", // first bytes packet | ||
{ | ||
from: firstAgent | ||
} | ||
) | ||
const escrow = marketJob.address | ||
const amount = new web3.BigNumber(1000) | ||
await token.approve(escrow, amount, { from: payer }) | ||
await marketJob.deposit(amount, { from: payer }) | ||
// balance after deposit === 0 | ||
assert.equal((await token.balanceOf(escrow)).toNumber(), amount, 'Contract Not full empty') | ||
|
||
const result = await marketJob.setJobCompleted(hash, { from: firstAgent }) | ||
const jobResult = await marketJob.jobResult.call() | ||
|
||
assert.equal(jobResult, hash, 'Inserted jobResult is different than actual inserted') | ||
assert.equal(result.logs[0].event, 'JobCompleted', 'Job was not completed') | ||
}) | ||
|
||
it('APPROVAL # only the the payer can set the job as approved ', async () => { | ||
|
||
|
||
const hash = '0x01' | ||
|
||
const token = await AGIToken.new(payer, 1000) | ||
const marketJob = await Market.new( | ||
[firstAgent, secondAgent, thirdAgent], // agents | ||
amounts, //amounts | ||
[101, 102, 103], // services id | ||
token.address, //token address | ||
payer, // payer address | ||
"0x0", // first bytes packet | ||
{ | ||
from: firstAgent | ||
} | ||
) | ||
const escrow = marketJob.address | ||
const amount = new web3.BigNumber(1000) | ||
await token.approve(escrow, amount, { from: payer }) | ||
await marketJob.deposit(amount, { from: payer }) | ||
// balance after deposit === 0 | ||
assert.equal((await token.balanceOf(escrow)).toNumber(), amount, 'Contract Not full empty') | ||
|
||
await marketJob.setJobCompleted(hash, { from: firstAgent }) | ||
const result = await marketJob.setJobAccepted() | ||
const jobAccepted = await marketJob.jobAccepted.call() | ||
|
||
assert.equal(jobAccepted, true,'the job state is euqal to approved') | ||
assert.equal(result.logs[0].event, 'JobApproved', 'Job was not approved') | ||
assert.equal(jobAccepted, true, 'the job state is euqal to approved') | ||
assert.equal(result.logs[0].event, 'JobAccepted', 'Job was not approved') | ||
}) | ||
|
||
it('WITHDRAW # only allowed agents can request a withdrawal', async () => { | ||
|
||
await marketJob.setJobCompleted("0x0") | ||
await marketJob.setJobAccepted() | ||
|
||
const result = await marketJob.withdraw() | ||
console.log(result) | ||
console.log(await token.balanceOf(firstAgent)) | ||
const hash = '0x01' | ||
|
||
const token = await AGIToken.new(payer, new web3.BigNumber(1000)) | ||
const marketJob = await Market.new( | ||
[firstAgent, secondAgent, thirdAgent], // agents | ||
amounts, //amounts | ||
[101, 102, 103], // services id | ||
token.address, //token address | ||
payer, // payer address | ||
"0x0", // first bytes packet | ||
{ | ||
from: firstAgent | ||
} | ||
) | ||
const escrow = marketJob.address | ||
|
||
const amount = new web3.BigNumber(1000) | ||
|
||
await token.approve(escrow, amount, { from: payer }) | ||
await marketJob.deposit(amount, { from: payer }) | ||
// balance after deposit === 0 | ||
assert.equal((await token.balanceOf(escrow)).toNumber(), amount, 'Contract Not full empty') | ||
await marketJob.setJobCompleted(hash, { from: firstAgent }) | ||
await marketJob.setJobAccepted({ from: payer }) | ||
|
||
|
||
/** | ||
* third agent | ||
*/ | ||
const resultThird = await marketJob.withdraw({ from: thirdAgent }) | ||
|
||
assert.strictEqual( | ||
(await token.balanceOf.call(thirdAgent)).toNumber(), | ||
amounts[2].toNumber() | ||
); | ||
|
||
assert.equal(resultThird.logs[0].event, 'Withdrew', 'Withdrawal in favor of ' + thirdAgent + ' was not approved') | ||
|
||
/** | ||
* second agent | ||
*/ | ||
const resultSecond = await marketJob.withdraw({ from: secondAgent }) | ||
|
||
assert.strictEqual( | ||
(await token.balanceOf.call(secondAgent)).toNumber(), | ||
amounts[1].toNumber() | ||
); | ||
|
||
assert.equal(resultSecond.logs[0].event, 'Withdrew', 'Withdrawal in favor of ' + secondAgent + ' was not approved') | ||
|
||
/** | ||
* first agent | ||
*/ | ||
|
||
const resultFirst = await marketJob.withdraw({ from: firstAgent }) | ||
|
||
assert.strictEqual( | ||
(await token.balanceOf.call(firstAgent)).toNumber(), | ||
amounts[1].toNumber() | ||
); | ||
|
||
assert.equal(resultFirst.logs[0].event, 'Withdrew', 'Withdrawal in favor of ' + firstAgent + ' was not approved') | ||
|
||
//Final balance === 0 | ||
|
||
assert.equal((await token.balanceOf(escrow)).toNumber(), 0, 'Contract Not full empty') | ||
|
||
try { | ||
const tryMore = await marketJob.withdraw({ from: firstAgent }) | ||
assert.fail('should have thrown before') | ||
} catch (error) { | ||
assert.isAbove(error.message.search('invalid opcode'), -1, 'Invalid opcode error must be returned'); | ||
} | ||
|
||
|
||
|
||
assert.equal(result.logs[0].event, 'Withdrew', 'Withdrawal was not approved') | ||
}) | ||
|
||
}) |