Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
[#1409] Update DisputeCaseRecord.PaymentCoin to repo.CurrencyCode
Browse files Browse the repository at this point in the history
  • Loading branch information
placer14 committed Jan 16, 2019
1 parent f78e1c9 commit 6e196ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
3 changes: 2 additions & 1 deletion api/jsonapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,12 +652,13 @@ func TestPurchasesGet(t *testing.T) {
}

func TestCasesGet(t *testing.T) {
paymentCoinCode := repo.CurrencyCode("BTC")
disputeCaseRecord := factory.NewDisputeCaseRecord()
disputeCaseRecord.BuyerContract.VendorListings[0].Metadata.AcceptedCurrencies = []string{"BTC"}
disputeCaseRecord.BuyerContract.VendorListings[0].Metadata.CoinType = "ZEC"
disputeCaseRecord.BuyerContract.VendorListings[0].Metadata.ContractType = pb.Listing_Metadata_CRYPTOCURRENCY
disputeCaseRecord.CoinType = "ZEC"
disputeCaseRecord.PaymentCoin = "BTC"
disputeCaseRecord.PaymentCoin = &paymentCoinCode
dbSetup := func(testRepo *test.Repository) error {
return testRepo.DB.Cases().PutRecord(disputeCaseRecord)
}
Expand Down
53 changes: 33 additions & 20 deletions repo/db/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *CasesDB) PutRecord(dispute *repo.DisputeCaseRecord) error {
dispute.Claim,
"",
"",
dispute.PaymentCoin,
dispute.PaymentCoin.String(),
dispute.CoinType,
)
if err != nil {
Expand All @@ -65,12 +65,16 @@ func (c *CasesDB) PutRecord(dispute *repo.DisputeCaseRecord) error {
}

func (c *CasesDB) Put(caseID string, state pb.OrderState, buyerOpened bool, claim string, paymentCoin string, coinType string) error {
paymentCoinCode, err := repo.NewCurrencyCode(paymentCoin)
if err != nil {
return fmt.Errorf("verifying paymentCoin: %s", err.Error())
}
record := &repo.DisputeCaseRecord{
CaseID: caseID,
Claim: claim,
IsBuyerInitiated: buyerOpened,
OrderState: state,
PaymentCoin: paymentCoin,
PaymentCoin: paymentCoinCode,
CoinType: coinType,
Timestamp: time.Now(),
}
Expand Down Expand Up @@ -388,22 +392,30 @@ func (c *CasesDB) GetCaseMetadata(caseID string) (buyerContract, vendorContract
func (c *CasesDB) GetByCaseID(caseID string) (*repo.DisputeCaseRecord, error) {
c.lock.Lock()
defer c.lock.Unlock()
var buyerCon []byte
var vendorCon []byte
var buyerOuts []byte
var vendorOuts []byte
var buyerAddr string
var vendorAddr string
var stateInt int
var isBuyerInitiated int
var buyerInitiated bool
var createdAt int64
var (
buyerAddr string
buyerCon []byte
buyerInitiated bool
buyerOuts []byte
createdAt int64
isBuyerInitiated int
paymentCoin string
stateInt int
vendorAddr string
vendorCon []byte
vendorOuts []byte
)

stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerPayoutAddress, vendorPayoutAddress, buyerOutpoints, vendorOutpoints, state, buyerOpened, timestamp from cases where caseID=?")
stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerPayoutAddress, vendorPayoutAddress, buyerOutpoints, vendorOutpoints, state, buyerOpened, timestamp, paymentCoin from cases where caseID=?")
if err != nil {
return nil, err
}
err = stmt.QueryRow(caseID).Scan(&buyerCon, &vendorCon, &buyerAddr, &vendorAddr, &buyerOuts, &vendorOuts, &stateInt, &isBuyerInitiated, &createdAt)
err = stmt.QueryRow(caseID).Scan(&buyerCon, &vendorCon, &buyerAddr, &vendorAddr, &buyerOuts, &vendorOuts, &stateInt, &isBuyerInitiated, &createdAt, &paymentCoin)
if err != nil {
return nil, err
}

paymentCoinCode, err := repo.NewCurrencyCode(paymentCoin)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -458,16 +470,17 @@ func (c *CasesDB) GetByCaseID(caseID string) (*repo.DisputeCaseRecord, error) {
return ret
}
return &repo.DisputeCaseRecord{
CaseID: caseID,
IsBuyerInitiated: buyerInitiated,
BuyerContract: brc,
BuyerPayoutAddress: buyerAddr,
BuyerOutpoints: toPointer(buyerOutpointsOut),
VendorContract: vrc,
VendorPayoutAddress: vendorAddr,
VendorOutpoints: toPointer(vendorOutpointsOut),
BuyerPayoutAddress: buyerAddr,
CaseID: caseID,
IsBuyerInitiated: buyerInitiated,
OrderState: pb.OrderState(stateInt),
PaymentCoin: paymentCoinCode,
Timestamp: time.Unix(createdAt, 0),
VendorContract: vrc,
VendorOutpoints: toPointer(vendorOutpointsOut),
VendorPayoutAddress: vendorAddr,
}, nil
}

Expand Down
48 changes: 30 additions & 18 deletions repo/db/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCasesDB_Count(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 5, true, "blah", "", "btc")
err = casesdb.Put("caseID", 5, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand All @@ -71,7 +71,7 @@ func TestPutCase(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestUpdateWithNil(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand All @@ -125,7 +125,7 @@ func TestUpdateWithNil(t *testing.T) {
}
buyerContract, _, _, _, _, _, _, _, _, _, err := casesdb.GetCaseMetadata("caseID")
if err != nil {
t.Error(err)
t.Fatal(err)
}
if buyerContract != nil {
t.Error("Vendor contract was not nil")
Expand All @@ -144,7 +144,7 @@ func TestDeleteCase(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestMarkCaseAsRead(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestMarkCaseAsUnread(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestUpdateBuyerInfo(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestUpdateVendorInfo(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 0, true, "blah", "", "btc")
err = casesdb.Put("caseID", 0, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestCasesGetCaseMetaData(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", "", "btc")
err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -407,6 +407,7 @@ func TestCasesGetCaseMetaData(t *testing.T) {

func TestGetByCaseID(t *testing.T) {
var (
expectedPaymentCoin = "BCH"
casesdb, teardown, err = buildNewCaseStore()
contract = factory.NewContract()
expectedBuyerOutpoints = []*pb.Outpoint{{Hash: "hash1", Index: 0, Value: 5}}
Expand All @@ -417,7 +418,7 @@ func TestGetByCaseID(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", "", "btc")
err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", expectedPaymentCoin, "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -478,6 +479,9 @@ func TestGetByCaseID(t *testing.T) {
if dispute.OrderState != pb.OrderState_DISPUTED {
t.Errorf("Expected state %s got %s", pb.OrderState_DISPUTED, dispute.OrderState)
}
if dispute.PaymentCoin.String() != expectedPaymentCoin {
t.Errorf("Expected paymentCoin %s got %s", expectedPaymentCoin, dispute.PaymentCoin.String())
}
}

func TestMarkAsClosed(t *testing.T) {
Expand All @@ -492,7 +496,7 @@ func TestMarkAsClosed(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", "", "btc")
err = casesdb.Put("caseID", pb.OrderState_DISPUTED, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand All @@ -512,7 +516,7 @@ func TestMarkAsClosed(t *testing.T) {
}
_, _, _, _, state, _, _, _, _, resolution, err := casesdb.GetCaseMetadata("caseID")
if err != nil {
t.Error(err)
t.Fatal(err)
}
if state != pb.OrderState_RESOLVED {
t.Error("Mark as closed failed to set state to resolved")
Expand All @@ -534,7 +538,7 @@ func TestCasesDB_GetAll(t *testing.T) {
}
defer teardown()

err = casesdb.Put("caseID", 10, true, "blah", "", "btc")
err = casesdb.Put("caseID", 10, true, "blah", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand All @@ -547,7 +551,7 @@ func TestCasesDB_GetAll(t *testing.T) {
t.Error(err)
}
time.Sleep(time.Second)
err = casesdb.Put("caseID2", 11, true, "asdf", "", "btc")
err = casesdb.Put("caseID2", 11, true, "asdf", "btc", "btc")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -969,7 +973,6 @@ func TestCasesDB_Put_PaymentCoin(t *testing.T) {
{[]string{"TBTC", "TBCH"}, "TBTC", "TBTC"},
{[]string{"TBCH", "TBTC"}, "TBTC", "TBTC"},
{[]string{"TBTC", "TBCH"}, "TBCH", "TBCH"},
{[]string{}, "", ""},
}
contract = factory.NewContract()
)
Expand All @@ -982,13 +985,17 @@ func TestCasesDB_Put_PaymentCoin(t *testing.T) {

contract.VendorListings[0].Metadata.AcceptedCurrencies = test.acceptedCurrencies
contract.BuyerOrder.Payment.Coin = test.paymentCoin
paymentCoin, err := repo.NewCurrencyCode(test.paymentCoin)
if err != nil {
t.Fatal(err)
}

err = casesdb.PutRecord(&repo.DisputeCaseRecord{
CaseID: "paymentCoinTest",
BuyerContract: contract,
VendorContract: contract,
IsBuyerInitiated: true,
PaymentCoin: test.paymentCoin,
PaymentCoin: paymentCoin,
})
if err != nil {
t.Error(err)
Expand All @@ -1010,7 +1017,7 @@ func TestCasesDB_Put_PaymentCoin(t *testing.T) {

func TestCasesDB_Put_CoinType(t *testing.T) {
var (
testsCoins = []string{"", "TBTC", "TETH"}
testsCoins = []string{"TBTC", "TETH"}
contract = factory.NewContract()
)

Expand All @@ -1020,6 +1027,10 @@ func TestCasesDB_Put_CoinType(t *testing.T) {
t.Fatal(err)
}

paymentCoinCode, err := repo.NewCurrencyCode(testCoin)
if err != nil {
t.Fatal(err)
}
contract.VendorListings[0].Metadata.CoinType = testCoin

err = casesdb.PutRecord(&repo.DisputeCaseRecord{
Expand All @@ -1028,6 +1039,7 @@ func TestCasesDB_Put_CoinType(t *testing.T) {
VendorContract: contract,
IsBuyerInitiated: true,
CoinType: testCoin,
PaymentCoin: paymentCoinCode,
})
if err != nil {
t.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion repo/dispute_case_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type DisputeCaseRecord struct {
VendorPayoutAddress string
IsBuyerInitiated bool
CoinType string
PaymentCoin string
PaymentCoin *CurrencyCode
}

// BuildModeratorDisputeExpiryFirstNotification returns a Notification with ExpiresIn set for the First Interval
Expand Down

0 comments on commit 6e196ae

Please sign in to comment.