@@ -34,17 +34,33 @@ func (me *AuthorityDeployment) String() string {
3434 return result
3535}
3636
37- func (me AuthorityDeployment ) Deploy (
37+ func (me * AuthorityDeployment ) Deploy (
3838 ctx context.Context ,
3939 client * ethclient.Client ,
4040 txOpts * bind.TransactOpts ,
4141) (common.Address , error ) {
42- contract , err := iauthorityfactory .NewIAuthorityFactory (me .FactoryAddress , client )
42+ zero := common.Address {}
43+ factory , err := iauthorityfactory .NewIAuthorityFactory (me .FactoryAddress , client )
4344 if err != nil {
4445 return common.Address {}, fmt .Errorf ("failed to instantiate contract: %v" , err )
4546 }
4647
47- tx , err := contract .NewAuthority0 (txOpts , me .OwnerAddress , big .NewInt (int64 (me .EpochLength )), me .Salt )
48+ // check if addresses are available (have no code)
49+ authorityAddress , err := factory .CalculateAuthorityAddress (nil , me .OwnerAddress , new (big.Int ).SetUint64 (me .EpochLength ), me .Salt )
50+ if err != nil {
51+ return zero , err
52+ }
53+
54+ authorityCode , err := client .CodeAt (ctx , authorityAddress , nil )
55+ if err != nil {
56+ return zero , err
57+ }
58+ if len (authorityCode ) != 0 {
59+ return zero , fmt .Errorf ("authority with address: %v already exists. Try a different salt." , authorityAddress )
60+ }
61+
62+ // deploy the contracts
63+ tx , err := factory .NewAuthority0 (txOpts , me .OwnerAddress , new (big.Int ).SetUint64 (me .EpochLength ), me .Salt )
4864 if err != nil {
4965 return common.Address {}, fmt .Errorf ("failed to create new authority: %v" , err )
5066 }
@@ -60,7 +76,7 @@ func (me AuthorityDeployment) Deploy(
6076
6177 // search for the matching event
6278 for _ , vLog := range receipt .Logs {
63- event , err := contract .ParseAuthorityCreated (* vLog )
79+ event , err := factory .ParseAuthorityCreated (* vLog )
6480 if err != nil {
6581 continue // Skip logs that don't match
6682 }
0 commit comments