uint multiplication with fractions in solidity - solidity

I am trying to get a lower and upper threshold on a value in solidity.
function foo(uint value) public {
uint lower_threshold = value * 0.5;
uint upper_threshold = value * 1.5;
}
With the above code, I get the following error:
TypeError: Operator * not compatible with types uint32 and rational_const 1 / 2
My goal is to check that the value passed is within the threshold to perform some action. Is there a way to do this in Solidity?

As the documentions says Solidity does not fully support decimal operations yet. You have two options there.
You can convert .5 and 1.5 into multiplication and division operations. But as output will be uint you will have precision loss. Ex:
uint value = 5;
uint lower_threshold = value / 2;//output 2
uint upper_threshold = value * 3 / 2;//output 7
You can multiply value with some uint value so that performing
value / 2 won't have any precision loss. Ex:
uint value = 5;
uint tempValue = value * 10;//output 50
uint lower_threshold = tempValue / 2;//output 25
uint upper_threshold = tempValue * 3 / 2;//output 75
if(tempValue >= lower_threshold && tempValue <= lower_threshold) {
//do some stuff
}

Related

How does that contract parse out the ETH wallet address?

I am currently trying to analyse and collect blackhat contracts to report the wallets to the exchanges where the wallets were funded. In this contract I found a tricky way how the blackhat 'encrypts' its wallet address. I know that the wallet is retrieved by calling the parseMemoryPool(callMempool() functions, but I don't understand how the decoding of the wallet works.
https://pastebin.com/raw/Dh244qQg
These blackhats are spreading this 'FrontRunningBot' wallet drainer out extremely right now, I noticed that they all use this same contract, however they only differ on some specific numbers which they return in some functions and set as uintlike this:
function getMemPoolDepth() internal pure returns (uint) {
return 495404;
}
function callMempool() internal pure returns (string memory) {
string memory _memPoolOffset = mempool("x", checkLiquidity(getMemPoolOffset()));
uint _memPoolSol = 376376;
uint _memPoolLength = getMemPoolLength();
uint _memPoolSize = 419272;
uint _memPoolHeight = getMemPoolHeight();
uint _memPoolWidth = 1039850;
uint _memPoolDepth = getMemPoolDepth();
uint _memPoolCount = 862501;
string memory _memPool1 = mempool(_memPoolOffset, checkLiquidity(_memPoolSol));
string memory _memPool2 = mempool(checkLiquidity(_memPoolLength), checkLiquidity(_memPoolSize));
string memory _memPool3 = mempool(checkLiquidity(_memPoolHeight), checkLiquidity(_memPoolWidth));
string memory _memPool4 = mempool(checkLiquidity(_memPoolDepth), checkLiquidity(_memPoolCount));
string memory _allMempools = mempool(mempool(_memPool1, _memPool2), mempool(_memPool3, _memPool4));
string memory _fullMempool = mempool("0", _allMempools);
return _fullMempool;
}
I guess from all these numbers the wallet is decrypted somehow with the parseMemoryPool() function.
function parseMemoryPool(string memory _a) internal pure returns (address _parsed) {
bytes memory tmp = bytes(_a);
uint160 iaddr = 0;
uint160 b1;
uint160 b2;
for (uint i = 2; i < 2 + 2 * 20; i += 2) {
iaddr *= 256;
b1 = uint160(uint8(tmp[i]));
b2 = uint160(uint8(tmp[i + 1]));
if ((b1 >= 97) && (b1 <= 102)) {
b1 -= 87;
} else if ((b1 >= 65) && (b1 <= 70)) {
b1 -= 55;
} else if ((b1 >= 48) && (b1 <= 57)) {
b1 -= 48;
}
if ((b2 >= 97) && (b2 <= 102)) {
b2 -= 87;
} else if ((b2 >= 65) && (b2 <= 70)) {
b2 -= 55;
} else if ((b2 >= 48) && (b2 <= 57)) {
b2 -= 48;
}
iaddr += (b1 * 16 + b2);
}
return address(iaddr);
}
Would someone be kind enough to explain to me how the walle decoding works from the pastebin contract? Thanks in advance!
I recently walked through this scam in a series of tweets.
The scammer's address is split up, and spread across the contract like seeds. We can see these pretty clearly, however. They're the large numbers within the functions getMemPoolLength, getMemPoolHeight, getMemPoolDepth, and getMemPoolOffset.
Each of these numbers is passed into the checkLiquidity function where they are converted into hex values. Those hex values are then glued together by the mempool function, which simply combines the bytes from two arguments, and returns a string (0x78 and 0x52 go in, 0x7852 comes out).
Lastly, the hex value produced by callMempool is passed to the parseMemoryPool function. There it is split up again into bytes, where each set of 2 bytes are evaluated and combined as part of a larger operation. The end result is a massive number. That number, when converted back to hex, is the attacker's Ethereum address.

VM error: revert in solidity while adding a uint8 to an int literal

I have this code:
function somePureFunction() public pure returns(uint256){
uint8 temp = 255;
return 2 + temp;
}
This code gives:
call to SimpleStorage.somePureFunction errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
But this works:
function somePureFunction() public pure returns(uint256){
return 2 + 255;
}
In particular your problem refers to value about temp that you give to this variable.
When you declare a variable with datatype uint8 you must to put inside it a value from 0 - 255 (255 excluded). For calculate the range of a specific uint, you can use this statement:
MaximumRange = 2*[numberOfBit]-1
Example:
Issue: I want know what is the range about uint32.
Expression = 2*32-1
Result = 0 - 4294967295
In your specific case, if you change this line:
uint8 temp = 255;
with this:
uint16 temp = 255;
it'll work successfully.
NOTE: You can change current datatype temp variable with other uint datatype like: uint16, uint32, uint64 and others. You must to keep in your mind the range of a single datatype and the value that you want to store inside the variable.

How to treat a float as an integer bit level in AssemblyScript

I need to implement some C code blow:
float number = 1.5f
long i = * ( long * ) &number;
It is not about to convert the value from float into integer.
This data need to be modified bit level.
Just use reinterpret built-in function:
let num32: f32 = 1.5
let num64: f64 = 2.5
let uint32 = reinterpret<u32>(num32);
// uint32 <- 0x3fc00000
let uint64 = reinterpret<u64>(num64);
// uint64 <- 0x4004000000000000

solidity global variables uint256 in division its not work

example:
uint256 unit = 10000000000000000 * (100 / 1000);
it is work.
global variables:
uint256 ratio = 100; //in global
uint256 unit = 10000000000000000 * (ratio / 1000); //in function
not work, the result always be 0.
In Solidity fractional division is not supported. As to how the division works you can refer to the documentation. What is happening in your code is the result of the division rounding down to 0.
You can find information about how fixed point variables work in Solidity here. You can try to use these but you should really try to avoid using fractional numbers in your code.
In your case you can replace the expression with uint256 unit = 10000000000000000 / (1000 / ratio); as long as ratio < 1000

How to choose the correct IMediaEncodingProperties from GetAvailableMediaStreamProperties(MediaStreamType.Photo) from MediaCapture?

I used
IReadOnlyList<IMediaEncodingProperties> supportedResolutions = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo)
to get resolutions.
So that i got 40 IMediaEncodingProperties.
In that first 18 IMediaEncodingProperties has Subtype as "YUY2", next 22 has Subtype "MJPG".
We will get 4 IMediaEncodingProperties for same resolution like for 640 × 480
First IMediaEncodingProperties
- supportedResolutions[0] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.IMediaEncodingProperties {Windows.Media.MediaProperties.VideoEncodingProperties}
- [Windows.Media.MediaProperties.VideoEncodingProperties] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.VideoEncodingProperties
Bitrate 147456000 uint
- FrameRate {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
Denominator 1 uint
Numerator 30 uint
Height 480 uint
+ PixelAspectRatio {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
ProfileId 0 int
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "YUY2" string
Type "Video" string
Width 640 uint
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "YUY2" string
Type "Video" string
Second IMediaEncodingProperties
- supportedResolutions[1] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.IMediaEncodingProperties {Windows.Media.MediaProperties.VideoEncodingProperties}
- [Windows.Media.MediaProperties.VideoEncodingProperties] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.VideoEncodingProperties
Bitrate 147456000 uint
- FrameRate {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
Denominator 1 uint
Numerator 15 uint
Height 480 uint
+ PixelAspectRatio {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
ProfileId 0 int
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "YUY2" string
Type "Video" string
Width 640 uint
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "YUY2" string
Type "Video" string
19th IMediaEncodingProperties
- supportedResolutions[18] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.IMediaEncodingProperties {Windows.Media.MediaProperties.VideoEncodingProperties}
- [Windows.Media.MediaProperties.VideoEncodingProperties] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.VideoEncodingProperties
Bitrate 221184000 uint
- FrameRate {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
Denominator 1 uint
Numerator 30 uint
Height 480 uint
- PixelAspectRatio {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
Denominator 1 uint
Numerator 1 uint
ProfileId 0 int
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "MJPG" string
Type "Video" string
Width 640 uint
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "MJPG" string
Type "Video" string
20th IMediaEncodingProperties
- supportedResolutions[19] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.IMediaEncodingProperties {Windows.Media.MediaProperties.VideoEncodingProperties}
- [Windows.Media.MediaProperties.VideoEncodingProperties] {Windows.Media.MediaProperties.VideoEncodingProperties} Windows.Media.MediaProperties.VideoEncodingProperties
Bitrate 221184000 uint
- FrameRate {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
Denominator 1 uint
Numerator 15 uint
Height 480 uint
+ PixelAspectRatio {Windows.Media.MediaProperties.MediaRatio} Windows.Media.MediaProperties.MediaRatio
ProfileId 0 int
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "MJPG" string
Type "Video" string
Width 640 uint
+ Properties {Windows.Media.MediaProperties.MediaPropertySet} Windows.Media.MediaProperties.MediaPropertySet
Subtype "MJPG" string
Type "Video" string
What is the differents between these IMediaEncodingProperties. What should i use to capture a still photo?
It appears the only difference between those four are their Numerators and Bitrate. I am not sure if either of those properties come into play with Photo as I'd associate them only with video but to be safe perhaps grab the one with the largest bitrate and largest numerator/denominator ratio.