Si vous utilisez golang pour vous connecter à Ethreum, il est facile de générer un code de liaison qui exécute les contrats intelligents d'Ethereum directement à partir de golang. De l'année précédente à l'année dernière, j'ai mis en œuvre cette méthode lorsque j'ai créé un service d'entiercement en utilisant le contrat intelligent d'Ethereum.
doux | Explication | Comment installer |
---|---|---|
solcjs | Liaison JavaScript pour Solidity | npm install solc |
abigen | Reliure golang basée sur ABI * Inclus avec geth |
go get github.com/ethereum/go-ethereum cd $GOPATH/src/github.com/ethereum/go-ethereum go install ./cmd/abigen |
Code source de solidité pour un contrat intelligent standard ERC20.
Erc20.sol
pragma solidity ^0.4.24;
contract ERC20 {
string public constant name = "";
string public constant symbol = "";
uint8 public constant decimals = 0;
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
Une fois que vous avez généré abi à partir de la solidité et la liaison golang à partir d'abi.
solidity -> abi (Erc20_sol_ERC20.Générer abi)
$ solcjs --abi Erc20.sol
abi ->reliure de Golang(Erc20.Générer go)
$ abigen --abi Erc20_sol_ERC20.abi -pkg contract -out Erc20.go
Une méthode est générée dans Erc20.go suivant l'interface de solidité.
func (_Erc20 *Erc20Raw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
func (_Erc20 *Erc20Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
func (_Erc20 *Erc20Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
func (_Erc20 *Erc20CallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error {
func (_Erc20 *Erc20TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
func (_Erc20 *Erc20TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
func (_Erc20 *Erc20Caller) Allowance(opts *bind.CallOpts, tokenOwner common.Address, spender common.Address) (*big.Int, error) {
func (_Erc20 *Erc20Session) Allowance(tokenOwner common.Address, spender common.Address) (*big.Int, error) {
func (_Erc20 *Erc20CallerSession) Allowance(tokenOwner common.Address, spender common.Address) (*big.Int, error) {
func (_Erc20 *Erc20Caller) BalanceOf(opts *bind.CallOpts, tokenOwner common.Address) (*big.Int, error) {
func (_Erc20 *Erc20Session) BalanceOf(tokenOwner common.Address) (*big.Int, error) {
func (_Erc20 *Erc20CallerSession) BalanceOf(tokenOwner common.Address) (*big.Int, error) {
func (_Erc20 *Erc20Caller) Decimals(opts *bind.CallOpts) (uint8, error) {
func (_Erc20 *Erc20Session) Decimals() (uint8, error) {
func (_Erc20 *Erc20CallerSession) Decimals() (uint8, error) {
func (_Erc20 *Erc20Caller) Name(opts *bind.CallOpts) (string, error) {
func (_Erc20 *Erc20Session) Name() (string, error) {
func (_Erc20 *Erc20CallerSession) Name() (string, error) {
func (_Erc20 *Erc20Caller) Symbol(opts *bind.CallOpts) (string, error) {
func (_Erc20 *Erc20Session) Symbol() (string, error) {
func (_Erc20 *Erc20CallerSession) Symbol() (string, error) {
func (_Erc20 *Erc20Caller) TotalSupply(opts *bind.CallOpts) (*big.Int, error) {
func (_Erc20 *Erc20Session) TotalSupply() (*big.Int, error) {
func (_Erc20 *Erc20CallerSession) TotalSupply() (*big.Int, error) {
func (_Erc20 *Erc20Transactor) Approve(opts *bind.TransactOpts, spender common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20Session) Approve(spender common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20TransactorSession) Approve(spender common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20Transactor) Transfer(opts *bind.TransactOpts, to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20Session) Transfer(to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20TransactorSession) Transfer(to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20Transactor) TransferFrom(opts *bind.TransactOpts, from common.Address, to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20Session) TransferFrom(from common.Address, to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (_Erc20 *Erc20TransactorSession) TransferFrom(from common.Address, to common.Address, tokens *big.Int) (*types.Transaction, error) {
func (it *Erc20ApprovalIterator) Next() bool {
func (it *Erc20ApprovalIterator) Error() error {
func (it *Erc20ApprovalIterator) Close() error {
func (_Erc20 *Erc20Filterer) FilterApproval(opts *bind.FilterOpts, tokenOwner []common.Address, spender []common.Address) (*Erc20ApprovalIterator, error) {
func (_Erc20 *Erc20Filterer) WatchApproval(opts *bind.WatchOpts, sink chan<- *Erc20Approval, tokenOwner []common.Address, spender []common.Address) (event.Subscription, error) {
func (it *Erc20TransferIterator) Next() bool {
func (it *Erc20TransferIterator) Error() error {
func (it *Erc20TransferIterator) Close() error {
func (_Erc20 *Erc20Filterer) FilterTransfer(opts *bind.FilterOpts, from []common.Address, to []common.Address) (*Erc20TransferIterator, error) {
func (_Erc20 *Erc20Filterer) WatchTransfer(opts *bind.WatchOpts, sink chan<- *Erc20Transfer, from []common.Address, to []common.Address) (event.Subscription, error) {
Désormais, vous pouvez facilement exécuter des contrats intelligents depuis golang.
client, _ := ethclient.Dial("URL de connexion Ethereum(infura.io etc.)")
erc20, _ := contract.NewErc20("Adresse du contrat(type:common.Address)", client)
balance, _ := erc20.BalanceOf(&bind.CallOpts{}, "Adresse où vous souhaitez vérifier le solde(type:common.Address)")
Recommended Posts