这是一个很难回答的问题,我认为这个问题不会花太多时间通过研究来回答。我正在用基本的javascript和稳健性来完成我的第一套dapp构建。通过它,我可以连接一个钱包,并通过javascript将eth发送给一个具有应付款捐赠功能的合同。耶。
我现在正试图向我的合同发送一个ERC20令牌。我已经通过我的javascript获得了使用ERC20令牌的批准。我现在正试图把这个记号寄给我的稳固契约(使用以太),但我的生命正在失败。我试过各种我能想到的方法(通过google),但是不断地将“无法读取未定义的属性”作为错误或"TypeError: contract.transferTokens不是一个函数“。从本质上说,我似乎无法让它认识到合同的作用。这是完全可靠的合同:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TransferContract {
function transferFrom(address recipient, uint256 amount) public {
address token = 0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7; IERC20(token).transferFrom(msg.sender, recipient, amount);
}
function transferTokens(uint256 _amount) public {
require(_amount > 0);
uint256 input = _amount; //It's reverting here?? I don't see the issue
address token = 0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7;
IERC20(token).transferFrom(msg.sender, 0x4B8C40757A00eD0479e4B8293C61d8178E23d2f1, input);
}
}这里是javascript行,我试图从中得到一些东西。
const _return = await contract.transferTokens(10000);请怜悯这个可怜的灵魂,因为我似乎找不到任何帮助我的东西。LOL。
谢谢!
发布于 2022-06-08 09:49:59
从您的Pastebin,似乎有什么问题,您的abi,我重新编译的合同,并使用abi生产的草帽,现在它的工作!
此外,我已经更改了您初始化签名者的位置,因为在您批准时获得它将强制任何传输首先或第二种方法的人不能工作,即使该用户事先已经批准了BUSD。
我无法运行它,因为我的acc上没有任何BUSD或BNB,但据我所见,执行是成功的,因为我收到了一个错误,告诉我转移金额超过了备抵:)

这是由从您的合同调用busd合同触发的。
IERC20(token).transferFrom(
msg.sender,
0x4B8C40757A00eD0479e4B8293C61d8178E23d2f1,
input
);
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
<script>
//const { ethers } = require("ethers");
const busdABI = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{
"name": "",
"type": "string"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "assetProtectionRole",
"outputs": [{
"name": "",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{
"name": "",
"type": "uint8"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "paused",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{
"name": "",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{
"name": "",
"type": "string"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "betaDelegateWhitelister",
"outputs": [{
"name": "",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "proposedOwner",
"outputs": [{
"name": "",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "EIP712_DOMAIN_HASH",
"outputs": [{
"name": "",
"type": "bytes32"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "supplyController",
"outputs": [{
"name": "",
"type": "address"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "currentOwner",
"type": "address"
},
{
"indexed": true,
"name": "proposedOwner",
"type": "address"
}],
"name": "OwnershipTransferProposed",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldProposedOwner",
"type": "address"
}],
"name": "OwnershipTransferDisregarded",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"name": "newOwner",
"type": "address"
}],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
"name": "Pause",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
"name": "Unpause",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "addr",
"type": "address"
}],
"name": "AddressFrozen",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "addr",
"type": "address"
}],
"name": "AddressUnfrozen",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "addr",
"type": "address"
}],
"name": "FrozenAddressWiped",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldAssetProtectionRole",
"type": "address"
},
{
"indexed": true,
"name": "newAssetProtectionRole",
"type": "address"
}],
"name": "AssetProtectionRoleSet",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}],
"name": "SupplyIncreased",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}],
"name": "SupplyDecreased",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldSupplyController",
"type": "address"
},
{
"indexed": true,
"name": "newSupplyController",
"type": "address"
}],
"name": "SupplyControllerSet",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "seq",
"type": "uint256"
},
{
"indexed": false,
"name": "fee",
"type": "uint256"
}],
"name": "BetaDelegatedTransfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldWhitelister",
"type": "address"
},
{
"indexed": true,
"name": "newWhitelister",
"type": "address"
}],
"name": "BetaDelegateWhitelisterSet",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "newDelegate",
"type": "address"
}],
"name": "BetaDelegateWhitelisted",
"type": "event"
},
{
"anonymous": false,
"inputs": [{
"indexed": true,
"name": "oldDelegate",
"type": "address"
}],
"name": "BetaDelegateUnwhitelisted",
"type": "event"
},
{
"constant": false,
"inputs": [],
"name": "initialize",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "initializeDomainSeparator",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{
"name": "",
"type": "uint256"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}],
"name": "transfer",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "balanceOf",
"outputs": [{
"name": "",
"type": "uint256"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}],
"name": "transferFrom",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}],
"name": "approve",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}],
"name": "allowance",
"outputs": [{
"name": "",
"type": "uint256"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_proposedOwner",
"type": "address"
}],
"name": "proposeOwner",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "disregardProposeOwner",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "claimOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "reclaimBUSD",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "pause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "unpause",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_newAssetProtectionRole",
"type": "address"
}],
"name": "setAssetProtectionRole",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "freeze",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "unfreeze",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "wipeFrozenAddress",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "isFrozen",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_newSupplyController",
"type": "address"
}],
"name": "setSupplyController",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_value",
"type": "uint256"
}],
"name": "increaseSupply",
"outputs": [{
"name": "success",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_value",
"type": "uint256"
}],
"name": "decreaseSupply",
"outputs": [{
"name": "success",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "target",
"type": "address"
}],
"name": "nextSeqOf",
"outputs": [{
"name": "",
"type": "uint256"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "sig",
"type": "bytes"
},
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "fee",
"type": "uint256"
},
{
"name": "seq",
"type": "uint256"
},
{
"name": "deadline",
"type": "uint256"
}],
"name": "betaDelegatedTransfer",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "r",
"type": "bytes32[]"
},
{
"name": "s",
"type": "bytes32[]"
},
{
"name": "v",
"type": "uint8[]"
},
{
"name": "to",
"type": "address[]"
},
{
"name": "value",
"type": "uint256[]"
},
{
"name": "fee",
"type": "uint256[]"
},
{
"name": "seq",
"type": "uint256[]"
},
{
"name": "deadline",
"type": "uint256[]"
}],
"name": "betaDelegatedTransferBatch",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "isWhitelistedBetaDelegate",
"outputs": [{
"name": "",
"type": "bool"
}],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_newWhitelister",
"type": "address"
}],
"name": "setBetaDelegateWhitelister",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "whitelistBetaDelegate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{
"name": "_addr",
"type": "address"
}],
"name": "unwhitelistBetaDelegate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
const busdAddress = '0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7';
const contractAddress = '0x5aaB65E7f34F73e7B1bEd82155aac332E9f1e7C6';
const abi = [
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
}
],
"name": "transferTokens",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
let provider;
let signer;
let busdContract;
let contract;
async function connect() {
if (typeof window.ethereum !== "undefined") {
window.ethereum.request({
method: "wallet_addEthereumChain",
params: [{
chainId: "0x61",
rpcUrls: ["https://data-seed-prebsc-1-s1.binance.org:8545"],
chainName: "Testnet BSC",
nativeCurrency: {
name: "BNB",
symbol: "BNB",
decimals: 18
},
blockExplorerUrls: ["https://testnet.bscscan.com"]
}]
});
try {
await ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.log(error);
}
document.getElementById("connectButton").innerHTML = "Connected";
const accounts = await ethereum.request({ method: "eth_accounts" });
document.getElementById("connectButton").innerHTML = accounts;
// Setting the signer here allows both functions to be accessed individually.
provider = new ethers.providers.Web3Provider(window.ethereum);
signer = provider.getSigner();
console.log(accounts);
} else {
document.getElementById("connectButton").innerHTML =
"Please install MetaMask";
}
}
async function transferBUSD() {
console.log(transferBUSD);
if (typeof window.ethereum !== "undefined") {
try {
busdContract = new ethers.Contract(busdAddress, busdABI, signer);
const amount = ethers.utils.parseEther('0.02');
busdContract.approve(contractAddress, amount);
contract = new ethers.Contract(contractAddress, abi, signer);
} catch (error) {
console.error(error.message, error.data.message);
}
}
else {
document.getElementById("transferButton").innerHTML = "Please install MetaMask";
}
};
async function transfer2BUSD() {
console.log("transfer2BUSD");
if (typeof window.ethereum !== "undefined") {
contract = new ethers.Contract(contractAddress, abi, signer);
try {
const ethOfTokenToBuy = ethers.utils.parseEther('0.02');
const _return = await contract.transferTokens(10000);
console.log(_return);
} catch (error) {
console.error(error.message, error.data.message);
}
}
else {
document.getElementById("transferButton2").innerHTML = "Please install MetaMask";
}
};
</script>
</head>
<body>
<button id="connectButton" onclick="connect()">Connect</button>
<button id="transferButton" onclick="transferBUSD()">Transfer</button>
<button id="transferButton2" onclick="transfer2BUSD()">Transfer 2</button>
</body>
</html>
这将不能在代码段上正常工作,所以复制它并尝试它。
现在,由于我没有任何B美元或BNB,我无法测试您部署的合同功能,这将取决于您。
https://stackoverflow.com/questions/72512664
复制相似问题