This repository was archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
Add price and listingType fields to order websocket notification #1223
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcce77b
[#1188] Add price and listingType to order websocket notification
placer14 9c9d910
Add golangci-lint tool to development docker build
placer14 e38ef41
[#1188] Use BuyerOrder.Payment.Coin to indicate order socket's currency
placer14 9248884
[#1188] Define core.Currency.Divisibility() for each supported currency
placer14 97cc9d0
[#1188] Ensure order websocket field coinDivisibility always has vali…
placer14 095c8ef
[#1188] Remove Currency; Defer to Wallet.ExchangeRate interface
placer14 54079a8
Merge branch 'multiwallet' into 1188-add-price-fields-order-socket
placer14 7c67fa9
Merge branch 'multiwallet' into 1188-add-price-fields-order-socket
placer14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core | ||
|
||
// DefaultCurrencyDivisibility is the Divisibility of the Currency if not | ||
// defined otherwise | ||
const DefaultCurrencyDivisibility uint32 = 1e8 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,9 +65,6 @@ const ( | |
PriceModifierMin = -99.99 | ||
// PriceModifierMax = max price modifier | ||
PriceModifierMax = 1000.00 | ||
|
||
// DefaultCoinDivisibility - decimals for price | ||
DefaultCoinDivisibility uint32 = 1e8 | ||
) | ||
|
||
type price struct { | ||
|
@@ -185,7 +182,7 @@ func (n *OpenBazaarNode) SignListing(listing *pb.Listing) (*pb.SignedListing, er | |
|
||
// Check the listing data is correct for continuing | ||
testingEnabled := n.TestNetworkEnabled() || n.RegressionNetworkEnabled() | ||
if err := validateListing(listing, testingEnabled); err != nil { | ||
if err := n.validateListing(listing, testingEnabled); err != nil { | ||
return sl, err | ||
} | ||
|
||
|
@@ -357,7 +354,7 @@ func (n *OpenBazaarNode) saveListing(listing *pb.Listing) error { | |
} | ||
|
||
if listing.Metadata.ContractType == pb.Listing_Metadata_CRYPTOCURRENCY { | ||
err := validateCryptocurrencyListing(listing) | ||
err := n.validateCryptocurrencyListing(listing) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -449,10 +446,6 @@ func setCryptocurrencyListingDefaults(listing *pb.Listing) { | |
listing.Metadata.Format = pb.Listing_Metadata_MARKET_PRICE | ||
} | ||
|
||
func coinDivisibilityForType(coinType string) uint32 { | ||
return DefaultCoinDivisibility | ||
} | ||
|
||
func (n *OpenBazaarNode) extractListingData(listing *pb.SignedListing) (ListingData, error) { | ||
listingPath := path.Join(n.RepoPath, "root", "listings", listing.Listing.Slug+".json") | ||
|
||
|
@@ -866,7 +859,7 @@ func (n *OpenBazaarNode) GetListingFromSlug(slug string) (*pb.SignedListing, err | |
/* Performs a ton of checks to make sure the listing is formatted correctly. We should not allow | ||
invalid listings to be saved or purchased as it can lead to ambiguity when moderating a dispute | ||
or possible attacks. This function needs to be maintained in conjunction with contracts.proto */ | ||
func validateListing(listing *pb.Listing, testnet bool) (err error) { | ||
func (n *OpenBazaarNode) validateListing(listing *pb.Listing, testnet bool) (err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
switch x := r.(type) { | ||
|
@@ -1173,7 +1166,7 @@ func validateListing(listing *pb.Listing, testnet bool) (err error) { | |
return err | ||
} | ||
} else if listing.Metadata.ContractType == pb.Listing_Metadata_CRYPTOCURRENCY { | ||
err := validateCryptocurrencyListing(listing) | ||
err := n.validateCryptocurrencyListing(listing) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -1287,7 +1280,7 @@ func validatePhysicalListing(listing *pb.Listing) error { | |
return nil | ||
} | ||
|
||
func validateCryptocurrencyListing(listing *pb.Listing) error { | ||
func (n *OpenBazaarNode) validateCryptocurrencyListing(listing *pb.Listing) error { | ||
switch { | ||
case len(listing.Coupons) > 0: | ||
return ErrCryptocurrencyListingIllegalField("coupons") | ||
|
@@ -1303,7 +1296,14 @@ func validateCryptocurrencyListing(listing *pb.Listing) error { | |
return ErrCryptocurrencyListingCoinTypeRequired | ||
} | ||
|
||
if listing.Metadata.CoinDivisibility != coinDivisibilityForType(listing.Metadata.CoinType) { | ||
var expectedDivisibility uint32 | ||
if wallet, err := n.Multiwallet.WalletForCurrencyCode(listing.Metadata.CoinType); err != nil { | ||
expectedDivisibility = DefaultCurrencyDivisibility | ||
} else { | ||
expectedDivisibility = uint32(wallet.ExchangeRates().UnitsPerCoin()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if UnitsPerCoin matched types with the listing data. (OpenBazaar/wallet-interface#9) |
||
} | ||
|
||
if listing.Metadata.CoinDivisibility != expectedDivisibility { | ||
return ErrListingCoinDivisibilityIncorrect | ||
} | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"github.com/OpenBazaar/multiwallet" | ||
"github.com/OpenBazaar/openbazaar-go/core" | ||
"github.com/OpenBazaar/openbazaar-go/pb" | ||
"github.com/OpenBazaar/openbazaar-go/repo" | ||
|
@@ -17,14 +18,14 @@ import ( | |
var log = logging.MustGetLogger("transaction-listener") | ||
|
||
type TransactionListener struct { | ||
db repo.Datastore | ||
broadcast chan repo.Notifier | ||
broadcast chan repo.Notifier | ||
db repo.Datastore | ||
multiwallet multiwallet.MultiWallet | ||
*sync.Mutex | ||
} | ||
|
||
func NewTransactionListener(db repo.Datastore, broadcast chan repo.Notifier) *TransactionListener { | ||
l := &TransactionListener{db, broadcast, new(sync.Mutex)} | ||
return l | ||
func NewTransactionListener(mw multiwallet.MultiWallet, db repo.Datastore, broadcast chan repo.Notifier) *TransactionListener { | ||
return &TransactionListener{broadcast, db, mw, new(sync.Mutex)} | ||
} | ||
|
||
func (l *TransactionListener) OnTransactionReceived(cb wallet.TransactionCallback) { | ||
|
@@ -149,11 +150,10 @@ func (l *TransactionListener) OnTransactionReceived(cb wallet.TransactionCallbac | |
} | ||
} | ||
} | ||
|
||
} | ||
|
||
func (l *TransactionListener) processSalePayment(txid string, output wallet.TransactionOutput, contract *pb.RicardianContract, state pb.OrderState, funded bool, records []*wallet.TransactionRecord) { | ||
funding := output.Value | ||
var funding = output.Value | ||
for _, r := range records { | ||
funding += r.Value | ||
// If we have already seen this transaction for some reason, just return | ||
|
@@ -179,14 +179,21 @@ func (l *TransactionListener) processSalePayment(txid string, output wallet.Tran | |
l.adjustInventory(contract) | ||
|
||
n := repo.OrderNotification{ | ||
repo.NewNotificationID(), | ||
"order", | ||
contract.VendorListings[0].Item.Title, | ||
contract.BuyerOrder.BuyerID.PeerID, | ||
contract.BuyerOrder.BuyerID.Handle, | ||
repo.Thumbnail{contract.VendorListings[0].Item.Images[0].Tiny, contract.VendorListings[0].Item.Images[0].Small}, | ||
orderId, | ||
contract.VendorListings[0].Slug, | ||
BuyerHandle: contract.BuyerOrder.BuyerID.Handle, | ||
BuyerID: contract.BuyerOrder.BuyerID.PeerID, | ||
ID: repo.NewNotificationID(), | ||
ListingType: contract.VendorListings[0].Metadata.ContractType.String(), | ||
OrderId: orderId, | ||
Price: repo.ListingPrice{ | ||
Amount: contract.BuyerOrder.Payment.Amount, | ||
CoinDivisibility: currencyDivisibilityFromContract(l.multiwallet, contract), | ||
CurrencyCode: contract.BuyerOrder.Payment.Coin, | ||
PriceModifier: contract.VendorListings[0].Metadata.PriceModifier, | ||
}, | ||
Slug: contract.VendorListings[0].Slug, | ||
Thumbnail: repo.Thumbnail{contract.VendorListings[0].Item.Images[0].Tiny, contract.VendorListings[0].Item.Images[0].Small}, | ||
Title: contract.VendorListings[0].Item.Title, | ||
Type: "order", | ||
} | ||
|
||
l.broadcast <- n | ||
|
@@ -218,6 +225,18 @@ func (l *TransactionListener) processSalePayment(txid string, output wallet.Tran | |
l.db.TxMetadata().Put(repo.Metadata{txid, "", title, orderId, thumbnail, bumpable}) | ||
} | ||
|
||
func currencyDivisibilityFromContract(mw multiwallet.MultiWallet, contract *pb.RicardianContract) uint32 { | ||
var currencyDivisibility = contract.VendorListings[0].Metadata.CoinDivisibility | ||
if currencyDivisibility != 0 { | ||
return currencyDivisibility | ||
} | ||
wallet, err := mw.WalletForCurrencyCode(contract.BuyerOrder.Payment.Coin) | ||
if err == nil { | ||
return uint32(wallet.ExchangeRates().UnitsPerCoin()) | ||
} | ||
return core.DefaultCurrencyDivisibility | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer divisibility from listing, then check wallet's preference, then fail to default as a last resort. |
||
} | ||
|
||
func (l *TransactionListener) processPurchasePayment(txid string, output wallet.TransactionOutput, contract *pb.RicardianContract, state pb.OrderState, funded bool, records []*wallet.TransactionRecord) { | ||
funding := output.Value | ||
for _, r := range records { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this here. Hope no one minds.