- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在测试包含 transferFrom
的函数时,我的代码正在恢复。还原被隔离到这一行,当被注释掉时,它运行正常。
到目前为止我的(错误的)假设:
发行批准代币(错误的from
地址,或错误的Loan
合约地址?)
将错误的 this.Token.address
传递给 createLoan
关于其他问题的任何想法?
这是我的契约(Contract) Loan.sol
pragma solidity ^0.5.0;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "./DSmath.sol";
contract Loan is Ownable, DSMath {
...
function createLoan
(
uint _loanAmount,
uint _collateralAmount,
address _collateralAddress
)
external {
require(loaneeToDebt[msg.sender] == 0, "User already owes tokens");
require
(
isCollateralized(_loanAmount, _collateralAmount, _collateralAddress),
"Collateral posted is insufficient to receive a loan"
);
require(tokenPrices[_collateralAddress] != 0, "Collateral token not registered to system");
ERC20(_collateralAddress).transferFrom(msg.sender, address(this), _collateralAmount); //REVERTS HERE
loaneeToDebt[msg.sender] = _collateralAmount;
}
我正在 Loan.test.js
中这样测试:
// Loan.test.js
const {BN, expectEvent, shouldFail, constants} = require("openzeppelin-test-helpers");
const Loan = artifacts.require("Loan");
const ERC20Mock = artifacts.require("ERC20Mock")
contract("Loan", function ([_, contractOwner, user]) {
const initialSupply = new BN(1).mul(new BN(10).pow(new BN(28)))
beforeEach(async function () {
this.Loan = await Loan.new({from: contractOwner});
this.Token = await ERC20Mock.new(user, initialSupply)
});
describe("#createLoan", function () {
const collateralAmount = new BN(5).mul(new BN(10).pow(new BN(27)))
const loanAmount = new BN(1).mul(new BN(10).pow(new BN(24)))
const tokenPrice = new BN(1)
beforeEach(async function () {
await this.Loan.setTokenPrice(this.Token.address, tokenPrice, {from: contractOwner});
});
it("should revert if the user has an outstanding loan", async function () {
await this.Token.approve(this.Loan.address, collateralAmount, {from: user}); // APPROVAL
await this.Loan.createLoan(loanAmount, collateralAmount, this.Token.address, {from: user}) // REVERTS HERE
shouldFail.reverting(this.Loan.createLoan(loanAmount, collateralAmount, this.Token.address, {from: user});
});
});
});
使用ERC20Mock
:
pragma solidity ^0.5.0;
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract ERC20Mock is ERC20 {
constructor (address initialAccount, uint256 initialBalance) public {
_mint(initialAccount, initialBalance);
}
function mint(address account, uint256 amount) public {
_mint(account, amount);
}
function burn(address account, uint256 amount) public {
_burn(account, amount);
}
function burnFrom(address account, uint256 amount) public {
_burnFrom(account, amount);
}
}
最佳答案
为了能够进行 transferFrom,您需要先批准用户。
function approve(address spender, uint256 value) external returns (bool);
在您的情况下,贷款契约(Contract)应由消息发件人批准,金额与 _collateralAmount 相同。
您还可以使用以下功能查看一个人有多少津贴:
function allowance(address owner, address spender) external view returns (uint256);
在 createLoan() 的开头添加一个 require 来检查津贴是否足够是值得的。
关于solidity - 在 ERC20.transferFrom 上恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54348232/
我是 solidity 和 erc20 的新手,所以我阅读了关于 openzeppelin 的 ERC20 描述。并找到这个我不清楚的功能。 approve(spender, amount) 允许sp
我正在使用下面的 elisp 代码来修改来自 ERC 的传入消息 (defun my-modify (msg) (replace-regexp-in-string "abc" "xyz" msg)
抽象账户是什么 抽象账户(也有叫合约钱包)是 EIP-4337 提案提出的一个标准。简单来说就是通过智能合约来实现一个“账户(account)”,在合约中自行实现签名验证的逻辑。这样,就使得该
有没有办法让 ERC 突出显示与某个正则表达式匹配的所有行?就上下文而言,我正在使用 ERC 连接到 bitlbee 服务器,并希望当我发出“blist”命令时,在线的 friend 以绿色突出显示,
有人能告诉我从以太坊区 block 链获取 ERC-20 代币信息(小数位数、名称和符号)的正确方法吗? 我认为可以通过 ABI 构造(如果用户 web3js 库)调用适当的函数 decimals()
我对 Web3 开发确实很陌生,想知道是否有一种方法可以从智能合约中找到的代币地址获取有关代币的更多信息(即我有一个代币地址,但不知道是什么类型的代币)直到我在 etherscan 上查找交易哈希)。
我对 Web3 开发确实很陌生,想知道是否有一种方法可以从智能合约中找到的代币地址获取有关代币的更多信息(即我有一个代币地址,但不知道是什么类型的代币)直到我在 etherscan 上查找交易哈希)。
Emacs erc 保持重新居中。因此很难跟上小缓冲区中的对话。 我尝试了以下方法,但似乎都不起作用... (erc-scrolltobottom-mode) (require 'erc-goodie
我正在开发一个智能合约 (SC) 和一个前端 Dapp,用于销售一些 NFT ERC-721 代币。当用户/地址购买其中一个 NFT 代币时,该代币将由 SC 为他的利益类型转换,而我的 Dapp 将
我有一份 ERC721 合约,我有一个问题,我正在尝试以另一种货币设置价格,例如 UNI 或 SUSHI但问题是我不知道如何更改它,我对契约(Contract)了解不多,这是代码。 我想知道是否有可能
我有一份来自 Hash Lip 的 github 的智能合约,据我所知应该一次类型转换 1 个,但每次都类型转换 2 个。代码如下: 设置代码: // SPDX-License-Identifier:
如果有人为 eth 出售或购买我们的代币(例如,我的意思是 uniswap),有没有办法检查 erc-20 代币的 transfer() 方向? 最佳答案 Uniswap 交易是掉期,而不是买卖。它可
我需要一个功能的帮助,一旦他的内容(Ipfs-Hash,见下面的程序)达到 n 个喜欢(假设每 100 个喜欢),就用 erc777-token 奖励用户,即每当 ' netLike' 计数器达到 1
The ERC Manual显示加载身份验证信息的代码: (加载“~/.emacs.d/.erc-auth”) 但没有显示身份验证信息的样子。我希望有一个示例文件。 最佳答案 它没有任何特定的格式。它
我最近一直在尝试学习有关智能合约的知识,但是当我试图了解代币转移的工作原理时,我遇到了这个问题。 ||执行恢复:ERC20:从零地址转账|| (ropsten 网络) 代码: import (
我正在寻找一种方法来获取 token 的买卖税,并检查它是否可以在购买后出售。 为此,这是我想到的方法: 1-在 DEX 路由器合约上模拟购买交易 2-在 DEX 路由器合约上模拟卖出交易 3-如果上
我正在尝试对我现有的 Rails 应用程序进行 docker 化。我使用 credentials.yml 已经有一段时间了。我的 credentials.yml 文件看起来像这样: productio
我是一名优秀的程序员,十分优秀!