Skip to content
This repository was archived by the owner on Jan 14, 2020. It is now read-only.

Commit 4126313

Browse files
committed
Merge branch 'develop'
2 parents 62fb82c + 715689a commit 4126313

27 files changed

+10396
-677
lines changed

packages/contracts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77

88
# dependencies
99
/node_modules
10+
11+
secret/

packages/contracts/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ If you get the following error:
3939
Error: Attempting to run transaction which calls a contract function, but recipient address 0x8cdaf0cd259887258bc23a92c0a6da92698644c0 is not a contract address
4040
```
4141

42-
Resolve this by deleting the previously built contracts:
42+
Resolve this by running all migrations again:
4343

4444
```sh
45-
rm build/contracts/*.json
45+
migrate --reset
4646
```

packages/contracts/contracts/Listing.sol

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ pragma solidity ^0.4.11;
33
/// @title Listing
44
/// @dev An indiviual Origin Listing representing an offer for booking/purchase
55

6+
import "./Purchase.sol";
7+
68
contract Listing {
79

810
/*
911
* Events
1012
*/
1113

12-
event ListingPurchased(uint _unitsToBuy, uint _value);
14+
event ListingPurchased(Purchase _purchaseContract);
1315

1416
/*
1517
* Storage
@@ -23,6 +25,7 @@ contract Listing {
2325
bytes32 public ipfsHash;
2426
uint public price;
2527
uint public unitsAvailable;
28+
Purchase[] public purchases;
2629

2730

2831
function Listing (
@@ -40,27 +43,70 @@ contract Listing {
4043
unitsAvailable = _unitsAvailable;
4144
}
4245

46+
/*
47+
* Modifiers
48+
*/
49+
50+
modifier isSeller() {
51+
require (msg.sender == owner);
52+
_;
53+
}
54+
55+
/*
56+
* Public functions
57+
*/
58+
4359

4460
/// @dev buyListing(): Buy a listing
4561
/// @param _unitsToBuy Number of units to buy
4662
function buyListing(uint _unitsToBuy)
4763
public
4864
payable
4965
{
50-
// Insure there is money to pay
51-
require (msg.value >= (price * _unitsToBuy));
52-
66+
// Ensure that this is not trying to purchase more than is available.
5367
require (_unitsToBuy <= unitsAvailable);
5468

69+
// Create purchase contract
70+
Purchase purchaseContract = new Purchase(this, msg.sender);
71+
5572
// Count units as sold
5673
unitsAvailable -= _unitsToBuy;
5774

58-
// Send funds to lister
59-
// TODO: In future there will likely be some sort of escrow
60-
owner.transfer(msg.value);
75+
purchases.push(purchaseContract);
76+
77+
// TODO STAN: How to call function *AND* transfer value??
78+
purchaseContract.pay.value(msg.value)();
6179

62-
// TODO: this needs to call logging back on the registry
63-
ListingPurchased(_unitsToBuy, msg.value);
80+
ListingPurchased(purchaseContract);
81+
}
82+
83+
/// @dev close(): Allows a seller to close the listing from further purchases
84+
function close()
85+
public
86+
isSeller
87+
{
88+
unitsAvailable = 0;
89+
}
90+
91+
/// @dev purchasesLength(): Return number of listings
92+
function purchasesLength()
93+
public
94+
constant
95+
returns (uint)
96+
{
97+
return purchases.length;
98+
}
99+
100+
/// @dev getPurchase(): Return listing info for given listing
101+
/// @param _index the index of the listing we want info about
102+
function getPurchase(uint _index)
103+
public
104+
constant
105+
returns (Purchase)
106+
{
107+
return (
108+
purchases[_index]
109+
);
64110
}
65111

66112
}

packages/contracts/contracts/ListingsRegistry.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contract ListingsRegistry {
1919
*/
2020

2121
// Contract owner
22-
address public owner_address;
22+
address public owner;
2323

2424
// Array of all listings
2525
Listing[] public listings;
@@ -35,7 +35,7 @@ contract ListingsRegistry {
3535

3636

3737
modifier isOwner() {
38-
require (msg.sender == owner_address);
38+
require (msg.sender == owner);
3939
_;
4040
}
4141

@@ -48,10 +48,11 @@ contract ListingsRegistry {
4848
public
4949
{
5050
// Defines origin admin address - may be removed for public deployment
51-
owner_address = msg.sender;
51+
owner = msg.sender;
5252

5353
// Sample Listings - May be removed for public deployment
54-
testingAddSampleListings();
54+
// 2018-04-04 - Temp commented out to get under gas limit.
55+
// testingAddSampleListings();
5556
}
5657

5758
function testingAddSampleListings()

packages/contracts/contracts/Purchase.sol

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ contract Purchase {
2121

2222
Stages public stage = Stages.AWAITING_PAYMENT;
2323

24-
Listing listingContract; // listing that is being purchased
24+
Listing public listingContract; // listing that is being purchased
2525
address public buyer; // User who is buying. Seller is derived from listing
26-
uint created;
26+
uint public created;
2727

2828
/*
2929
* Modifiers
@@ -49,36 +49,39 @@ contract Purchase {
4949
*/
5050

5151
function Purchase(
52-
address _listingContractAddress
52+
address _listingContractAddress,
53+
address _buyer
5354
)
5455
public
5556
{
56-
buyer = msg.sender;
57+
buyer = _buyer;
5758
listingContract = Listing(_listingContractAddress);
5859
created = now;
5960
}
6061

61-
62-
function buyerPay()
62+
// Pay for listing
63+
// We used to limit this to buyer, but that prevents Listing contract from
64+
// paying
65+
function pay()
6366
public
6467
payable
65-
isBuyer
6668
atStage(Stages.AWAITING_PAYMENT)
6769
{
6870
if (this.balance >= listingContract.price()) {
69-
70-
// Buyer has paid enough to cover purchase
71+
// Buyer (or their proxy) has paid enough to cover purchase
7172
stage = Stages.BUYER_PENDING;
7273

7374
// Mark item as no longer available for sale in Listing
74-
// TODO: presumable we call function on Listing(), proving that we have the funds to cover purchase.
75+
// TODO: presumably we call function on Listing(), proving that we have
76+
// the funds to cover purchase.
7577
}
78+
// Possible that nothing happens, and contract just accumulates sent value
7679
}
7780

7881

7982
function buyerConfirmReceipt()
80-
isBuyer
8183
public
84+
isBuyer
8285
atStage(Stages.BUYER_PENDING)
8386
{
8487
stage = Stages.SELLER_PENDING;
@@ -90,14 +93,16 @@ contract Purchase {
9093
isSeller
9194
atStage(Stages.SELLER_PENDING)
9295
{
96+
stage = Stages.COMPLETE;
97+
9398
// Send contract funds to seller (ie owner of Listing)
99+
// Transfering money always needs to be the last thing we do, do avoid
100+
// rentrancy bugs. (Though here the seller would just be getting their own money)
94101
listingContract.owner().transfer(this.balance);
95-
96-
stage = Stages.COMPLETE;
97102
}
98103

99104

100-
function openDisute()
105+
function openDispute()
101106
public
102107
{
103108
// Must be buyer or seller

packages/contracts/contracts/UserRegistry.sol

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,7 @@ contract UserRegistry {
1616
*/
1717

1818
// Mapping of all users
19-
mapping(address => userStruct) public users;
20-
21-
/*
22-
* Structs
23-
*/
24-
25-
struct userStruct {
26-
bytes32 ipfsHash;
27-
bool isSet;
28-
}
29-
30-
/*
31-
* Modifiers
32-
*/
33-
34-
modifier isValidUserAddress() {
35-
require (users[msg.sender].isSet);
36-
_;
37-
}
19+
mapping(address => bytes32) public users;
3820

3921
/*
4022
* Public functions
@@ -47,20 +29,34 @@ contract UserRegistry {
4729
)
4830
public
4931
{
50-
users[msg.sender] = userStruct(_ipfsHash, true);
32+
users[msg.sender] = _ipfsHash;
5133
NewUser(msg.sender);
5234
}
5335

54-
/// @dev create_another(): Create a new user and associates attenstion or proof with user
36+
/// @dev createAnother(): Create a new user and associates attenstion or proof with user
5537
// @param wallet id
5638
// Attestation or proof to associate to the user
57-
function create_another(string _id, string payload) public returns (string){
39+
// TODO: (Brad David) replace with real function
40+
function createAnother(
41+
string _id,
42+
string payload)
43+
public
44+
pure
45+
returns (string)
46+
{
47+
_id; // Dummy "operation" to silence copiler warnigns
5848
return payload;
5949
}
6050

6151
/// @dev get(): returns and existing user associated with wallet id
6252
// @param wallet id
63-
function get(string _id) public returns (string){
53+
function get(
54+
string _id
55+
)
56+
public
57+
pure
58+
returns (string)
59+
{
6460
return _id;
6561
}
6662
}

0 commit comments

Comments
 (0)