@@ -86,14 +86,14 @@ func NewParams() *Params {
86
86
}
87
87
88
88
// New - swap constructor
89
- func New (stateStore state.Store , prvkey * ecdsa.PrivateKey , contract common. Address , backend contract.Backend ) * Swap {
89
+ func New (stateStore state.Store , prvkey * ecdsa.PrivateKey , backend contract.Backend ) * Swap {
90
90
return & Swap {
91
91
store : stateStore ,
92
92
balances : make (map [enode.ID ]int64 ),
93
93
cheques : make (map [enode.ID ]* Cheque ),
94
94
peers : make (map [enode.ID ]* Peer ),
95
95
backend : backend ,
96
- owner : createOwner (prvkey , contract ),
96
+ owner : createOwner (prvkey ),
97
97
params : NewParams (),
98
98
paymentThreshold : DefaultPaymentThreshold ,
99
99
disconnectThreshold : DefaultDisconnectThreshold ,
@@ -127,13 +127,12 @@ func keyToID(key string, prefix string) enode.ID {
127
127
}
128
128
129
129
// createOwner assings keys and addresses
130
- func createOwner (prvkey * ecdsa.PrivateKey , contract common. Address ) * Owner {
130
+ func createOwner (prvkey * ecdsa.PrivateKey ) * Owner {
131
131
pubkey := & prvkey .PublicKey
132
132
return & Owner {
133
+ address : crypto .PubkeyToAddress (* pubkey ),
133
134
privateKey : prvkey ,
134
135
publicKey : pubkey ,
135
- Contract : contract ,
136
- address : crypto .PubkeyToAddress (* pubkey ),
137
136
}
138
137
}
139
138
@@ -522,9 +521,9 @@ func (s *Swap) GetParams() *swap.Params {
522
521
return s .contract .ContractParams ()
523
522
}
524
523
525
- // verifyContract checks if the bytecode found at address matches the expected bytecode
526
- func (s * Swap ) verifyContract ( ctx context. Context , address common.Address ) error {
527
- return contract . ValidateCode ( ctx , s . backend , address )
524
+ // setChequebookAddr sets the chequebook address
525
+ func (s * Swap ) setChequebookAddr ( chequebookAddr common.Address ) {
526
+ s . owner . Contract = chequebookAddr
528
527
}
529
528
530
529
// getContractOwner retrieve the owner of the chequebook at address from the blockchain
@@ -537,8 +536,38 @@ func (s *Swap) getContractOwner(ctx context.Context, address common.Address) (co
537
536
return contr .Issuer (nil )
538
537
}
539
538
540
- // Deploy deploys the Swap contract
541
- func (s * Swap ) Deploy (ctx context.Context , backend swap.Backend , path string ) error {
539
+ // StartChequebook deploys a new instance of a chequebook if chequebookAddr is empty, otherwise it wil bind to an existing instance
540
+ func (s * Swap ) StartChequebook (chequebookAddr common.Address ) error {
541
+ if chequebookAddr != (common.Address {}) {
542
+ if err := s .BindToContractAt (chequebookAddr ); err != nil {
543
+ return err
544
+ }
545
+ log .Info ("Using the provided chequebook" , "chequebookAddr" , chequebookAddr )
546
+ } else {
547
+ if err := s .Deploy (context .Background (), s .backend ); err != nil {
548
+ return err
549
+ }
550
+ log .Info ("New SWAP contract deployed" , "contract info" , s .DeploySuccess ())
551
+ }
552
+ return nil
553
+ }
554
+
555
+ // BindToContractAt binds an instance of an already existing chequebook contract at address and sets chequebookAddr
556
+ func (s * Swap ) BindToContractAt (address common.Address ) (err error ) {
557
+
558
+ if err := contract .ValidateCode (context .Background (), s .backend , address ); err != nil {
559
+ return fmt .Errorf ("contract validation for %v failed: %v" , address , err )
560
+ }
561
+ s .contract , err = contract .InstanceAt (address , s .backend )
562
+ if err != nil {
563
+ return err
564
+ }
565
+ s .setChequebookAddr (address )
566
+ return nil
567
+ }
568
+
569
+ // Deploy deploys the Swap contract and sets the contract address
570
+ func (s * Swap ) Deploy (ctx context.Context , backend swap.Backend ) error {
542
571
opts := bind .NewKeyedTransactor (s .owner .privateKey )
543
572
// initial topup value
544
573
opts .Value = big .NewInt (int64 (s .params .InitialDepositAmount ))
@@ -550,7 +579,7 @@ func (s *Swap) Deploy(ctx context.Context, backend swap.Backend, path string) er
550
579
log .Error ("unable to deploy swap" , "error" , err )
551
580
return err
552
581
}
553
- s .owner . Contract = address
582
+ s .setChequebookAddr ( address )
554
583
log .Info ("swap deployed" , "address" , address .Hex (), "owner" , opts .From .Hex ())
555
584
556
585
return err
0 commit comments