Foundry not outputting console.log (solidity) - solidity

Followed docs at https://book.getfoundry.sh/reference/forge-std/console-log
Tests are passing but console.log is not doing anything. I excepted it would print the output in the terminal.
Test contract is as follows:
pragma solidity =0.8.17;
import "forge-std/console.sol";
import "forge-std/Test.sol";
contract ProjectTest is Test {
ProjectRouter router;
function setUp() public {
router = new ProjectRouter();
console.log("DONE"); // NOT WORKING
}
function test() public { // PASSED
string memory name = router.name();
assertEq(router.name(), "Router");
}
function test123() public { // PASSED
console.log(123); // NOT WORKING
}
}
Tried
contract ProjectTest is console, Test
But that gave more than 256 errors which look like this:
error[9097]: DeclarationError: Identifier already declared.
--> lib/forge-std/src/console.sol:2090:3:
|
2090 | function log(bool p0, bool p1, bool p2, uint p3) internal view {
| ^ (Relevant source part starts here and spans across multiple lines).
Note: The previous declaration is here:
--> lib/forge-std/lib/ds-test/src/test.sol:19:5:
|
19 | event log (string);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When typing console.log, foundry will give you list of types that you can use,
for example, to print out string "DONE", use
console.logString("DONE");
To print out number you wrote everything right, exept, forgot about ""
console.log("123");
For more info check
https://github.com/TateB/foundry-docs/blob/master/src/reference/forge-std/console-log.md

Related

Testing ChainlinkClient callbacks - how to bypass recordChainlinkFulfillment?

I've got a Chainlink client contract which makes a DirectRequest to an oracle. The oracle does its thing and then returns the answer via the typical callback selector passed in via the ChainlinkRequest. It all works well, but I'd like to write some tests that test the callback implementation
My client contract is as follows:
contract PriceFeed is Ownable, ChainlinkClient {
function updatePrice() onlyOwner returns (bytes32 requestId) {
// makes Chainlink request specifying callback via this.requestCallback.selector
}
function requestCallback(bytes32 _requestId, uint256 _newPrice) public
recordChainlinkFulfillment(_requestId) {
price = _newPrice;
}
}
The problem arises when the test code calls requestCallback(...) and the code hits the recordChainlinkFulfillment(...) modifier. The ChainlinkClient complains that the requestId being passed in by the test below isn't in the underling private pendingRequests mapping maintained by the ChainlinkClient.
The simplified version of ChainlinkClient looks like this:
contract ChainlinkClient {
mapping(bytes32 => address) private pendingRequests;
modifier recordChainlinkFulfillment(bytes32 _requestId) {
require(msg.sender == pendingRequests[_requestId], "Source must be the oracle of the request");
delete pendingRequests[_requestId];
emit ChainlinkFulfilled(_requestId);
_;
}
}
My Foundry/Solidity test is as follows:
contract PriceFeedTest is Test {
function testInitialCallback() public {
priceFeed.requestCallback("abc123", 1000000); // fails on this line
assertEq(1000000, priceFeed.price(), "Expecting price to be 1000000");
}
}
The code fails on first line of the testInitialCallback() line with: Source must be the oracle of the request
How can I trick the ChainklinkClient into allowing my callback to get past the modifier check? AFAIK I can't access and pre-populate the private pendingRequests mapping. Is there another way?
I know that Foundry provides Cheatcodes to help in testing and there's a stdstorage cheatcode, but I'm not familiar on how to construct a call to stdstorage to override pendingRequests if thats even possible with a cheatcode.
contract PriceFeedTest is Test {
function testInitialCallback2() public {
stdstore
.target(address(priceFeed))
.sig("pendingRequests()")
.with_key("abc123")
.checked_write(address(this));
priceFeed.requestCallback("abc123", 1000000);
assertEq(1000000, priceFeed.price(), "Expecting price to be 1000000");
}
}
The above code throws the following error: No storage use detected for target
Any help would be greatly appreciated. Many thanks.
When you execute the updatePrice function in your test, you should be able to strip out the requestId from the transaction receipt event. Once you have that, you can then use it in your call to requestCallback. Check out this example unit test from the hardhat starter kit for an example of this

Chainlink request not returning the integer at the followed path

I have a very simple smart contract which creates and sends a Chainlink request to the Kovan Linkpool node using the get>uint256 job. The contract looks like this (API private key removed).
contract OracleChainlink is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public H_Index;
address private Oracle;
bytes32 private JobId;
uint256 private Fee = .1 * 10 ** 18; //kovan is .1 link per call
constructor() public {
setPublicChainlinkToken();
Oracle = 0x56dd6586DB0D08c6Ce7B2f2805af28616E082455; //Chainlink linkpool node on Kovan
JobId = "b6602d14e4734c49a5e1ce19d45a4632";
}
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
function RequestH_index() public returns (bytes32 Reqid) {
Chainlink.Request memory Req = buildChainlinkRequest(JobId, address(this), this.fulfill.selector);
Req.add("get", "https://serpapi.com/.....");
Req.add("path", "cited_by.table.1.h_index.all");
return sendChainlinkRequestTo(Oracle, Req, Fee);
}
function fulfill(bytes32 Reqid, uint256 _Hindex) public recordChainlinkFulfillment(Reqid) {
H_Index = _Hindex;
}
The Google Scholar Author API https://serpapi.com/google-scholar-author-api returns a pretty large json, seen at the link if you scroll down. The snippet/path I need to follow is shown below (cited_by is at the top level of the json).
"cited_by": {
"table": [
{
"citations": {
"all": 23351,
"since_2016": 13660
}
},
{
"h_index": {
"all": 46,
"since_2016": 37
}
},
{
"i10_index": {
"all": 60,
"since_2016": 53
}
}
],
When ran, I get logs of Chainlink request events, but the public H_Index value remains 0. Am I missing something in terms of adapters? I have tried all sorts of path formats through the JSON with no luck. I have also tried different nodes and jobs. Is there any way to ensure that the API is even being called? What am I missing?
Your JSON Path should look like this:
Req.add("path", "cited_by.table.1.h_index.all");
Looking at the Etherscan activity, it looks like the node you are using may be inactive. Try this node and jobId:
Oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
JobId = "d5270d1c311941d0b08bead21fea7747";
These were taken from the Chainlink Official Docs.
To check to see if a node is running or not, check out the oracle address in a block explorer. You can see here that the original node you tried to use hasn't posted a transaction in quite a long time.
If a node is inactive you will need to find a new one or host one yourself. To find more nodes and jobs, you can check market.link or use the one found in the docs as mentioned earlier.

What parameter should I feed to Frida `ObjC.api.class_addMethod()` to make it happy?

I want to use Frida to add a class method to the existing Objective C class on Mac OS. After I read the Frida docs, I tried the following code:
const NSString = ObjC.classes.NSString
function func (n) { console.log(n) }
var nativeCb = new NativeCallback(func, 'void', ['int'])
ObjC.api.class_addMethod(
NSString.handle,
ObjC.selector('onTest:'),
nativeCb,
ObjC.api.method_getTypeEncoding(nativeCb)
)
The above code looks straightforward. However, after the ObjC.api.class_addMethod() call, the attached App and the Frida REPL both froze, it looks that the pointers are not right.
I have tried many possible parameter values for a whole night but still can figure the problem out. What's wrong with my code?
Only two issues:
method_getTypeEncoding() can only be called on a Method, which the NativeCallback is not. You could pass it the handle of an existing Objective-C method that has the same signature as the one you're adding, or use Memory.allocUtf8String() to specify your own signature from scratch.
Objective-C methods, at the C ABI level, have two implicit arguments preceding the method's arguments. These are:
self: The class/instance the method is being invoked on.
_cmd: The selector.
Here's a complete example in TypeScript:
const { NSAutoreleasePool, NSString } = ObjC.classes;
const onTest = new NativeCallback(onTestImpl, "void", ["pointer", "pointer", "int"]);
function onTestImpl(selfHandle: NativePointer, cmd: NativePointer, n: number): void {
const self = new ObjC.Object(selfHandle);
console.log(`-[NSString onTestImpl]\n\tself="${self.toString()}"\n\tn=${n}`);
}
function register(): void {
ObjC.api.class_addMethod(
NSString,
ObjC.selector("onTest:"),
onTest,
Memory.allocUtf8String("v#:i"));
}
function test(): void {
const pool = NSAutoreleasePool.alloc().init();
try {
const s = NSString.stringWithUTF8String_(Memory.allocUtf8String("yo"));
s.onTest_(42);
} finally {
pool.release();
}
}
function exposeToRepl(): void {
const g = global as any;
g.register = register;
g.test = test;
}
exposeToRepl();
You can paste it into https://github.com/oleavr/frida-agent-example, and then with one terminal running npm run watch you can load it into a running app using the REPL: frida -n Telegram -l _agent.js. From the REPL you can then call register() to plug in the new method, and test() to take it for a spin.

Is this a remix,compiler or an openzeplin bug?

I just wrote a simple code to test openzeplin Safemath library. I am using the latest version of remix ide and compiling for ^0.5.0.
Remix is using 0.5.0_commit.1d4f565a compiler
The environment is JavaScript VM
EVM Version is the compiler default
The add function does not seem to be working in the code given below
I have tried x.sub(1) it throws an exception as expected, i have also tried initializing x to different values but still does not work.
pragma solidity ^0.5.0;
import "./SafeMath.sol";
contract SimpleStorage {
using SafeMath for uint;
uint x;
event incremented(uint x);
constructor() public{
x=0;
}
function increment() public {
x.add(1);
emit incremented(x);
}
function get() external view returns (uint) {
return x;
}
}
Expected output is an increment by one on every call to the function but getting the same value every time. Emit also shows the same value.
Well, it's your bug :)
Instead of x.add(1) try x = x.add(1). Add function is not inplace, new value is returned and you need to assign the new value to x.

Example Oraclize files return 0: string: when called in Remix

I want to use Oraclize in Remix, to test it. I'm too stupid to use their examples.
How can I make this work?
From their Github I took the YouTube-Views code and copied it into Remix
pragma solidity >= 0.5.0 < 0.6.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract YoutubeViews is usingOraclize {
string public viewsCount;
event LogYoutubeViewCount(string views);
event LogNewOraclizeQuery(string description);
constructor()
public
{
update(); // Update views on contract creation...
}
function __callback(
bytes32 _myid,
string memory _result
)
public
{
require(msg.sender == oraclize_cbAddress());
viewsCount = _result;
emit LogYoutubeViewCount(viewsCount);
// Do something with viewsCount, like tipping the author if viewsCount > X?
}
function update()
public
payable
{
emit LogNewOraclizeQuery("Oraclize query was sent, standing by for the answer...");
oraclize_query("URL", 'html(https://www.youtube.com/watch?v=9bZkp7q19f0).xpath(//*[contains(#class, "watch-view-count")]/text())');
}
}
When I use the viewCount it returns:
0: string:
This happens with all the other examples aswell.
With WolframAlpha eg. I also get the following error:
transact to WolframAlpha.update errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value. Debug the transaction to get more information.
Ok you don't see the answer like a normal result in Remix:
You have to go under settings and open the Oraclize plug in.
If you then deploy the contract and or click update, you get the result shown in the plug in.