Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity >=0.4.21 <0.6.0;
- contract GiftRegistry {
- uint256 idCounter = 0;
- enum GiftStatus { Offered }
- struct Gift {
- string description;
- uint256 value;
- GiftStatus status;
- address giftGiver;
- address giftReceiver;
- address giftApprover;
- }
- mapping(uint256 => Gift) public giftMap;
- function doesGiftExist(uint256 _giftId) private view returns(bool){
- Gift memory gift = giftMap[_giftId];
- bytes memory giftAsBytes = bytes(gift.description);
- return giftAsBytes.length > 0;
- }
- function getGift(uint256 _giftId) public view returns(string memory, uint256, GiftStatus, address, address, address){
- require(doesGiftExist(_giftId), 'Gift not found');
- Gift memory gift = giftMap[_giftId];
- return (
- gift.description,
- gift.value,
- gift.status,
- gift.giftGiver,
- gift.giftReceiver,
- gift.giftApprover
- );
- }
- function offerGift(string memory _description, uint256 _value, address _giftReceiver) public returns(uint256) {
- uint256 currentId = idCounter;
- giftMap[currentId] = Gift(_description, _value, GiftStatus.Offered, msg.sender, _giftReceiver, address(0));
- idCounter = idCounter + 1;
- return currentId;
- }
- }
- const GiftRegistry = artifacts.require('GiftRegistry');
- const truffleAssert = require('truffle-assertions');
- contract('The Gift Registry contract', accounts => {
- let contract;
- const giftGiverAddress = accounts[1];
- const giftReceiverAddress = accounts[2];
- beforeEach(async () => {
- contract = await GiftRegistry.new({from: accounts[0]});
- });
- describe('given offerGift is called', () => {
- const OfferedStatus = 0;
- it('the status should be set to offered', async () => {
- const fiveDollars = 500;
- // Calling like this returns a tx instead of the id, the gift can be found with a hard coded index of 0
- const giftId1 = await contract.offerGift('coffee', fiveDollars, giftReceiverAddress, {from: giftGiverAddress});
- // This call returns the new giftId, however the gift can't be found and the call to getGift throws the error
- // Error: Returned error: VM Exception while processing transaction: revert Gift not found
- const giftId2 = await contract.offerGift.call('coffee', fiveDollars, giftReceiverAddress, {from: giftGiverAddress});
- const gift = await contract.getGift.call(0);
- assert.equal(OfferedStatus, gift[2]);
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement