github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/contracts/chequebook/gencode.go (about)

     1  // Copyright 2016 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // +build none
    18  
    19  // This program generates contract/code.go, which contains the chequebook code
    20  // after deployment.
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"io/ioutil"
    26  	"math/big"
    27  
    28  	"github.com/SmartMeshFoundation/Spectrum/accounts/abi/bind"
    29  	"github.com/SmartMeshFoundation/Spectrum/accounts/abi/bind/backends"
    30  	"github.com/SmartMeshFoundation/Spectrum/contracts/chequebook/contract"
    31  	"github.com/SmartMeshFoundation/Spectrum/core"
    32  	"github.com/SmartMeshFoundation/Spectrum/crypto"
    33  )
    34  
    35  var (
    36  	testKey, _  = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    37  	testAccount = core.GenesisAccount{
    38  		Address: crypto.PubkeyToAddress(testKey.PublicKey),
    39  		Balance: big.NewInt(500000000000),
    40  	}
    41  )
    42  
    43  func main() {
    44  	backend := backends.NewSimulatedBackend(testAccount)
    45  	auth := bind.NewKeyedTransactor(testKey)
    46  
    47  	// Deploy the contract, get the code.
    48  	addr, _, _, err := contract.DeployChequebook(auth, backend)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	backend.Commit()
    53  	code, err := backend.CodeAt(nil, addr, nil)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  	if len(code) == 0 {
    58  		panic("empty code")
    59  	}
    60  
    61  	// Write the output file.
    62  	content := fmt.Sprintf(`package contract
    63  
    64  // ContractDeployedCode is used to detect suicides. This constant needs to be
    65  // updated when the contract code is changed.
    66  const ContractDeployedCode = "%#x"
    67  `, code)
    68  	if err := ioutil.WriteFile("contract/code.go", []byte(content), 0644); err != nil {
    69  		panic(err)
    70  	}
    71  }