Skip to content

Commit

Permalink
MarketJob Factory && array of services into Job struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tiero committed Nov 18, 2017
1 parent 5658a53 commit 729a6da
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 36 deletions.
4 changes: 3 additions & 1 deletion dao/contracts/agent/Agent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ contract Agent is AgentInterface, Ownable {
bytes[] public packets;
MarketJobInterface public job;

function() payable { }
function Agent() {

}

function sendPacket(address target, bytes packet) external onlyOwner {
Agent(target).appendPacket(packet);
Expand Down
44 changes: 31 additions & 13 deletions dao/contracts/market/MarketJob.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
pragma solidity ^0.4.15;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
import 'zeppelin-solidity/contracts/math/SafeMath.sol';
import "./MarketJobInterface.sol";


contract MarketJob is MarketJobInterface, Ownable {
using SafeMath for uint256;


address public payer;
address public masterAgent;
bytes public lastPacket;
bytes public firstPacket;
bool public jobCompleted;

mapping (address => uint) public amounts;
event JobCompleted();
event Withdraw(address payee, uint256 amount);

struct Job {
uint256 amount;
uint256 idService;
}

mapping (address => Job) public amounts;

modifier jobDone {
require(jobCompleted == true);
Expand All @@ -25,30 +37,36 @@ contract MarketJob is MarketJobInterface, Ownable {

function MarketJob(
address[] _agents,
uint[] _amounts,
uint256[] _amounts,
uint256[] _services,
address _payer,
bytes _firstPacket,
bytes _lastPacket ) payable
{
bytes _firstPacket ) {
require(_agents.length == _amounts.length);
masterAgent = msg.sender;
payer = _payer;
lastPacket = _lastPacket;
firstPacket = _firstPacket;

for (uint i = 0; i < _amounts.length; i++) {
amounts[_agents[i]] = _amounts[i];
for (uint256 i = 0; i < _amounts.length; i++) {
amounts[_agents[i]] = Job(_amounts[i],_services[i]);
}
}

function deposit() payable {

}

function withdraw() external jobDone {
require(amounts[msg.sender] > 0);
uint256 amount = amounts[msg.sender];
require(amounts[msg.sender].amount > 0);
uint256 amount = amounts[msg.sender].amount;

amounts[msg.sender] = 0;
msg.sender.transfer(amount);
amounts[msg.sender].amount = 0;
Withdraw(msg.sender,amount);
//msg.sender.transfer(amount);
}

function setJobCompleted() external jobPending {
function setJobCompleted(bytes _lastPacket) onlyOwner jobPending {
jobCompleted = true;
lastPacket = _lastPacket;
JobCompleted();
}
}
18 changes: 18 additions & 0 deletions dao/contracts/market/MarketJobFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pragma solidity ^0.4.15;

import './MarketJob.sol';


contract MarketJobFactory {

function create(
address[] agents,
uint256[] amounts,
uint256[] services,
address payer,
bytes firstPacket) public returns (MarketJob)
{
return new MarketJob(agents,amounts,services,payer,firstPacket);
}

}
4 changes: 3 additions & 1 deletion dao/contracts/market/MarketJobInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ pragma solidity ^0.4.15;

contract MarketJobInterface {

event JobCompleted();
event Withdraw(address payee, uint256 amount);

function withdraw() external;
function setJobCompleted() external;

}
5 changes: 4 additions & 1 deletion dao/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const fs = require('fs')

const Escrow = artifacts.require('Escrow.sol')
const Agent = artifacts.require('agent/Agent.sol')
const MarketJob = artifacts.require('MarketJob.sol')
const MarketJob = artifacts.require('market/MarketJob.sol')
const MarketJobFactory = artifacts.require('market/MarketJobFactory.sol')
const AgentFactory = artifacts.require('agent/AgentFactory.sol')
const AgentRegistry = artifacts.require('registries/AgentRegistry.sol')
const SingularityNetToken = artifacts.require('tokens/SingularityNetToken.sol')
Expand All @@ -14,6 +15,7 @@ module.exports = function(deployer, network, accounts) {
MarketJob,
AgentFactory,
AgentRegistry,
MarketJobFactory,
SingularityNetToken
]).then(() => {
const fileName = "addresses.json"
Expand All @@ -23,6 +25,7 @@ module.exports = function(deployer, network, accounts) {
MarketJob: MarketJob.address,
AgentFactory: AgentFactory.address,
AgentRegistry: AgentRegistry.address,
MarketJobFactory: MarketJobFactory.address,
SingularityNetToken: SingularityNetToken.address
}

Expand Down
24 changes: 4 additions & 20 deletions dao/test/TestMarketJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,13 @@ contract('Market Job', function (accounts) {
it('verifies that master agent can create a Market Job', async () => {
let marketJob = await Market.new(
[accounts[0], accounts[1], accounts[2], accounts[3]], // agents
[30, 20, 30, 20], // amounts
[30, 20, 30, 20],
[1, 2, 3, 72], // amounts
accounts[4], // payer address
"0x0", "0x0101", // first and last packet)
{value: 100}
"0x0" // first and last packet)
)

assert.isNotNull(marketJob)
})

it('verifies that any agent allowed can be payed for its job', async () => {
let market = await Market.new(
[accounts[0], accounts[1], accounts[2], accounts[3]], // agents
[30, 20, 30, 20], // amounts
accounts[4], // payer address
"0x0", "0x0101", // first and last packet
{value: 100}
)

await market.setJobCompleted()

let result = await market.withdraw({from: accounts[1]})

assert.isNotNull(result)
assert.isNotNull(marketJob.firstPacket.call())
})

})
23 changes: 23 additions & 0 deletions dao/test/TestMarketJobFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Factory = artifacts.require('market/MarketJobFactory.sol')

contract('Market Job Factory', function (accounts) {
let factory

beforeEach(async () => {
factory = await Factory.new()
})
// master agent: accounts[0]
it('verifies that someone can create a market job', async () => {
const marketJob = await factory.create(
[accounts[0]], // agents
[100], // amounts
[12300], //id service
accounts[1], // payer address
"0x0" // first packet )
)

assert.isNotNull(marketJob)
})


})

0 comments on commit 729a6da

Please sign in to comment.