Skip to content

Commit

Permalink
[1658]: Start work on a Fulfillment struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Sep 19, 2023
1 parent 47ad792 commit 9c823c1
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions x/exchange/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// Define the type strings and bytes to use for each order type.
Expand Down Expand Up @@ -339,3 +340,85 @@ func (b BidOrder) Validate() error {

return errors.Join(errs...)
}

// IndexedInputs is a slice of bank Inputs with an index by address
type IndexedInputs struct {
inputs []banktypes.Input
indexes map[string]int
}

func NewIndexedInputs() *IndexedInputs {
return &IndexedInputs{
indexes: make(map[string]int),
}

Check warning on line 353 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L350-L353

Added lines #L350 - L353 were not covered by tests
}

// Add adds the coins to the input with the given address (creating it if needed).
func (i *IndexedInputs) Add(addr string, coins sdk.Coins) {
n, known := i.indexes[addr]
if !known {
n = len(i.inputs)
i.indexes[addr] = n
i.inputs = append(i.inputs, banktypes.Input{Address: addr})
}
i.inputs[n].Coins = i.inputs[n].Coins.Add(coins...)

Check warning on line 364 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L357-L364

Added lines #L357 - L364 were not covered by tests
}

// Get returns all the known inputs.
func (i *IndexedInputs) Get() []banktypes.Input {
return i.inputs

Check warning on line 369 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L368-L369

Added lines #L368 - L369 were not covered by tests
}

// IndexedOutputs is a slice of bank Outputs with an index by address
type IndexedOutputs struct {
inputs []banktypes.Output
indexes map[string]int
}

func NewIndexedOutputs() *IndexedOutputs {
return &IndexedOutputs{
indexes: make(map[string]int),
}

Check warning on line 381 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L378-L381

Added lines #L378 - L381 were not covered by tests
}

// Add adds the coins to the output with the given address (creating it if needed).
func (i *IndexedOutputs) Add(addr string, coins sdk.Coins) {
n, known := i.indexes[addr]
if !known {
n = len(i.inputs)
i.indexes[addr] = n
i.inputs = append(i.inputs, banktypes.Output{Address: addr})
}
i.inputs[n].Coins = i.inputs[n].Coins.Add(coins...)

Check warning on line 392 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L385-L392

Added lines #L385 - L392 were not covered by tests
}

// Get returns all the known outputs.
func (i *IndexedOutputs) Get() []banktypes.Output {
return i.inputs

Check warning on line 397 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L396-L397

Added lines #L396 - L397 were not covered by tests
}

// Fulfillment is a struct containing the bank inputs/outputs that will fulfill some orders.
type Fulfillment struct {
baseOrder *Order
fullyFilledOrders []*Order
partiallFilledOrder *Order

assetInputs *IndexedInputs
assetOutputs *IndexedOutputs
priceInputs *IndexedInputs
priceOutputs *IndexedOutputs
feeInputs *IndexedInputs
}

func NewFulfillment(order *Order) *Fulfillment {
rv := &Fulfillment{
baseOrder: order,
assetInputs: NewIndexedInputs(),
assetOutputs: NewIndexedOutputs(),
priceInputs: NewIndexedInputs(),
priceOutputs: NewIndexedOutputs(),
feeInputs: NewIndexedInputs(),
}
// TODO[1658]: Finish NewFulfillment.
return rv

Check warning on line 423 in x/exchange/orders.go

View check run for this annotation

Codecov / codecov/patch

x/exchange/orders.go#L413-L423

Added lines #L413 - L423 were not covered by tests
}

0 comments on commit 9c823c1

Please sign in to comment.