Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConsenSys-Academy/2b07533c64d6224c5b32c2e311c321a1 to your computer and use it in GitHub Desktop.
Save ConsenSys-Academy/2b07533c64d6224c5b32c2e311c321a1 to your computer and use it in GitHub Desktop.
// ===== DO NOT MODIFY THIS FILE =====
const BN = web3.utils.BN
const SupplyChain = artifacts.require('SupplyChain')
contract('SupplyChain', accounts => {
const owner = accounts[0]
const alice = accounts[1]
const bob = accounts[2]
const price = '1000'
let supplyChain
let itemId
before("Setup contract", async () => {
supplyChain = await SupplyChain.deployed()
})
it("Should add an item with the provided name and price.", async() => {
const emptyAddress = '0x0000000000000000000000000000000000000000'
const name = 'book'
const feeInWei = await web3.utils.toWei('1', 'finney')
let eventEmitted = false
const contractBalanceBefore = await web3.eth.getBalance(supplyChain.address)
const tx = await supplyChain.addItem(name, price, {
from: alice,
value: feeInWei
})
if (tx.logs[0].event) {
itemId = tx.logs[0].args[0].toString(10)
eventEmitted = true
}
const result = await supplyChain.getItem.call(itemId)
const contractBalanceAfter = await web3.eth.getBalance(supplyChain.address)
assert.equal(result[0], name, "The name of the last added item does not match the expected value.")
assert.equal(result[1].toString(10), price, "The price of the last added item does not match the expected value.")
assert.equal(result[2].toString(10), 0, "The state of the item should be 'ForSale', which should be declared first in the State enum.")
assert.equal(result[3], alice, "The address adding the item should be listed as the seller.")
assert.equal(result[4], emptyAddress, "The buyer address should be set to 0 when an item is added.")
assert.equal(eventEmitted, true, "Adding an item should emit a For Sale event.")
assert.equal(new BN(contractBalanceAfter).toString(), new BN(contractBalanceBefore).add(new BN(feeInWei)).toString(), "The contract balance should be increased by the price of the fee (1 finney).")
})
it("Should allow someone to purchase an item.", async() => {
let eventEmitted = false
const amount = '2000'
const aliceBalanceBefore = await web3.eth.getBalance(alice)
const bobBalanceBefore = await web3.eth.getBalance(bob)
const tx = await supplyChain.buyItem(itemId, {from: bob, value: amount})
if (tx.logs[0].event) {
itemId = tx.logs[0].args[0].toString(10)
eventEmitted = true
}
let aliceBalanceAfter = await web3.eth.getBalance(alice)
let bobBalanceAfter = await web3.eth.getBalance(bob)
const result = await supplyChain.getItem.call(itemId)
assert.equal(result[2].toString(10), 1, "The state of the item should be 'Sold', which should be declared second in the State enum.")
assert.equal(result[4], bob, "The buyer address should be set bob when he purchases an item.")
assert.equal(eventEmitted, true, "Adding an item should emit a Sold event.")
assert.equal(new BN(aliceBalanceAfter).toString(), new BN(aliceBalanceBefore).add(new BN(price)).toString(), "Alice's balance should be increased by the price of the item.")
assert.isBelow(Number(bobBalanceAfter), Number(new BN(bobBalanceBefore).sub(new BN(price))), "Bob's balance should be reduced by more than the price of the item (including gas costs).")
})
it("Should allow the seller to mark the item as shipped.", async() => {
let eventEmitted = false
const tx = await supplyChain.shipItem(itemId, {from: alice})
if (tx.logs[0].event) {
itemId = tx.logs[0].args[0].toString(10)
eventEmitted = true
}
const result = await supplyChain.getItem.call(itemId)
assert.equal(eventEmitted, true, "Adding an item should emit a Shipped event.")
assert.equal(result[2].toString(10), 2, "The state of the item should be 'Shipped', which should be declared third in the State enum.")
})
it("Should allow the buyer to mark the item as received.", async() => {
let eventEmitted = false
const tx = await supplyChain.receiveItem(itemId, {from: bob})
if (tx.logs[0].event) {
itemId = tx.logs[0].args[0].toString(10)
eventEmitted = true
}
const result = await supplyChain.getItem.call(itemId)
assert.equal(eventEmitted, true, "Adding an item should emit a Shipped event.")
assert.equal(result[2].toString(10), 3, "The state of the item should be 'Received', which should be declared fourth in the State enum.")
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment