Skip to content

Instantly share code, notes, and snippets.

@aleph-v
Created July 26, 2018 21:54
Show Gist options
  • Save aleph-v/cb8d8a9e5a33ad84e2c0d37619d8293d to your computer and use it in GitHub Desktop.
Save aleph-v/cb8d8a9e5a33ad84e2c0d37619d8293d to your computer and use it in GitHub Desktop.
let Deposit = artifacts.require("./DepositContract")
let Flame = artifacts.require("./FlameToken")
let Master = artifacts.require("./MasterDeposit")
contract("DepositContract", function(accounts) {
let deposit
let master
let someone = accounts[0]
let token
let sender = accounts[1]
let wallet = accounts[2]
beforeEach(async () => {
token = await Flame.new(2000, { from: sender }).should.be.fulfilled
master = await Master.new(wallet, token.address, {from : sender}).should.be.fulfilled
deposit = await Deposit.new(master.address).should.be.fulfilled
await token.mint(deposit.address, 1000, { from : sender })
})
describe("#DepositContract", async () => {
context("Constructor called with address zero", async () => {
let invalid = "0x0000000000000000000000000000000000000000"
it("should throw", async () =>{
await Deposit.new(invalid).should.not.be.fulfilled
})
})
context("Constructor called with valid address", async () => {
it("should not throw", async () =>{
await Deposit.new(master.address).should.be.fulfilled
})
it("should set master correctly" , async () => {
let deposit = await Deposit.new(master.address).should.be.fulfilled
let returnedMaster = await deposit.getMasterAddress.call()
returnedMaster.should.be.eq(master.address)
})
})
})
describe("#transferTokensToColdWallet", async () => {
context("It has a reentrant call" , async () => {
it("should fail", async () => {
})
})
context("Nonreentrant call" , async () => {
it("should set the token balance of this to zero", async () => {
await deposit.transferTokensToColdWallet().should.be.fulfilled
let balance = await token.balanceOf.call(deposit.address).should.be.fulfilled
balance.toNumber().should.be.eq(0)
})
it("should set the token balance of the cold wallet properly", async () => {
await deposit.transferTokensToColdWallet().should.be.fulfilled
let balance = await token.balanceOf.call(wallet).should.be.fulfilled
balance.toNumber().should.be.eq(1000)
})
})
})
describe("#returnTokensToHolder", async () => {
context("Not called by master", async () => {
it("should fail", async () => {
await deposit.returnTokensToHolder(token.address, someone, 100, {from : someone}).should.not.be.fulfilled
})
})
context("It has a reentrant call" , async () => {
it("should fail", async () => {
})
})
context("Nonreentrant call by master" , async () => {
it("Should set the token balance of the deposit contract properly" , async () => {
deposit = await Deposit.new(sender).should.be.fulfilled
await token.mint(deposit.address, 1000, { from: sender}).should.be.fulfilled
await deposit.returnTokensToHolder(token.address, someone, 100, {from : sender}).should.be.fulfilled
let balance = await token.balanceOf.call(deposit.address)
balance.toNumber().should.be.eq(900)
})
it("Should set the token balance of the return to address properly" , async () => {
deposit = await Deposit.new(sender).should.be.fulfilled
await token.mint(deposit.address, 1000, { from: sender}).should.be.fulfilled
await deposit.returnTokensToHolder(token.address, someone, 100, {from : sender}).should.be.fulfilled
let balance = await token.balanceOf.call(someone)
balance.toNumber().should.be.eq(100)
})
})
})
describe("#changeMaster", async () => {
context("Not called by master", async () => {
it("should fail", async () => {
await deposit.changeMaster(someone, {from : someone}).should.not.be.fulfilled
})
})
context("Call by master" , async () => {
it("Should set the master address properly" , async () => {
deposit = await Deposit.new(sender);
await deposit.changeMaster(someone, {from : sender})
let masterAddress = await deposit.getMasterAddress.call()
masterAddress.should.be.eq(someone)
})
})
})
})
contract( "MasterDeposit", function (accounts) {
let master
let sender = accounts[0]
let wallet = accounts[1]
let token
let someone = accounts[3]
beforeEach( async () => {
token = await Flame.new(1000, {from : sender}).should.be.fulfilled
master = await Master.new(wallet, token.address , { from : sender}).should.be.fulfilled
})
describe("#MasterDeposit", async () => {
context("Constructor Called", async () => {
it("Should set proper Owner", async () =>{
let hold = await master.owner.call()
await hold.should.be.eq(sender)
})
it("Should set proper Wallet", async () =>{
let hold = await master.getWallet.call()
hold.should.be.eq(wallet)
})
it("Should set proper Token", async () =>{
let hold = await master.getToken.call()
await hold.should.be.eq(token.address)
})
})
})
describe("#setWallet", async () => {
context("Not called by owner", async () => {
it("should fail", async () => {
await master.setWallet(someone, {from : someone}).should.not.be.fulfilled
})
})
context("Called by owner with zero address", async () => {
it("should fail", async () => {
let invalid = "0x0000000000000000000000000000000000000000"
await master.setWallet(invalid, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with valid address", async () => {
it("should set wallet varible properly", async () => {
await master.setWallet(someone, {from: sender})
let temp = await master.getWallet.call()
temp.should.be.eq(someone)
})
})
})
describe("#setToken", async () => {
context("Not called by owner", async () => {
it("should fail", async () => {
await master.setToken(someone, {from: someone}).should.not.be.fulfilled
})
})
context("Called by owner with zero address", async () => {
it("should fail", async () => {
let invalid = "0x0000000000000000000000000000000000000000"
await master.setToken(invalid, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with valid address", async () => {
it("should set token varible properly", async () => {
await master.setToken(someone, {from : sender})
hold = await master.getToken.call()
hold.should.be.eq(someone)
})
})
})
describe("#isTokenSupported", async () => {
context("Called with the wrong address", async () => {
it("should return false", async () => {
flag = await master.isTokenSupported.call(someone)
flag.should.be.eq(false)
})
})
context("Called with the right address", async () => {
it("should return true", async () => {
flag = await master.isTokenSupported.call(token.address)
flag.should.be.eq(true)
})
})
})
describe("#createDeposit", async () => {
let events
context("Called by owner" , async () => {
beforeEach( async () => {
let events = await master.createDeposit(
{from : sender}
).should.be.fulfilled.then((tx) => {
return tx.logs
})
})
it("should have emited one event", async () => {
let events = await master.createDeposit(
{from : sender}
).should.be.fulfilled.then((tx) => {
return tx.logs
})
events.length.should.be.eq(1)
})
it("should be a createdDeposit event", async () => {
let events = await master.createDeposit(
{from : sender}
).should.be.fulfilled.then((tx) => {
return tx.logs
})
let emited = events[0].event
emited.should.be.eq("CreatedDeposit")
})
it("should mark the deposit as created", async () => {
let events = await master.createDeposit(
{from : sender}
).should.be.fulfilled.then((tx) => {
return tx.logs
})
deposit = events[0].args.deposit
let mapping = await master.deposits.call(deposit)
mapping.should.be.eq(true)
})
})
context("Not called by owner" , async () => {
it("should fail", async () => {
await master.createDeposit({from : someone}).should.not.be.fulfilled
})
})
})
describe("#returnTokensToHolderFromDeposit", async () => {
context("Called by owner with invalid deposit" , async () => {
it("should fail", async () => {
let deposit = await master.createDeposit.call({from : sender})
await token.mint(deposit, 100, { from: sender }).should.be.fulfilled
await master.returnTokensToHolderFromDeposit(deposit, token.address, wallet, 100, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with zero return address " , async () => {
it("should fail", async () => {
let deposit = await master.createDeposit.call({from : sender})
await token.mint(deposit, 100, { from: sender }).should.be.fulfilled
let invalid = "0x0000000000000000000000000000000000000000"
await master.returnTokensToHolderFromDeposit(deposit, token.address, invalid, 100, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with valid arguments" , async () => {
it("should deduct the tokens from the deposit contract", async () => {
let events = await master.createDeposit({from : sender}).should.be.fulfilled.then( (tx) => {return tx.logs})
events.length.should.be.eq(1)
events[0].event.should.be.eq("CreatedDeposit")
let deposit = events[0].args.deposit
await token.mint(deposit, 100, { from: sender }).should.be.fulfilled
await master.returnTokensToHolderFromDeposit(deposit, token.address, wallet, 100, {from : sender})
let hold = await token.balanceOf(deposit)
hold.toNumber().should.be.eq(0)
})
it("should add the tokens to the given account", async () => {
let events = await master.createDeposit({from : sender}).should.be.fulfilled.then( (tx) => {return tx.logs})
events.length.should.be.eq(1)
events[0].event.should.be.eq("CreatedDeposit")
let deposit = events[0].args.deposit
await token.mint(deposit, 100, { from: sender }).should.be.fulfilled
await master.returnTokensToHolderFromDeposit(deposit, token.address, wallet, 100, {from : sender})
let hold = await token.balanceOf(wallet)
hold.toNumber().should.be.eq(100)
})
})
context("Not called by owner" , async () => {
it("should fail", async () => {
let deposit = await master.createDeposit.call({from : sender})
await token.mint(deposit, 100, { from: sender }).should.be.fulfilled
await master.returnTokensToHolderFromDeposit(deposit, token.address, wallet, 100, {from : someone}).should.not.be.fulfilled
})
})
})
describe("#changeMasterForDeposit", async () => {
context("Not called by owner" , async () => {
it("should fail", async () => {
let deposit = await master.createDeposit.call({from : sender})
await master.changeMasterForDeposit(someone, deposit, {from : someone}).should.not.be.fulfilled
})
})
context("Called by owner with invalid deposit address" , async () => {
it("should fail", async () => {
await master.changeMasterForDeposit(someone, someone, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with zero as newMaster" , async () => {
it("should fail", async () => {
let deposit = await master.createDeposit.call({from : sender})
let invalid = "0x0000000000000000000000000000000000000000"
await master.changeMasterForDeposit(invalid, deposit, {from : sender}).should.not.be.fulfilled
})
})
context("Called by owner with valid imputs" , async () => {
it("properly changes the master of the deposit contract", async () => {
let events = await master.createDeposit({from : sender}).should.be.fulfilled.then( (tx) => {return tx.logs})
events.length.should.be.eq(1)
events[0].event.should.be.eq("CreatedDeposit")
let deposit = events[0].args.deposit
await master.changeMasterForDeposit(someone, deposit, {from : sender}).should.be.fulfilled
let depositInstance = await Deposit.at(deposit).should.be.fulfilled
let hold = await depositInstance.getMasterAddress.call()
hold.should.be.eq(someone)
})
it("marks the deposit contract as not belonging to this contract", async () => {
let hold = await master.deposits.call(deposit)
hold.should.be.eq(false)
})
})
})
})
//Integration Test
contract('MasterDeposit and DepositContract', function (accounts) {
let master
let newMaster
let deposit
let token
let sender = accounts[0]
let wallet = accounts[1]
before(async () => {
token = await Flame.new(1000, { from: sender }).should.be.fulfilled
await token.mint(sender, 1000, { from: sender }).should.be.fulfilled
master = await Master.new(wallet, token.address, { from: sender }).should.be.fulfilled
newMaster = await Master.new(wallet, token.address, { from: sender }).should.be.fulfilled
})
it('Should execute successfully', async () => {
// Create a new deposit instance through the first master contract
token = await Flame.new(1000, { from: sender }).should.be.fulfilled
await token.mint(sender, 1000, { from: sender }).should.be.fulfilled
master = await Master.new(wallet, token.address, { from: sender }).should.be.fulfilled
newMaster = await Master.new(wallet, token.address, { from: sender }).should.be.fulfilled
let events = await master.createDeposit(
{ from: sender }
).should.be.fulfilled.then((tx) => {
return tx.logs
})
// Ensure that one event was emitted
events.length.should.be.eq(1)
// Ensure that the emitted event is a CreatedDeposit event
events[0].event.should.be.eq('CreatedDeposit')
// Get the address of the deposit contract that was created
deposit = events[0].args.deposit
// Transfer tokens to the deposit contract
await token.transfer(deposit, 1000, { from: sender }).should.be.fulfilled
// Change the master address of the deposit contract
await master.changeMasterForDeposit(
newMaster.address, deposit, { from: sender }
).should.be.fulfilled
// Call return tokens to holder
await newMaster.returnTokensToHolderFromDeposit(
deposit, token.address, sender, 1000, { from: sender }
).should.be.fulfilled
// Ensure that the sender has the correct balance now
let balance = await token.balanceOf.call(sender).should.be.fulfilled
balance.should.be.eq(1000)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment