Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fee mechanism #14

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions actions/burn_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func (*BurnAsset) StateKeysMaxChunks() []uint16 {
return []uint16{storage.AssetChunks, storage.BalanceChunks}
}

func (*BurnAsset) OutputsWarpMessage() bool {
return false
}

func (b *BurnAsset) Execute(
ctx context.Context,
_ chain.Rules,
Expand Down Expand Up @@ -76,7 +72,7 @@ func (b *BurnAsset) Execute(
return nil, nil
}

func (*BurnAsset) ComputeUnits(chain.Rules) uint64 {
func (*BurnAsset) ComputeUnits(codec.Address, chain.Rules) uint64 {
return BurnComputeUnits
}

Expand Down Expand Up @@ -104,3 +100,7 @@ func (*BurnAsset) ValidRange(chain.Rules) (int64, int64) {
func (*BurnAsset) NMTNamespace() []byte {
return DefaultNMTNamespace
}

func (*BurnAsset) UseFeeMarket() bool {
return false
}
3 changes: 3 additions & 0 deletions actions/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
mintAssetID uint8 = 2
transferID uint8 = 3
msgID uint8 = 4
oracleID uint8 = 5
)

const (
Expand All @@ -21,6 +22,8 @@ const (

MsgComputeUnits = 15

OracleComputeUnits = 10

MaxSymbolSize = 8
MaxMemoSize = 256
MaxMetadataSize = 256
Expand Down
6 changes: 5 additions & 1 deletion actions/create_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *CreateAsset) Execute(
return nil, nil
}

func (*CreateAsset) ComputeUnits(chain.Rules) uint64 {
func (*CreateAsset) ComputeUnits(codec.Address, chain.Rules) uint64 {
return CreateAssetComputeUnits
}

Expand Down Expand Up @@ -102,3 +102,7 @@ func (*CreateAsset) ValidRange(chain.Rules) (int64, int64) {
func (*CreateAsset) NMTNamespace() []byte {
return DefaultNMTNamespace
}

func (*CreateAsset) UseFeeMarket() bool {
return false
}
6 changes: 5 additions & 1 deletion actions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ package actions

import "errors"

var ErrNoSwapToFill = errors.New("no swap to fill")
var (
ErrNoSwapToFill = errors.New("no swap to fill")
ErrRelayerIDsUnitGasPricesMismatch = errors.New("len of relayerIDs and unitGasPrices mismatched")
ErrNotWhiteListed = errors.New("address not whitelisted")
)
6 changes: 5 additions & 1 deletion actions/mint_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (m *MintAsset) Execute(
return nil, nil
}

func (*MintAsset) ComputeUnits(chain.Rules) uint64 {
func (*MintAsset) ComputeUnits(codec.Address, chain.Rules) uint64 {
return MintAssetComputeUnits
}

Expand Down Expand Up @@ -111,3 +111,7 @@ func (*MintAsset) ValidRange(chain.Rules) (int64, int64) {
func (*MintAsset) NMTNamespace() []byte {
return DefaultNMTNamespace
}

func (*MintAsset) UseFeeMarket() bool {
return false
}
67 changes: 47 additions & 20 deletions actions/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/AnomalyFi/hypersdk/codec"
"github.com/AnomalyFi/hypersdk/consts"
"github.com/AnomalyFi/hypersdk/state"
"github.com/AnomalyFi/nodekit-seq/storage"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/math"
)

var _ chain.Action = (*SequencerMsg)(nil)
Expand All @@ -19,49 +22,69 @@ type SequencerMsg struct {
ChainId []byte `json:"chain_id"`
Data []byte `json:"data"`
FromAddress codec.Address `json:"from_address"`
RelayerID int `json:"relayer_id"`
RelayerID uint32 `json:"relayer_id"`
}

func (*SequencerMsg) GetTypeID() uint8 {
return msgID
}

func (*SequencerMsg) StateKeys(_ codec.Address, actionID ids.ID) state.Keys {
return state.Keys{}
func (s *SequencerMsg) StateKeys(actor codec.Address, actionID ids.ID) state.Keys {
return state.Keys{
string(storage.RelayerGasPriceKey(s.RelayerID)): state.Read,
string(storage.BalanceKey(actor, ids.Empty)): state.All,
string(storage.RelayerBalanceKey(s.RelayerID)): state.All,
}
}

// TODO fix this
func (*SequencerMsg) StateKeysMaxChunks() []uint16 {
return []uint16{}
return []uint16{
storage.RelayerGasChunks,
storage.BalanceChunks,
storage.RelayerGasChunks,
}
}

func (t *SequencerMsg) Execute(
func (s *SequencerMsg) Execute(
ctx context.Context,
_ chain.Rules,
mu state.Mutable,
_ int64,
actor codec.Address,
_ ids.ID,
) ([][]byte, error) {
price, err := storage.GetRelayerGasPrice(ctx, mu, s.RelayerID)
if err != nil && err != database.ErrNotFound {
return nil, err
}
cost, err := math.Mul64(price, uint64(len(s.Data)))
if err != nil {
return nil, err
}
// deduct DA costs from the actor's balance
if err := storage.SubBalance(ctx, mu, actor, ids.Empty, cost); err != nil {
return nil, err
}
// add DA costs to the relayer's balance
if err := storage.AddRelayerBalance(ctx, mu, s.RelayerID, cost); err != nil {
return nil, err
}
return nil, nil
}

func (*SequencerMsg) ComputeUnits(chain.Rules) uint64 {
func (*SequencerMsg) ComputeUnits(codec.Address, chain.Rules) uint64 {
return MsgComputeUnits
}

func (*SequencerMsg) Size() int {
// TODO this should be larger because it should consider the max byte array length
// We use size as the price of this transaction but we could just as easily
// use any other calculation.
return codec.AddressLen + ids.IDLen + consts.Uint64Len + consts.IntLen
func (s *SequencerMsg) Size() int {
return codec.AddressLen + consts.Uint64Len + len(s.ChainId) + len(s.Data)
}

func (t *SequencerMsg) Marshal(p *codec.Packer) {
p.PackAddress(t.FromAddress)
p.PackBytes(t.Data)
p.PackBytes(t.ChainId)
p.PackInt(t.RelayerID)
func (s *SequencerMsg) Marshal(p *codec.Packer) {
p.PackAddress(s.FromAddress)
p.PackBytes(s.Data)
p.PackBytes(s.ChainId)
p.PackUint64(uint64(s.RelayerID))
}

func UnmarshalSequencerMsg(p *codec.Packer) (chain.Action, error) {
Expand All @@ -70,7 +93,7 @@ func UnmarshalSequencerMsg(p *codec.Packer) (chain.Action, error) {
// TODO need to correct this and check byte count
p.UnpackBytes(-1, true, &sequencermsg.Data)
p.UnpackBytes(-1, true, &sequencermsg.ChainId)
sequencermsg.RelayerID = p.UnpackInt(true)
sequencermsg.RelayerID = uint32(p.UnpackUint64(true))
return &sequencermsg, p.Err()
}

Expand All @@ -79,6 +102,10 @@ func (*SequencerMsg) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}

func (t *SequencerMsg) NMTNamespace() []byte {
return t.ChainId
func (s *SequencerMsg) NMTNamespace() []byte {
return s.ChainId
}

func (*SequencerMsg) UseFeeMarket() bool {
return true
}
112 changes: 112 additions & 0 deletions actions/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package actions

import (
"context"

"github.com/AnomalyFi/hypersdk/chain"
"github.com/AnomalyFi/hypersdk/codec"
"github.com/AnomalyFi/hypersdk/consts"
"github.com/AnomalyFi/hypersdk/state"
"github.com/AnomalyFi/nodekit-seq/storage"
"github.com/ava-labs/avalanchego/ids"
)

var _ chain.Action = (*Oracle)(nil)

type Oracle struct {
RelayerIDs []uint32 `json:"relayer_ids"`
UnitGasPrices []uint64 `json:"unit_gas_prices"`
}

func (*Oracle) GetTypeID() uint8 {
return oracleID
}

func (o *Oracle) StateKeys(_ codec.Address, actionID ids.ID) state.Keys {
keys := make(state.Keys, 2*len(o.RelayerIDs))
for _, relayerID := range o.RelayerIDs {
keys.Add(string(storage.RelayerGasPriceKey(relayerID)), state.Allocate|state.Write)
keys.Add(string(storage.RelayerGasPriceUpdateTimeStampKey(relayerID)), state.Allocate|state.Write)
}
return keys
}

func (o *Oracle) StateKeysMaxChunks() []uint16 {
var chunks []uint16
for range o.RelayerIDs {
chunks = append(chunks, storage.RelayerGasChunks)
chunks = append(chunks, storage.RelayerGasTimeStampChunks)
}
return chunks
}

func (o *Oracle) Execute(
ctx context.Context,
rules chain.Rules,
mu state.Mutable,
timeStamp int64,
actor codec.Address,
_ ids.ID,
) ([][]byte, error) {
whitelistedAddressesB, _ := rules.FetchCustom("whitelisted.Addresses")
whitelistedAddresses := whitelistedAddressesB.([]codec.Address)
if !ContainsAddress(whitelistedAddresses, actor) {
return nil, ErrNotWhiteListed
}
if len(o.RelayerIDs) != len(o.UnitGasPrices) {
return nil, ErrRelayerIDsUnitGasPricesMismatch
}
for i := range o.RelayerIDs {
if err := storage.StoreRelayerGasPrice(ctx, mu, o.RelayerIDs[i], o.UnitGasPrices[i]); err != nil {
return nil, err
}
if err := storage.StoreRelayerGasPriceUpdateTimeStamp(ctx, mu, o.RelayerIDs[i], timeStamp); err != nil {
return nil, err
}
}
return nil, nil
}

func (*Oracle) ComputeUnits(codec.Address, chain.Rules) uint64 {
return OracleComputeUnits
}

func (o *Oracle) Size() int {
return 2*consts.IntLen + 2*len(o.RelayerIDs)*consts.Uint64Len
}

func (o *Oracle) Marshal(p *codec.Packer) {
p.PackInt(len(o.RelayerIDs))
for _, relayerID := range o.RelayerIDs {
p.PackUint64(uint64(relayerID))
}
p.PackInt(len(o.UnitGasPrices))
for _, unitGasPrice := range o.UnitGasPrices {
p.PackUint64(unitGasPrice)
}
}

func UnmarshalOracle(p *codec.Packer) (chain.Action, error) {
var oracle Oracle
relayerIDsLen := p.UnpackInt(true)
for i := 0; i < relayerIDsLen; i++ {
oracle.RelayerIDs = append(oracle.RelayerIDs, uint32(p.UnpackUint64(true)))
}
unitGasPricesLen := p.UnpackInt(true)
for i := 0; i < unitGasPricesLen; i++ {
oracle.UnitGasPrices = append(oracle.UnitGasPrices, p.UnpackUint64(true))
}
return &oracle, nil
}

func (*Oracle) ValidRange(chain.Rules) (int64, int64) {
return -1, -1
}

func (*Oracle) NMTNamespace() []byte {
return DefaultNMTNamespace
}

func (*Oracle) UseFeeMarket() bool {
return false
}
6 changes: 5 additions & 1 deletion actions/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (t *Transfer) Execute(
return nil, nil
}

func (*Transfer) ComputeUnits(chain.Rules) uint64 {
func (*Transfer) ComputeUnits(codec.Address, chain.Rules) uint64 {
return TransferComputeUnits
}

Expand Down Expand Up @@ -105,3 +105,7 @@ func (*Transfer) ValidRange(chain.Rules) (int64, int64) {
func (*Transfer) NMTNamespace() []byte {
return DefaultNMTNamespace
}

func (*Transfer) UseFeeMarket() bool {
return false
}
12 changes: 12 additions & 0 deletions actions/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package actions

import "github.com/AnomalyFi/hypersdk/codec"

func ContainsAddress(addrs []codec.Address, addr codec.Address) bool {
for _, a := range addrs {
if a == addr {
return true
}
}
return false
}
Loading