Skip to content

Commit

Permalink
Fixed tests MarketJob 🎉 🎉 && fixed withdraw using transfer instead of…
Browse files Browse the repository at this point in the history
… send
  • Loading branch information
tiero committed Nov 26, 2017
1 parent b4f8759 commit fad1a3c
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 45 deletions.
4 changes: 1 addition & 3 deletions dao/contracts/market/MarketJob.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ contract MarketJob is MarketJobInterface {
}

function deposit(uint256 amount) onlyPayer jobPending public {
require(token.balanceOf(msg.sender) >= amount);
require(token.transferFrom(msg.sender, address(this), amount));
Deposited(msg.sender,amount);
}
Expand All @@ -95,10 +94,9 @@ contract MarketJob is MarketJobInterface {
address agent = msg.sender;
uint256 amount = amounts[agent].amount;
require(amount > 0);
require(this.balance >= amount);

amounts[agent].amount = 0;
assert(agent.send(amount));
token.transfer(agent,amount);
Withdrew(agent,amount);
}
}
1 change: 1 addition & 0 deletions dao/contracts/market/MarketJobFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pragma solidity ^0.4.18;

import './MarketJob.sol';


contract MarketJobFactory {

function create(
Expand Down
194 changes: 152 additions & 42 deletions dao/test/TestMarketJob.js
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')
})

})

0 comments on commit fad1a3c

Please sign in to comment.