laconicd-deprecated/types/account.go

95 lines
2.1 KiB
Go
Raw Normal View History

2018-08-24 18:56:43 +00:00
package types
import (
"fmt"
2018-08-24 18:56:43 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/ethermint/x/bank"
2018-08-24 18:56:43 +00:00
ethcmn "github.com/ethereum/go-ethereum/common"
)
var _ auth.Account = (*Account)(nil)
// BaseAccount implements the auth.Account interface and embeds an
// auth.BaseAccount type. It is compatible with the auth.AccountMapper.
type Account struct {
*auth.BaseAccount
2018-08-24 18:56:43 +00:00
Root ethcmn.Hash // merkle root of the storage trie
CodeHash []byte
}
2018-08-24 18:56:43 +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
// Balance returns the balance of an account.
func (acc Account) Balance() sdk.Int {
return acc.GetCoins().AmountOf(bank.DenomEthereum)
}
// SetBalance sets an account's balance.
func (acc Account) SetBalance(amt sdk.Int) {
acc.SetCoins(sdk.Coins{sdk.NewCoin(bank.DenomEthereum, amt)})
2018-08-24 18:56:43 +00:00
}
// // NewAccount returns a reference to a new initialized account.
// func NewAccount(base auth.BaseAccount, code []byte) *Account {
// return &Account{
// BaseAccount: base,
// Code: code,
// Storage: storage,
// }
// }
2018-08-24 18:56:43 +00:00
// GetAccountDecoder returns the auth.AccountDecoder function for the custom
// Account type.
// func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder {
// return func(accBytes []byte) (auth.Account, error) {
// if len(accBytes) == 0 {
// return nil, sdk.ErrTxDecode("account bytes are empty")
// }
// acc := new(Account)
2018-08-24 18:56:43 +00:00
// err := cdc.UnmarshalBinaryBare(accBytes, &acc)
// if err != nil {
// return nil, sdk.ErrTxDecode("failed to decode account bytes")
// }
2018-08-24 18:56:43 +00:00
// return acc, err
// }
// }
2018-08-24 18:56:43 +00:00
// Account code and storage type aliases.
type (
Code []byte
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
}
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
}