![](/img/trans.png)
[英]SyntaxError: Unexpected token o in JSON at position 1. In Solidity version ^0.8.7
[英]Solidity - When deploying the contract I get "SyntaxError: Unexpected token o in JSON at position 1"
我刚开始学习solidity,但我无法成功部署合约。 当我npm run test
时,我得到: SyntaxError: Unexpected token o in JSON at position 1
我的合同Inbox.sol
:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Inbox {
string public message;
constructor(string memory initialMessage) {
message = initialMessage;
}
function setMessage(string memory newMessage) public {
message = newMessage;
}
}
我的编译器compile.js
:
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
var input = {
language: 'Solidity',
sources: {
'Inbox.sol' : {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
};
var output = JSON.parse(solc.compile(JSON.stringify(input)))
module.exports = output.contracts["Inbox.sol"]["Inbox"];
我的摩卡测试Inbox.test.js
:
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { abi, evm } = require('../compile.js')
var accounts;
var inbox;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(JSON.parse(evm))
.deploy({ data: abi, arguments: ['Hi there!'] })
.send({ from: accounts[0], gas: '1000000' })
})
describe('Inbox Contract', ()=> {
it('Can test', ()=> {
console.log(inbox)
})
})
我不确定 JSON 的哪一部分是错误的,因为它无法正确解析它。 当我在 Inbox.test.js 中登录abi
时,我得到了这个:
Inbox Contract
[ { inputs: [ [Object] ],
stateMutability: 'nonpayable',
type: 'constructor' },
{ inputs: [],
name: 'message',
outputs: [ [Object] ],
stateMutability: 'view',
type: 'function' },
{ inputs: [ [Object] ],
name: 'setMessage',
outputs: [],
stateMutability: 'nonpayable',
type: 'function' } ]
记录evm
给了我一个非常大的代码块,我不会在这里粘贴。
在版本 pragma solidity ^0.8.9 中,我的工作代码如下所示
收件箱.sol
pragma solidity ^0.8.9;
contract Inbox {
string public message;
constructor(string memory initialMessage) {
message = initialMessage;
}
function setMessage(string memory newMessage) public {
message = newMessage;
}
}
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
const input = {
language: 'Solidity',
sources: {
'Inbox.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};
module.exports = JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol'].Inbox;
收件箱.test.js
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { abi, evm } = require('../compile');
let accounts;
let inbox;
beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(abi)
.deploy({
data: evm.bytecode.object,
arguments: ['Hi there!'],
})
.send({ from: accounts[0], gas: '1000000' });
});
describe('Inbox', () => {
it('deploys a contract', () => {
console.log(inbox);
});
});
在您的Inbox.test.js
文件中,我可以看到您在部署合同时传递了错误的 arguments。 您需要在创建新合约时传递interface (abi)
数据,并将bytecode
作为data
object 在deploy()
function 中传递。
代码应如下所示:
inbox = await new web3.eth.Contract(abi)
.deploy({ data: evm.bytecode.object, arguments: ['Hi there!'] })
.send({ from: accounts[0], gas: '1000000' })
另外,检查solidity 编译器版本。 Go 到您的package.json
文件。 在dependencies
项 object 下,您将找到solc
版本。 在此确保solidity 版本与您在Inbox.sol
文件中使用的版本相同。
如果版本不同,请使用npm
安装正确的版本在我的情况下,我在我的solidity文件中使用^0.8.8。 所以我运行以下命令:
npm install solc@0.8.8
您可以在这里查看我的相关答案: SyntaxError: Unexpected token o in JSON at position 1. 在 Solidity 版本中 ^0.8.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.