gencode.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build none
  17. // This program generates contract/code.go, which contains the chequebook code
  18. // after deployment.
  19. package main
  20. import (
  21. "fmt"
  22. "io/ioutil"
  23. "math/big"
  24. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  25. "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  26. "github.com/ethereum/go-ethereum/contracts/chequebook/contract"
  27. "github.com/ethereum/go-ethereum/core"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. )
  30. var (
  31. testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  32. testAlloc = core.GenesisAlloc{
  33. crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(500000000000)},
  34. }
  35. )
  36. func main() {
  37. backend := backends.NewSimulatedBackend(testAlloc)
  38. auth := bind.NewKeyedTransactor(testKey)
  39. // Deploy the contract, get the code.
  40. addr, _, _, err := contract.DeployChequebook(auth, backend)
  41. if err != nil {
  42. panic(err)
  43. }
  44. backend.Commit()
  45. code, err := backend.CodeAt(nil, addr, nil)
  46. if err != nil {
  47. panic(err)
  48. }
  49. if len(code) == 0 {
  50. panic("empty code")
  51. }
  52. // Write the output file.
  53. content := fmt.Sprintf(`package contract
  54. // ContractDeployedCode is used to detect suicides. This constant needs to be
  55. // updated when the contract code is changed.
  56. const ContractDeployedCode = "%#x"
  57. `, code)
  58. if err := ioutil.WriteFile("contract/code.go", []byte(content), 0644); err != nil {
  59. panic(err)
  60. }
  61. }