I have a function in my Solidity which takes a struct as first parameter:
function mintGenesisAnimal(AnimalInfo memory _animalInfo, string memory _tokenUri) onlyOwner public {
// ...
}
but when I'm trying to test this function with the code below:
const animalInfo = {
raceName: 'Tiger',
}
await Animal.mintGenesisAnimal(animalInfo, 'https://google.com')
The test fails with a weird issue relative to "BigNumber":
Error: invalid BigNumber value (argument="value", value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.6.0)
Why is a BigNumber reported when I'm trying to pass a struct (js object) as parameter ?
Related
I am learning the tutorials in 「Mastering Ethereum: Building Smart Contracts and DApps」(O'Reilly)
I copied the following sample code and created a solidity contract(METoken.sol).
Next, I compiled it with the「truffle compile」 command, but it gave me an error.Please assist, thanks
//Error Message
TypeError: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
// METoken.sol
pragma solidity ^0.4.21;
import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
contract METoken is StandardToken {
string public constant name = 'Mastering Ethereum Token';
string public constant symbol = 'MET'; uint8 public constant decimals = 2; uint constant
_initial_supply = 2100000000;
function METoken() public {
totalSupply_ = _initial_supply;
balances[msg.sender] = _initial_supply;
emit Transfer(address(0), msg.sender, _initial_supply);
}
}
The Standard token was renamed to ERC20.sol in openzeppelin contracts 2.0 version.
You have to modify your imports , then the contract will get compile.
Refer..
https://github.com/OpenZeppelin/openzeppelin-contracts/issues/1557
In your contract, if you have method that receives an Enum type, how would you pass the arguments from hardhat script?
contract SomeContract {
enum WinStatus {
PENDING,
LOST,
WON
}
WinStatus status;
function updateWinStatus(WinStatus _status) public {
status = _status;
}
}
// in your hardhat script
...
await someContract.updateWinStatus() // how should i call it. bare in mind hardhat is setup using javascript not typescript in my case.
i tried passing a number, hoping it will get it by order(index). But I am getting 'invalid BigNumber value'. Also, I tried passing a string like "PENDING" or "WinType.PENDING" :thinking:
Javascript natively doesn't support very large numbers (up to the uint256 type supported in Solidity), so Ethers.js (included in Hardhat) accepts a BigNumber instance instead.
const myNumber = ethers.BigNumber.from("0") // pass the numeric value as a string
await someContract.updateWinStatus(myNumber) // pass the BigNumber instance
I am trying to create mapping of type mapping(string => string) where the you store some text by keyed by its the string representation of its hash, but I've been stymied by the inability to take the calculated hash value and convert it to its string representation.
I tried the below, but it doesn't work. The hashing function appears to work, but the conversion to string doesn't. (Running function hash() returns an error which I don't really understand.
pragma solidity 0.8.4;
contract HashTextMap {
mapping(string=>string) textMap;
function set(string memory text) public {
bytes32 val;
val = sha256(abi.encodePacked(text));
string memory key = string(abi.encodePacked(val));
textMap[key] = text;
}
function get(string memory key) public view returns(string memory) {
return textMap[key];
}
function hash(string memory text) public pure returns(string memory) {
bytes32 val;
val = sha256(abi.encodePacked(text));
string memory key = string(abi.encodePacked(val));
return key;
}
}
Running this in the remix ide, the contract compiles and sets returns normally, but attempting I can't text get because I can't get the hash using hash() which produces this error.
{ "error": "Failed to decode output: null: invalid codepoint at offset 0; unexpected continuation byte (argument=\"bytes\", value={\"0\":153,\"1\":168,\"2\":124,\"3\":145,\"4\":53,\"5\":23,\"6\":70,\"7\":37,\"8\":43,\"9\":238,\"10\":126,\"11\":38,\"12\":250,\"13\":191,\"14\":48,\"15\":2,\"16\":61,\"17\":234,\"18\":227,\"19\":36,\"20\":138,\"21\":6,\"22\":125,\"23\":166,\"24\":226,\"25\":63,\"26\":146,\"27\":129,\"28\":199,\"29\":135,\"30\":194,\"31\":139}, code=INVALID_ARGUMENT, version=strings/5.4.0)" }
This should help you
Solidity: How to represent bytes32 as string
The problem is that currently you are trying to convert your hash to a utf-8 string. The hash has values that aren't supported by utf-8. I think what you meant to do, was represent your hash as a string.
I'm trying to get a random number with Chainlink VRF,
So Hi have follow this demo step by step : https://www.youtube.com/watch?v=JqZWariqh5s
here is what i've copied on Remix :
pragma solidity 0.6.6;
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/VRFConsumerBase.sol";
contract RandomNumberConsumer is VRFConsumerBase {
bytes32 public keyHash;
uint256 public fee;
uint256 public randomResult;
constructor() VRFConsumerBase(
0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
0xa36085F69e2889c224210F603D836748e7dC0088 // LINK Token
) public
{
keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
fee = 0.1 * 10 ** 18; // 0.1 LINK
}
function getRandomNumber(uint256 userProvidedSeed) public returns (bytes32 requestId) {
return requestRandomness(keyHash, fee, userProvidedSeed);
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness.mod(50).add(1);
}
}
when i click on getRandomNumber, i always get this error :
Error encoding arguments: Error: invalid BigNumber string (argument="value", value="", code=INVALID_ARGUMENT, version=bignumber/5.0.8)
and with the fulfillRandomness, i get this error :
Error encoding arguments: Error: invalid arrayify value (argument="value", value="", code=INVALID_ARGUMENT, version=bytes/5.0.5)
Add some seed number into the function, and then click it.
Also, be sure to fund it with LINK.
Also, fulfillRandomness is only callable by the Chainlink VRF, so no worries on that part.
It looks like you are not passing the userProvidedSeed as an argument to getRandomNumber()
Try putting any number into the box beside the getRandomNumber method in Remix and then click on the method.
Also, fulfillRandomness is only callable by the Chainlink VRF, so do no worry about calling that function.
I'm developing dummy test contract in solidity with truffle, for below code,
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/SkillDevelopment.sol";
contract TestSkillDevelopment {
SkillDevelopment skillDevelopmentContract = SkillDevelopment(DeployedAddresses.SkillDevelopment());
function testSetStudentEnrollInfo() public {
skillDevelopmentContract.setStudentEnrollInfo("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
string expected = string("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
Assert.equal(skillDevelopmentContract.getStudentEnrollInfo(), expected, "The message should be set");
}
}
but getting error
" TypeError: Explicit type conversion not allowed from "literal_string to "string storage pointer"."
while running "truffle test" command.
Please suggest, how is incorrect here.
Try this instead:
string memory expected = "{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}";
The default location for a string is storage, which would mean you would have to point it at some state variable in storage. Switching to memory solves that issue. Finally, the explicit cast to string is unneeded (because the value is already a string) and causes the compilation error you saw.