How this uint accept Address value in this smart contract? - ethereum

Can anyone Explain me how uint variable accept address in this smart contract
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;
library CairoConstants {
uint256 public constant FIELD_PRIME =
0x800000000000011000000000000000000000000000000000000000000000001;
}
As per my Knowledge Uint Only accept unsigned integer values so why on Compilation it not throws an Error Message
Please Clear my Doubt
Thank You
Hello Everyone,
Can anyone Explain me how uint variable accept address in this smart contract
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;
library CairoConstants {
uint256 public constant FIELD_PRIME =
0x800000000000011000000000000000000000000000000000000000000000001;
}
As per my Knowledge Uint Only accept unsigned integer values so why on Compilation it not throws an Error Message
Please Clear my Doubt
Thank You

If the size of a given hex value is 20bytes, the compiler thinks it's an address, otherwise, it thinks it's a number.
So if you use 0x8000000000000110000000000000000000000000, it might throw an error since it's an address, but in your code, the given value is not an address.
In addition, if the length of the value is 39 or 41 hex digits, it also throws an error since the compiler thinks it looks like an address but there's a typo.

Related

Error of Big number while using constructer in solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
struct account{
string _name;
uint _acc_id;
uint balance;
}
contract My_acc{
account public person;
constructor(string memory name, uint acc_id, uint _balance){
person._name = name;
person._acc_id = acc_id;
person.balance = _balance;
}
}
I am trying to pass values to struct variable through constructor But I am getting this following error.
creation of My_acc errored: Error encoding arguments: Error: invalid BigNumber string (argument="value", value="", code=INVALID_ARGUMENT, version=bignumber/5.5.0)
I tried running the same code and it worked just fine.
Constructor runs only once and that is at the time of deployment. Since you are passing the values to constructor, you should pass the values at the time of deployment.
My reputation does not seem to allow me to upload the image. Please go through the link below.
https://i.stack.imgur.com/AFBWd.png
So guys, I resolved the error. It wasn't actually an error. It was just a silly mistake that I made as a beginner.
SOLUTION: -
I wasn't actually passing values to constructor initially. Actually when we add constructor in our contract we basically get a input section with our deploy button automatically (even before actually deploying). which have to be used to pass values before deploying our contract.

TypeError: Member "length" is read-only and cannot be used to resize arrays

I am using this version of solidity, pragma solidity >=0.4.22 <0.9.0;
Whenever I compile truffle I get this TypeError: Member "length". When I change the version to 0.4.0 the error goes away, but I can't use this version. I need to use this pragma solidity >=0.4.22 <0.9.0;
Here is the error:
TypeError: Member "length" is read-only and cannot be used to resize arrays.
--> project:/contracts/Ballot1.sol:23:9:
|
23 | proposals.length = _numProposal;
| ^^^^^^^^^^^^^^^^
Compilation failed. See above.
From Solidity v0.6.0 and onwards, you can use an array slice such as proposals[start:end] (note: it only supports calldata array).
Otherwise, I think you might need to build a copy of the array like so:
function slice(
uint256 start,
uint256 end,
uint256[] memory proposals
) public pure returns (uint256[] memory) {
uint256[] memory result;
uint256 idx = 0;
for (uint256 i = start; i < end; i++) {
result[idx] = proposals[i];
idx++;
}
return result;
}
Note this operation can be quite costly if you have a large array.

Calling solidity contract function from ethers with uint16 parameter only works when the number is 9 or smaller

I have this code in my solidity contract:
uint256 constant maxNum = 10000;
function mintNewFull(uint16 tokenId) public {
require (0 <= tokenId && tokenId < maxNum;
// do other stuff
}
And I called it using this code in ethers which worked:
contractWithSigner.mintNewFull(3);
But then later when I changed it to trying to mint with tokenID 11:
contractWithSigner.mintNewFull(11);
It didn't work. And I tried and every number under 10 seems to work and numbers greater than that don't.
Is it some uint16 uint256 problem? Should my constant maxNum be changed to uint16, is it impossible to call using ethers a function with a uint16 parameter? I have no idea how to pass in a uimt16 instead of a uint256 because I couldn't find how to declare parameter types in the ethers docs. It seems like everyone just uses numbers or strings so that's confusing (especially when I will later have to pass in an array.)
I figured it out!!! Turns out it was something in the
// do other stuff
part where I was causing an integer overflow.
Yayyy I'm so happy :):)

Is my Solidity code correct and safe to use

I am very new using Solidity to create Ethereum contracts and would like to know if the contract I have created is first correct and safe to use.
What I am doing in this contract is allowing the user to send some ETH from their wallet to a friends wallet but also take a $0.05 fee (194740000000000 wei at time of writing) for using my services.
Now when testing the contract using remix all works fine, but as I am new to solidity contracts I just wanted to get some expert opinions to make sure I am doing everything correctly and safety.
My contract code is:
pragma solidity ^0.5.1;
contract Forwarder {
address payable public receiversAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
address payable public feeAddress = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
constructor() payable public {
uint amountToSendReceiver = msg.value-194740000000000;
receiversAddress.transfer(amountToSendReceiver);
feeAddress.transfer(194740000000000);
}
}
As mentioned everything works fine in remix, but is my contract code correct and safe to use? There is no way to use the contract after to maybe steal funds or have any vulnerabilities?
Yes, your code is pretty safe. The only thing I would like to add is line with require condition for code maintainability
And if your vars are not changeable, its better to use constant to make your contract cheaper for deploy ;)
Functions can be declared constant in which case they promise not to
modify the state.
pragma solidity ^0.5.1;
contract Forwarder {
address payable public constant receiversAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
address payable public constant feeAddress = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
uint256 constant feeAmount = 194740000000000;
constructor() payable public {
require(msg.value >= feeAmount); // for maintainability, your tx will be reverted anyway, just to show why
feeAddress.transfer(feeAmount); // transfer fee
receiversAddress.transfer(address(this).balance); // transfer remaining amount of msg.value
}
}

Creating instance of contract inside another contract and calling it's methods results in thrown exception

I have 2 basic contracts: one is for token and the second is for sale.
Token сontract:
contract MyToken is StandardToken, Ownable {
string public constant name = "My Sample Token";
string public constant symbol = "MST";
uint32 public constant decimals = 18;
function MyToken(uint _totalSupply) {
require (_totalSupply > 0);
totalSupply = _totalSupply;
balances[msg.sender] = totalSupply;
}
}
Sale Contract
contract Sale {
address owner;
address public founderAddress;
uint256 public constant foundersAmount = 50;
MyToken public token = new MyToken(1000);
uint256 public issuedTokensAmount = 0;
function Sale() {
owner = msg.sender;
founderAddress = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;
token.transfer(founderAddress, foundersAmount);
}
function() external payable {
token.transfer(msg.sender, 1);
owner.transfer(msg.value);
}
}
StandardToken and Ownable are all standard implementations from OpenZeppelin repository. Full contract source is available here.
So basically in my Sale Contract I create an instance of my token contract with fixed supply and assign all of the tokens to the caller. Then I transfer some amount of tokens to founder address. When I try to send some ethereum to Sale contract I'm attempting to transfer some of my tokens to the sender (Running all code in Remix browser, I create an instance of Sale contract and call "fallback" method specifying some ether amount). However, this fails with "Exception during execution. (invalid opcode). Please debug the transaction for more information." message. All that I can see when debugging is that code fails in payable method at line:
token.transfer(msg.sender, 1);
I can't see the exact reason for this as I'm not able to step into this method and see whats going on inside.
Interesting thing is that when I remove a call to transfer method on token instance in Sale Contract constructor - code seems to run fine without any exceptions.
What am I missing?
I debugged into the the contract using remix, and the invalid opcode is thrown by:
290 DUP8
291 DUP1
292 EXTCODESIZE
293 ISZERO
294 ISZERO
295 PUSH2 012f
298 JUMPI
299 PUSH1 00
301 DUP1
302 INVALID
I left out the rest, but essentially it loads the address of the token contract and calls EXTCODESIZE which retrieves the contracts code size, and checks that it's not equal to 0 (the token contract exists), unfortunately, it did equate to 0. At this point, I'm unsure whether this is a limitation in remix or I've misunderstood the setup.
I tried the identical contract setup on truffle + testrpc and it deployed, accepted the currency successfully. Do note however that testrpc indicated:
Gas usage: 59137
Meaning that this is above the default sendTransaction w/ no data default (21,000 gas). This means that in a live environment, ensure that you inform users to include extra gas, otherwise the fallback function would probably fail due to OOG errors.
The reason behind this is that you're using a fallback function. Try using a normal function and it should happen.