@@ -4,66 +4,89 @@ package ethutil
44
55import (
66 "context"
7+ "encoding/hex"
78 "fmt"
89
9- "github.com/cartesi/rollups-node/internal/model"
1010 "github.com/cartesi/rollups-node/pkg/contracts/iapplicationfactory"
1111 "github.com/ethereum/go-ethereum/accounts/abi/bind"
1212 "github.com/ethereum/go-ethereum/common"
1313 "github.com/ethereum/go-ethereum/ethclient"
1414)
1515
16- // NOTE: json formatting breaks if we don't use a separate type, don't know why
16+ type IApplicationDeployment interface {
17+ Deploy (ctx context.Context , client * ethclient.Client , txOpts * bind.TransactOpts ) (common.Address , IApplicationDeploymentResult , error )
18+ GetFactoryAddress () common.Address
19+ }
20+ type IApplicationDeploymentResult interface {}
21+
1722type ApplicationDeployment struct {
18- model.Application `json:"application"`
19- FactoryAddress common.Address `json:"factory"`
20- OwnerAddress common.Address `json:"owner"`
21- Salt SaltBytes `json:"salt"`
22- WithAuthority * AuthorityDeployment `json:"authority,omitempty"`
23- WithSelfHosted * SelfhostedDeployment `json:"selfhosted,omitempty"`
23+ FactoryAddress common.Address `json:"factory"`
24+ Consensus common.Address `json:"consensus"`
25+ OwnerAddress common.Address `json:"owner"`
26+ DataAvailability []byte `json:"-"`
27+ TemplateHash common.Hash `json:"template_hash"`
28+ Salt SaltBytes `json:"salt"`
29+
30+ // needed by model.Application
31+ InputBoxAddress common.Address `json:"inputbox_address"`
32+ IInputBoxBlock uint64 `json:"inputbox_block"`
33+ EpochLength uint64 `json:"epoch_length"`
34+
35+ Verbose bool
2436}
2537
26- // function does one of 3 things:
27- // - withSelfHosted != nil => application + authority (together via SelfHosted contract)
28- // - withAuthority != nil => application + authority (as two separate deployments)
29- // - withSelfHosted == nil && withAuthority == nil => application only
30- func (me * ApplicationDeployment ) Deploy (ctx context.Context , client * ethclient.Client , txOpts * bind.TransactOpts ) (common.Address , error ) {
31- var err error
32- zero := common.Address {}
33- var applicationAddress common.Address
38+ type ApplicationDeploymentResult struct {
39+ Deployment * ApplicationDeployment `json:"deployment"`
3440
35- if me .WithSelfHosted != nil {
36- applicationAddress , me .IConsensusAddress , err = me .WithSelfHosted .Deploy (ctx , client , txOpts )
37- if err != nil {
38- return zero , err
39- }
40- return applicationAddress , nil
41- }
41+ ApplicationAddress common.Address `json:"address"`
42+ }
4243
43- if me .WithAuthority != nil {
44- me .IConsensusAddress , err = me .WithAuthority .Deploy (ctx , client , txOpts )
45- if err != nil {
46- return zero , err
47- }
44+ func (me * ApplicationDeployment ) String () string {
45+ result := ""
46+ result += fmt .Sprintf ("application deployment:\n " )
47+ result += fmt .Sprintf ("\t application owner: %v\n " , me .OwnerAddress )
48+ result += fmt .Sprintf ("\t consensus address: %v\n " , me .Consensus )
49+ if me .Verbose {
50+ result += fmt .Sprintf ("\t factory address: %v\n " , me .FactoryAddress )
51+ result += fmt .Sprintf ("\t template hash: %v\n " , me .TemplateHash )
52+ result += fmt .Sprintf ("\t data availability: 0x%v\n " , hex .EncodeToString (me .DataAvailability ))
53+ result += fmt .Sprintf ("\t salt: %v\n " , me .Salt )
54+ result += fmt .Sprintf ("\t epoch length: %v\n " , me .EpochLength )
4855 }
56+ return result
57+ }
58+
59+ func (me * ApplicationDeploymentResult ) String () string {
60+ result := ""
61+ result += fmt .Sprintf ("\t application address: %v\n " , me .ApplicationAddress )
62+ return result
63+ }
4964
65+ func (me * ApplicationDeployment ) Deploy (
66+ ctx context.Context ,
67+ client * ethclient.Client ,
68+ txOpts * bind.TransactOpts ,
69+ ) (common.Address , IApplicationDeploymentResult , error ) {
70+ zero := common.Address {}
71+ result := & ApplicationDeploymentResult {}
72+ result .Deployment = me
5073 factory , err := iapplicationfactory .NewIApplicationFactory (me .FactoryAddress , client )
5174 if err != nil {
52- return zero , fmt .Errorf ("failed to instantiate contract: %v" , err )
75+ return zero , nil , fmt .Errorf ("failed to instantiate contract: %v" , err )
5376 }
5477
55- tx , err := factory .NewApplication (txOpts , me .IConsensusAddress , me .OwnerAddress , me .TemplateHash , me .DataAvailability , me .Salt )
78+ tx , err := factory .NewApplication (txOpts , me .Consensus , me .OwnerAddress , me .TemplateHash , me .DataAvailability , me .Salt )
5679 if err != nil {
57- return zero , fmt .Errorf ("transaction failed: %v" , err )
80+ return zero , nil , fmt .Errorf ("transaction failed: %v" , err )
5881 }
5982
6083 receipt , err := bind .WaitMined (ctx , client , tx )
6184 if err != nil {
62- return zero , fmt .Errorf ("failed to wait for transaction mining: %v" , err )
85+ return zero , nil , fmt .Errorf ("failed to wait for transaction mining: %v" , err )
6386 }
6487
6588 if receipt .Status != 1 {
66- return zero , fmt .Errorf ("transaction failed" )
89+ return zero , nil , fmt .Errorf ("transaction failed" )
6790 }
6891
6992 // Look for the specific event in the receipt logs
@@ -73,7 +96,12 @@ func (me *ApplicationDeployment) Deploy(ctx context.Context, client *ethclient.C
7396 if err != nil {
7497 continue // Skip logs that don't match
7598 }
76- return event .AppContract , nil
99+ result .ApplicationAddress = event .AppContract
100+ return result .ApplicationAddress , result , nil
77101 }
78- return zero , fmt .Errorf ("failed to find ApplicationCreated event in receipt logs" )
102+ return zero , nil , fmt .Errorf ("failed to find ApplicationCreated event in receipt logs" )
103+ }
104+
105+ func (me * ApplicationDeployment ) GetFactoryAddress () common.Address {
106+ return me .FactoryAddress
79107}
0 commit comments