Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
workflow issuance+acquisition+redemption
Browse files Browse the repository at this point in the history
  • Loading branch information
vdg committed Aug 12, 2017
1 parent 364ba83 commit a7f3023
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 21 deletions.
7 changes: 5 additions & 2 deletions contracts/CouponExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ contract CouponExample is StandardCoupon {

string public name;
string public version = 'v0.1';

uint8 public termDiscount;

function CouponExample (
string _name,
uint256 _initialSupply,
uint8 _termDiscount
) {
issuer = msg.sender;
require(_initialSupply > 0); /* hard cap limitation rules TBD */
creator = msg.sender;
name = _name;
termDiscount = _termDiscount;
totalFree = _initialSupply;
totalCoupon = _initialSupply;
totalFreeCoupon = _initialSupply;
}

}
13 changes: 13 additions & 0 deletions contracts/CouponRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pragma solidity ^0.4.12;

contract CouponRegistry {
uint storedData;

function set(uint x) {
storedData = x;
}

function get() constant returns (uint) {
return storedData;
}
}
33 changes: 17 additions & 16 deletions contracts/StandardCoupon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ pragma solidity ^0.4.12;

import "./Coupon.sol";

/* rename CouponCampaign ? */

contract StandardCoupon is Coupon {

/*
todo ? struct Authority to replace issuer in some meta task ..
TODO ? struct Authority to replace issuer in some meta task ..
*/

enum States { Created, Issued, Expired, Archived }
Expand All @@ -59,27 +61,26 @@ contract StandardCoupon is Coupon {
issuance is locking RGE futur bying (in eth)
*/

address issuer; /* Issuer has been set up at the contract creation */
address public creator; /* Creator is set up at the contract creation */
address public issuer; /* Issuer is now always same as creator */

/* set up some parameters like expiration at issuance ? */

function issue() atState(States.Created) onlyBy(issuer) {
state = States.Issued;
function issue() atState(States.Created) onlyBy(creator) {
state = States.Issued;
issuer = creator;
}

uint256 totalFree;
uint256 totalAcquired = 0;
uint256 totalRedeemed = 0;
uint256 public totalCoupon;
uint256 public totalFreeCoupon;
uint256 public totalAcquiredCoupon = 0; /* duplicate info */
uint256 public totalRedeemedCoupon = 0;

/* ********** ********** ********** */
/* the acquisition Register (track coupons effectively distributed to Users) */

mapping (address => bool) acquisitionRegister;

function freeCouponSupply() constant returns (uint256 free) {
return totalFree;
}

function hasCoupon(address _user) constant returns (bool yes) {
require(_user != issuer); /* SI_10 issuer is excluded for now to simplify tests */
return acquisitionRegister[_user];
Expand All @@ -88,9 +89,9 @@ contract StandardCoupon is Coupon {
function distributeCoupon(address _to) atState(States.Issued) private returns (bool success) {
require(_to != issuer); /* SI_10 issuer is excluded for now to simplify tests */
require(!hasCoupon(_to));
if (totalFree > 0) {
totalFree -= 1;
totalAcquired += 1;
if (totalFreeCoupon > 0) {
totalFreeCoupon -= 1;
totalAcquiredCoupon += 1;
acquisitionRegister[_to] = true;
return true;
} else {
Expand Down Expand Up @@ -130,14 +131,14 @@ contract StandardCoupon is Coupon {

function hasRedeemed(address _user) constant returns (bool yes) {
require(_user != issuer);
require(hasCoupon(_user));
return redemptionRegister[_user];
}

function redeemCoupon(address _user) atState(States.Issued) private returns (bool success) {
require(_user != issuer); /* SI_10 issuer is excluded for now to simplify tests */
require(hasCoupon(_user));
require(!hasRedeemed(_user));
totalRedeemed += 1;
totalRedeemedCoupon += 1;
redemptionRegister[_user] = true;
return true;
}
Expand Down
31 changes: 31 additions & 0 deletions test/CouponAquisition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var CouponExample = artifacts.require("./CouponExample.sol");

contract('CouponExample', function(accounts) {

var user1 = accounts[2];
const initialBalance_user1 = web3.eth.getBalance(user1)
console.log(web3.fromWei(initialBalance_user1).toString())

it("give a coupon to a user", function() {
var coupon;
var issuer;

return CouponExample.deployed().then(function(instance) {
coupon = instance;
return coupon.creator.call();
}).then(function(address) {
creator = address
return coupon.issue({from: creator});
}).then(function() {
return coupon.hasCoupon.call(user1);
}).then(function(result) {
assert.equal(result, false, "User1 already acquire this coupon");
return coupon.giveCoupon(user1,{from: creator});
}).then(function() {
return coupon.hasCoupon.call(user1);
}).then(function(result) {
assert.equal(result, true, "User1 acquisition didnt work");
});
});

});
28 changes: 28 additions & 0 deletions test/CouponIssuance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var CouponExample = artifacts.require("./CouponExample.sol");

contract('CouponExample', function(accounts) {

it("should have 100 free coupons at creation", function() {
return CouponExample.deployed().then(function(instance) {
return instance.totalFreeCoupon.call();
}).then(function(total) {
assert.equal(total.valueOf(), 100, "Total free Coupon(s) should be 100 initially");
});
});

it("should change state after issuance", function() {
var coupon;

return CouponExample.deployed().then(function(instance) {
coupon = instance;
return coupon.creator.call();
}).then(function(creator) {
return coupon.issue({from: creator});
}).then(function() {
return coupon.state.call();
}).then(function(state) {
assert.equal(state, 1, "State has not changed after issuance");
});
});

});
7 changes: 4 additions & 3 deletions test/TestCouponExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/CouponExample.sol";


contract TestCouponExample {

function testInitialFreeSupplyUsingDeployedContract() {
CouponExample coupon = CouponExample(DeployedAddresses.CouponExample());

uint expected = 100;

Assert.equal(coupon.freeCouponSupply(), expected, "Free Coupon supply should be 100 initially");
Assert.equal(coupon.totalFreeCoupon(), expected, "Free Coupon supply should be 100 initially");
}

function testInitialFreeSupplyWithNewCouponExample() {

uint expected = 99;

CouponExample coupon = new CouponExample('COUPON test', expected, 10);
CouponExample coupon = new CouponExample('COUPON test', expected, 0);

Assert.equal(coupon.freeCouponSupply(), expected, "Owner should have 99 CouponExample initially");
Assert.equal(coupon.totalFreeCoupon(), expected, "Owner should have 99 CouponExample initially");
}

}

0 comments on commit a7f3023

Please sign in to comment.