2018-08-24 18:56:43 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2018-10-03 00:22:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-08-24 18:56:43 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ auth.Account = (*Account)(nil)
|
|
|
|
|
2018-10-24 13:46:26 +00:00
|
|
|
const (
|
|
|
|
// DenomDefault defines the single coin type/denomination supported in
|
|
|
|
// Ethermint.
|
2019-07-04 19:46:54 +00:00
|
|
|
DenomDefault = "photon"
|
2018-10-24 13:46:26 +00:00
|
|
|
)
|
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Main Ethermint account
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2019-07-02 19:36:22 +00:00
|
|
|
// Account implements the auth.Account interface and embeds an
|
2018-10-03 00:22:15 +00:00
|
|
|
// auth.BaseAccount type. It is compatible with the auth.AccountMapper.
|
|
|
|
type Account struct {
|
|
|
|
*auth.BaseAccount
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-04 16:06:19 +00:00
|
|
|
// merkle root of the storage trie
|
|
|
|
//
|
|
|
|
// TODO: good chance we may not need this
|
|
|
|
Root ethcmn.Hash
|
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
CodeHash []byte
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
// ProtoBaseAccount defines the prototype function for BaseAccount used for an
|
|
|
|
// account mapper.
|
|
|
|
func ProtoBaseAccount() auth.Account {
|
|
|
|
return &Account{BaseAccount: &auth.BaseAccount{}}
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
// Balance returns the balance of an account.
|
|
|
|
func (acc Account) Balance() sdk.Int {
|
2018-10-24 13:46:26 +00:00
|
|
|
return acc.GetCoins().AmountOf(DenomDefault)
|
2018-10-03 00:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBalance sets an account's balance.
|
|
|
|
func (acc Account) SetBalance(amt sdk.Int) {
|
2019-07-02 19:36:22 +00:00
|
|
|
// nolint:errcheck
|
2018-10-24 13:46:26 +00:00
|
|
|
acc.SetCoins(sdk.Coins{sdk.NewCoin(DenomDefault, amt)})
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:57:02 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Code & Storage
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2018-10-03 00:22:15 +00:00
|
|
|
type (
|
2019-07-02 19:36:22 +00:00
|
|
|
// Code is account Code type alias
|
|
|
|
Code []byte
|
|
|
|
// Storage is account storage type alias
|
2018-10-03 00:22:15 +00:00
|
|
|
Storage map[ethcmn.Hash]ethcmn.Hash
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c Code) String() string {
|
|
|
|
return string(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Storage) String() (str string) {
|
|
|
|
for key, value := range c {
|
|
|
|
str += fmt.Sprintf("%X : %X\n", key, value)
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
2018-10-03 00:22:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of storage.
|
|
|
|
func (c Storage) Copy() Storage {
|
|
|
|
cpy := make(Storage)
|
|
|
|
for key, value := range c {
|
|
|
|
cpy[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return cpy
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|