这是我的测试文件
const { assert } = require('chai');
const chaiAsPromised = require('chai-as-promised')
const MyToken = artifacts.require("Saubaan");
const MySwap = artifacts.require("MySwap");
require('chai')
.use(chaiAsPromised)
.should()
function tokens(n){
return web3.utils.toWei(n, 'ether')
}
contract('MySwap', (deployer, investor) => {
let token, myswap
before(async() =>{
token = await MyToken.new()
myswap = await MySwap.new()
await token.transfer(myswap.address,tokens('190000000'))
})
describe('MyToken deployment', async() => {
it('contract has a name', async() => {
const name = await token.name()
assert.equal(name , 'MyToken')
})
})
describe('MySwap deployment', async() => {
it('contract has a name', async() => {
const name = await myswap.name()
assert.equal(name , 'MySwap Ultimate Exchange')
})
it('contract has tokens', async() => {
let balance = await token.balanceOf(myswap.address)
assert.equal(balance.toString(), tokens('190000000'))
})
})
describe ('buyTokens()', async() => {
before( async() => {
result = await myswap.buyTokens({from: investor, value: web3.utils.toWei('1','ether')})
})
it('Allows user to instantly buy tokens from myswap for a fixed price ',async() => {
//check investor balance after purchase
let investorBalance = await token.balancrOf(investor)
assert.equal(investorBalance.toString(), tokens('100'))
//check myswap alance after purchase
let myswapBalance
myswapBalance = await token.balanceOf(myswap.address)
assert.equal(myswapBalance.toString(), tokens('99900'))
myswapbalance = await web3.eth.getBalance(myswap.address)
assert.equal(myswapBalance.toString(), web3.utils.toWei('1', 'ether'))
const event = result.logs[0].args
assert.equal(event.account,investor)
assert.equal(event.token, token.address)
assert.equal(event.amount.toString(), token('100').toString())
assert.equal(event.rate.toString(), '100')
})
})
describe ('sellTokens()', async() => {
let result
before( async() => {
// investor must approve the purchase
await token.approve(myswap.address, tokens('100'), {from: investor})
result = await myswap.sellTokens(tokens('100'), {from: investor})
})
it('Allows user to instantly sell tokens from myswap for a fixed price ',async() => {
// Check investro balance after purchase
let investorBalance = await token.balanceOf(investor)
assert.equal(investorBalance.toString(), tokens('100'))
})
})
})
这是我的交换合同
pragma solidity 0.8.6;
import "./MyToken.sol";
contract MySwap {
string public name = "MySwap Ultimate Exchange";
Saubaan public token;
uint public rate = 100;
event TokenPurchased(
address account,
address token,
uint amount,
uint rate
);
constructor (Saubaan _token) public {
token = _token;
}
function buyTokens() public payable {
uint tokenAmount = msg.value*rate;
// Require that myswap has enough tokens
require(token.balanceOf(address(this)) >= tokenAmount);
// Transfer tokens to the user
token.transfer(msg.sender, tokenAmount );
// Emit an event
emit TokenPurchased(msg.sender, address(token), tokenAmount, rate);
}
function sellTokens(uint _amount) public payable {
//Calculate the amount of Ether to redeem
uint etherAmount = _amount / rate;
//perform sale
token.transferFrom(msg.sender, address(this), _amount);
payable(msg.sender).transfer(etherAmount);
}
}
这是我的代币合同
pragma solidity 0.8.6;
interface ERC20Interface{
function totalSupply() external view returns (uint256);
function balanceOf(address tokenOwner) external view returns (uint);
function allowance(address tokenOwner, address spender)
external view returns (uint);
function transfer(address to, uint tokens) external returns (bool);
function approve(address spender, uint tokens) external returns (bool);
function transferFrom(address from, address to, uint tokens) external
returns
(bool);
event Approval(address indexed tokenOwner, address indexed spender,
uint tokens);
event Transfer(address indexed from, address indexed to,
uint tokens);
}
contract Saubaan is ERC20Interface{
string public symbol;
string public name;
uint8 public decimals;
uint public _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
constructor() public {
symbol = "SAU";
name = "Saubaan";
decimals = 2;
_totalSupply = 19000000000;
balances[msg.sender] = _totalSupply;
emit Transfer(address(0), 0x9fC33138878450a7475ff8b83028277a6BBc60DB,
_totalSupply);
}
function totalSupply() public view override returns (uint) {
return _totalSupply - balances[address(0)];
}
function balanceOf(address tokenOwner) public view override returns (uint
balance) {
return balances[tokenOwner];
}
function transfer(address to, uint tokens) public override returns (bool )
{
require(tokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}
function approve(address spender, uint tokens) public override returns
(bool ) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public
override returns (bool ) {
require(tokens <= balances[from]);
require(tokens <= allowed[from][msg.sender]);
balances[from] = balances[from].sub(tokens);
allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
balances[to] = balances[to] + tokens;
emit Transfer(from, to, tokens);
return true;
}
function allowance(address tokenOwner, address spender) public override
view returns (uint ) {
return allowed[tokenOwner][spender];
}
using SafeMath for uint;
}
library SafeMath{
function sub(uint a, uint b) internal pure returns(uint){
assert(b <= a);
return a - b;
}
function add(uint a, uint b) internal pure returns(uint){
uint c = a + b;
assert(c >= a);
return c;
}
}
现在的问题是,每当我尝试运行块菌测试时,它都会在控制台中显示这个错误
Contract: MySwap
1) "before all" hook in "Contract: MySwap"
0 passing (1s)
1 failing
1) Contract: MySwap
"before all" hook in "Contract: MySwap":
Error: Invalid number of parameters for "undefined". Got 0 expected 1
我不知道如何解决这个错误,我已经在互联网上搜索过了,但我找不到任何解决方法。有人请帮助我,因为我是新的编码和东西提前感谢
暂无答案!
目前还没有任何答案,快来回答吧!