坚固性错误&quot的参数数量无效。 '

发布于 2025-01-21 18:56:03 字数 3656 浏览 0 评论 0原文

我正在尝试关注YouTube上的坚固教程之一。而且我被困在 this 特定时刻。

这是一个坚固的代码:

    pragma solidity ^0.5.0;
    
    import "./Token.sol";
    
    contract EthSwap {
        string public name = "EthSwap Instant Exchange";
        Token public token;
    
        constructor(Token _token) public {
            token = _token;
        }
    }

这是一个部署代码:

const Token = artifacts.require("Token");
const EthSwap = artifacts.require("EthSwap");

module.exports = async function(deployer) {
  // Deploy Token
  await deployer.deploy(Token);
  const token = await Token.deployed()

  // Deploy EthSwap
  await deployer.deploy(EthSwap, token.address);
  const ethSwap = await EthSwap.deployed()

  // Transfer all tokens to EthSwap (1 million)
  await token.transfer(ethSwap.address, '1000000000000000000000000')
};

这是一个自动测试的代码:

const Token = artifacts.require("Token");
const EthSwap = artifacts.require("EthSwap");

require('chai')
    .use(require('chai-as-promised'))
    .should()

function tokens(n) {
    return web3.utils.toWei(n, 'ether');
}

contract('EthSwap', (accounts) => {
    let token, ethSwap

    before(async () => {
        token = await Token.new()
        ethSwap = await EthSwap.new(token.address)
        // Transfer all tokens to EthSwap (1 million)
        await token.transfer(ethSwap.address, tokens('1000000'))
    })

    describe('Token deployment', async () => {
        it('contract has a name', async () => {
            let token = await Token.new()
            const name = await token.name()
            assert.equal(name, 'DApp Token')
        })
    })

    describe('EthSwap deployment', async () => {
        it('contract has a name', async () => {
            let ethSwap = await EthSwap.new()
            const name = await ethSwap.name()
            assert.equal(name, 'EthSwap Instant Exchange')
        })

        it('contract has tokens', async () => {
            let balance = await token.balanceOf(ethSwap.address)
            assert.equal(balance.toString(), tokens('1000000'))
        })
    })
})

在我将此代码添加到固体代码中之前,测试正常运行:

    constructor(Token _token) public {
        token = _token;
    }

现在我在终端中获得以下内容:

2通过(559ms) 1失败

  1. 合同:ETHSWAP ETHSWAP部署 合同有一个名称: “未定义”的参数数量无效。得到0的预期1! 错误 在吸引力(c:\ user \ user \ appdata \ roaming \ npm \ npm \ node_modules \ truffle \ build \ build \ webpack:\ packages \ contract \ contract \ lib \ lib \ cockievent.js:9:1) 在C:\ USER \ USER \ user \ AppData \ roaming \ npm \ node_modules \ Truffle \ Truffle \ build \ webpack:\ packages \ contract \ contract \ lib \ execute.js:223:1 在function.new(c:\ user \ user \ user \ appdata \ roaming \ npm \ npm \ node_modules \ truffle \ build \ build \ webpack:\ packages \ contract \ contract \ lib \ contruct \ Contructormortorods.js:57:1) 在_callee4 $(c:/users/user/eth_swap/test/ethswap.test.js:32:24) 在TryCatch(node_modules@babel \ runtime \ node_modules \ regenerator-runtime \ runtime.js:45:40) 在generator.invoke [as _invoke](node_modules@babel \ runtime \ node_modules \ regenerator-runtime \ runtime \ runtime.js:271:22) 在Generator.protype上。 [AS NEXT](node_modules@babel \ runtime \ node_modules \ regenerator-runtime \ runtime.js:97:21) 在步骤(test \ ethswap.test.js:3:191) 在C:\ user \ user \ eth_swap \ test \ ethswap.test.js:3:437 在新承诺() 在上下文。 (test \ ethswap.test.js:3:99) 在ProcessImmediate(节点:内部/计时器:466:21)

有人可以解释可能是问题所在。看来我完全按照教程完成了所有操作。

I'm trying to follow along with one of the solidity tutorials on youtube. And I'm getting stuck at this particular moment.

This is a solidity code:

    pragma solidity ^0.5.0;
    
    import "./Token.sol";
    
    contract EthSwap {
        string public name = "EthSwap Instant Exchange";
        Token public token;
    
        constructor(Token _token) public {
            token = _token;
        }
    }

Here is a deployment code:

const Token = artifacts.require("Token");
const EthSwap = artifacts.require("EthSwap");

module.exports = async function(deployer) {
  // Deploy Token
  await deployer.deploy(Token);
  const token = await Token.deployed()

  // Deploy EthSwap
  await deployer.deploy(EthSwap, token.address);
  const ethSwap = await EthSwap.deployed()

  // Transfer all tokens to EthSwap (1 million)
  await token.transfer(ethSwap.address, '1000000000000000000000000')
};

And this is a code for automated test:

const Token = artifacts.require("Token");
const EthSwap = artifacts.require("EthSwap");

require('chai')
    .use(require('chai-as-promised'))
    .should()

function tokens(n) {
    return web3.utils.toWei(n, 'ether');
}

contract('EthSwap', (accounts) => {
    let token, ethSwap

    before(async () => {
        token = await Token.new()
        ethSwap = await EthSwap.new(token.address)
        // Transfer all tokens to EthSwap (1 million)
        await token.transfer(ethSwap.address, tokens('1000000'))
    })

    describe('Token deployment', async () => {
        it('contract has a name', async () => {
            let token = await Token.new()
            const name = await token.name()
            assert.equal(name, 'DApp Token')
        })
    })

    describe('EthSwap deployment', async () => {
        it('contract has a name', async () => {
            let ethSwap = await EthSwap.new()
            const name = await ethSwap.name()
            assert.equal(name, 'EthSwap Instant Exchange')
        })

        it('contract has tokens', async () => {
            let balance = await token.balanceOf(ethSwap.address)
            assert.equal(balance.toString(), tokens('1000000'))
        })
    })
})

The test runed normally before I added this code to the solidity code:

    constructor(Token _token) public {
        token = _token;
    }

Now I get the following in my terminal:

2 passing (559ms)
1 failing

  1. Contract: EthSwap
    EthSwap deployment
    contract has a name:
    Invalid number of parameters for "undefined". Got 0 expected 1!
    Error
    at PromiEvent (C:\Users\user\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\promievent.js:9:1)
    at C:\Users\user\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\execute.js:223:1
    at Function.new (C:\Users\user\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\contract\lib\contract\constructorMethods.js:57:1)
    at _callee4$ (C:/Users/user/eth_swap/test/EthSwap.test.js:32:24)
    at tryCatch (node_modules@babel\runtime\node_modules\regenerator-runtime\runtime.js:45:40)
    at Generator.invoke [as _invoke] (node_modules@babel\runtime\node_modules\regenerator-runtime\runtime.js:271:22)
    at Generator.prototype. [as next] (node_modules@babel\runtime\node_modules\regenerator-runtime\runtime.js:97:21)
    at step (test\EthSwap.test.js:3:191)
    at C:\Users\user\eth_swap\test\EthSwap.test.js:3:437
    at new Promise ()
    at Context. (test\EthSwap.test.js:3:99)
    at processImmediate (node:internal/timers:466:21)

Can someone explane what might be the problem. It seems that I did everything exactly as done in the tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2025-01-28 18:56:03

您的合同具有构造函数函数集。 构造函数函数意味着当您将实例捕获时,您必须传递构造函数(token _token)中使用的参数。这个论点可能是“地址”。因此,在您的部署函数中,

 await deployer.deploy(Token);

您必须将令牌作为第二个参数

 await deployer.deploy(Token,tokenAddress);

,如果您需要更多参数,则只需按顺序传递它们即可。第一个参数是合同,然后是构造函数中的参数,最后一个参数是选项对象。

Your contract has constructor function set. constructor function means when you initalize an instance, you have to pass arguments that used in constructor(Token _token). this argument probably is the "address". So in your deployment function

 await deployer.deploy(Token);

you have to pass the token as the second argument

 await deployer.deploy(Token,tokenAddress);

if you needed more arguments, you would just pass them in order. first argument is the contract, then arguments in constructor, and the last argument is the options object if needed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文