Skip to content

Commit 4231405

Browse files
committed
Fixed multi-tx functions
1 parent 4079762 commit 4231405

7 files changed

Lines changed: 99 additions & 62 deletions

File tree

src/rocketpool-cli/commands/node/stake-rpl.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func nodeStakeRpl(c *cli.Context) error {
9797
}
9898

9999
// Handle boosting the allowance
100-
if stakeResponse.Data.Allowance.Cmp(amountWei) < 0 {
100+
if stakeResponse.Data.ApproveTxInfo != nil {
101101
fmt.Println("Before staking RPL, you must first give the staking contract approval to interact with your RPL.")
102102
fmt.Println("This only needs to be done once for your node.")
103103

@@ -106,7 +106,7 @@ func nodeStakeRpl(c *cli.Context) error {
106106
utils.PrintMultiTransactionNonceWarning()
107107
}
108108

109-
// Run the approve TX
109+
// Run the Approve TX
110110
validated, err := tx.HandleTx(c, rp, stakeResponse.Data.ApproveTxInfo,
111111
"Do you want to let the staking contract interact with your RPL?",
112112
"approving RPL for staking",
@@ -119,6 +119,11 @@ func nodeStakeRpl(c *cli.Context) error {
119119
fmt.Println("Successfully approved staking access to RPL.")
120120
}
121121

122+
// Build the stake TX once approval is done
123+
stakeResponse, err = rp.Api.Node.StakeRpl(amountWei)
124+
if err != nil {
125+
return err
126+
}
122127
}
123128

124129
// Run the stake TX

src/rocketpool-cli/commands/node/utils.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func SwapRpl(c *cli.Context, rp *client.Client, amountWei *big.Int) error {
414414
}
415415

416416
// Handle boosting the allowance
417-
if response.Data.Allowance.Cmp(amountWei) < 0 {
417+
if response.Data.ApproveTxInfo != nil {
418418
fmt.Println("Before swapping legacy RPL for new RPL, you must first give the new RPL contract approval to interact with your legacy RPL.")
419419
fmt.Println("This only needs to be done once for your node.")
420420

@@ -436,6 +436,11 @@ func SwapRpl(c *cli.Context, rp *client.Client, amountWei *big.Int) error {
436436
fmt.Println("Successfully approved access to legacy RPL.")
437437
}
438438

439+
// Get the TX once approval is done
440+
response, err = rp.Api.Node.SwapRpl(amountWei)
441+
if err != nil {
442+
return err
443+
}
439444
}
440445

441446
// Run the swap TX

src/rocketpool-cli/commands/odao/join.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,44 @@ func join(c *cli.Context) error {
5454
if !response.Data.CanJoin {
5555
fmt.Println("Cannot join the oracle DAO:")
5656
if response.Data.ProposalExpired {
57-
fmt.Println("The proposal for you to join the oracle DAO does not exist or has expired.")
57+
fmt.Println("The proposal for you to join the Oracle DAO does not exist or has expired.")
5858
}
5959
if response.Data.AlreadyMember {
60-
fmt.Println("The node is already a member of the oracle DAO.")
60+
fmt.Println("The node is already a member of the Oracle DAO.")
6161
}
6262
if response.Data.InsufficientRplBalance {
6363
fmt.Println("The node does not have enough RPL to pay the RPL bond.")
6464
}
6565
return nil
6666
}
6767

68-
// Run the Approve TX
69-
validated, err := tx.HandleTx(c, rp, response.Data.ApproveTxInfo,
70-
"Do you want to let the Oracle DAO manager interact with your RPL? This is required to post your bond in order to join it.",
71-
"approving RPL for bond",
72-
"Approving RPL for joining the Oracle DAO...",
73-
)
74-
if err != nil {
75-
return err
76-
}
77-
if validated {
78-
fmt.Println("Successfully approved bond access to RPL.")
68+
// Check if approval is required first
69+
if response.Data.ApproveTxInfo != nil {
70+
// Run the Approve TX
71+
validated, err := tx.HandleTx(c, rp, response.Data.ApproveTxInfo,
72+
"Do you want to let the Oracle DAO manager interact with your RPL? This is required to post your bond in order to join it.",
73+
"approving RPL for bond",
74+
"Approving RPL for joining the Oracle DAO...",
75+
)
76+
if err != nil {
77+
return err
78+
}
79+
if validated {
80+
fmt.Println("Successfully approved bond access to RPL.")
81+
}
82+
83+
// Build the Join TX once approval is done
84+
response, err = rp.Api.ODao.Join()
85+
if err != nil {
86+
return err
87+
}
7988
}
8089

8190
// Run the Join TX
82-
validated, err = tx.HandleTx(c, rp, response.Data.JoinTxInfo,
91+
validated, err := tx.HandleTx(c, rp, response.Data.JoinTxInfo,
8392
"Are you sure you want to join the oracle DAO? Your RPL bond will be locked until you leave.",
8493
"joining Oracle DAO",
85-
"Joining the ODAO...",
94+
"Joining the Oracle DAO...",
8695
)
8796
if err != nil {
8897
return err
@@ -92,6 +101,6 @@ func join(c *cli.Context) error {
92101
}
93102

94103
// Log & return
95-
fmt.Println("Successfully joined the oracle DAO.")
104+
fmt.Println("Successfully joined the Oracle DAO.")
96105
return nil
97106
}

src/rocketpool-daemon/api/node/stake-rpl.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,22 @@ func (c *nodeStakeRplContext) PrepareData(data *api.NodeStakeRplData, opts *bind
100100
data.CanStake = !(data.InsufficientBalance)
101101

102102
if data.CanStake {
103-
// Check allowance
104-
if c.amount.Cmp(c.allowance) > 0 {
105-
approvalAmount := big.NewInt(0).Sub(c.amount, c.allowance)
103+
if c.allowance.Cmp(c.amount) < 0 {
104+
// Do the approve TX if needed
105+
approvalAmount := getMaxApproval()
106106
txInfo, err := c.rpl.Approve(c.nsAddress, approvalAmount, opts)
107107
if err != nil {
108108
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info to approve increasing RPL's allowance: %w", err)
109109
}
110110
data.ApproveTxInfo = txInfo
111+
} else {
112+
// Just do the stake
113+
txInfo, err := c.node.StakeRpl(c.amount, opts)
114+
if err != nil {
115+
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info for StakeRpl: %w", err)
116+
}
117+
data.StakeTxInfo = txInfo
111118
}
112-
113-
txInfo, err := c.node.StakeRpl(c.amount, opts)
114-
if err != nil {
115-
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info for StakeRpl: %w", err)
116-
}
117-
data.StakeTxInfo = txInfo
118119
}
119120
return types.ResponseStatus_Success, nil
120121
}

src/rocketpool-daemon/api/node/swap-rpl.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,22 @@ func (c *nodeSwapRplContext) PrepareData(data *api.NodeSwapRplData, opts *bind.T
9999
data.CanSwap = !(data.InsufficientBalance)
100100

101101
if data.CanSwap {
102-
// Check allowance
103-
if c.amount.Cmp(c.allowance) > 0 {
104-
// Calculate max uint256 value
105-
approvalAmount := big.NewInt(2)
106-
approvalAmount = approvalAmount.Exp(approvalAmount, big.NewInt(256), nil)
107-
approvalAmount = approvalAmount.Sub(approvalAmount, big.NewInt(1))
102+
if c.allowance.Cmp(c.amount) < 0 {
103+
// Do the approve TX if needed
104+
approvalAmount := getMaxApproval()
108105
txInfo, err := c.fsrpl.Approve(c.rplAddress, approvalAmount, opts)
109106
if err != nil {
110107
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info to approve increasing legacy RPL's allowance: %w", err)
111108
}
112109
data.ApproveTxInfo = txInfo
110+
} else {
111+
// Just do the swap
112+
txInfo, err := c.rpl.SwapFixedSupplyRplForRpl(c.amount, opts)
113+
if err != nil {
114+
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info for SwapFixedSupplyRPLForRPL: %w", err)
115+
}
116+
data.SwapTxInfo = txInfo
113117
}
114-
115-
txInfo, err := c.rpl.SwapFixedSupplyRplForRpl(c.amount, opts)
116-
if err != nil {
117-
return types.ResponseStatus_Error, fmt.Errorf("error getting TX info for SwapFixedSupplyRPLForRPL: %w", err)
118-
}
119-
data.SwapTxInfo = txInfo
120118
}
121119
return types.ResponseStatus_Success, nil
122120
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package node
2+
3+
import "math/big"
4+
5+
func getMaxApproval() *big.Int {
6+
// Calculate max uint256 value
7+
maxApproval := big.NewInt(2)
8+
maxApproval = maxApproval.Exp(maxApproval, big.NewInt(256), nil)
9+
maxApproval = maxApproval.Sub(maxApproval, big.NewInt(1))
10+
return maxApproval
11+
}

src/rocketpool-daemon/api/odao/join.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6063
func (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

Comments
 (0)