Msg/BuyDirect

Credits can be bought directly:

  • when the sell order exists
  • when the buyer is not the seller
  • when the bid denom matches the sell denom
  • when the buyer has a bank balance greater than or equal to the total cost
  • when the buyer provides a bid price greater than or equal to the ask price
  • when the buyer provides a quantity less than or equal to the sell order quantity
  • when the number of decimal places in quantity is less than or equal to the credit type precision
  • the buyer cannot disable auto-retire when auto-retire is enabled for the sell order
  • the sell order is removed when the sell order is filled
  • the sell order quantity is updated when the sell order is not filled
  • the seller bank balance is updated
  • the buyer bank balance is updated
  • the seller batch balance is updated
  • the buyer batch balance is updated
  • the batch supply is updated when the credits are auto-retired

The sell order must exist

Background

Given a credit type.

the sell order exists

Given alice created a sell order with id "1"

When bob attempts to buy credits with sell order id "1"

Then expect no error.

the sell order does not exist

When bob attempts to buy credits with sell order id "1"

Then expect the error "orders[0]: sell order with id 1: not found: invalid request".

The buyer must not be the seller

Background

Given a credit type.

the buyer is not the seller

Given alice created a sell order with id "1"

When bob attempts to buy credits with sell order id "1"

Then expect no error.

the buyer is the seller

Given alice created a sell order with id "1"

When alice attempts to buy credits with sell order id "1"

Then expect the error "orders[0]: buyer account cannot be the same as seller account: unauthorized".

The bid denom must match the sell denom

Background

Given a credit type

And alice created a sell order with ask denom "regen"

And bob has a bank balance with denom "regen".

bid denom matches sell denom

When bob attempts to buy credits with bid denom "regen"

Then expect no error.

bid denom does not match sell denom

When bob attempts to buy credits with bid denom "atom"

Then expect the error "orders[0]: bid price denom: atom, ask price denom: regen: invalid request".

The buyer must have a bank balance greater than or equal to the total cost

Background

Given a credit type.

buyer bank balance is greater than or equal to total cost (single buy order)

Given alice created a sell order with quantity "10" and ask amount "10"

And bob has a bank balance with amount "[balance-amount]"

When bob attempts to buy credits with quantity "10" and bid amount "10"

Then expect no error.

Examples

descriptionbalance-amount
greater than200
equal to100

buyer bank balance is greater than or equal to total cost (multiple buy orders)

Given alice created two sell orders each with quantity "10" and ask amount "10"

And bob has a bank balance with amount "[balance-amount]"

When bob attempts to buy credits in two orders each with quantity "10" and bid amount "10"

Then expect no error.

Examples

descriptionbalance-amount
greater than400
equal to200

buyer bank balance is less than total cost (single buy order)

Given alice created a sell order with quantity "10" and ask amount "10"

And bob has a bank balance with amount "50"

When bob attempts to buy credits with quantity "10" and bid amount "10"

Then expect the error "orders[0]: quantity: 10, ask price: 10regen, total price: 100regen, bank balance: 50regen: insufficient funds".

buyer bank balance is less than total cost (multiple buy orders)

Given alice created two sell orders each with quantity "10" and ask amount "10"

And bob has a bank balance with amount "150"

When bob attempts to buy credits in two orders each with quantity "10" and bid amount "10"

Then expect the error "orders[1]: quantity: 10, ask price: 10regen, total price: 100regen, bank balance: 50regen: insufficient funds".

The buyer must provide a bid price greater than or equal to the ask price

Background

Given a credit type

And alice created a sell order with ask amount "10"

And bob has a bank balance with amount "100".

bid price greater than or equal to ask price

When bob attempts to buy credits with quantity "10" and bid amount "[bid-amount]"

Then expect no error.

Examples

descriptionbid-amount
greater than20
equal to10

bid price less than ask price

When bob attempts to buy credits with quantity "10" and bid amount "5"

Then expect the error "orders[0]: ask price: 10regen, bid price: 5regen, insufficient bid price: invalid request".

The buyer must provide a quantity less than or equal to the sell order quantity

Background

Given a credit type

And alice created a sell order with quantity "10"

And bob has a bank balance with amount "150".

quantity less than or equal to sell order quantity

When bob attempts to buy credits with quantity "[quantity]"

Then expect no error.

Examples

descriptionquantity
less than5
equal to10

quantity more than sell order quantity

When bob attempts to buy credits with quantity "15"

Then expect the error "orders[0]: requested quantity: 15, sell order quantity 10: invalid request".

The number of decimal places in quantity must be less than or equal to the credit type precision

Background

Given a credit type with precision "6".

quantity decimal places less than or equal to precision

Given alice created a sell order with quantity "[quantity]"

When bob attempts to buy credits with quantity "[quantity]"

Then expect no error.

Examples

descriptionquantity
less than9.12345
equal to9.123456

quantity decimal places more than precision

Given alice created a sell order with quantity "9.1234567"

When bob attempts to buy credits with quantity "9.1234567"

Then expect the error "orders[0]: decimal places exceeds precision: quantity: 9.1234567, credit type precision: 6: invalid request".

The buyer cannot disable auto-retire if the sell order has auto-retire enabled

Background

Given a credit type.

auto retire not required

Given alice created a sell order with disable auto retire "true"

When bob attempts to buy credits with disable auto retire "[disable-auto-retire]"

Then expect no error.

Examples

disable-auto-retire
true
false

auto retire required and buyer enables

Given alice created a sell order with disable auto retire "false"

When bob attempts to buy credits with disable auto retire "false"

Then expect no error.

auto retire required and buyer disables

Given alice created a sell order with disable auto retire "false"

When bob attempts to buy credits with disable auto retire "true"

Then expect the error "orders[0]: cannot disable auto-retire for a sell order with auto-retire enabled: invalid request".

The sell order is removed when the sell order is filled

Background

Given a credit type

And alice created a sell order with quantity "10".

the sell order is removed

When bob attempts to buy credits with quantity "10"

Then expect no sell order with id "1".

the sell order is not removed

When bob attempts to buy credits with quantity "5"

Then expect sell order with id "1".

The sell order quantity is updated when the sell order is not filled

Background

Given a credit type.

Given alice created a sell order with quantity "20"

When bob attempts to buy credits with quantity "10"

Then expect sell order with quantity "10".

the buyer bank balance is updated

Background

Given a credit type.

buyer bank balance updated

Given alice created a sell order with quantity "10" and ask price "10regen"

And bob has the bank balance "100regen"

When bob attempts to buy credits with quantity "10" and bid price "10regen"

Then expect bob bank balance "0regen".

the seller bank balance is updated

Background

Given a credit type

And alice created a sell order with quantity "100" and ask price "1regen"

And alice has bank balance "0regen".

seller bank balance updated

When bob attempts to buy credits with quantity "100" and bid price "1regen"

Then expect alice bank balance "100regen".

the buyer batch balance is updated

Background

Given a credit type

And alice created a sell order with quantity "10" and disable auto retire "true"

And bob has the batch balance

{
  "retired_amount": "0",
  "tradable_amount": "0",
  "escrowed_amount": "0"
}

buyer batch balance updated with retired credits

When bob attempts to buy credits with quantity "10" and disable auto retire "false"

Then expect bob batch balance

{
  "retired_amount": "10",
  "tradable_amount": "0",
  "escrowed_amount": "0"
}

buyer batch balance updated with tradable credits

When bob attempts to buy credits with quantity "10" and disable auto retire "true"

Then expect bob batch balance

{
  "retired_amount": "0",
  "tradable_amount": "10",
  "escrowed_amount": "0"
}

the seller batch balance is updated

Background

Given a credit type

And alice created a sell order with quantity "10"

And alice has the batch balance

{
  "retired_amount": "0",
  "tradable_amount": "0",
  "escrowed_amount": "10"
}

seller batch balance updated

When bob attempts to buy credits with quantity "10"

Then expect alice batch balance

{
  "retired_amount": "0",
  "tradable_amount": "0",
  "escrowed_amount": "0"
}

the batch supply is updated when the credits are auto-retired

Background

Given a credit type

And alice created a sell order with quantity "10" and disable auto retire "true"

And the batch supply

{
  "retired_amount": "0",
  "tradable_amount": "10"
}

batch supply updated

When bob attempts to buy credits with quantity "10" and disable auto retire "false"

Then expect batch supply

{
  "retired_amount": "10",
  "tradable_amount": "0"
}

batch supply not updated

When bob attempts to buy credits with quantity "10" and disable auto retire "true"

Then expect batch supply

{
  "retired_amount": "0",
  "tradable_amount": "10"
}

Events are emitted

Background

Given a credit type

And alice's address "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68"

And bob's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6".

EventTransfer is emitted

Given alice created a sell order with id "1"

When bob attempts to buy credits with quantity "10"

Then expect event transfer with properties

{
  "sender": "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68",
  "recipient": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
  "batch_denom": "C01-001-20200101-20210101-001",
  "tradable_amount": "0",
  "retired_amount": "10"
}

EventRetire is emitted

Given alice created a sell order with id "1"

When bob attempts to buy credits with sell order id "1" and retirement reason "offsetting electricity consumption"

Then expect event retire with properties

{
  "owner": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
  "batch_denom": "C01-001-20200101-20210101-001",
  "amount": "10",
  "reason": "offsetting electricity consumption"
}

EventBuyDirect is emitted

Given alice created a sell order with id "1"

When bob attempts to buy credits with sell order id "1"

Then expect event buy direct with properties

{
  "sell_order_id": 1
}