Test case for vestingWallet smart contract returning "header not found" - ethereum

network,
I've been trying to finish a unit test for an investing wallet smart contract on Solidity as mentioned above:
The idea is to create a test cenarium for a vesting wallet smart contract.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "#openzeppelin/contracts/finance/VestingWallet.sol";
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract VestingContract is VestingWallet {
constructor(
address beneficiaryAddress,
uint64 startTimestamp,
uint64 durationSeconds
)
public
VestingWallet(beneficiaryAddress, startTimestamp, durationSeconds)
{}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
function getBalanceFromToken(address _token) public view returns (uint256) {
return IERC20(_token).balanceOf(address(this));
}
}
and I also created these tests.
const VestingContract = artifacts.require("VestingContract");
const IoraContract = artifacts.require("Iora");
contract("VestingContract", function (accounts) {
it("should assert true", async function () {
await VestingContract.deployed();
return assert.isTrue(true);
});
it("beneficiary and accounts[0] should be the same", async function () {
const wallet = accounts[0];
const vestingInstance = await VestingContract.deployed();
const walletFromSmartContract = await vestingInstance.beneficiary();
return assert.isTrue(wallet === walletFromSmartContract)
});
it("actual timestamp should be the high or the same when it was deployed", async function () {
const vestingInstance = await VestingContract.deployed();
const vestingStarted = await vestingInstance.start();
const startTimestamp = Math.floor(Date.now() / 1000);
return assert.isTrue(startTimestamp >= vestingStarted.toNumber())
});
it("balance from erc20 token should be the same on this contract balance", async function () {
const vestingInstance = await VestingContract.deployed();
const ioraInstance = await IoraContract.deployed();
await ioraInstance.transfer(vestingInstance.address, 150);
const ioraBalance = await ioraInstance.balanceOf(vestingInstance.address);
const ioraTokenBalance = await vestingInstance.getBalanceFromToken(ioraInstance.address);
return assert.isTrue(ioraBalance.toNumber() === ioraTokenBalance.toNumber())
});
it("total released should be the same as we transfered", async function () {
const amount = 3000;
const vestingInstance = await VestingContract.deployed();
const ioraInstance = await IoraContract.deployed();
await ioraInstance.transfer(vestingInstance.address, amount);
await vestingInstance.release(ioraInstance.address);
const releasedVesting = await vestingInstance.released(vestingInstance.address);
return assert.isTrue(amount === releasedVesting.toNumber())
});
});
but, I'm getting this error:
1) Contract: VestingContract
total released should be the same as we transfered:
Error: Returned error: header not found
at Context.<anonymous> (test/vesting_contract.js:47:51)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
My question is, why my header was not found and also how can I solve it?

Related

VRFCoordinatorV2Mock's fulfillRandomWords not being called in hardhat local deployment

I'm building a smart contract that generates 3 random numbers using Chainlink VRF. My contract (SlotMachine.sol) implements the VRFConsumerBaseV2 and the constructor has 2 parameters: _subscriptionId and _vrfCoordinator:
constructor(
uint64 _subscriptionId,
address _vrfCoordinator
) payable VRFConsumerBaseV2(_vrfCoordinator)
I have a play() function which calls the requestRandonWords through the vrfCoordinator and I overrided the fulfillRandomWord function to use the generated random words.
I want to do 2 things: 1) create the unit tests in slotmachine.js and 2) test my contract in the hardhat network (chainId = 31337) through deploy.js
For this I'm using the VRFCoordinatorV2Mock which helps me mock an oracle's behavior (as I understand).
My unit tests in slotmachine.js are working well. The fulfillRandomWords is called and then my tests are passing. However when I add a similar logic to the deploy.js file (deploy the mock contract and then the slot machine contract) the fulfillRandomWords is not being called when I call the play() function (which has the requestRandomWords inside it) from my deployed contract.
slotmachine.js
const { ethers } = require("hardhat");
const { expect } = require("chai");
const { BigNumber } = require("ethers");
const provider = ethers.getDefaultProvider();
describe("Decentralized Slot Machine", async function () {
let myContract;
let hardhatVrfCoordinatorV2Mock;
describe("Testing Decentralized Slot Machine", function () {
//1. Contract deployment
it("Should deploy Slot Machine Contract", async function () {
const SlotMachine = await ethers.getContractFactory("SlotMachine");
let vrfCoordinatorV2Mock = await ethers.getContractFactory(
"VRFCoordinatorV2Mock"
);
hardhatVrfCoordinatorV2Mock = await vrfCoordinatorV2Mock.deploy(0, 0);
await hardhatVrfCoordinatorV2Mock.createSubscription();
await hardhatVrfCoordinatorV2Mock.fundSubscription(
1,
ethers.utils.parseEther("7")
);
myContract = await SlotMachine.deploy(
1,
hardhatVrfCoordinatorV2Mock.address,
{
value: ethers.utils.parseEther("100"),
}
);
await hardhatVrfCoordinatorV2Mock.addConsumer(1, myContract.address);
});
//2. Play
describe("First Play - First Player", function () {
it("Contract should receive random numbers", async () => {
const [account1, account2] = await ethers.getSigners();
let tx = await myContract.play(ethers.constants.AddressZero, {
value: ethers.utils.parseEther("1"),
});
let { events } = await tx.wait();
let [reqId] = events.filter((x) => x.event === "RequestedRandomness")[0]
.args;
await expect(
hardhatVrfCoordinatorV2Mock.fulfillRandomWords(
reqId,
myContract.address
)
).to.emit(myContract, "ReceivedRandomness");
let round = await myContract.rounds(reqId);
expect(round.userAddress).to.be.equal(account1.address);
expect(round.number1).to.be.equal(1);
expect(round.number2).to.be.equal(9);
expect(round.number3).to.be.equal(6);
});
});
});
});
deploy.js
const { ethers } = require("hardhat");
const localChainId = "31337";
module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
const chainId = await getChainId();
await deploy("VRFCoordinatorV2Mock", {
from: deployer,
args: [0, 0],
log: true,
waitConfirmations: 5,
});
const hardhatVrfCoordinatorV2Mock = await ethers.getContract(
"VRFCoordinatorV2Mock",
deployer
);
await hardhatVrfCoordinatorV2Mock.createSubscription();
await hardhatVrfCoordinatorV2Mock.fundSubscription(
1,
ethers.utils.parseEther("7")
);
const myContract = await deploy("SlotMachine", {
from: deployer,
args: [1, hardhatVrfCoordinatorV2Mock.address],
log: true,
waitConfirmations: 5,
});
await hardhatVrfCoordinatorV2Mock.addConsumer(1, myContract.address);
console.log("Contract address: ", myContract.address);
};
module.exports.tags = ["SlotMachine"];
Question
Can I deploy my contract in the hardhat network with the expected behavior of the VRFCoordinatorV2Mock which is to call automatically fulfillRandomWords? If so, how can I do this? What do I have to change in my code? If the answer is no, is there another alternative than deploying to a testnet?

Unhandled Rejection (Error): call revert exception problem

Error Image
Solution found online
const submitHandler = async (e) => {
e.preventDefault();
// handleMint();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const erc20 = new ethers.Contract("0xbe3ccbfc866b16bd983b795b6ffa8c0f98b2540e", Abi, signer);
// const tokenName = await erc20.name();
const { data } = await axios.get(`/api/products/`);
// console.log("my data: ",data);
await erc20.createNFT(""); //change krna h
let mintId= await erc20.TokenID();
let tokenId = parseInt(mintId._hex, 16);
console.log(tokenId);
// const check = await erc20.ownerOf(tokenId);
// console.log(check);
try{
await erc20.listNFT(tokenId, price); //added for sale
}catch(err){
console.log(err);
}
const add3= erc20.ownerOf(tokenId);
console.log(add3);
dispatch(createProduct(name, tokenId, price, warranty, description, image, countInStock));
};
I think there is problem with the minting is there any solutions? I am newbie to this field. Thank you.strong text

× Unhandled Rejection (Error): invalid BigNumber value (argument="value", value=[11,15,17,18,19,21], code=INVALID_ARGUMENT, version=bignumber/5.6.2)

I'm attempting to rework this function which currently cycles through the call of the walletOfOwner to return all owned tokenIDs, then stages each for a separate transaction.
I cannot seem to get the array pulled in a way that is accepted as it can be on etherscan and looking for some assistance.
Original Code
async function stakeall() {
var rawnfts = await vaultcontract.methods.tokensOfOwner(account).call();
const arraynft = Array.from(rawnfts.map(Number));
const tokenid = arraynft.filter(Number);
await Web3Alc.eth.getMaxPriorityFeePerGas().then((tip) => {
Web3Alc.eth.getBlock('pending').then((block) => {
var baseFee = Number(block.baseFeePerGas);
var maxPriority = Number(tip);
var maxFee = maxPriority + baseFee;
tokenid.forEach(async (id) => {
await vaultcontract.methods.stake([id])
.send({
from: account,
maxFeePerGas: maxFee,
maxPriorityFeePerGas: maxPriority
})
})
});
})
}
Reworked
async function stakeall() {
var rawnfts = await contract.methods.walletOfOwner(account).call();
const arraynft = Array.from(rawnfts.map(Number));
const tokenids = arraynft
await Web3Alc.eth.getMaxPriorityFeePerGas().then((tip) => {
Web3Alc.eth.getBlock('pending').then((block) => {
var baseFee = Number(block.baseFeePerGas);
var maxPriority = Number(tip);
var maxFee = maxPriority + baseFee;
vaultcontract.methods.stake([tokenids])
.send({
from: account,
maxFeePerGas: maxFee,
maxPriorityFeePerGas: maxPriority
})
});
})
}
I can perform this function successfully with the same array on etherscan, but am not too sure where to go from here, everything I've found states it's only possible to send one tokenId at a time, however, I can call the same function on etherscan successfully using their ui

How can you sign hyperledger-sawtooth transactions using metamask keys?

Hyperledger sawtooth uses secp256k1 ECDSA to sign transactions:
https://sawtooth.hyperledger.org/docs/core/releases/1.2.5/_autogen/txn_submit_tutorial.html?highlight=transaction%20sign
And aparently ethereum uses the same type of signature:
https://hackernoon.com/a-closer-look-at-ethereum-signatures-5784c14abecc
Thus, it would seem that because Metamask is used with Ethereum it would also work with sawtooth. However, I haven't found examples of this, and although I've tried signing transactions with web3.js and ethers.js with Metamask those signatures get rejected by Sawtooth.
It's possible, this is an example I made using web3:0.20.7:
https://github.com/le99/sawtooth-with-metamask-signatures/blob/master/src/App.js
The important function is onClick()
import './App.css';
import React, { useState } from 'react';
var ethUtil = require('ethereumjs-util')
const secp256k1 = require('secp256k1')
const CryptoJS = require('crypto-js');
const axios = require('axios').default;
const cbor = require('cbor')
const Web3 = require('web3');
//https://github.com/ethereum/web3.js/blob/0.20.7/DOCUMENTATION.md
// let web3 = new Web3(Web3.givenProvider || "ws://localhost:8545");
let web3;
if (typeof window.web3 !== 'undefined') {
web3 = new Web3(window.web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
const hash = (x) =>
CryptoJS.SHA512(x).toString(CryptoJS.enc.Hex)
// https://stackoverflow.com/questions/33914764/how-to-read-a-binary-file-with-filereader-in-order-to-hash-it-with-sha-256-in-cr
function arrayBufferToWordArray(ab) {
var i8a = new Uint8Array(ab);
var a = [];
for (var i = 0; i < i8a.length; i += 4) {
a.push(i8a[i] << 24 | i8a[i + 1] << 16 | i8a[i + 2] << 8 | i8a[i + 3]);
}
return CryptoJS.lib.WordArray.create(a, i8a.length);
}
async function onClick(){
const ethereum = window.ethereum;
var from = web3.eth.accounts[0]
// var msgHash = ethUtil.keccak256(Buffer.from('An amazing message, for use with MetaMask!'))
var msgHash = Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex');
console.log(from);
let signature1 = await new Promise((resolve, reject)=>{
web3.eth.sign(from, msgHash, function (err, result) {
if (err) return reject(err)
return resolve(result)
})
});
const rpk3 = secp256k1.ecdsaRecover(Uint8Array.from(Buffer.from(signature1.slice(2, -2), 'hex')), parseInt(signature1.slice(-2), 16) - 27, Uint8Array.from(msgHash));
let publicKey = Buffer.from(rpk3, 'hex').toString('hex')
console.log(msgHash.toString('hex'));
console.log(signature1);
console.log(publicKey);
console.log();
const INT_KEY_FAMILY = 'intkey'
const INT_KEY_NAMESPACE = hash(INT_KEY_FAMILY).substring(0, 6)
const address = INT_KEY_NAMESPACE + hash('foo').slice(-64)
console.log('address:',address);
const payload = {
Verb: 'set',
Name: 'foo',
Value: 41
}
console.log('public:', publicKey);
const payloadBytes = cbor.encode(payload)
const protobuf = require('sawtooth-sdk/protobuf')
const transactionHeaderBytes = protobuf.TransactionHeader.encode({
familyName: 'intkey',
familyVersion: '1.0',
inputs: [address],
outputs: [address],
signerPublicKey: publicKey,
// In this example, we're signing the batch with the same private key,
// but the batch can be signed by another party, in which case, the
// public key will need to be associated with that key.
batcherPublicKey: publicKey,
// In this example, there are no dependencies. This list should include
// an previous transaction header signatures that must be applied for
// this transaction to successfully commit.
// For example,
// dependencies: ['540a6803971d1880ec73a96cb97815a95d374cbad5d865925e5aa0432fcf1931539afe10310c122c5eaae15df61236079abbf4f258889359c4d175516934484a'],
dependencies: [],
payloadSha512: CryptoJS.SHA512(arrayBufferToWordArray(payloadBytes)).toString(CryptoJS.enc.Hex),
nonce:"hey4"
}).finish()
let sss=CryptoJS.SHA256(arrayBufferToWordArray(transactionHeaderBytes)).toString(CryptoJS.enc.Hex);
let dataHash=Uint8Array.from(Buffer.from(sss, 'hex'));
let signature = await new Promise((resolve, reject)=>{
web3.eth.sign(from, dataHash, function (err, result) {
if (err) return reject(err)
return resolve(result)
})
});
signature = signature.slice(2, -2)
console.log('sha1:', CryptoJS.SHA512(arrayBufferToWordArray(transactionHeaderBytes)).toString(CryptoJS.enc.Hex))
console.log('signature1:', signature)
const transaction = protobuf.Transaction.create({
header: transactionHeaderBytes,
headerSignature: signature,
payload: payloadBytes
})
//--------------------------------------
//Optional
//If sending to sign outside
const txnListBytes = protobuf.TransactionList.encode({transactions:[
transaction
]}).finish()
//const txnBytes2 = transaction.finish()
let transactions = protobuf.TransactionList.decode(txnListBytes).transactions;
//----------------------------------------
//transactions = [transaction]
const batchHeaderBytes = protobuf.BatchHeader.encode({
signerPublicKey: publicKey,
transactionIds: transactions.map((txn) => txn.headerSignature),
}).finish()
//
sss=CryptoJS.SHA256(arrayBufferToWordArray(batchHeaderBytes)).toString(CryptoJS.enc.Hex);
dataHash=Uint8Array.from(Buffer.from(sss, 'hex'));
signature = await new Promise((resolve, reject)=>{
web3.eth.sign(from, dataHash, function (err, result) {
if (err) return reject(err)
return resolve(result)
})
});
signature = signature.slice(2, -2)
const batch = protobuf.Batch.create({
header: batchHeaderBytes,
headerSignature: signature,
transactions: transactions
})
const batchListBytes = protobuf.BatchList.encode({
batches: [batch]
}).finish()
console.log(Buffer.from(batchListBytes).toString('hex'));
console.log('batchListBytes has the batch bytes that ca be sent to sawtooth')
// axios.post(`${HOST}/batches`, batchListBytes, {
// headers: {'Content-Type': 'application/octet-stream'}
// })
// .then((response) => {
// console.log(response.data);
// })
// .catch((err)=>{
// console.log(err);
// });
}
The example is based on:
https://sawtooth.hyperledger.org/docs/core/releases/1.2.6/_autogen/sdk_submit_tutorial_js.html
There is a lot of low level stuff, hyperledger and Metamask represent signatures slightly differently. Also most libraries for Metamask automatically wrap the data (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-accounts.html#sign), they then hash it using keccak256, and that hash is what is finnally signed with secp256k1, which is not what you need for Sawtooth.
An example where no wraping or intermediaries are used to sign is: https://github.com/danfinlay/js-eth-personal-sign-examples/blob/master/index.js

Interacting with deployed Ethereum/Quorum contract

I'm trying to deploy a contract in a private Ethereum (Quorum with RAFT but executing only public tx) using solc version 0.5.1 and web3 version 1.0.0-beta.36. Number contract is deployed and contractAddress is returned upon receiving receipt.
Though might seem cumbersome, for easier reproducibility, I've written all code here. The problem I'm having is in the last block (interacting with contract).
const solc = require('solc');
const Web3 = require('web3');
const input = {
language: 'Solidity',
sources: {
solContract: {
content: `
pragma solidity ^0.5.1;
contract Number {
uint private num = 100;
function getNum() public view returns(uint) {
return num;
}
function setNum(uint _num) public returns(bool success) {
num = _num;
return true;
}
}`
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)))
const abi = output.contracts.solContract.Number.abi
const bytecode = `0x${output.contracts.solContract.Number.evm.bytecode.object}`
const web3 = new Web3('http://127.0.0.1:8555');
const privateKey = '0x82f74b773d7f948153d7eb8c192bd9819e3e94073d8bdc0e03d659aa42cd34ba';
// const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const contract = new web3.eth.Contract(abi);
web3.eth.personal.unlockAccount('0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2', 'passwordstring', 600)
.then(
contract.deploy({
data: bytecode,
arguments: []
})
.send({
from: '0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2',
gas: 0x47b760
}, (error, transactionHash) => {
if (error) {
console.log(error)
} else if (transactionHash) {
web3.eth.getTransaction(transactionHash)
.then((data) => {
console.log(data)
})
}
})
.on('receipt', (receipt) => {
console.log(receipt.contractAddress)
}))
// Interacting with deployed contract
// contract address returned from above
// 0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB
contract.options.address = '0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB'
contract.methods.getNum().call({
from: 0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2,
gas: 0x47b760
}, (err, data) => {
err ? console.log(err) : console.log(data)
})
The issue you're having here is that when you execute the last block of code, the contract has actually not yet been deployed.
You need to perform the call to getNum() within the on() block, e.g.:
.on('receipt', (receipt) => {
console.log("CONTRACT CREATED, ADDRESS = ", receipt.contractAddress)
performGetNum(receipt.contractAddress)
})
function performGetNum(contractAddress) {
sampleContract.options.address = contractAddress
sampleContract.methods.getNum().call({
from: "0xed9d02e382b34818e88b88a309c7fe71e65f419d",
gas: 0x47b760
}, (err, data) => {
err ? console.log(err) : console.log("getNum() returned value: ", data)
})
}