使用node.js获取和兑换ERC20token出错
我已经创建了一个自定义ERC20令牌使用openxappelin合同..其他API端点做得很好,但我不能获取或赎回令牌我下面的初始化代码片段
export const client = ipfsHttpClient("http://192.168.0.152:5002");
export const contractaddress = "0x2BbFA7769d2dad0c66a70368b6e5Fc8721085532";
export const provider = new ethers.BrowserProvider("https://eth-sepolia.g.alchemy.com/v2/q1jVC3tLd3_PoadQyMR08osZJ8SDKEqq");
const manager_wallet_private_key = privateKey;
const manager = new ethers.Wallet(manager_wallet_private_key, provider);
export const usercontract = new ethers.Contract(contractaddress, abi, manager);
下面是我的获取令牌路由代码
app.post("/redeem-tokens", async (req, res) => {
const {address, amount } = req.body;
try {
await usercontract.redeemTokens(BigInt(amount),address);
res.status(200).json({ message: "Tokens redeemed successfully!" });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/fetch-tokens", async (req, res) => {
const {address, amount } = req.body;
try {
await usercontract.fetchTokens(BigInt(amount),address);
res.status(200).json({ message: "Tokens redeemed successfully!" });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
误差
Server is running on 3000
TypeError: unsupported addressable value (argument="target", value=null, code=INVALID_ARGUMENT, version=6.7.1)
at makeError (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/utils/errors.js:113:21)
at assert (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/utils/errors.js:136:15)
at assertArgument (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/utils/errors.js:147:5)
at resolveAddress (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/address/checks.js:112:5)
at file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/contract/contract.js:107:24
at #walkAsync (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/abi/fragments.js:581:24)
at ParamType.walkAsync (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/abi/fragments.js:599:24)
at file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/contract/contract.js:104:22
at Array.map (<anonymous>)
at resolveArgs (file:///E:/ERC20/js-backend/node_modules/ethers/lib.esm/contract/contract.js:103:37) {
code: 'INVALID_ARGUMENT',
argument: 'target',
value: undefined
}
下面的代码
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./database_helper.sol";
contract Hikiko is ERC20Capped, Ownable, UserDB, ERC20Burnable{
address public Manager;
uint256 public limit = 2000000000 ;
uint256 public initial_coinbase = 10000000 ;
constructor() ERC20("Hikiko", "HK") ERC20Capped(limit) Ownable() {
Manager = msg.sender;
_mint(Manager, initial_coinbase);
}
function _mint(address account, uint256 amount) internal override(ERC20, ERC20Capped) {
ERC20Capped._mint(account, amount);
}
// Allow users to transfer tokens to the Manager when redeeming rewards
function redeemTokens(uint256 amount,address x) public onlyOwner {
_transfer(x, Manager, amount);
}
// Allow the user to fetch tokens from the contract
function fetchTokens(uint256 amount,address x) public onlyOwner{
transfer(x, amount);
}
function delete_user(string memory name) public onlyOwner{
address x= wallets[name].wallet;
uint256 amount = balanceOf(x);
_transfer(x, Manager, amount); // Transfer all the amount of tokens before deletion
burn(amount);//burn the extra tokens
delete wallets[name];
}
// Function to check if minting is needed
function shouldMintCoins() internal view returns (bool) {
return balanceOf(Manager) < initial_coinbase / 10;
}
// Function to mint coins to the manager
function additionalcoins() private {
uint256 a;
if(shouldMintCoins()==false){
a=0;
}
else{
a = (initial_coinbase / 2) - balanceOf(Manager);
_mint(Manager,a);
}
}
//Update the wallet address
function Update_address(string memory name, address data) public onlyOwner{
address x= wallets[name].wallet;
uint256 amount = balanceOf(x);
_transfer(x, data, amount);//Before doing so send the existing tokens to new account
wallets[name].wallet = data;
}
function saveUser(address wallet, string memory name, string memory password) public onlyOwner {
wallets[name].username = name;
wallets[name].wallet = wallet;
wallets[name].password = password;
wallets[name].cid = "default";//The animated smoking gif will be displayed at first
}
function Update_password(string memory name, string memory data) public onlyOwner {
wallets[name].password = data;
}
function Update_profile(string memory name, string memory data) public onlyOwner {
wallets[name].cid = data;
}
function Update_mentorship(string memory name) public onlyOwner {
wallets[name].ismentor = true;
}
}
我希望将我的自定义令牌发送到用户的帐户我的响应正文中的 Postman { "address": "0x017603449335929525e1eda99f8785207e74A003", "amount": 1200 }
但我得到错误,请告诉我如何修复..
{ "error": "Error Encountered" }
1条答案
按热度按时间ss2ws0br1#
抱歉给您带来麻烦;但实际上我在routes.mjs文件中声明了相同的API端点;所以碰撞发生了,我是一个初学者,我错误地这样做。我删除了重复的代码从路由,它是工作