@@ -30,7 +30,7 @@ import console from "console"; // import console because jest swallows messages
30
30
// ------------------------------ TEST CONFIG ---------------------------------
31
31
32
32
const TestConfig = {
33
- logLevel : 0 ,
33
+ logLevel : 1 ,
34
34
moneroBinsDir : "../haveno/.localnet" ,
35
35
testDataDir : "./testdata" ,
36
36
networkType : monerojs . MoneroNetworkType . STAGENET ,
@@ -308,7 +308,7 @@ test("Can manage an account", async () => {
308
308
assert ( size > 0 ) ;
309
309
310
310
// delete account which shuts down server
311
- await charlie . deleteAccount ( ) ;
311
+ await charlie . deleteAccount ( ) ; // TODO: support deleting and restoring account without shutting down server, #310
312
312
assert ( ! await charlie . isConnectedToDaemon ( ) ) ;
313
313
await releaseHavenoProcess ( charlie ) ;
314
314
@@ -709,7 +709,7 @@ test("Can get market prices", async () => {
709
709
const price = await alice . getPrice ( assetCode ) ;
710
710
expect ( price ) . toBeGreaterThan ( 0 ) ;
711
711
}
712
-
712
+
713
713
// test that prices are reasonable
714
714
const usd = await alice . getPrice ( "USD" ) ;
715
715
expect ( usd ) . toBeGreaterThan ( 50 ) ;
@@ -720,7 +720,7 @@ test("Can get market prices", async () => {
720
720
const btc = await alice . getPrice ( "BTC" ) ;
721
721
expect ( btc ) . toBeGreaterThan ( 0.0004 )
722
722
expect ( btc ) . toBeLessThan ( 0.4 ) ;
723
-
723
+
724
724
// test invalid currency
725
725
await expect ( async ( ) => { await alice . getPrice ( "INVALID_CURRENCY" ) } )
726
726
. rejects
@@ -769,20 +769,19 @@ test("Can get market depth", async () => {
769
769
expect ( marketDepth . getSellPricesList ( ) . length ) . toEqual ( marketDepth . getSellDepthList ( ) . length ) ;
770
770
771
771
// test buy prices and depths
772
- const priceDivisor = 100000000 ; // TODO: offer price = price * 100000000
773
- const buyOffers = ( await alice . getOffers ( assetCode , "buy" ) ) . concat ( await alice . getMyOffers ( assetCode , "buy" ) ) . sort ( function ( a , b ) { return a . getPrice ( ) - b . getPrice ( ) } ) ;
774
- expect ( marketDepth . getBuyPricesList ( ) [ 0 ] ) . toEqual ( 1 / ( buyOffers [ 0 ] . getPrice ( ) / priceDivisor ) ) ; // TODO: price when posting offer is reversed. this assumes crypto counter currency
775
- expect ( marketDepth . getBuyPricesList ( ) [ 1 ] ) . toEqual ( 1 / ( buyOffers [ 1 ] . getPrice ( ) / priceDivisor ) ) ;
776
- expect ( marketDepth . getBuyPricesList ( ) [ 2 ] ) . toEqual ( 1 / ( buyOffers [ 2 ] . getPrice ( ) / priceDivisor ) ) ;
772
+ const buyOffers = ( await alice . getOffers ( assetCode , "buy" ) ) . concat ( await alice . getMyOffers ( assetCode , "buy" ) ) . sort ( function ( a , b ) { return parseFloat ( a . getPrice ( ) ) - parseFloat ( b . getPrice ( ) ) } ) ;
773
+ expect ( marketDepth . getBuyPricesList ( ) [ 0 ] ) . toEqual ( 1 / parseFloat ( buyOffers [ 0 ] . getPrice ( ) ) ) ; // TODO: price when posting offer is reversed. this assumes crypto counter currency
774
+ expect ( marketDepth . getBuyPricesList ( ) [ 1 ] ) . toEqual ( 1 / parseFloat ( buyOffers [ 1 ] . getPrice ( ) ) ) ;
775
+ expect ( marketDepth . getBuyPricesList ( ) [ 2 ] ) . toEqual ( 1 / parseFloat ( buyOffers [ 2 ] . getPrice ( ) ) ) ;
777
776
expect ( marketDepth . getBuyDepthList ( ) [ 0 ] ) . toEqual ( 0.15 ) ;
778
777
expect ( marketDepth . getBuyDepthList ( ) [ 1 ] ) . toEqual ( 0.30 ) ;
779
778
expect ( marketDepth . getBuyDepthList ( ) [ 2 ] ) . toEqual ( 0.65 ) ;
780
779
781
780
// test sell prices and depths
782
- const sellOffers = ( await alice . getOffers ( assetCode , "sell" ) ) . concat ( await alice . getMyOffers ( assetCode , "sell" ) ) . sort ( function ( a , b ) { return b . getPrice ( ) - a . getPrice ( ) } ) ;
783
- expect ( marketDepth . getSellPricesList ( ) [ 0 ] ) . toEqual ( 1 / ( sellOffers [ 0 ] . getPrice ( ) / priceDivisor ) ) ;
784
- expect ( marketDepth . getSellPricesList ( ) [ 1 ] ) . toEqual ( 1 / ( sellOffers [ 1 ] . getPrice ( ) / priceDivisor ) ) ;
785
- expect ( marketDepth . getSellPricesList ( ) [ 2 ] ) . toEqual ( 1 / ( sellOffers [ 2 ] . getPrice ( ) / priceDivisor ) ) ;
781
+ const sellOffers = ( await alice . getOffers ( assetCode , "sell" ) ) . concat ( await alice . getMyOffers ( assetCode , "sell" ) ) . sort ( function ( a , b ) { return parseFloat ( b . getPrice ( ) ) - parseFloat ( a . getPrice ( ) ) } ) ;
782
+ expect ( marketDepth . getSellPricesList ( ) [ 0 ] ) . toEqual ( 1 / parseFloat ( sellOffers [ 0 ] . getPrice ( ) ) ) ;
783
+ expect ( marketDepth . getSellPricesList ( ) [ 1 ] ) . toEqual ( 1 / parseFloat ( sellOffers [ 1 ] . getPrice ( ) ) ) ;
784
+ expect ( marketDepth . getSellPricesList ( ) [ 2 ] ) . toEqual ( 1 / parseFloat ( sellOffers [ 2 ] . getPrice ( ) ) ) ;
786
785
expect ( marketDepth . getSellDepthList ( ) [ 0 ] ) . toEqual ( 0.3 ) ;
787
786
expect ( marketDepth . getSellDepthList ( ) [ 1 ] ) . toEqual ( 0.6 ) ;
788
787
expect ( marketDepth . getSellDepthList ( ) [ 2 ] ) . toEqual ( 1 ) ;
@@ -860,6 +859,8 @@ test("Can create fiat payment accounts", async () => {
860
859
const accountForm = await alice . getPaymentAccountForm ( paymentMethodId ) ;
861
860
862
861
// edit form
862
+ accountForm . tradeCurrencies = "gbp,eur,usd" ;
863
+ accountForm . selectedTradeCurrency = "usd" ;
863
864
accountForm . accountName = "Revolut account " + GenUtils . getUUID ( ) ;
864
865
accountForm . userName = "user123" ;
865
866
@@ -927,28 +928,50 @@ test("Can create crypto payment accounts", async () => {
927
928
}
928
929
} ) ;
929
930
930
- test ( "Can post and remove offers" , async ( ) => {
931
+ test ( "Can prepare for trading" , async ( ) => {
932
+
933
+ // create payment accounts
934
+ if ( ! await hasPaymentAccount ( alice , "eth" ) ) await createPaymentAccount ( alice , "eth" ) ;
935
+ if ( ! await hasPaymentAccount ( alice , "bch" ) ) await createPaymentAccount ( alice , "bch" ) ;
936
+ if ( ! await hasPaymentAccount ( alice , "usd" ) ) await createPaymentAccount ( alice , "usd" ) ;
937
+ if ( ! await hasPaymentAccount ( bob , "eth" ) ) await createPaymentAccount ( bob , "eth" ) ;
938
+ if ( ! await hasPaymentAccount ( bob , "bch" ) ) await createPaymentAccount ( bob , "bch" ) ;
939
+ if ( ! await hasPaymentAccount ( bob , "usd" ) ) await createPaymentAccount ( bob , "usd" ) ;
940
+
941
+ // fund wallets
942
+ const tradeAmount = BigInt ( "250000000000" ) ;
943
+ await fundOutputs ( [ aliceWallet , bobWallet ] , tradeAmount * BigInt ( "6" ) , 4 ) ;
944
+
945
+ // wait for havenod to observe funds
946
+ await wait ( TestConfig . walletSyncPeriodMs ) ;
947
+ } ) ;
931
948
932
- // wait for alice to have at least 5 outputs of 0.5 XMR
933
- await fundOutputs ( [ aliceWallet ] , BigInt ( "500000000000" ) , 5 ) ;
949
+ test ( "Can post and remove offers" , async ( ) => {
950
+
951
+ // wait for alice to have unlocked balance to post offer
952
+ await waitForUnlockedBalance ( BigInt ( "250000000000" ) * BigInt ( "2" ) , alice ) ;
934
953
935
954
// get unlocked balance before reserving funds for offer
936
955
const unlockedBalanceBefore = BigInt ( ( await alice . getBalances ( ) ) . getUnlockedBalance ( ) ) ;
937
956
938
957
// post crypto offer
939
- let assetCode = "ETH " ;
958
+ let assetCode = "BCH " ;
940
959
let price = 1 / 17 ;
941
960
price = 1 / price ; // TODO: price in crypto offer is inverted
942
961
let offer : OfferInfo = await postOffer ( alice , { assetCode : assetCode , price : price } ) ;
943
962
assert . equal ( offer . getState ( ) , "AVAILABLE" ) ;
944
963
assert . equal ( offer . getBaseCurrencyCode ( ) , assetCode ) ; // TODO: base and counter currencies inverted in crypto offer
945
964
assert . equal ( offer . getCounterCurrencyCode ( ) , "XMR" ) ;
946
- assert . equal ( offer . getPrice ( ) , price * 100000000 ) ; // TODO: price when posting crypto offer is inverted and * 100000000.
965
+ assert . equal ( parseFloat ( offer . getPrice ( ) ) , price ) ;
947
966
948
967
// has offer
949
968
offer = await alice . getMyOffer ( offer . getId ( ) ) ;
950
969
assert . equal ( offer . getState ( ) , "AVAILABLE" ) ;
951
970
971
+ // peer sees offer
972
+ await wait ( TestConfig . maxTimePeerNoticeMs ) ;
973
+ if ( ! getOffer ( await bob . getOffers ( assetCode , TestConfig . postOffer . direction ) , offer . getId ( ) ) ) throw new Error ( "Offer " + offer . getId ( ) + " was not found in peer's offers after posted" ) ;
974
+
952
975
// cancel offer
953
976
await alice . removeOffer ( offer . getId ( ) ) ;
954
977
@@ -965,7 +988,7 @@ test("Can post and remove offers", async () => {
965
988
assert . equal ( offer . getState ( ) , "AVAILABLE" ) ;
966
989
assert . equal ( offer . getBaseCurrencyCode ( ) , "XMR" ) ;
967
990
assert . equal ( offer . getCounterCurrencyCode ( ) , "USD" ) ;
968
- assert . equal ( offer . getPrice ( ) , price * 10000 ) ; // TODO: price = price * 10000
991
+ assert . equal ( parseFloat ( offer . getPrice ( ) ) , price ) ;
969
992
970
993
// has offer
971
994
offer = await alice . getMyOffer ( offer . getId ( ) ) ;
@@ -997,7 +1020,7 @@ test("Can schedule offers with locked funds", async () => {
997
1020
await fundOutputs ( [ charlieWallet ] , outputAmt , 2 , false ) ;
998
1021
999
1022
// schedule offer
1000
- const assetCode = "ETH " ;
1023
+ const assetCode = "BCH " ;
1001
1024
const direction = "BUY" ;
1002
1025
let offer : OfferInfo = await postOffer ( charlie , { assetCode : assetCode , direction : direction , awaitUnlockedBalance : false } ) ;
1003
1026
assert . equal ( offer . getState ( ) , "SCHEDULED" ) ;
@@ -1202,7 +1225,7 @@ test("Can resolve disputes", async () => {
1202
1225
1203
1226
// wait for alice and bob to have unlocked balance for trade
1204
1227
const tradeAmount = BigInt ( "250000000000" ) ;
1205
- await fundOutputs ( [ aliceWallet , bobWallet ] , tradeAmount * BigInt ( "6" ) , 4 , true ) ;
1228
+ await fundOutputs ( [ aliceWallet , bobWallet ] , tradeAmount * BigInt ( "6" ) , 4 ) ;
1206
1229
1207
1230
// register to receive notifications
1208
1231
const aliceNotifications : NotificationMessage [ ] = [ ] ;
@@ -2071,25 +2094,34 @@ function testDestination(destination: XmrDestination) {
2071
2094
}
2072
2095
2073
2096
function getRandomAssetCode ( ) {
2074
- return TestConfig . assetCodes [ GenUtils . getRandomInt ( 0 , TestConfig . assetCodes . length - 1 ) ] ;
2097
+ return TestConfig . assetCodes [ GenUtils . getRandomInt ( 0 , TestConfig . assetCodes . length - 1 ) ] ;
2098
+ }
2099
+
2100
+ async function hasPaymentAccount ( trader : HavenoClient , assetCode : string ) : Promise < boolean > {
2101
+ for ( const paymentAccount of await trader . getPaymentAccounts ( ) ) {
2102
+ if ( paymentAccount . getSelectedTradeCurrency ( ) ! . getCode ( ) === assetCode . toUpperCase ( ) ) return true ;
2103
+ }
2104
+ return false ;
2075
2105
}
2076
2106
2077
2107
async function createPaymentAccount ( trader : HavenoClient , assetCode : string ) : Promise < PaymentAccount > {
2078
- return isCrypto ( assetCode ) ? createCryptoPaymentAccount ( trader , assetCode ) : createRevolutPaymentAccount ( trader ) ;
2108
+ return isCrypto ( assetCode ) ? createCryptoPaymentAccount ( trader , assetCode ) : createRevolutPaymentAccount ( trader ) ;
2079
2109
}
2080
2110
2081
2111
function isCrypto ( assetCode : string ) {
2082
- return getCryptoAddress ( assetCode ) !== undefined ;
2112
+ return getCryptoAddress ( assetCode ) !== undefined ;
2083
2113
}
2084
2114
2085
2115
function getCryptoAddress ( currencyCode : string ) : string | undefined {
2086
- for ( const cryptoAddress of TestConfig . cryptoAddresses ) {
2087
- if ( cryptoAddress . currencyCode === currencyCode . toUpperCase ( ) ) return cryptoAddress . address ;
2088
- }
2116
+ for ( const cryptoAddress of TestConfig . cryptoAddresses ) {
2117
+ if ( cryptoAddress . currencyCode === currencyCode . toUpperCase ( ) ) return cryptoAddress . address ;
2118
+ }
2089
2119
}
2090
2120
2091
2121
async function createRevolutPaymentAccount ( trader : HavenoClient ) : Promise < PaymentAccount > {
2092
2122
const accountForm = await trader . getPaymentAccountForm ( 'REVOLUT' ) ;
2123
+ accountForm . tradeCurrencies = "gbp,eur,usd" ;
2124
+ accountForm . selectedTradeCurrency = "usd" ;
2093
2125
accountForm . accountName = "Revolut account " + GenUtils . getUUID ( ) ;
2094
2126
accountForm . userName = "user123" ;
2095
2127
return trader . createPaymentAccount ( accountForm ) ;
0 commit comments