Programming Project - Phase 2

This is my CoinFlip-Dapp. I build it using ChainLink Oracle for random numbers.
Running on the Kovan Network. You can check it out here:
https://kovan.etherscan.io/address/0x7480690a7e1a50bf119c3797f030418244552cca

Code here : https://github.com/dhipgraby/ethereum-dice-Dapp

It is a betting game, where user have to choose if the lucky number will be higher than 51 or lower than 49. Users gets 40% reward plus current bet amount.

hi @dan-i thanks for the insight. I looked through my code but I’m still unable to pinpoint exactly what the issue here is unfortunately, would very much appreciate further input on this.

Hey @Laurenzius,

Congratulations for your first project.

As you correctly said, you can clean up your code now :slight_smile:

Some consideration about the code and how to keep it cleaner:

uint constant GAMBLING_FEE = 50000000000000000;

modifier mincosts(uint cost){
      require(msg.value >= cost, "Not enough Ether!");
      _;
  }

function flip(bool betOnHeads) public payable mincosts(GAMBLING_FEE){
require(GAMBLING_FEE<msg.value,'REQUIRE: flip(): must be more than the gambling fee, which is 0.01 ether');

You have redundant verifications here.

require(GAMBLING_FEE<msg.value is the same as require(msg.value >= cost, "Not enough Ether!");

As you are sending GAMBLING_FEE as param to the modifier.

Also consider that GAMBLING_FEE is global in your contract therefore you do not need to pass it as parameter to the modifier.

uint constant GAMBLING_FEE = 50000000000000000;

modifier mincosts(){
      require(msg.value >= GAMBLING_FEE, "Not enough Ether!");
      _;
  }

function flip(bool betOnHeads) public payable mincosts (){
......
}

This should be enough.

Also your function flip calls startDraw where you check require(msg.value >= _bet, "REQUIRED: startDraw(): msg.value >= _bet,");
Because you already verified msg.value are you sure that require is useful?

There are other things you can improve, feel free to ask me if you need help!

Again congrats for your 1st dapp!

Happy coding,
Dani

1 Like

Hey dude good job :slight_smile:

I never dared to convert an int to string in Solidity, this language hate strings and charges you an insane amount of gas to handle them.
Good one anyway, I saw something new

 function uintToStr(uint256 _i)
        internal
        returns (string memory _uintAsString)
    {
        uint256 number = _i;
        if (number == 0) {
            return "0";
        }
        uint256 j = number;
        uint256 len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint256 k = len - 1;
        while (number != 0) {
            bstr[k--] = bytes1(uint8(48 + (number % 10)));
            number /= 10;
        }
        return string(bstr);
    }
1 Like

hi @dan-i, @filip
i am getting this error
expected <BN: 12> to be an instance of string, number or BigNumber
can anyone help me in resolving this??

Hi @Paras_Arya

Please post the code where this error is generated.

Thanks
Dani

@dan-i
const BigNumber = web3.BigNumber;

const TESTtoken = artifacts.require(‘TESTtoken’);

require(‘chai’)
.use(require(‘chai-bignumber’)(BigNumber))
.should();

contract(‘TESTtoken’, accounts => {
const _name = ‘TEST Protocol’;
const _symbol = ‘TEST’;
const _decimals = 18;

beforeEach(async function () {
this.token = await TESTtoken.new(_name, _symbol, _decimals);
});

describe(‘token attributes’, function() {
it(‘has the correct name’, async function() {
const name = await this.token.name();
name.should.equal(_name);
});

it('has the correct symbol', async function() {
  const symbol = await this.token.symbol();
  symbol.should.equal(_symbol);
});

it('has the correct decimals', async function() {
  const decimals = await this.token.decimals();
  decimals.should.be.bignumber.equal(_decimals);
});

});
});

used this for the test in truffle(THIS IS TEST FILE)

Hi @Paras_Arya

Which test fails and generates the error?
I assume is the last one (has the correct decimals) because 12 is the HEX representation of 18 (your decimals).
If that’s the case try

const decimals = parseInt(await this.token.decimals());
  decimals.should.equal(_decimals);

Also the libraries you are using to assert look a bit messy, just use the assert, is the most used and makes the code readability much better.

const assert = require('assert');

assert.strictEqual(1,1,“the numbers do not match”);
assert.notStrictEqual(1,2,“the numbers match but they should not”);

Docs: https://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message

Happy learning,
Dani

1 Like

@dan-i thanks a lot for the help!!

1 Like

hey @dan-i
i have made a ether.js file having this set of code

export default function ether(n){

    return new web3.BigNumber(web3.utils.toWei(n,"ether"));
}

which I am using in test file

import ether from './helpers/ether';
const BigNumber = web3.BigNumber;
const assert = require('assert');


const TESTtoken = artifacts.require(‘TESTtoken’);

require(‘chai’)
.use(require(‘chai-bignumber’)(BigNumber))
.should();

contract(‘TESTtoken’, accounts => {
const _name = ‘TEST Protocol’;
const _symbol = ‘TEST’;
const _decimals = 18;

beforeEach(async function () {
this.token = await TESTtoken.new(_name, _symbol, _decimals);
});

describe(‘token attributes’, function() {
it(‘has the correct name’, async function() {
const name = await this.token.name();
name.should.equal(_name);
});
it('has the correct symbol', async function() {
  const symbol = await this.token.symbol();
  symbol.should.equal(_symbol);
});

  it('tracks the rate', async function(){
      const rate = parseInt(await this.crowdsale.rate());
      rate.should.equal(this._rate);
    });
    it('tracks the wallet', async function(){
      const wallet = await this.crowdsale.wallet();
      wallet.should.equal(this._wallet);
    });
  });

  describe('accepting payments', function() {
  it('should accept payments', async function() {
    const value = ether(1);
    const purchaser = investor2;
    await this.crowdsale.sendTransaction({ value: value, from: investor1 }).should.be.fulfilled;
    await this.crowdsale.buyTokens(investor1, { value: value, from: purchaser }).should.be.fulfilled;
  });

});

});

used this for the test in truffle(THIS IS TEST FILE)

getting the error

i have found a solution too

but I am passing the variable here instead of value so can you please help.

Hi @dan-i,

thanks a lot for the code review and your hints to remove redundancy. I think it will help to save some gas. Im am curious to know what else I could improve.

Thanks
Laurenzius

Hi @Paras_Arya

The compiler is asking you to pass a string instead of a number.
Try ether('1');

1 Like

Hi @dan-i

I installed hdwallet provider, but I get this error. How to solve this?

thanks for the help

stuck again can you please help

and when i use .equal instead of .isEqualto getting this error

Hey @oneworldcoder

Install this package npm i @truffle/hdwallet-provider

Happy coding,
Dani

Hi @dan-i

I have 5ETH Balance in Metamask. However I get an insufficient funds error. My github How to solve this? (Please note I have not uploaded the node modules in github because there are many files)

@oneworldcoder

You have to move funds to accounts[1] (in metamask) because that’s the default account used to deploy a contract.

happy learning,
Dani

1 Like

Hi @dan-i

I want to reset my deployed contract. I used truffle migrate --reset --network ropsten Only Ownable.sol got compiled while others did not. Also I got the following error. How to correct this?

That one is probably an issue with Infura.
Try again or even use another infura key and check if the situation imporves.

Also make sure to use node v: 10.21.0

You can check your node version by opening the terminal and type node --version
Screenshot 2021-01-05 at 16.12.09

If your node version is different than this one, follow the faq here: FAQ - How to downgrade Node.Js

hi @dan-i do you mind taking a look through my code? Thank you as always.