I am using the Etherscan API to download the Solidity code for certain verified contracts. Whilst this does work to an extent, I lose the newline characters which I need to keep.
For instance, consider the code for the verified contract on https://etherscan.io/address/0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1#code
A snippet of the Solidity code for this contract is:
pragma solidity ^0.4.18;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
more code....
Ultimately, I want the Solidity code to be stored as a string including the newlines, ie I want
str_a <- "pragma solidity ^0.4.18;\\n\\n/**\\n* @title SafeMath\\n* @dev Math operations with safety checks that throw on error\\n*/\\nlibrary SafeMath {\\nfunction mul(uint256 a, uint256 b) internal pure returns (uint256) {\\nuint256 c = a * b;\\nassert(a == 0 || c / a == b);\\nreturn c;\\n}\\nfunction div(uint256 a, uint256 b) internal pure returns (uint256) {\\n// assert(b > 0); // Solidity automatically throws when dividing by 0\\nuint256 c = a / b;\\n// assert(a == b * c + a % b); // There is no case in which this doesn't hold\\nreturn c;\\n}"
However, the code I have written produces
pragma solidity ^0.4.18;/** * @title SafeMath * @dev Math operations with safety checks that throw on error */library SafeMath { function mul(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a * b; assert(a == 0 || c / a == b); return c; } function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; }
My code to produce the output is below, where gstr_api_key is a global string defined earlier in the code to represent the API key:
str_address <- "0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1"
# Build string needed for URL
str_APIAction <- "/api?module=contract&action=getsourcecode&address="
str_APIKey <- paste("&apikey=", gstr_api_key, sep="")
str_path <- paste(str_APIAction, str_address, str_APIKey, sep="")
# Send request to API
raw_result <- httr::GET(url = gstr_eth_url, path = str_path)
# Convert response in to useful
raw_char <- rawToChar(raw_result$content)
raw_list <- jsonlite::fromJSON(raw_char)
str_a <- raw_list$result[1]
I have limited knowledge of the JSON format, but I understand that newlines are not permitted in JSON. Is that correct?
How can I modify my code so that I obtain the desired output?
I've solved the problem - can't be done using the API. Instead scrapped the page using httr and XML libraries. Easy enough in the end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.