What could cause Too few arguments to function Illuminate\Database\Eloquent\Builder::findOrFail(), 0 passed inlaravel? - doctype

here is my code public function edit($id)
{
$shark = Shark::whereId($id)->findOrFail();
return redirect('sharks.edit',compact('shark'));
}
My Route::get('shark/{id}/edit',[SharkController::class,'edit']);

Related

Partial calldata using ethers

I'd like to serialize a call using ethers, but only partially.
contract MockCall {
struct VoteResults {
uint256 votes;
bytes data;
}
event LogParam(uint256 param);
event LogParam(string name);
event LogParam(VoteResults[] results);
bool public isAction1 = false;
bool public isAction2 = false;
//I've only created this method so type chain would generate method stub
function Action(uint256 param1, string calldata name) public {
isAction1 = true;
}
// Intentionally leave 2 calldata parameters to verify appending calldata can work with trailing dynamic parameter
function Action(uint256 param1, string calldata name, VoteResults[] calldata voteResults) public {
isAction2 = true;
emit LogParam(param1);
emit LogParam(name);
emit LogParam(voteResults);
}
}
I would like to partially build the calldata, and my contract is going to handle injecting the rest of the parameters.
I've tried a few ways:
This way throws an error, in hexify, and sometimes json
let encodedFunctionCall = '';
try {
const rawInterface = new Interface("Action(uint256,string,(uint256,bytes)[]");
encodedFunctionCall = rawInterface.encodeFunctionData("Action(uint256,string,(uint256,bytes)[]", [BigNumber.from(5), "Abra"]);
}
catch(err) {
console.log(err);
}
This doesn't compile since it expects 3 parameters
const encodedFunctionCall = mockCall.interface.encodeFunctionData("Action(uint256,string,(uint256,bytes)[])", [BigNumber.from(5), "Abra"])
How do I partially construct calldata?

Interfaces. Why am I getting a type error when trying to use an external function?

Here is the code for my main smart contract.
Errors on the last two functions.
from solidity: TypeError: Invalid type for argument in function call.
Invalid implicit conversion from type(uint256) to uint256 requested.
--> contracts/DIV4.sol:39:57: | 39 | randomness_interface(_random).fulfillRandomness(uint256); |
^^^^^^
from solidity: TypeError: Invalid type for argument in function call.
Invalid implicit conversion from type(bytes32) to bytes32 requested.
--> contracts/DIV4.sol:43:55: | 43 | randomness_interface(_random).getRandomNumber(bytes32); |
^^^^^^^
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import {randomness_interface} from "./randomness_interface.sol";
contract Divergence is ERC1155, Ownable {
uint256 public constant One = 0;
uint256 public constant Two = 1;
uint256 public constant Three = 2;
uint256 public constant Four = 3;
constructor() ERC1155 ("https://ipfs.io/ipfs/dsfgijdsfklj348rue0ur099045948.json"){
_mint(msg.sender, item1, 1000, "" );
_mint(msg.sender, item2, 130, "" );
_mint(msg.sender, item3, 65, "" );
_mint(msg.sender, item4, 3, "" );
}
function uri(uint256 _tokenId) override public view returns (string memory) {
return string(
abi.encodePacked(
"https://ipfs.io/ipfs/Qmf4WrTGA2fYJXitigvqJ7FDVMPreJQW4of8HZ1k5Wzkd3?filename=Divergence1",
Strings.toString(_tokenId),
".json"
)
);
}
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
function getRandomNumberfromOutside(address _random) external {
randomness_interface(_random).getRandomNumber(bytes32);
}
Interface.sol file.
pragma solidity ^0.8.0;
interface randomness_interface {
function fulfillRandomness(uint256 randomness) external view returns (uint);
function getRandomNumber(bytes32 requestId) external;
}
Finally, the file with all the randomization happening.
pragma solidity ^0.6.6;
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/VRFConsumerBase.sol";
contract RandomNumber is VRFConsumerBase {
bytes32 public keyHash;
uint256 public fee;
uint256 public randomResult;
constructor() VRFConsumerBase(0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, 0xa36085F69e2889c224210F603D836748e7dC0088) public {
keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
fee = 0.1 * 10 ** 18; //0.1 LINK
}
function getRandomNumber() public returns (bytes32 requestId) {
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness.mod(100).add(1);
}
}
You are not passing correct arguments to the functions.
-
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
You are calling randomness_interface(_random).fulfillRandomness and fulfillRandomness expects
function fulfillRandomness(bytes32 requestId, uint256 randomness)
I am not sure if this is the correct way of calling fulfillRandomness
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
Because as far as I know, fulfillRandomness gets called by the chainlink node, so you make request, get the random number and then chainlink will call fulfillRandomness. I think you should have written like this:
function fulfillRandomness(bytes32 requestId,uint256 randomNumber) internal override{
}

Custom sonar rule for annotation parameters check

I have a task to create custom rule using SonarJava. Rule purpose is checking methods. If method is annottated with #Test it also needs to have #TestInfo annotation with not empty testCaseId parameter.
This is what I have prepared:
public class AvoidEmptyTestCaseIdParameterRule extends IssuableSubscriptionVisitor {
private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
#Override
public List<Tree.Kind> nodesToVisit() {
return ImmutableList.of(Tree.Kind.METHOD);
}
#Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {
List<AnnotationInstance> annotations = methodTree.symbol().metadata().annotations();
for (int i = 0; i < annotations.size(); i++) {
if (annotations.get(i).symbol().name().equals("TestInfo")
&& !testInfoAnnotationContainsNonEmptyTestCaseIdParameter(annotations.get(i))) {
reportIssue(methodTree.simpleName(),
"Method annotated with #TestInfo should have not empty testCaseId parameter");
}
}
} else {
reportIssue(methodTree.simpleName(),
"Method annotated with #Test should also be annotated with #TestInfo");
}
}
}
private boolean testInfoAnnotationContainsNonEmptyTestCaseIdParameter(AnnotationInstance annotation) {
return <--this is where I stuck-->;
}
}
this is how my test class look:
public class TestClass {
#Test
#TestInfo(testCaseId = "", component = "Policy.IndividualBenefits")
public void testMethod() {
}
}
Question:
-Is it possible to get annotation parameters (properly or as String line)?
-Is there other possible way to get this parameter?
I figured it out. I switched to using Tree.Kind.ANNOTATION.
This is the code for search of needed argument:
Arguments arguments = annotationTree.arguments();
if (!arguments.isEmpty()) {
for (int i = 0; i < arguments.size(); i++) {
String parameter = arguments.get(i).firstToken().text();
String parameterValue = arguments.get(i).lastToken().text();
if (isParameterTestCaseId(parameter) && isTestCaseIdEmpty(parameterValue)) {
reportIssue(arguments.get(i),
"Method annotated with #TestInfo should have not empty testCaseId parameter");
}
}
}
Methods for checking argument and its value:
private boolean isParameterTestCaseId(String parameter) {
return parameter.matches("testCaseId");
}
private boolean isTestCaseIdEmpty(String parameterValue) {
return parameterValue.length() != 0;
}

Prestashop development - how configure a Controller (Property Tab->name is empty)

I want to set up a Controller following this guide:
https://webkul.com/blog/create-modules-admin-controllers-without-creating-tab-prestashop/
So in my custom module I do this:
....
public function install() {
return (parent::install()
&& $this->registerHook('header')
&& $this->registerHook('footer')
&& $this->installTab()
);
}
public function installTab() {
$tab = new Tab();
$tab->active = 1;
$tab->class_name = 'abandonedCartsAdminModuleController';
$tab->name = "test";
//If you don't want to create a tab for your admin controller then Pass id_parent value as -1.
$tab->id_parent = -1;
$tab->module = $this->name;
return $tab->add();
}
This is the Controller: abandonedCartsAdminModuleController.php
<?php
class abandonedCartsAdminModuleController extends AdminModuleController {
public function __construct() {
parent::__construct();
$this->context = Context::getContext();
}
public function init() {
$this->retrieve();
}
public function retrieve() {
...
}
}
What happens when I try to install my module is I have the PrestaShopException: "Property Tab->name is empty
at line 887 in file classes/ObjectModel.php"
$tab->name must be an array, one name by language.
$tab->name = array();
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'test';
}

how to config container with request variable

In the first place I had to configure parameters using the class "ParametersCompilerPass" to get data from database.Here si my class :
class ParametersCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$em = $container->get('doctrine.orm.default_entity_manager');
$boutique = $em->getRepository('AcmeBundle:Boutique')->findOneByNom($container->getParameter('boutique.config'));
if(null !== $boutique){
$container->setParameter('url_site', $boutique->getUrl());
$container->setParameter('idboutique', $boutique->getId());
}else{
$container->setParameter('url_site', null);
$container->setParameter('idboutique', 0);
}
}
}
and when i set a parameter from request, it dont work, i tried in adding this code :
$request = $container->get('request_stack')->getCurrentRequest();
if($request->getMethod() == 'POST'){
if (null !== $choixbout = $request->get('choixbout')){
// $this->container->setParameter('idboutique',$choixbout);
}
}
the service request_stack return null.
I do not know how to configure a parameter from a POST variable.
Hope you can help me.
thanks
Is it solid requirement to have the parameter set?
It could be handy to create a service which has a request dependency that can act as a boutique parameter holder.
For example
# app/config/services.yml
app.boutique:
class: AppBundle\Boutique\Boutique
arguments: ['#request_stack']
app.boutique_info_dependant1:
class: AppBundle\Boutique\BoutiqueDependant1
arguments: ['#app.boutique']
app.boutique_info_dependant2:
class: AppBundle\Boutique\BoutiqueDependant2
arguments: ['#app.boutique']
This would be a parameter handler.
# AppBundle/Boutique/Boutique.php
class Boutique
{
/** #var RequestStack */
private $requestStack;
/**
* BoutiqueListener constructor.
* #param ContainerInterface $container
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getBoutique()
{
$request = $this->requestStack->getCurrentRequest();
/// here you can add an extra check if the request is master etc.
if ($request->getMethod() == Request::METHOD_POST) {
if (null !== $choixbout = $request->get('choixbout')) {
return $choixbout;
}
}
return null;
}
}
Then using the handler
class BoutiqueDependant1
{
public function __construct(Boutique $boutique)
{
$this->myBoutique = $boutique->getBoutique();
}
}
This does not look like the best solution but could work...
Other option would be to rethink the application architecture to handle boutique information somehow differently.