Using JSON strings as an ABI #3990
-
Ethers Version6.3.0 Search TermsNo response Describe the ProblemThis is my code: The issue is related to the Code Snippetconst provider = new ethers.BrowserProvider(ethereum, "any");
const FAUCET_CONTRACT_ADDRESS = "0x9BdCbC868519Cf2907ECE4E9602346c3fC9e6c8e";
const abi = `[
{
"inputs": [],
"name": "withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
]`;
// this is what is causing the compile error for some reason
const faucetContract = new ethers.Contract(
FAUCET_CONTRACT_ADDRESS,
abi,
provider.getSigner()
); Contract ABINo response ErrorsUnexpected token ']', ..."n"
},
]" is not valid JSON
SyntaxError: Unexpected token ']', ..."n"
},
]" is not valid JSON
at JSON.parse (<anonymous>)
at Interface.from (http://localhost:3000/static/js/bundle.js:79628:33)
at new BaseContract (http://localhost:3000/static/js/bundle.js:81080:72)
at new Contract (http://localhost:3000/static/js/bundle.js:81437:1)
at ./src/App.js (http://localhost:3000/static/js/bundle.js:53:24)
at options.factory (http://localhost:3000/static/js/bundle.js:92171:31)
at __webpack_require__ (http://localhost:3000/static/js/bundle.js:91565:33)
at fn (http://localhost:3000/static/js/bundle.js:91828:21)
at ./src/index.js (http://localhost:3000/static/js/bundle.js:380:62)
at options.factory (http://localhost:3000/static/js/bundle.js:92171:31) EnvironmentBrowser (Chrome, Safari, etc) Environment (Other)Using create-react-app so webpack |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Can you remove the back ticks (" |
Beta Was this translation helpful? Give feedback.
-
If passing a string in as an ABI, it must be valid JSON. To check if your string is valid, you can use The above JSON is invalid because it has a trailing comma ( |
Beta Was this translation helpful? Give feedback.
If passing a string in as an ABI, it must be valid JSON. To check if your string is valid, you can use
JSON.parse
in your code, in which case you can just pass the parsed object. :)The above JSON is invalid because it has a trailing comma (
,
) in the 8th line. It looks like it was likely cut-and-paste from a larger ABI, but it's important to note the JSON is very strict as to its grammar.