keep getting transaction failed when trying to do pancake swap transactions in nodejs with web3 and ethersjs - solidity

error message (code attached below)
node_modules\#ethersproject\logger\lib\index.js:180
var error = new Error(message);
^
Error: transaction failed (transactionHash="0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374", transaction={"nonce":364,"gasPrice":{"type":"BigNumber","hex":"0x02540be400"},"gasLimit":{"type":"BigNumber","hex":"0x7a1200"},"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","value":{"type":"BigNumber","hex":"0x00"},"data":"0xa5be382e000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000003975383df42df8ed045d636b255bf0829d8d5970000000000000000000000000000000000000000000000000000000006698a9df0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000055d398326f99059ff775485246999027b3197955","chainId":56,"v":147,"r":"0xc1a926e6f3989a50185b2d75c4bc877a76c8a2f30d505a1055fa89271feba035","s":"0x0ebeb2da65954541c82ddf468177cc6a49324a0e7a6e3fb3f5795f41c1055261","from":"0x3975383Df42Df8ED045d636b255Bf0829d8D5970","hash":"0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374","type":null}, receipt={"to":"0x10ED43C718714eb63d5aA57B78B54704E256024E","from":"0x3975383Df42Df8ED045d636b255Bf0829d8D5970","contractAddress":null,"transactionIndex":19,"gasUsed":{"type":"BigNumber","hex":"0x5a67"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","blockHash":"0xd504dfc3e7703e7657239e04f0d36e8118633689395cfc6fb9b3d6f213893724","transactionHash":"0x03e0911d26d2175d55b233b4a7b17d06202e7c2fb52a2ecfd35f3863814cb374","logs":[],"blockNumber":7497373,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x216ce6"},"status":0,"byzantium":true}, code=CALL_EXCEPTION, version=providers/5.1.2)
at Logger.makeError (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\logger\lib\index.js:180:21)
at Logger.throwError (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\logger\lib\index.js:189:20)
at JsonRpcProvider.<anonymous> (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\providers\lib\base-provider.js:1162:36)
at step (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\providers\lib\base-provider.js:48:23)
at Object.next (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\providers\lib\base-provider.js:29:53)
at fulfilled (D:\codeforFun\322-uniswap-trading-bot\node_modules\#ethersproject\providers\lib\base-provider.js:20:58)
at processTicksAndRejections (node:internal/process/task_queues:94:5) {
code, get from here
const ethers = require('ethers');
const {ChainId, Token, TokenAmount, Fetcher, Pair, Route, Trade, TradeType, Percent} =
require('#pancakeswap-libs/sdk');
const Web3 = require('web3');
const {JsonRpcProvider} = require("#ethersproject/providers");
require("dotenv").config();
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
const web3 = new Web3('wss://apis.ankr.com/wss/c40792ffe3514537be9fb4109b32d257/946dd909d324e5a6caa2b72ba75c5799/binance/full/main');
const { address: admin } = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
console.log(`Modulos cargados`);
// Command Line Input
const InputTokenAddr = web3.utils.toChecksumAddress(process.argv[2]);
// var BUSD = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56';
const OutputTokenAddr = web3.utils.toChecksumAddress(process.argv[3]);
// var WBNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const InputTokenAmount = process.argv[4]
const Slipage = process.argv[5];
const PANCAKE_ROUTER = process.argv[6];
// const PANCAKE_ROUTER_V2 = '0x10ed43c718714eb63d5aa57b78b54704e256024e';
// const PANCAKE_ROUTER_V1 = '0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F';
// 1/1000 = 0.001
const ONE_ETH_IN_WEI = web3.utils.toBN(web3.utils.toWei('1'));//BN->(BIG NUMBER) || toWei -> Converts any ether value value into wei.
const tradeAmount = ONE_ETH_IN_WEI.div(web3.utils.toBN('1000'));//tradeAmount = ONE_ETH_IN_WEI/1000
console.log(`tradeAmount ` + tradeAmount );
const init = async () => {
const [INPUT_TOKEN, OUTPUT_TOKEN] = await Promise.all(
[InputTokenAddr, OutputTokenAddr].map(tokenAddress => (
new Token(
ChainId.MAINNET,
tokenAddress,
18
)
)));
console.log(` <<<<<------- pair-------->>>>>`);
const pair = await Fetcher.fetchPairData(INPUT_TOKEN, OUTPUT_TOKEN, provider);
//console.log(JSON.stringify(pair));
console.log(` <<<<<------- route-------->>>>>`);
const route = await new Route([pair], INPUT_TOKEN);
//console.log(JSON.stringify(route));
console.log(` <<<<<------- Trade-------->>>>>`);
const trade = await new Trade(route, new TokenAmount(INPUT_TOKEN, tradeAmount), TradeType.EXACT_INPUT);
//console.log(JSON.stringify(trade));
//https://uniswap.org/docs/v2/javascript-SDK/trading/
const slippageTolerance = new Percent(Slipage, '100'); //
console.log("slippageTolerance: " + JSON.stringify(slippageTolerance));
// create transaction parameters
const amountOutMin = trade.minimumAmountOut(slippageTolerance).raw;
const path = [INPUT_TOKEN.address, OUTPUT_TOKEN.address];
const to = admin;
const deadline = Math.floor(Date.now() / 1000) + 60 * 20;
// Create signer
const wallet = new ethers.Wallet(
Buffer.from(
process.env.PRIVATE_KEY,
"hex"
)
);
const signer = wallet.connect(provider);
// Create Pancakeswap ethers Contract
const pancakeswap = new ethers.Contract(
PANCAKE_ROUTER,
['function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'],
signer
);
//Allow input token
if(true)
{
console.log(`Allow Pancakeswap <<<<<------- START-------->>>>>`);
let abi = ["function approve(address _spender, uint256 _value) public returns (bool success)"];
let contract = new ethers.Contract(INPUT_TOKEN.address, abi, signer);
let aproveResponse = await contract.approve(PANCAKE_ROUTER, ethers.utils.parseUnits('1000.0', 18), {gasLimit: 100000, gasPrice: 5e9});
console.log(JSON.stringify(aproveResponse));
console.log(`Allow Pancakeswap <<<<<------- END-------->>>>>`);
}
if(true)
{
console.log(`Ejecutando transaccion`);
var amountInParam = ethers.utils.parseUnits(InputTokenAmount, 18);
var amountOutMinParam = ethers.utils.parseUnits(web3.utils.fromWei(amountOutMin.toString()), 18);
console.log("amountInParam: " + amountInParam);
console.log("amountOutMinParam: " + amountOutMinParam);
console.log("amountOutMin: " + amountOutMin);
const tx = await pancakeswap.swapExactTokensForTokens(
amountInParam,
amountOutMinParam,
path,
to,
deadline,
{ gasLimit: ethers.utils.hexlify(300000), gasPrice: ethers.utils.parseUnits("9", "gwei") }
);
console.log(`Tx-hash: ${tx.hash}`)
const receipt = await tx.wait();
console.log(`Tx was mined in block: ${receipt.blockNumber}`);
}
}
init();

Related

ethers.js, Swap on uniswapV3 failed tx

Im trying to use exactInput() function for UniV3 interface but when trying to execute the code the transactions fails https://goerli.etherscan.io/tx/0xb0d5e4b491610b9db8d98cc938008ba2a4e1a06e67b05ed87ac6c0ca3ad61dab
I know eth send shows 0 in this one but even especifying amount it fails, I dont know what to change..
I have checked many codes out there and cant see the mistake, please could someone give me some advice?
const {abi: V3SwapRouterABI} = require('#uniswap/v3-periphery/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json')
const { ethers } = require("ethers")
require("dotenv").config()
const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET
const PRIVATE_KEY = process.env.PRIVATE_KEY
const WALLET_ADDRESS = process.env.WALLET_ADDRESS
// now you can call sendTransaction
const wethToken= "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"
const Uni= "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"
const UniswapRouter="0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
const UniV3Contract = new ethers.Contract(
UniswapRouter,
V3SwapRouterABI
)
const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_TESTNET)
const wallet = new ethers.Wallet(PRIVATE_KEY)
const signer = wallet.connect(provider)
const FEE_SIZE = 3
function encodePath(path, fees) {
if (path.length != fees.length + 1) {
throw new Error('path/fee lengths do not match')
}
let encoded = '0x'
for (let i = 0; i < fees.length; i++) {
// 20 byte encoding of the address
encoded += path[i].slice(2)
// 3 byte encoding of the fee
encoded += fees[i].toString(16).padStart(2 * FEE_SIZE, '0')
}
// encode the final token
encoded += path[path.length - 1].slice(2)
return encoded.toLowerCase()
}
async function getToken() {
const path = encodePath([wethToken, Uni], [3000])
const deadline = Math.floor(Date.now()/1000) + (60*10)
const params = {
path: path,
recipient: WALLET_ADDRESS,
deadline: deadline,
amountIn: ethers.utils.parseEther('0.01'),
amountOutMinimum: 0
}
const encodedData = UniV3Contract.interface.encodeFunctionData("exactInput", [params])
const txArg = {
to: UniswapRouter,
from: WALLET_ADDRESS,
data: encodedData,
gasLimit: ethers.utils.hexlify(1000000)
}
const tx = await signer.sendTransaction(txArg)
console.log('tx: ', tx)
const receipt = tx.wait()
console.log('receipt: ', receipt)
}
module.exports = { getToken
You will need to remove the Deadline.. The new router 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45 moved deadline to the multi-call function (since the router is designed to be multi-call)

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

How can I mint a Hedera token that is not owned by the contract I am using to mint it?

I want to mint a Hedera Token with a smart contract. This token is not owned by the smart contract I am writing. I have been told on the Discord to 'put the private key in the contract' but this is not very specific and feels weird. The mintToken function provided in the Hedera Token Service takes as parameter:
an address
an amount
some metadata in the case of NFTs.
It's not impossible to mint a token made of a smart contract with HederaTokenService.
You may use a smart contract to make a token on hedera like ERC-20 or ERC-721, they works but they are not recognized as tokens in hedera network.
You have to use HederaTokenService to make a token on hedera.
You can use a smart contract using HederaTokenService interface(its address is 0x167) or transactions like below.
You seems using HederaTokenService.sol.
The second parameter only has meaning when you mint a fungible token.
And the second parameter only has meaning when you mint a non-funtible token.
You can create a fungible token and mint like this.
IHederaTokenService.TokenKey[]
memory keys = new IHederaTokenService.TokenKey[](1);
// Set this contract as supply
keys[0] = getSingleKey(
KeyType.SUPPLY,
KeyValueType.CONTRACT_ID,
address(this)
);
IHederaTokenService.HederaToken memory token;
token.name = _name;
token.symbol = _symbol;
token.treasury = address(this);
token.memo = _memo;
token.tokenSupplyType = true; // set supply to FINITE
token.maxSupply = _maxSupply;
token.freezeDefault = false;
token.tokenKeys = keys;
token.expiry = createAutoRenewExpiry(address(this), _autoRenewPeriod); // Contract automatically renew by himself
(int256 responseCode, address createdToken) = HederaTokenService
.createFungibleToken(token, 0, _decimals);
if (responseCode != HederaResponseCodes.SUCCESS) {
revert("Failed to create fungible token");
}
tokenAddress = createdToken;
(int256 response, uint64 newSupply, ) = HederaTokenService.mintToken(
tokenAddress,
uint64(amount),
new bytes[](0)
);
Or a Non-fungible token like this.
IHederaTokenService.TokenKey[]
memory keys = new IHederaTokenService.TokenKey[](2);
// Set this contract as supply
keys[0] = getSingleKey(
KeyType.SUPPLY,
KeyValueType.CONTRACT_ID,
address(this)
);
keys[1] = getSingleKey(
KeyType.PAUSE,
KeyValueType.CONTRACT_ID,
address(this)
);
IHederaTokenService.HederaToken memory token;
token.name = _name;
token.symbol = _symbol;
token.treasury = address(this);
token.memo = _memo;
token.tokenSupplyType = true; // set supply to FINITE
token.maxSupply = int64(int256(_maxSupply));
token.freezeDefault = false;
token.tokenKeys = keys;
token.expiry = createAutoRenewExpiry(address(this), _autoRenewPeriod); // Contract automatically renew by himself
(int256 responseCode, address createdToken) = HederaTokenService
.createNonFungibleToken(token);
if (responseCode != HederaResponseCodes.SUCCESS) {
revert("Failed to create non-fungible token");
}
tokenAddress = createdToken;
(int256 response, uint64 newSupply, ) = HederaTokenService.mintToken(
tokenAddress,
uint64(amount),
new bytes[](0)
);
(int256 response, , int64[] memory serialNumbers) = HederaTokenService
.mintToken(tokenAddress, 0, metadata);
Here is a code of using transactions.
Hope it will help you.
const {
Client,
AccountId,
AccountBalanceQuery,
ContractExecuteTransaction,
ContractFunctionParameters,
ContractCallQuery,
TokenCreateTransaction,
Hbar,
HbarUnit,
TransactionRecord,
TokenSupplyType,
TokenType,
PrivateKey,
AccountCreateTransaction,
TokenId,
TokenMintTransaction,
TokenAssociateTransaction,
TokenDissociateTransaction,
TransferTransaction,
} = require("#hashgraph/sdk");
// Allow access to ourenv file variables
require("dotenv").config();
const print = require("./utils.js").print;
// Grab your account ID and private key from the .env file
const operatorAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const operatorPublicKey = operatorPrivateKey.publicKey;
const treasuryId = AccountId.fromString(process.env.TREASURY_ID);
const treasuryKey = PrivateKey.fromString(process.env.TREASURY_PVKEY);
const aliceId = AccountId.fromString(process.env.ALICE_ID);
const aliceKey = PrivateKey.fromString(process.env.ALICE_PVKEY);
const supplyKey = PrivateKey.fromString(process.env.SUPPLY_PVKEY);
const tokenId = TokenId.fromString("0.0.*");
// If we weren't able to grab it, we should throw a new error
if (operatorPrivateKey == null || operatorAccountId == null) {
throw new Error(
"environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present"
);
}
// Create our connection to the Hedera network
// The Hedera JS SDK makes this really easy!
const client = Client.forTestnet();
// Set your client account ID and private key used to pay for transaction fees and sign transactions
client.setOperator(operatorAccountId, operatorPrivateKey);
async function printBalance(accountAlias, accountId) {
var currentBalance = await new AccountBalanceQuery()
.setAccountId(accountId.toString())
.execute(client);
console.log(
`Balance of ${accountAlias} (accountId ${accountId}): ${currentBalance.toString()}`
);
}
async function queryContract() {
const tx = new ContractCallQuery()
.setGas(300000)
.setContractId(tokenId.toString())
.setFunction(
"tokenURI",
new ContractFunctionParameters().addUint256(12)
// .addString("Golden")
);
// .setQueryPayment(Hbar.fromTinybars(300000));
const contractFunctionResult = await tx.execute(client);
console.log();
const ret_0 = contractFunctionResult.getString(0);
console.log(`return value :>> ${ret_0.toString()}`);
}
async function generateKey() {
const privateKey = await PrivateKey.generateED25519Async();
console.log(
`New key generated.\nPublic Key is ${privateKey.publicKey}\nPrivateKey is ${privateKey}`
);
return privateKey;
}
async function createAccount() {
const privateKey = await generateKey();
const transaction = new AccountCreateTransaction()
.setKey(privateKey.publicKey)
.setInitialBalance(new Hbar(1000));
//Sign the transaction with the client operator private key and submit to a Hedera network
const txResponse = await transaction.execute(client);
//Request the receipt of the transaction
const receipt = await txResponse.getReceipt(client);
//Get the account ID
const newAccountId = receipt.accountId;
console.log("The new account ID is " + newAccountId);
}
async function writeContract() {
const tx = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(300000) // Increase if revert
.setPayableAmount(Hbar.from(1.00000001)) // Increase if revert
.setFunction("pay", new ContractFunctionParameters().addString("Donate"));
const txRes = await tx.execute(client);
print("txResponse", txRes);
const receipt = await txRes.getReceipt(client);
print("recepit", receipt);
const txRec = await txRes.getRecord(client);
print("txRecord", txRec);
const value = txRec.contractFunctionResult.getUint256(0);
console.log(`First return value is: ${value.toString()}`);
const ret_0 = txRec.contractFunctionResult.getString(1);
console.log(`second return value is: ${ret_0.toString()}`);
// console.log(`First return value is: ${AccountId.fromSolidityAddress(ret_0)}`);
}
async function createToken() {
const transaction = await new TokenCreateTransaction()
.setTokenName("My Token")
.setTokenSymbol("MYT")
.setTokenType(TokenType.NonFungibleUnique)
.setDecimals(0)
.setInitialSupply(0)
.setTreasuryAccountId(treasuryId)
.setSupplyType(TokenSupplyType.Finite)
.setMaxSupply(1000)
.setSupplyKey(supplyKey)
.freezeWith(client);
//Sign the transaction with the token adminKey and the token treasury account private key
const signTx = await transaction.sign(treasuryKey);
//Sign the transaction with the client operator private key and submit to a Hedera network
const txRes = await signTx.execute(client);
print("txResponse", txRes);
const receipt = await txRes.getReceipt(client);
print("recepit", receipt);
const txRec = await txRes.getRecord(client);
print("txRecord", txRec);
//Get the token ID
let tokenId = receipt.tokenId;
//Log the token ID
console.log(`- Created NFT with Token ID: ${tokenId} \n`);
}
async function mintToken() {
// Mint new NFT
let mintTx = await new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([
Buffer.from(
"A METADATA LINK"
),
])
.freezeWith(client);
//Sign the transaction with the supply key
let mintTxSign = await mintTx.sign(supplyKey);
print("Singed Tx", mintTxSign);
//Submit the transaction to a Hedera network
let minttxRes = await mintTxSign.execute(client);
print("Tx Response", minttxRes);
//Get the transaction receipt
let mintRx = await minttxRes.getReceipt(client);
print("Tx Receipt", mintRx);
//Get the transaction record
let mintRec = await minttxRes.getRecord(client);
print("Tx Record", mintRec);
//Log the serial number
console.log(
`- Created NFT ${tokenId} with serial: ${mintRx.serials[0].low} \n`
);
}
async function associateToken(tokenIds) {
//Create the associate transaction and sign with Alice's key
let tx = await new TokenAssociateTransaction()
.setAccountId(aliceId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(aliceKey);
//Submit the transaction to a Hedera network
let txRes = await tx.execute(client);
print("Tx Response", txRes);
//Get the transaction receipt
let txRx = await txRes.getReceipt(client);
print("Tx Receipt", txRx);
//Get the transaction record
let txRec = await txRes.getRecord(client);
print("Tx Record", txRec);
//Confirm the transaction was successful
console.log(`- NFT association with Alice's account: ${txRx.status}\n`);
}
async function dissociateToken(tokenIds) {
//Create the associate transaction and sign with Alice's key
let tx = await new TokenDissociateTransaction()
.setAccountId(operatorAccountId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(aliceKey);
//Submit the transaction to a Hedera network
let txRes = await tx.execute(client);
print("Tx Response", txRes);
//Get the transaction receipt
let txRx = await txRes.getReceipt(client);
print("Tx Receipt", txRx);
//Get the transaction record
let txRec = await txRes.getRecord(client);
print("Tx Record", txRec);
//Confirm the transaction was successful
console.log(`- NFT association with Alice's account: ${txRx.status}\n`);
}
async function transferToken() {
// Check the balance before the transfer for the treasury account
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(treasuryId)
.execute(client);
console.log(
`- Treasury balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Check the balance before the transfer for Alice's account
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client);
console.log(
`- Alice's balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Transfer the NFT from treasury to Alice
// Sign with the treasury key to authorize the transfer
let tokenTransferTx = await new TransferTransaction()
.addNftTransfer(tokenId, 2 /*SN*/, treasuryId, aliceId)
.freezeWith(client)
.sign(treasuryKey);
let tokenTransferSubmit = await tokenTransferTx.execute(client);
print("Tx Response", tokenTransferSubmit);
let tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
print("Tx Receipt", tokenTransferRx);
let tokenTransferRec = await tokenTransferSubmit.getRecord(client);
print("Tx Record", tokenTransferRec);
console.log(
`\n- NFT transfer from Treasury to Alice: ${tokenTransferRx.status} \n`
);
// Check the balance of the treasury account after the transfer
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(treasuryId)
.execute(client);
console.log(
`- Treasury balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
// Check the balance of Alice's account after the transfer
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client);
console.log(
`- Alice's balance: ${balanceCheckTx.tokens._map.get(
tokenId.toString()
)} NFTs of ID ${tokenId}`
);
}
// Hedera is an asynchronous environment :)
(async function () {
await printBalance("Operator", operatorAccountId);
try {
// await printBalance("Treasury", treasuryId);
// await createAccount();
// await generateKey();
// await createToken();
// await mintToken();
// await associateToken();
// await dissociateToken();
// await transferToken();
// await queryContract();
// await writeContract();
// await printBalance("Treasury", treasuryId);
} catch (err) {
console.error(err);
}
await printBalance("Operator", operatorAccountId);
process.exit();
})();

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.

CREATE PDF FROM GOOGLE SHEETS

function createBulkPDFs(){
const docFile = DriveApp.getFileById("id");
const tempFolder = DriveApp.getFolderById("id");
const pdfFolder = DriveApp.getFolderById("id");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
const data = currentSheet.getRange(2,1,currentSheet.getLastRow()-1,15).getDisplayValues();
let errors = [];
data.forEach(row => {
try{
createpdf(row[0],row[1],row[5],row[6],row[7],row[8],row[9],row[0] + " " + row[1],docFile,tempFolder,pdfFolder);
errors.push("");
} catch(err){
errors.push("Failed");
}
}); //close forEach
currentSheet.getRange(2,15,currentSheet.getLastRow()-1,1).setValues(errors);
}
function
createpdf(First_name,Last_name,Description,Address,Location,Date_of_letter,Date_of_Def,pdfname,docFile,tempFolder,pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{First name}",First_name);
body.replaceText("{Last name}",Last_name);
body.replaceText("{Description}",Description);
body.replaceText("{Address}",Address);
body.replaceText("{Location}",Location);
body.replaceText("{Date of letter}",Date_of_letter);
body.replaceText("{Date of Def}",Date_of_Def);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs(MineType.Pdf);
pdfFolder.createFile(pdfContentBlob).setName("pdfname");
I tried to replicate your code and found some issues.
The use of setValues() when populating rows of data. The data should be in 2 dimensional. Each sub-array represents row of data.
The value used in getAs() should be application/pdf instead of MineType.pdf.
Here is my sample data:
Code:
function createBulkPDFs() {
const docFile = DriveApp.getFileById("id");
const tempFolder = DriveApp.getFolderById("id");
const pdfFolder = DriveApp.getFolderById("id");
const currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
const data = currentSheet.getRange(1, 1, currentSheet.getLastRow(), 11).getDisplayValues();
let errors = [];
data.forEach(row => {
try {
createpdf(row[0], row[1], row[5], row[6], row[7], row[8], row[9], row[0] + " " + row[1], docFile, tempFolder, pdfFolder);
errors.push("Success");
} catch (err) {
errors.push("Failed");
}
}); //close forEach
let newArr = [];
while(errors.length > 0) {
newArr.push(errors.splice(0,1));
}
currentSheet.getRange(1, 12, currentSheet.getLastRow(), 1).setValues(newArr);
}
function createpdf(First_name, Last_name, Description, Address, Location, Date_of_letter, Date_of_Def, pdfname, docFile, tempFolder, pdfFolder){
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{First name}", First_name);
body.replaceText("{Last name}", Last_name);
body.replaceText("{Description}", Description);
body.replaceText("{Address}", Address);
body.replaceText("{Location}", Location);
body.replaceText("{Date of letter}", Date_of_letter);
body.replaceText("{Date of Def}", Date_of_Def);
tempDocFile.saveAndClose();
const pdfContentBlob = tempFile.getAs('application/pdf');
pdfFolder.createFile(pdfContentBlob).setName(pdfname);
}
PDF:
Sheets:
References:
getAs()
setValues()