2020-10-28 17:35:56 +00:00
package store
2020-10-30 05:23:08 +00:00
import (
"math/big"
2020-10-31 03:48:15 +00:00
"time"
2020-10-30 05:23:08 +00:00
2020-11-12 14:18:30 +00:00
"github.com/Secured-Finance/dione/ethclient"
2020-10-31 03:48:15 +00:00
"github.com/Secured-Finance/dione/lib"
"github.com/Secured-Finance/dione/types"
2020-10-30 05:23:08 +00:00
"github.com/ethereum/go-ethereum/common"
2020-10-31 03:48:15 +00:00
validation "github.com/go-ozzo/ozzo-validation"
2020-10-30 05:23:08 +00:00
)
2020-10-28 17:35:56 +00:00
2020-10-30 05:23:08 +00:00
type DioneStakeInfo struct {
2020-10-31 03:48:15 +00:00
ID int
MinerStake * big . Int
TotalStake * big . Int
2020-11-12 14:18:30 +00:00
MinerAddress string
2020-10-31 03:48:15 +00:00
MinerEthWallet string
Timestamp time . Time
2020-11-12 14:18:30 +00:00
Ethereum * ethclient . EthereumClient
2020-10-30 05:23:08 +00:00
}
2020-11-12 14:18:30 +00:00
func NewDioneStakeInfo ( minerStake , totalStake * big . Int , minerWallet , minerEthWallet string , ethereumClient * ethclient . EthereumClient ) * DioneStakeInfo {
2020-10-30 05:23:08 +00:00
return & DioneStakeInfo {
2020-10-31 03:48:15 +00:00
MinerStake : minerStake ,
TotalStake : totalStake ,
2020-11-12 14:18:30 +00:00
MinerAddress : minerWallet ,
2020-10-31 03:48:15 +00:00
MinerEthWallet : minerEthWallet ,
Ethereum : ethereumClient ,
2020-10-30 05:23:08 +00:00
}
}
2020-10-31 03:48:15 +00:00
func ( d * DioneStakeInfo ) UpdateMinerStake ( minerEthAddress common . Address ) error {
minerStake , err := d . Ethereum . GetMinerStake ( minerEthAddress )
2020-10-30 05:23:08 +00:00
if err != nil {
return err
}
d . MinerStake = minerStake
return nil
}
2020-10-31 03:48:15 +00:00
func ( d * DioneStakeInfo ) UpdateTotalStake ( ) error {
2020-10-30 05:23:08 +00:00
totalStake , err := d . Ethereum . GetTotalStake ( )
if err != nil {
return err
}
d . TotalStake = totalStake
return nil
2020-10-28 17:35:56 +00:00
}
2020-10-31 03:48:15 +00:00
// Put miner's staking information into the database
func ( s * Store ) CreateDioneStakeInfo ( stakeStore * DioneStakeInfo ) error {
if err := stakeStore . Validate ( ) ; err != nil {
return err
}
now := lib . Clock . Now ( )
return s . db . QueryRow (
2020-11-12 14:18:30 +00:00
"INSERT INTO staking (miner_stake, total_stake, miner_address, miner_eth_wallet, timestamp) VALUES ($1, $2, $3, $4, $5) RETURNING id" ,
2020-10-31 03:48:15 +00:00
stakeStore . MinerStake ,
stakeStore . TotalStake ,
2020-11-12 14:18:30 +00:00
stakeStore . MinerAddress ,
2020-10-31 03:48:15 +00:00
stakeStore . MinerEthWallet ,
now ,
) . Scan ( & stakeStore . ID )
}
func ( s * Store ) GetLastStakeInfo ( wallet , ethWallet string ) ( * DioneStakeInfo , error ) {
var stake * DioneStakeInfo
if err := s . db . Select ( & stake ,
2020-11-12 14:18:30 +00:00
` SELECT miner_stake, total_stake, miner_address, miner_eth_wallet, timestamp FROM staking ORDER BY TIMESTAMP DESC LIMIT 1 WHERE miner_address=$1, miner_eth_wallet=$2 ` ,
2020-10-31 03:48:15 +00:00
wallet ,
ethWallet ,
) ; err != nil {
return nil , err
}
return stake , nil
}
// Before puting the data into the database validating all required fields
func ( s * DioneStakeInfo ) Validate ( ) error {
return validation . ValidateStruct (
s ,
validation . Field ( & s . MinerStake , validation . Required , validation . By ( types . ValidateBigInt ( s . MinerStake ) ) ) ,
validation . Field ( & s . TotalStake , validation . Required , validation . By ( types . ValidateBigInt ( s . TotalStake ) ) ) ,
2020-11-12 14:18:30 +00:00
validation . Field ( & s . MinerAddress , validation . Required ) ,
2020-10-31 03:48:15 +00:00
validation . Field ( & s . MinerEthWallet , validation . Required ) ,
)
}