Bitcoin Forum
June 15, 2024, 06:28:56 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [dev]HelpCoin need your help.  (Read 273 times)
babiypetr (OP)
Full Member
***
Offline Offline

Activity: 322
Merit: 102


View Profile WWW
August 15, 2017, 04:29:18 PM
Last edit: August 15, 2017, 06:04:38 PM by babiypetr
 #1

Hello, my Dear Friends. I'm developing project https://bitcointalk.org/index.php?topic=2082404 and i need help:

HelpCoin  in transaction should deduct a commission of 3% I did so:

Quote
function transfer(address _to, uint256 _value) returns (bool) {
        CalculateOut(_value);
        var commiss = _value.div(100).mul(3);
        if (_value < 100) { return false; }
        if (commiss < 5) { commiss = 5; }
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(_value);
        Transfer(msg.sender, _to, _value);
        balances[_to] = balances[_to].sub(commiss);
        balances[ComissionAccount] = balances[ComissionAccount].add(commiss);
        Transfer(_to, ComissionAccount, commiss);
        return true;
    }

It works, but only on small digits such as 100 or 200. On large numbers for example, 10000000000 stops working (. I know that there are no fractional numbers in solidity, but I do not know how to properly separate the transaction. Help me Please.

P.S. HelpCoin have decimail = 8

babiypetr (OP)
Full Member
***
Offline Offline

Activity: 322
Merit: 102


View Profile WWW
August 19, 2017, 09:29:03 AM
 #2

I'm testing contract and I need your help!
This is code of contract:
Code:
pragma solidity ^0.4.11;

import "./SafeMath.sol";

/**
 * IssueID 8
 */

contract ERC20Basic {
  uint256 public totalSupply;
  function balanceOf(address who) constant returns (uint256);
  function transfer(address to, uint256 value) returns (bool);
  event Transfer(address indexed from, address indexed to, uint256 value);
}
contract ERC20 is ERC20Basic {
  function allowance(address owner, address spender) constant returns (uint256);
  function transferFrom(address from, address to, uint256 value) returns (bool);
  function approve(address spender, uint256 value) returns (bool);
  event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract BasicToken is ERC20Basic {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

  /**
  * @dev transfer token for a specified address
  * @param _to The address to transfer to.
  * @param _value The amount to be transferred.
  */
  function transfer(address _to, uint256 _value) returns (bool) {
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
  }

  /**
  * @dev Gets the balance of the specified address.
  * @param _owner The address to query the the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function balanceOf(address _owner) constant returns (uint256 balance) {
    return balances[_owner];
  }

}
contract StandardToken is ERC20, BasicToken {
   
    string public standard;
    string public name;
    string public symbol;
    uint8  public decimals;
    uint256 public totalSupply;

   
   
  function StandardToken( uint256 initialSupply, string _standard, string _name, string _symbol, uint8 _decimals ){
        totalSupply = initialSupply;
        standard = _standard;
        name = _name;
        symbol = _symbol;
        decimals = _decimals;
        balances[msg.sender] = initialSupply;
      }

  mapping (address => mapping (address => uint256)) allowed;


  /**
   * @dev Transfer tokens from one address to another
   * @param _from address The address which you want to send tokens from
   * @param _to address The address which you want to transfer to
   * @param _value uint256 the amout of tokens to be transfered
   */
  function transferFrom(address _from, address _to, uint256 _value) returns (bool) {
    var _allowance = allowed[_from][msg.sender];

    // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
    // require (_value <= _allowance);

    balances[_to] = balances[_to].add(_value);
    balances[_from] = balances[_from].sub(_value);
    allowed[_from][msg.sender] = _allowance.sub(_value);
    Transfer(_from, _to, _value);
    return true;
  }

  /**
   * @dev Aprove the passed address to spend the specified amount of tokens on behalf of msg.sender.
   * @param _spender The address which will spend the funds.
   * @param _value The amount of tokens to be spent.
   */
  function approve(address _spender, uint256 _value) returns (bool) {

    // To change the approve amount you first have to reduce the addresses`
    //  allowance to zero by calling `approve(_spender, 0)` if it is not
    //  already 0 to mitigate the race condition described here:
    //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    require((_value == 0) || (allowed[msg.sender][_spender] == 0));

    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
  }

  /**
   * @dev Function to check the amount of tokens that an owner allowed to a spender.
   * @param _owner address The address which owns the funds.
   * @param _spender address The address which will spend the funds.
   * @return A uint256 specifing the amount of tokens still available for the spender.
   */
  function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
    return allowed[_owner][_spender];
  }

}

contract HelpCoin is StandardToken {

    string  public _standard = "ERC20";
    string  public _name = "HelpCoin";
    string  public _symbol = "HLP";
    uint8   public _decimail = 8;
    uint256 public _initialSupply;
    uint256[] public _id;
    address[] public FirstOwner;
 
    address public ComissionAccount;

    event Commission(address indexed _to, uint256[] _id);
    event Debug(uint256 value);
    //event CalculateOut(uint256 amount);
     
    function HelpCoin( uint256 _initialSupply ) StandardToken(_initialSupply,_standard,_name,_symbol,_decimail){}
   
   
    function mintToken(address target, uint256 mintedAmount) {
//        setFirstOwner(target);
        balances[target] = balances[target].add(mintedAmount);
        totalSupply = totalSupply.add(mintedAmount);
        Transfer(0, this, mintedAmount);
        Transfer(this, target, mintedAmount);
    }

    function transfer(address _to, uint256 _value) returns (bool) {
        if (balances[msg.sender] < _value) { return false; }
        uint256 comiss = _value.div(100).mul(3);
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[ComissionAccount] = balances[ComissionAccount].add(comiss);
        balances[_to] = balances[_to].add(_value.sub(comiss));
        Transfer(msg.sender, _to, _value);
        Commission(ComissionAccount, _id);
        return true;
    }

    function setCommissionAccount(address _CommissionAccount) {
      ComissionAccount = _CommissionAccount;
    }

   
//   function setFirstOwner(address target) {
//        FirstOwner = target;
//    }

}

Testing transfer function, it must deduct 3% comission in other account (contract).
This is code of function:
Code:
function transfer(address _to, uint256 _value) returns (bool) {
        if (balances[msg.sender] < _value) { return false; }
        uint256 comiss = _value.div(100).mul(3);
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[ComissionAccount] = balances[ComissionAccount].add(comiss);
        balances[_to] = balances[_to].add(_value.sub(comiss));
        Transfer(msg.sender, _to, _value);
        Commission(ComissionAccount, _id);
        return true;
    }

This is js test , which I'm testing:
Code:
var HelpCoin = artifacts.require("HelpCoin");

contract('HelpCoin', function(accounts) {
    var hlp;

it("Deploy contract", function(done) {
var totalSupple = 100000000000;

HelpCoin.deployed(totalSupple).then(function(instance){
hlp = instance;
assert.isOk(hlp);
message = "Deployed address :" + hlp.address;
console.log(message);
done();
})
})
it("Пepeвoд тoкeнoв c oтчиcлeниeм кoммиccии", function(done){
var main_acc = accounts[0];
var target_acc = accounts[1];
var commiss_acc = accounts[2];

var main_bal_s;
var main_bal_e;
var target_bal_s;
var target_bal_e;
var commiss_bal_s;
var commiss_bal_e;

var ammount;
var commiss;

var message;

hlp.setCommissionAccount(commiss_acc).then(function(){
            return hlp.balanceOf.call(main_acc);
}).then(function(bal){
main_bal_s = bal.toNumber();
            message = "Start balance main account: " + main_bal_s + " HLP";
            console.log(message);
            return hlp.balanceOf.call(target_acc);
}).then(function(bal){
target_bal_s = bal.toNumber();
message = "Start balance target account: " + target_bal_s + " HLP";
console.log(message);
return hlp.balanceOf.call(commiss_acc);
}).then(function(bal){
commiss_bal_s = bal.toNumber();
message = "Start balance commiss account: " + commiss_bal_s + " HLP";
console.log(message);
ammount = 100;
message = "Transfer " + ammount + " HLP with 3% commission";
console.log(message);
return hlp.transfer(target_acc,ammount);
}).then(function(res){
message = "GasUsed for this transaction: " + res.receipt.gasUsed;
console.log(message);
return hlp.balanceOf.call(commiss_acc);
}).then(function(bal){
            commiss_bal_e = bal.toNumber();
            commiss = bal.toNumber();
            message = "The commssion comprised: " + commiss + " HLP";
            console.log(message);
return hlp.balanceOf.call(main_acc);
}).then(function(bal){
main_bal_e = bal.toNumber();
message = "End balance main account: " + main_bal_e + " HLP";
console.log(message);
return hlp.balanceOf.call(target_acc);
}).then(function(bal){
target_bal_e = bal.toNumber();
message = "End balance target account: " + target_bal_e + " HLP";
console.log(message);
message = "End balance commiss account: " + commiss_bal_e + " HLP";
console.log(message);

assert.equal(main_bal_e,main_bal_s - ammount,"Toкeны нe пepeвeдeны c глaвнoгo aккayнтa");
assert.equal(target_bal_e,target_bal_s + ammount - commiss,"Toкeны нe пepeвeдeны нa пpинмaющий aккayнт");
assert.equal(commiss_bal_e,target_bal_s + commiss,"Кoммиccия нe пepeвeдeнa");
done();
})
})
})

According to the test, we initialize 1000 HLPs (100000000000 in the unit units) and place them on the main account, and then transfer 100 HLP, the test result:
Code:
root@helpcoin:/home/projects/HelpCoin/Testing# truffle test
Using network 'development'.

Compiling ./contracts/HelpCoin.sol...
Compiling ./contracts/SafeMath.sol...


  Contract: HelpCoin
Deployed address :0x1b92206f541de5b49a4e7914b0f4454d5ad78078
    ✓ Deploy contract
Start balance main account: 1000 HLP
Start balance target account: 0 HLP
Start balance commiss account: 0 HLP
Transfer 100 HLP with 3% commission
GasUsed for this transaction: 73839
The commssion comprised: 3 HLP
End balance main account: 900 HLP
End balance target account: 97 HLP
End balance commiss account: 3 HLP
    ✓ Пepeвoд тoкeнoв c oтчиcлeниeм кoммиccии (330ms)


  2 passing (371ms)

root@helpcoin:/home/projects/HelpCoin/Testing#

As you can see, everything worked as it should, and judging from the test, the function does not work with minimal units, as 100 HLP moved, but if we reduce the number of tokens transferred to 10, we get a problem:
Code:
root@helpcoin:/home/projects/HelpCoin/Testing# truffle test
Using network 'development'.

Compiling ./contracts/HelpCoin.sol...
Compiling ./contracts/SafeMath.sol...


  Contract: HelpCoin
Deployed address :0xdb1934157218b2d927135261d72346d17d64c7af
    ✓ Deploy contract
Start balance main account: 1000 HLP
Start balance target account: 0 HLP
Start balance commiss account: 0 HLP
Transfer 10 HLP with 3% commission
GasUsed for this transaction: 58797
The commssion comprised: 0 HLP
End balance main account: 990 HLP
End balance target account: 10 HLP
End balance commiss account: 0 HLP
    ✓ Пepeвoд тoкeнoв c oтчиcлeниeм кoммиccии (353ms)


  2 passing (388ms)

root@helpcoin:/home/projects/HelpCoin/Testing#

As we can see, the transfer of tokens was successful, but the commission was not calculated because of the fact that in HLP it is (0.3 HLP) and in fact we can take it away as we have 8 zeros in our token, but rummaged all the documentation and that and did not understand how Do you want to do this? Can not see the obvious ... Please help me!

europesss
Sr. Member
****
Offline Offline

Activity: 546
Merit: 250



View Profile
August 19, 2017, 09:52:18 AM
 #3

Sorry man , I am not a tech guy, I think you can get help  at this board Project Development.
babiypetr (OP)
Full Member
***
Offline Offline

Activity: 322
Merit: 102


View Profile WWW
August 19, 2017, 10:01:01 AM
 #4

Sorry man , I am not a tech guy, I think you can get help  at this board Project Development.

Thank's , I originally there and posted this topic, but moderator moved it here (

babiypetr (OP)
Full Member
***
Offline Offline

Activity: 322
Merit: 102


View Profile WWW
August 19, 2017, 12:27:48 PM
 #5

I'm so sorry! I'm stupid men)) I forgot about the file 2_helpcoin_migration.js

Code:
var HelpCoin =  artifacts.require("./HelpCoin.sol");

var initialSupply = 1000;
module.exports = function(deployer) {
deployer.deploy(HelpCoin,initialSupply);
};
In it the variable InitialSupple, And then what was in the test, respectively, did not work, and I always had 1000 HLP (

I'm fix this:
Code:
var HelpCoin =  artifacts.require("./HelpCoin.sol");

var initialSupply = 1000*(10**8);
module.exports = function(deployer) {
deployer.deploy(HelpCoin,initialSupply);
};

And test code:
Code:
var HelpCoin = artifacts.require("HelpCoin");

contract('HelpCoin', function(accounts) {
    var hlp;

it("Deploy contract", function(done) {
HelpCoin.deployed().then(function(instance){
hlp = instance;
assert.isOk(hlp);
message = "Deployed address :" + hlp.address;
console.log(message);
done();
})
})
it("Пepeвoд тoкeнoв c oтчиcлeниeм кoммиccии", function(done){
var main_acc = accounts[0];
var target_acc = accounts[1];
var commiss_acc = accounts[2];

var main_bal_s;
var main_bal_e;
var target_bal_s;
var target_bal_e;
var commiss_bal_s;
var commiss_bal_e;

var ammount;
var commiss;

var message;

hlp.setCommissionAccount(commiss_acc).then(function(){
            return hlp.balanceOf.call(main_acc);
}).then(function(bal){
main_bal_s = bal.toNumber();
            message = "Start balance main account: " + main_bal_s + " HLP";
            console.log(message);
            return hlp.balanceOf.call(target_acc);
}).then(function(bal){
target_bal_s = bal.toNumber();
message = "Start balance target account: " + target_bal_s + " HLP";
console.log(message);
return hlp.balanceOf.call(commiss_acc);
}).then(function(bal){
commiss_bal_s = bal.toNumber();
message = "Start balance commiss account: " + commiss_bal_s + " HLP";
console.log(message);
ammount = 1000000000;
message = "Transfer " + ammount + " HLP with 3% commission";
console.log(message);
return hlp.transfer(target_acc,ammount);
}).then(function(res){
message = "GasUsed for this transaction: " + res.receipt.gasUsed;
console.log(message);
return hlp.balanceOf.call(commiss_acc);
}).then(function(bal){
            commiss_bal_e = bal.toNumber();
            commiss = bal.toNumber();
            message = "The commssion comprised: " + commiss + " HLP";
            console.log(message);
return hlp.balanceOf.call(main_acc);
}).then(function(bal){
main_bal_e = bal.toNumber();
message = "End balance main account: " + main_bal_e + " HLP";
console.log(message);
return hlp.balanceOf.call(target_acc);
}).then(function(bal){
target_bal_e = bal.toNumber();
message = "End balance target account: " + target_bal_e + " HLP";
console.log(message);
message = "End balance commiss account: " + commiss_bal_e + " HLP";
console.log(message);

assert.equal(main_bal_e,main_bal_s - ammount,"Toкeны нe пepeвeдeны c глaвнoгo aккayнтa");
assert.equal(target_bal_e,target_bal_s + ammount - commiss,"Toкeны нe пepeвeдeны нa пpинмaющий aккayнт");
assert.equal(commiss_bal_e,target_bal_s + commiss,"Кoммиccия нe пepeвeдeнa");
done();
})
})
})
Result test:
Code:
root@helpcoin:/home/projects/HelpCoin/Testing# truffle test
Using network 'development'.

Compiling ./contracts/HelpCoin.sol...
Compiling ./contracts/SafeMath.sol...


  Contract: HelpCoin
Deployed address :0x9df17d35f384184057e8813252eb88a7e16128f3
    ✓ Deploy contract
Start balance main account: 100000000000 HLP
Start balance target account: 0 HLP
Start balance commiss account: 0 HLP
Transfer 1000000000 HLP with 3% commission
GasUsed for this transaction: 73967
The commssion comprised: 30000000 HLP
End balance main account: 99000000000 HLP
End balance target account: 970000000 HLP
End balance commiss account: 30000000 HLP
    ✓ Пepeвoд тoкeнoв c oтчиcлeниeм кoммиccии (313ms)


  2 passing (351ms)

root@helpcoin:/home/projects/HelpCoin/Testing#

Now everything works as it should. Maybe someone will help this information.

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!