@@ -11,6 +11,7 @@ import (
1111 "github.com/gorilla/mux"
1212 batch "github.com/rocket-pool/batch-query"
1313 "github.com/rocket-pool/node-manager-core/eth"
14+ "github.com/rocket-pool/rocketpool-go/core"
1415 "github.com/rocket-pool/rocketpool-go/dao/oracle"
1516 "github.com/rocket-pool/rocketpool-go/rocketpool"
1617 "github.com/rocket-pool/rocketpool-go/tokens"
@@ -49,12 +50,14 @@ type oracleDaoJoinContext struct {
4950 handler * OracleDaoHandler
5051 rp * rocketpool.RocketPool
5152 nodeAddress common.Address
52-
53- odaoMember * oracle.OracleDaoMember
54- odaoMgr * oracle.OracleDaoManager
55- oSettings * oracle.OracleDaoSettings
56- rpl * tokens.TokenRpl
57- rplBalance * big.Int
53+ dnta * core.Contract
54+
55+ odaoMember * oracle.OracleDaoMember
56+ odaoMgr * oracle.OracleDaoManager
57+ oSettings * oracle.OracleDaoSettings
58+ rpl * tokens.TokenRpl
59+ rplBalance * big.Int
60+ rplAllowance * big.Int
5861}
5962
6063func (c * oracleDaoJoinContext ) Initialize () (types.ResponseStatus , error ) {
@@ -82,6 +85,10 @@ func (c *oracleDaoJoinContext) Initialize() (types.ResponseStatus, error) {
8285 return types .ResponseStatus_Error , fmt .Errorf ("error creating Oracle DAO manager binding: %w" , err )
8386 }
8487 c .oSettings = c .odaoMgr .Settings
88+ c .dnta , err = c .rp .GetContract (rocketpool .ContractName_RocketDAONodeTrustedActions )
89+ if err != nil {
90+ return types .ResponseStatus_Error , fmt .Errorf ("error getting RPL token contract: %w" , err )
91+ }
8592 return types .ResponseStatus_Success , nil
8693}
8794
@@ -92,6 +99,7 @@ func (c *oracleDaoJoinContext) GetState(mc *batch.MultiCaller) {
9299 c .odaoMember .Exists ,
93100 c .oSettings .Member .RplBond ,
94101 )
102+ c .rpl .GetAllowance (mc , & c .rplAllowance , c .nodeAddress , c .dnta .Address )
95103 c .rpl .BalanceOf (mc , & c .rplBalance , c .nodeAddress )
96104}
97105
@@ -113,23 +121,23 @@ func (c *oracleDaoJoinContext) PrepareData(data *api.OracleDaoJoinData, opts *bi
113121 data .CanJoin = ! (data .ProposalExpired || data .AlreadyMember || data .InsufficientRplBalance )
114122
115123 // Get the tx
116- if data .CanJoin && opts != nil {
117- dnta , err := c .rp .GetContract (rocketpool .ContractName_RocketDAONodeTrustedActions )
118- if err != nil {
119- return types .ResponseStatus_Error , fmt .Errorf ("error getting RPL token contract: %w" , err )
120- }
121-
122- approveTxInfo , err := c .rpl .Approve (dnta .Address , rplBond , opts )
123- if err != nil {
124- return types .ResponseStatus_Error , fmt .Errorf ("error getting TX info for RPL approval: %w" , err )
125- }
126- data .ApproveTxInfo = approveTxInfo
127-
128- joinTxInfo , err := c .odaoMgr .Join (opts )
129- if err != nil {
130- return types .ResponseStatus_Error , fmt .Errorf ("error getting TX info for Join: %w" , err )
124+ if data .CanJoin {
125+ if c .rplAllowance .Cmp (rplBond ) < 0 {
126+ // Do the approve TX if needed
127+ diff := big .NewInt (0 ).Sub (rplBond , c .rplAllowance )
128+ approveTxInfo , err := c .rpl .Approve (c .dnta .Address , diff , opts )
129+ if err != nil {
130+ return types .ResponseStatus_Error , fmt .Errorf ("error getting TX info for RPL approval: %w" , err )
131+ }
132+ data .ApproveTxInfo = approveTxInfo
133+ } else {
134+ // Just do the join
135+ joinTxInfo , err := c .odaoMgr .Join (opts )
136+ if err != nil {
137+ return types .ResponseStatus_Error , fmt .Errorf ("error getting TX info for Join: %w" , err )
138+ }
139+ data .JoinTxInfo = joinTxInfo
131140 }
132- data .JoinTxInfo = joinTxInfo
133141 }
134142 return types .ResponseStatus_Success , nil
135143}
0 commit comments