Remix macht es so einfach, die JSON ABI zu erhalten.
Hat jemand eine Empfehlung, wie man die JSON ABI am besten erhält, nachdem man erfolgreich einen Vertrag mit Truffle bereitgestellt hat, ohne Remix nutzen zu müssen?
wenn du rennst
$ truffle compile
truffle erstellt und speichert eine JSON-Datei pro Vertrag in /yourProjectPath/build/contracts
. In dieser JSON-Datei finden Sie den abi, den Bytecode, die Themen (falls Sie welche event
in Ihrem Smart Contract haben), die Funktionsdefinitionen usw. nur um ein breites Verständnis zu bekommen).
Persönlich verwende ich Python für meine Projekte. So lade ich mein abi automatisch in meine Skripte, um mit meinen Verträgen zu handeln (mit web3py )
import json
PATH_TRUFFLE_WK = '/home/myUserName/Projects/myEthereumProjet/'
truffleFile = json.load(open(PATH_TRUFFLE_WK + '/build/contracts/myContractName.json'))
abi = truffleFile['abi']
bytecode = truffleFile['bytecode']
Sie können dasselbe mit Ihrer bevorzugten Programmiersprache tun oder einfach Ihr abi von Hand kopieren und einfügen.
@salanfe: Deine Antwort ist richtig.
Es gibt jedoch einige Probleme mit dem Truffle-Compiler. Ich hatte vor allem Probleme mit Funktionen, die keine Eingabeparameter hatten...
Aufgrund dieses Problems schlage ich vor, stattdessen solc zu verwenden:
solcjs --abi path/to/your/contract.sol
Ich hoffe es hilft.
Ich habe ein einfaches Tool geschrieben, das genau das tut, was @salanfe vorgeschlagen hat. Dies ist praktisch, wenn Sie mehrere Vertragsdefinitionen haben:
# Install it from npm
$ npm install -g truffle-export-abi
# Run it in your truffle project
$ truffle-export-abi
ABI extracted and output file wrote to: build/ABI.json
Github-Repository: https://github.com/maxme/truffle-export-abi
Erwägen Sie die direkte Verwendung von solc -compiler.
Bequemer ist es, das Solc-Docker-Image zu verwenden :
docker run \
-v {smart_contract_location_path}:/src:ro \
-v {result_abi_location_path}:/build \
ethereum/solc:0.8.3 \
--allow-paths /src/node_modules \
--overwrite \
-o /build \
--abi \
/src/contracts/test.sol
Bemerkungen:
--ast-compact-json AST of all source files in a compact JSON format.
--asm EVM assembly of the contracts.
--asm-json EVM assembly of the contracts in JSON format.
--opcodes Opcodes of the contracts.
--bin Binary of the contracts in hex.
--bin-runtime Binary of the runtime part of the contracts in hex.
--abi ABI specification of the contracts.
--ir Intermediate Representation (IR) of all contracts
(EXPERIMENTAL).
--ir-optimized Optimized intermediate Representation (IR) of all
contracts (EXPERIMENTAL).
--ewasm Ewasm text representation of all contracts
(EXPERIMENTAL).
--hashes Function signature hashes of the contracts.
--userdoc Natspec user documentation of all contracts.
--devdoc Natspec developer documentation of all contracts.
--metadata Combined Metadata JSON whose Swarm hash is stored
on-chain.
--storage-layout Slots, offsets and types of the contract's state
variables.
Um Importfehler wie „Datei außerhalb der erlaubten Verzeichnisse“ oder „Datei nicht gefunden“ zu vermeiden , definieren Sie den „strengen“ Pfad zur Datei:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
contract Hello is Ownable {
..
}
Führen Sie diesen Befehl aus, um Hilfe zu erhalten:
docker run ethereum/solc:0.8.3 --help
..
--base-path path Use the given path as the root of the source tree
instead of the root of the filesystem.
--allow-paths path(s)
Allow a given path for imports. A list of paths can be
supplied by separating them with a comma.
--ignore-missing Ignore missing files.
..
> truffle compile
Dann auf dem js
import ERC20Contract from '../build/contracts/ERC20';
const ERC20Token = new web3.eth.Contract(ERC20Contract.abi, contractAddress)
scibuff