solidity : bytecode optimization : require( condition ) issue - optimization

I have a solidity code which uses an optimization=200 in remix.
I'm suspicious if optimization is buggy on my code.
when I use for example :
mapping(address => uint8) allowedUsers;
function doSomething(address wallet) external {
...
require( allowedUsers[wallet]!=1, "User already set" );
allowedUsers[wallet]=1;
...
if I run it on binance mainnet or under rinkeby, there is no issue.
But sometimes randomly when used on ethereum mainnet I'm getting
revert error "User already set".
What is strange, It occurs on the first function call for a wallet. But not always.
It seems mapping & require have issues together. because it can occur on another require with mapping not just as on this example.
I cannot disable optimization since bytecode is nearly 25400 bytes out of 25476 allowed.
Any idea what's wrong?
I'm using solidity 0.8.15

The issue was something completely different, and was happening in the ethereum mempool.
I understand what happened : I've been front-runned by a MEV BOT. It detected an ETH transaction was sent from my smartcontract, and sent a copy-cat of the transaction earlier than mine.
I've put counter-actions to avoid it. it's okay now.

Related

Get pool balances from BalancerV2 in Solidity

I am trying to inspect the pool balances of a decentralized exchange called Balancer. The implementation is done with the main version V2.
I want to get the pool balance with
vault.getPoolTokens(0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014);
The contract looks like this:
contract BalancerV2 {
IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
function getPoolTokens() public view returns(uint) {
return vault.getPoolTokens(0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014);
}
}
But it gives me the following error message:
from solidity:
contracts/BalancerV2.sol:8:37: TypeError: Type literal_string "0xBA12222222228d8Ba445958a75a0704d566BF2C8" is not implicitly convertible to expected type contract IVault.
IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
^------------------------------------------^
My question is:
How is it possible that even in the official GitHub repository they have a working example of how to initialize the vault with solidity compiler version 0.7.0 and I'm literally trying the same with getting this error?
By the way, the example in the documentation has a typo so it won't compile. Just in case if someone from the devs sees this.
https://dev.balancer.fi/resources/pool-interfacing#pool-balances
I am trying to compile this contract and am expecting to get this result:
tokens: [0xba100000625a3754423978a60c9317c58a424e3D,
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2]
balances: [5720903090084350251216632,
7939247003721636150710]
Instead of:
IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
try this:
IVault private constant vault = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);

what is the difference between these two ethers.js functions [duplicate]

Can somebody please point me to the documentation (official or otherwise ) that explains the function ethers.getContractAt():
the original context of this is as follows:
vrfCoordinator = await ethers.getContractAt('VRFCoordinatorMock', VRFCoordinatorMock.address, signer)
and the full code can be found here...
https://github.com/PatrickAlphaC/all-on-chain-generated-nft/blob/main/deploy/02_Deploy_RandomSVG.js
In the absence of such documentation, an explanation would be much appreciated. Thank you!
The getContractAt() function is part of the hardhat-ethers plugin for Hardhat, expanding the ethers object.
It's not a part of the core Ethers package, so it's not included in their documentation.
Hardhat docs mentioning the plugin: https://hardhat.org/plugins/nomiclabs-hardhat-ethers.html#helpers
Basically, it is doing the same thing, that attach do but in one line. i.e you can actually interact with an already deployed contract.
For reference below is the code if you are using attach
let xyzContract = await hre.ethers.getContractFactory("Name of the contract");
let xyzContractInstance = xyzContract.attach('Address of the contract');
You can accomplish the same thing in one line via getContractAt() when using hardhat
let xyzContractInstance = await hre.ethers.getContractAt(
"Contract Address",
deployed contract address
);

Raku Cro service subscribing to data "in the background" general guidance

I am attempting to put together a Cro service that has a react/whenever block consuming data "in the background" So unlike many examples of websocket usage with Cro, this has nothing to do with routes that may be accessed via the browser.
My use case is to consume message received via an MQTT topic and do some processing with them. At a later stage in development I might create a supply out of this data, but for now, when data is received it will be stored in a variable and dependant on certain conditions, be sent to another service via a http post.
My thought was to include a provider() in the Cro::HTTP::Server setup like so:
use Cro::HTTP::Log::File;
use Cro::HTTP::Server;
use Routes;
use DataProvider; # Here
my Cro::Service $http = Cro::HTTP::Server.new(
http => <1.1>,
host => ...,
port => ...,
application => [routes(), provider()], # Made this into an array of subs?
after => [
Cro::HTTP::Log::File.new(logs => $*OUT, errors => $*ERR)
]
);
And in the DataProvider.pm6:
use MQTT::Client;
sub provider() is export {
my $mqtt = MQTT::Client.new: server => 'localhost';
react {
whenever $mqtt.subscribe('some/mqtt/topic') {
say "+ topic: { .<topic> } => { .<message>.decode("utf8-c8") }";
}
}
}
This throws a bunch of errors:
A react block:
in sub provider at DataProvider.pm6 (DataProvider) line 5
in block <unit> at service.p6 line 26
Died because of the exception:
Invocant of method 'write' must be an object instance of type
'IO::Socket::Async', not a type object of type 'IO::Socket::Async'. Did
you forget a '.new'?
in method subscribe at /home/cam/raku/share/perl6/site/sources/42C762836A951A1C11586214B78AD34262EC465F (MQTT::Client) line 133
in sub provider at DataProvider.pm6 (DataProvider) line 6
in block <unit> at service.p6 line 26
To be perfectly honest, I am totally guessing that this is how I would approach the need to subscribe to data in the background of a Cro service, but I was not able to find any information on what might be considered the recommended approach.
Initially I had my react/whenever block in the main service.pm6 file, but that did not seem right. And needed to be wrapped in a start{} block because as I have just learned, react is blocking :) and cro was not able to actually start.
But following the pattern of how Routes are implemented seemed logical, but I am missing something. The error speaks about setting up a new method, but I'm not convinced that is the root cause. Routes.pm6 does not have a constructor.
Can anyone point me in the right direction please?
Thanks to all who have provided information, this has been a very valuable learning exercise.
The approach of passing additional sub routines, along side router() in the application parameter to Cro::HTTP::Server.new gave further trouble. (an array is not allowed, and broke routing)
Instead, I have moved the background work into a class of it's own, and given it a start and stop method more akin to Cro::HTTP::Server.
My new approach:
service.pm6
use Cro::HTTP::Log::File;
use Cro::HTTP::Server;
use Routes;
use KlineDataSubscriber; # Moved mqtt functionality here
use Database;
my $dsn = "host=localhost port=5432 dbname=act user=.. password=..";
my $dbh = Database.new :$dsn;
my $mqtt-host = 'localhost';
my $subscriber = KlineDataSubscriber.new :$mqtt-host;
$subscriber.start; # Inspired by $http.start below
my Cro::Service $http = Cro::HTTP::Server.new(
http => <1.1>,
host => ...,
port => ...,
application => routes($dbh), # Basically back the way it was originally
after => [
Cro::HTTP::Log::File.new(logs => $*OUT, errors => $*ERR)
]
);
$http.start;
say "Listening at...";
react {
whenever signal(SIGINT) {
say "Shutting down...";
$subscriber.stop;
$http.stop;
done;
}
}
And in KlineDataSubscriber.pm6
use MQTT::Client;
class KlineDataSubscriber {
has Str $.mqtt-host is required;
has MQTT::Client $.mqtt = Nil;
submethod TWEAK() {
$!mqtt = MQTT::Client.new: server => $!mqtt-host;
await $!mqtt.connect;
}
method start(Str $topic = 'act/feed/exchange/binance/kline-closed/+/json') {
start {
react {
whenever $!mqtt.subscribe($topic) {
say "+ topic: { .<topic> } => { .<message>.decode("utf8-c8") }";
}
}
}
}
method stop() {
# TODO Figure how to unsubscribe and cleanup nicely
}
}
This feels much more "Cro idiomatic" to me, but I would be happy to be corrected.
More importantly, it works as expected and I feel is somewhat future proof. I should be able to create a supply to make real-time data available to the router, and push data to any connected web clients.
I also intend to have a http GET endpoint /status with various checks to ensure everything healthy
The root cause
The error speaks about setting up a new method, but I'm not convinced that is the root cause.
It's not about setting up a new method. It's about a value that should be defined instead being undefined. That typically means a failure to attempt to initialize it, which typically means a failure to call .new.
Can anyone point me in the right direction please?
Hopefully this question helps.
Finding information on a recommended approach
I am totally guessing that this is how I would approach the need to subscribe to data in the background of a Cro service, but I was not able to find any information on what might be considered the recommended approach.
It might be helpful for you to list which of the get-up-to-speed steps you've followed from Getting started with Cro, including the basics but also the "Learn about" steps at the end.
The error message
A react block:
in sub provider ...
Died because of the exception:
...
in method subscribe ...
The error message begins with the built in react construct reporting that it caught an exception (and handled it by throwing its own exception in response). A "backtrace" corresponding to where the react appeared in your code is provided indented from the initial "A react block:".
The error message continues with the react construct summarizing its own exception (Died because ...) and explains itself by reporting the original exception, further indented, in subsequent lines. This includes another backtrace, this time one corresponding to the original exception, which will likely have occurred on a different thread with a different callstack.
(All of Raku's structured multithreading constructs[1] use this two part error reporting approach for exceptions they catch and handle by throwing another exception.)
The first backtrace indicates the react line:
in sub provider at DataProvider.pm6 (DataProvider) line 5
use MQTT::Client;
sub provider() is export {
my $mqtt = MQTT::Client.new: server => 'localhost';
react {
The second backtrace is about the original exception:
Invocant of method 'write' must be an object instance of type
'IO::Socket::Async', not a type object of type 'IO::Socket::Async'. ...
in method subscribe at ... (MQTT::Client) line 133
This reports that the write method called on line 133 of MQTT::Client requires its invocant is an instance of type 'IO::Socket::Async'. The value it got was of that type but was not an instance, but instead a "type object". (All values of non-native types are either type objects or instances of their type.).
The error message concludes with:
Did you forget a '.new'?
This is a succinct hint based on the reality that 99 times out of a hundred the reason a type object is encountered when an instance is required is that code has failed to initialize a variable. (One of the things type objects are used for is to serve the role of "undefined" in languages like Perl.)
So, can you see why something that should have been an initialized instance of 'IO::Socket::Async' is instead an uninitialized one?
Footnotes
[1] Raku's constructs for parallelism, concurrency, and asynchrony follow the structured programming paradigm. See Parallelism, Concurrency, and Asynchrony in Raku for Jonathan Worthington's video presentation of this overall approach. Structured constructs like react can cleanly observe, contain, and manage events that occur anywhere within their execution scope, including errors such as error exceptions, even if they happen on other threads.
You seem to be fine now but when I first saw this I made this https://github.com/jonathanstowe/Cro-MQTT which turns the MQTT client into a first class Cro service.
I haven't released it yet but it may be instructive.

Problem with Prefix in Discord.js (including .toUpperCase())

Basically, I've been developing a bot for several weeks now using the discord.js library and recently encountered a small but crucial issue. Essentially when I declare my argument, I also made it so that the message content (message.content) would be capitalized using .toUpperCase(). Essentially in doing so regardless of the type of prefix you would enter (symbol wise) it would all be read by the program as valid.
For example only: !help - should work, however if I would enter .help, it would also be read as valid.
In any case, here's the code. I appreciate all the help!
bot.on('message', message =>{
let args = message.content.toUpperCase().substring(PREFIX.length).split(" ");
const sender = message.member;
switch(args[0])
{
case 'HELP':
message.reply("I've sent you some documentation on all the commands that you can use...").then(d_msg => {d_msg.delete(3000); });
message.delete(3000);
const attachment = new Attachment('./UtilityBot_Documentation.txt')
message.author.send('[Education] Bot - Documentation');
message.author.send(attachment);
break;
}
})
The discord.js tutorial covers an extremely similar problem to what you're trying to do. I recommend you check it out. The page I linked in specific is doing a very similar thing to you, but it's worth giving the whole thing a read through if you haven't done so already. In general, I would include the following line just above where you established args.
if (!message.content.startsWith(PREFIX)) return;
What I'm doing here is saying if the message does not start with the prefix, stop running until a new message is sent. I might be missing something, but certainly check out the tutorial. It's really well written.
https://discordjs.guide/creating-your-bot/commands-with-user-input.html#basic-arguments

How to update deployed Ethereum smart contract with ABI only through console (Truffle)

I need to update value on deployed contract ( I dont have sol neither it has been deployed on my platform )
I want to update param with setter in contract and call getter to see changed value .. always default value comes back!
here is the code Im using :
new web3.eth.Contract(HelloWorld.abi,'0x085Ab4C596535FFCE5B520D277f1C01236a656CB').methods.setMessage('Hi').call()
=> Result {}
then call :
new web3.eth.Contract(HelloWorld.abi,'0x085Ab4C596535FFCE5B520D277f1C01236a656CB').methods.getMessage().call()
=>
'hello world10' 🤔
Im using Truffle console:
truffle console --network ropsten
one comment if I use :
HelloWorld.deployed().then(instance => instance.setMessage('new Hi').call())
then call:
HelloWorld.deployed().then(instance => instance.getMessage.call()).then(result => message = result)
then I got expected value .. but I don't have deployed contract. neither sol. all what I have part ABI
Is it wrong with my calls that should be async .. or I need to redeploy contract ?
Im still new in this crypto-world 🙂
welcome to the "crypto-world" as you named it.
I've got a bit lost on your question but what you want is to deploy a contract with an initial message and then change that message using setMessage method, all through the console.
First thing you are doing wrong is calling the "setMessage" method using .call(). When you want to change something in the blockchain, you will send a transaction, and to send a transaction you need to specify an account. The correct method should be .send({ from: "you-account-address" }).
In order to simplify a bit, let me just show a few lines that you can use and then explain
$ > truffle console --network ropsten
$ (truffle) > helloInstance = await HelloWorld.deployed()
$ (truffle) > await helloInstance.getMessage()
$ (truffle) > await helloInstance.setMessage("new Hi", { from: "you-account-address" })
So, basically, all calls are async. First, you call HelloWorld.deployed() because when you deployed the contracts with truffle, truffle created a folder named "build" where it saved the contracts with all the necessary data, that's why you don't need to specify the address neither the abi. This is totally different from using web3.js in which you need to specify both.
Then, you can just call the methods.
You can find more detailed information here https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts