是的,Abigen生成的代码可以在Quorum上工作。以下是一个示例:
在终端中,使用以下命令将智能合约编译为ABI和bin文件:
solc --abi MyContract.sol -o build/
solc --bin MyContract.sol -o build/
使用Abigen生成Go语言的绑定:
abigen --bin=build/MyContract.bin --abi=build/MyContract.abi --pkg=main --out=MyContract.go
在Quorum中使用上述Go代码:
package main
import (
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"./MyContract" // 导入生成的代码
)
func main() {
client, err := ethclient.Dial("http://localhost:8545")
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("私钥")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
auth.GasLimit = uint64(300000)
auth.GasPrice = gasPrice
address := common.HexToAddress("智能合约地址")
instance, err :=