sending more than 1 addresses to solidity function - solidity

I am trying to send more than 1 addresses as parameters to solidity function but I am getting error while 1 address is working fine. Here is my web3 code :
feeval = (Math.random() * (0.0090 - 0.0110) + 0.0110) * 1e18;
sttcontract.methods.getAirdrop(s1,s2).send({from:addr, value: feeval}, (err, res) => {
if(!err) console.log(res);
else console.log(err);
});
and solidity function :
function getAirdrop(address _s1, address _s2) public payable returns (bool success){
aComm1 = 10;
aComm1 = (msg.value * aComm1) / 100;
address(uint160(_s1)).transfer(aComm1);
address(uint160(_s2)).transfer(aComm1);
}
the same code with 1 address is working fine.

Related

using hardhat test transfer USDC to my contract have bug

function depositETH() public payable returns (bool) {
// 应该是先把ETH存入该合约中,然后把该合约的资产存入到compound中
CEth cToken = CEth(cETH);
// Amount of current exchange rate from cToken to underlying
uint256 exchangeRateMantissa = cToken.exchangeRateCurrent();
emit TradeLog(
"Exchange Rate (scaled up by 1e18): ",
exchangeRateMantissa
);
// Amount added to you supply balance this block
uint256 supplyRateMantissa = cToken.supplyRatePerBlock();
emit TradeLog("Supply Rate (scaled up by 1e18): ", supplyRateMantissa);
// 必须这样传参数
cToken.mint{value: msg.value}();
console.log("cToken cETH balance: ", cToken.balanceOf(address(this)));
emit DepositETH("Supply ETH amount: ", msg.value);
return true;
}
use hardhat test
describe("depositUSDC", function () {
it("Test mint usdc", async function () {
const { tradingAccount, USDCSigner, USDCContract } =
await loadFixture(deployFixture);
let transferApprove = await USDCContract.connect(
USDCSigner
).approve(
tradingAccount.address,
ethers.BigNumber.from("180000000")
);
let ret1 = await transferApprove.wait();
let transferTx = await USDCContract.connect(USDCSigner).transfer(
tradingAccount.address,
ethers.BigNumber.from("180000000")
);
let ret2 = await transferTx.wait();
console.log(ret1);
console.log(ret2);
let getBalance = await USDCContract.connect(USDCSigner).balanceOf(
tradingAccount.address
);
console.log("getBalance: ", getBalance);
let tx0 = await tradingAccount
.connect(USDCSigner)
.depositUSDC(ethers.BigNumber.from("180000000"));
let receipt = await tx0.wait();
// // 可以捕获到相对应的事件参数
// receipt.events?.filter((x) => {
// if (x.event == "DepositUSDC") {
// console.log(x.args);
// }
// });
});
});
Execute transaction tx0, check the hardhat node log, and find that the contract has not received the USDC of the previous transaction transfer.
enter image description here
enter image description here
But different things happened in the following test
function flashSwap(address _tokenBorrow, uint _amount) public {
uint256 beforeBalance = IERC20(USDC).balanceOf(address(this));
console.log("beforeBalance --- %d", beforeBalance);
address pair = IUniswapV2Factory(FACTORY).getPair(_tokenBorrow, USDC);
require(pair != address(0), "!pair");
address token0 = IUniswapV2Pair(pair).token0();
address token1 = IUniswapV2Pair(pair).token1();
// 借的如果是usdc, amount0Out就 > 0 == 借的如果是weth, amount1Out > 0
uint amount0Out = _tokenBorrow == token0 ? _amount : 0;
uint amount1Out = _tokenBorrow == token1 ? _amount : 0;
// need to pass some data to trigger uniswapV2Call
bytes memory data = abi.encode(_tokenBorrow, _amount);
IUniswapV2Pair(pair).swap(amount0Out, amount1Out, address(this), data);
if (amount0Out > 0) {
emit FlashSwapUSDC("flashswap usdc amount: ", amount0Out);
} else {
emit FlashSwapETH("flashswap WETH amount: ", amount1Out);
}
}
it("Test flashSwap borrow WETH", async function () {
const { tradingAccount, USDCContract, USDCSigner } =
await loadFixture(deployFixture);
let transferTx = await USDCContract.connect(USDCSigner).transfer(
tradingAccount.address,
ethers.BigNumber.from("180000000000")
);
await transferTx.wait();
let tx0 = await tradingAccount.flashSwap(
WETH,
ethers.utils.parseEther("1.5")
);
let receipt = await tx0.wait();
receipt.events?.filter((x) => {
if (x.event == "FlashSwapETH") {
console.log(x.args);
}
});
});
enter image description here
enter image description here
Can someone explain this reason to me?
Why is the result of the first test different from that of the first test

Error during minting with code from Opensea guide

I'm trying to deploy my first SmartContract following the Opensea guide. Everything was working fine until I set a price for my tokens and added the payable keyword. Now when I try to mint, I get the error Transaction value did not equal the mint price. Looking at the code I'm thinking I need to send ETH in the mint request in msg.value somehow but I'm not sure what the syntax would be for that?
Here's how I'm minting in shell:
npx hardhat mint --address {wallet_address}
Here's the mint function in JS:
task("mint", "Mints from the NFT contract")
.addParam("address", "The address to receive a token")
.setAction(async function (taskArguments, hre) {
const contract = await getContract("NFT", hre);
const transactionResponse = await contract.mintTo(taskArguments.address, {
gasLimit: 500_000,
});
console.log(`Transaction Hash: ${transactionResponse.hash}`);
});
And the mintTo function in the .sol contract:
// Main minting function
function mintTo(address recipient) public payable returns (uint256) {
uint256 tokenId = currentTokenId.current();
require(tokenId < TOTAL_SUPPLY, "Max supply reached");
require(msg.value == MINT_PRICE, "Transaction value did not equal the mint price");
currentTokenId.increment();
uint256 newItemId = currentTokenId.current();
_safeMint(recipient, newItemId);
return newItemId;
}
I figured out a solution for this issue. You need to set the price inside the mint task in mint.js like so:
task("mint", "Mints from the NFT contract")
.addParam("address", "The address to receive a token")
.setAction(async function (taskArguments, hre) {
const contract = await getContract("NFT", hre);
const transactionResponse = await contract.mintTo(taskArguments.address, {
gasLimit: 500_000,
value: ethers.utils.parseEther("0.01")
});
console.log(`Transaction Hash: ${transactionResponse.hash}`);
});
I have not figured out a way to have this import the MINT_PRICE variable from the contract. Note: You may need to add const { ethers } = require("ethers"); at the top of the file.

Uncaught (in promise) BigNumber Error for address

I have a function in ERC721 contract which returns an address.
I am passing the returned address to ERC20 transferFrom function, and getting the following error.
My javascript code
Voting.deployed().then(function(ERC20Instance) {
let buyerAddress = $("#to-address").val();
ERC20Instance.approve(buyerAddress, 100, {gas: 1000000, from: web3.eth.accounts[0]});
ERC20Instance.transferFrom(buyerAddress, contractInstance.getCreator(), 10, {gas: 1000000, from: web3.eth.accounts[0]});
^^^^This line is causing an error.
})
and ERC721 contract:
function getCreator() public view returns (address) {
return nftInfo[owner].creator;
}
function mint(string calldata nftName) external payable {
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
owner = msg.sender;
nftInfo[msg.sender].nftId = newItemId;
nftInfo[msg.sender].name = nftName;
nftInfo[msg.sender].creator = msg.sender;
allValidTokenIndex[newItemId] = allValidTokenIds.length;
allValidTokenIds.push(newItemId);
_tokenIds.increment();
}
contractInstance.getCreator()
This JS function returns a Promise - not the actual address. You need to resolve the Promise first to get the address.
Same goes with the approve() function. You should wait until the function has been processed (i.e. after the JS Promise has been resolved) to prevent unexpected behavior resulting from the transferFrom() being executed asynchronously before the approve() finishes running.
Voting.deployed().then(async function(ERC20Instance) { // add `async`
let buyerAddress = $("#to-address").val();
await ERC20Instance.approve( // recommended: add `await`
buyerAddress,
100,
{gas: 1000000, from: web3.eth.accounts[0]}
);
await ERC20Instance.transferFrom( // recommended: add `await`
buyerAddress,
await contractInstance.getCreator(), // add `await`
10,
{gas: 1000000, from: web3.eth.accounts[0]}
);

Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.1.0)

I'm Getting this error when trying to deploy a smart contract with a function:
Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.1.0) (argument="tokenAddress", value=undefined, code=INVALID_ARGUMENT, version=abi/5.0.7)
Here is my code:
const handlePresale = async (e) => {
e.preventDefault();
const web3 = await getWeb3();
const abiData = SafeHubPresaleAbi;
const contractAddress = "0x4498F943E0a13D70B28e7565CF4E33bF443e6Bf9";
const duration = {
seconds: function (val) {
return val;
},
minutes: function (val) {
return val * this.seconds(60);
},
hours: function (val) {
return val * this.minutes(60);
},
days: function (val) {
return val * this.hours(24);
},
weeks: function (val) {
return val * this.days(7);
},
years: function (val) {
return val * this.days(365);
},
};
const latestTime = new Date().getTime();
const openingTime = latestTime + duration.minutes(1);
const closingTime = openingTime + duration.minutes(10);
const liqAddingTime = closingTime + duration.minutes(5);
let _info = {
address : "0x4498F943E0a13D70B28e7565CF4E33bF443e6Bf9",
tokenPrice : web3.utils.toWei("10", "ether"),
hardCap: web3.utils.toWei("100", "ether"),
softCap: web3.utils.toWei("30", "ether"),
minInv: web3.utils.toWei("0.1", "ether"),
maxInv: web3.utils.toWei("5", "ether"),
openingTime: openingTime,
closingTime: closingTime,
};
let _uniInfo = {
listingPrice: web3.utils.toWei("20", "ether"),
liqTime: liqAddingTime,
lockTime: 365,
precentLock: 25,
};
let _stringInfo = {
listingName: "WER sale",
};
const preslaeContract = await new web3.eth.Contract(
abiData,
contractAddress
);
// console.log(preslaeContract.methods)
preslaeContract.methods
.createPresale(_info,_uniInfo,_stringInfo)
.call((err, result) => {
console.log(result), console.log(err);
});
}
Solidity constructor:
constructor(address _safuFactoryAddress, address _safuDevAddress) public {
require(_safuFactoryAddress != address(0));
require(_safuDevAddress != address(0));
safuFactoryAddress = payable(_safuFactoryAddress);
safuDevAddress = payable(_safuDevAddress);
}
since there are still no answers yet, but over 1k views though, I'll leave here a solution if anyone else is facing the same error.
The solidity constructor in your example expects 2 arguments of type address. This means if you deploy your contract you also need to provide those 2.
As for the deployment:
No matter how you deploy your contract, providing all constructor arguments should solve your problem and your contract should be able to get deployed now.
If you work with truffle, you need to adjust your deployer .../migrations/2_deploy_contracts.js and pass 2 addresses to satisfy the constructor parameters.
var SafeHubPresale = artifacts.require("./SafeHubPresale.sol");
module.exports = function(deployer) {
deployer.deploy(SafeHubPresale, [address1], [address2]);
};
The same applies for test:
let's say:
var myContract = await SafeHubPresale.new(accounts[1], accounts[2], {from: accounts[0], gas: 0});
For those in Remix, select the drop down next to "deploy" and you will need to provide a "to" & "from" address for this to work
In your smart contract, more than one argument may present and cannot fulfill its value when you deploy. When you deploy you take these values and call deploy() it's working correctly.
In your constructor
constructor(address _safuFactoryAddress, address _safuDevAddress)
more than one address value present there you may not provide this value when you deploy.

Web3 | TransferFrom() transactions always Failed

I'm trying to send Custom ERC20 Token to Owner Wallet Address using transferFrom() function using Web3.js
However all the Transactions are failed. Which is the same problem occur on Remix IDE.
Some answers on stackOverflow here says that the approve() is needed to call first before the transferFrom() function. so, I tried on Remix frist but I got the same problem. And then tried with using Web3.js like below.
const myContract = new web3.eth.Contract(abi);
const amount = sendAmount;
let address = myAddress;
myContract.options.address = contractAddress;
myContract.options.from = TokenOwner;
let options = myContract.options;
let data1 = myContract.methods.approve(address, amount).encodeABI();
let data2 = myContract.methods.transferFrom(address, TokenOwner, amount).encodeABI();
const ethAccount = fromPrivateKey(toBuffer("0x..."));
const fromPrivateKeyBuffer = ethAccount.getPrivateKey();
web3.eth.getTransactionCount(TokenOwner, (err, count) => {
if (err) return;
const txData = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contractAddress,
from: TokenOwner,
value: 0x0,
nonce: web3.utils.toHex(count),
data: data1
};
const tx = new ethTx(txData);
tx.sign(fromPrivateKeyBuffer);
const serializedTx = tx.serialize().toString("hex");
if (!serializedTx) {
return;
} else {
web3.eth.sendSignedTransaction(`0x${serializedTx}`, (err, MuiTXHash) => {
console.log("err : ", err, "Data1-MuiTXHash : ", MuiTXHash);
// START DATA2
web3.eth.getTransactionCount(TokenOwner, (err, count) => {
if (err) return;
const txData2 = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contarctAddress,
from: TokenOwner,
value: 0x0,
nonce: web3.utils.toHex(count + 1),
data: data2
};
const tx2 = new ethTx(txData2);
tx2.sign(fromPrivateKeyBuffer);
const serializedTx2 = tx2.serialize().toString("hex");
if (!serializedTx2) {
return;
} else {
web3.eth.sendSignedTransaction(`0x${serializedTx2}`, (err, MuiTXHash2) => {
console.log("err : ", err, "Data2-MuiTXHash : ", MuiTXHash2);
});
}
});
// END DATA2
});
}
});
};
I got the two Transaction Hash return data and the TransferFrom() transaction is failed again.
What is the problem?
How can I make it success? I have to withdraw the custom ERC20 token from specific address to owner. so I have to use transferFrom() transaction.
Please let me know how to do. Thanks.
I guess the compiling on Remix had some errors so I created new solidity file with same code on it and deployed it. The new smart contract worked well and no errors occurred. The errors was not about my code. If someone's suffering like this problems, then create a new contract address and I don't recommend using 'At Address' on Remix. This is not work properly at all.
I was tried appove() + transferFrom () , allowance() on Solidity working well but on web3 not working error same as you
I guess the transferFrom function should be called by the spender (i.e the address to which the TokenOwner has approved to spend). So,
const txData2 = {
chainId: 0x03,
gasPrice: web3.utils.toHex(42000000000),
gasLimit: web3.utils.toHex(90000),
to: contarctAddress,
from: address, // spender's address
value: 0x0,
nonce: web3.utils.toHex(count + 1),
data: data2
};
And don't forget to use spender's privateKey to sign the txn and spender's address in getTransactionCount
Read Here : Ethereum Wiki
Hope it helps. :)