2020-06-26 00:31:25 +08:00
/ *
Copyright 2020 DODO ZOO .
SPDX - License - Identifier : Apache - 2.0
* /
2020-09-08 20:25:59 +08:00
import * as assert from 'assert' ;
2020-06-26 00:31:25 +08:00
import { DODOContext , getDODOContext } from './utils/Context' ;
import { decimalStr } from './utils/Converter' ;
import { logGas } from './utils/Log' ;
2020-09-08 20:25:59 +08:00
let lp : string ;
let trader : string ;
2020-06-26 00:31:25 +08:00
async function init ( ctx : DODOContext ) : Promise < void > {
2020-09-08 20:25:59 +08:00
await ctx . setOraclePrice ( decimalStr ( "100" ) ) ;
lp = ctx . spareAccounts [ 0 ] ;
trader = ctx . spareAccounts [ 1 ] ;
await ctx . approveDODO ( lp ) ;
await ctx . approveDODO ( trader ) ;
await ctx . mintTestToken ( lp , decimalStr ( "10" ) , decimalStr ( "1000" ) ) ;
await ctx . mintTestToken ( trader , decimalStr ( "10" ) , decimalStr ( "1000" ) ) ;
await ctx . DODO . methods
. depositBaseTo ( lp , decimalStr ( "10" ) )
. send ( ctx . sendParam ( lp ) ) ;
await ctx . DODO . methods
. depositQuoteTo ( lp , decimalStr ( "1000" ) )
. send ( ctx . sendParam ( lp ) ) ;
2020-06-26 00:31:25 +08:00
}
describe ( "Trader" , ( ) = > {
2020-09-08 20:25:59 +08:00
let snapshotId : string ;
let ctx : DODOContext ;
2020-06-26 00:31:25 +08:00
before ( async ( ) = > {
2020-09-08 20:25:59 +08:00
ctx = await getDODOContext ( ) ;
2020-06-26 00:31:25 +08:00
await init ( ctx ) ;
2020-09-08 20:25:59 +08:00
} ) ;
2020-06-26 00:31:25 +08:00
beforeEach ( async ( ) = > {
snapshotId = await ctx . EVM . snapshot ( ) ;
} ) ;
afterEach ( async ( ) = > {
2020-09-08 20:25:59 +08:00
await ctx . EVM . reset ( snapshotId ) ;
2020-06-26 00:31:25 +08:00
} ) ;
describe ( "R goes above ONE" , ( ) = > {
it ( "buy when R equals ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await logGas (
ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"buy base token when balanced"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await logGas ( ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) , ctx . sendParam ( trader ) , "buy base token when balanced" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "11" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"898581839502056240973"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.001" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0" )
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "8.999" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1101418160497943759027"
) ;
2020-07-11 11:50:33 +08:00
// price update
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . getMidPrice ( ) . call ( ) ,
"102353368821735563400"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
2020-06-26 13:09:07 +08:00
it ( "buy when R is ABOVE ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "130" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"buy when R is ABOVE ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "130" ) , "0x" ) , ctx . sendParam ( trader ) , "buy when R is ABOVE ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "12" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"794367183433412077653"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.002" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0" )
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "7.998" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1205632816566587922347"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "sell when R is ABOVE ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . sellBaseToken (
decimalStr ( "0.5" ) ,
decimalStr ( "40" ) ,
"0x"
) ,
ctx . sendParam ( trader ) ,
"sell when R is ABOVE ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . sellBaseToken ( decimalStr ( "0.5" ) , decimalStr ( "40" ) , "0x" ) , ctx . sendParam ( trader ) , "sell when R is ABOVE ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "10.5" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"949280846351657143136"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.001" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"50851561534203512"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "9.499" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1050668302086808653352"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "sell when R is ABOVE ONE and RStatus back to ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . sellBaseToken (
"1003002430889317763" ,
decimalStr ( "90" ) ,
"0x"
) ,
ctx . sendParam ( trader ) ,
"sell when R is ABOVE ONE and RStatus back to ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . sellBaseToken ( "1003002430889317763" , decimalStr ( "90" ) , "0x" ) , ctx . sendParam ( trader ) , "sell when R is ABOVE ONE and RStatus back to ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// R status
2020-09-08 20:25:59 +08:00
assert . equal ( await ctx . DODO . methods . _R_STATUS_ ( ) . call ( ) , "0" ) ;
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
"9996997569110682237"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"999695745518506168723"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.001" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"101418160497943759"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
"10002002430889317763"
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1000202836320995887518"
) ;
2020-06-26 00:31:25 +08:00
// target status
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _TARGET_BASE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"10002002430889317763"
) ;
assert . equal (
await ctx . DODO . methods . _TARGET_QUOTE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"1000202836320995887518"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "sell when R is ABOVE ONE and RStatus becomes BELOW ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . sellBaseToken ( decimalStr ( "2" ) , decimalStr ( "90" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"sell when R is ABOVE ONE and RStatus becomes BELOW ONE [gas cost worst case]"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "110" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . sellBaseToken ( decimalStr ( "2" ) , decimalStr ( "90" ) , "0x" ) , ctx . sendParam ( trader ) , "sell when R is ABOVE ONE and RStatus becomes BELOW ONE [gas cost worst case]" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// R status
2020-09-08 20:25:59 +08:00
assert . equal ( await ctx . DODO . methods . _R_STATUS_ ( ) . call ( ) , "2" ) ;
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "9" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1098020621600061709144"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.001" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"200038898794388634"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "10.999" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"901779339501143902222"
) ;
2020-06-26 00:31:25 +08:00
// target status
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _TARGET_BASE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"10002002430889317763"
) ;
assert . equal (
await ctx . DODO . methods . _TARGET_QUOTE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"1000400077797588777268"
) ;
} ) ;
} ) ;
2020-06-26 00:31:25 +08:00
describe ( "R goes below ONE" , ( ) = > {
it ( "sell when R equals ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await logGas (
ctx . DODO . methods . sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"sell base token when balanced"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await logGas ( ctx . DODO . methods . sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" ) , ctx . sendParam ( trader ) , "sell base token when balanced" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "9" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1098617454226610630663"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"0"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"98914196817061816"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "11" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"901283631576572307521"
) ;
2020-07-11 11:50:33 +08:00
// price update
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . getMidPrice ( ) . call ( ) ,
"97736983274307939149"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
2020-06-26 00:56:40 +08:00
it ( "sell when R is BELOW ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "3" ) , decimalStr ( "90" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . sellBaseToken ( decimalStr ( "3" ) , decimalStr ( "90" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"sell when R is BELOW ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . sellBaseToken ( decimalStr ( "3" ) , decimalStr ( "90" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . sellBaseToken ( decimalStr ( "3" ) , decimalStr ( "90" ) , "0x" ) , ctx . sendParam ( trader ) , "sell when R is BELOW ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "4" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1535961012052716726151"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"0"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"537573733252474148"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "16" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"463501414214030799701"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "buy when R is BELOW ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . buyBaseToken (
decimalStr ( "0.5" ) ,
decimalStr ( "60" ) ,
"0x"
) ,
ctx . sendParam ( trader ) ,
"buy when R is BELOW ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . buyBaseToken ( decimalStr ( "0.5" ) , decimalStr ( "60" ) , "0x" ) , ctx . sendParam ( trader ) , "buy when R is BELOW ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "9.5" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1049294316148665165453"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.0005" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"98914196817061816"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "10.4995" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"950606769654517772731"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "buy when R is BELOW ONE and RStatus back to ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . buyBaseToken (
"997008973080757728" ,
decimalStr ( "110" ) ,
"0x"
) ,
ctx . sendParam ( trader ) ,
"buy when R is BELOW ONE and RStatus back to ONE"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . buyBaseToken ( "997008973080757728" , decimalStr ( "110" ) , "0x" ) , ctx . sendParam ( trader ) , "buy when R is BELOW ONE and RStatus back to ONE" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// R status
2020-09-08 20:25:59 +08:00
assert . equal ( await ctx . DODO . methods . _R_STATUS_ ( ) . call ( ) , "0" ) ;
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
"9997008973080757728"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"999703024198699411500"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"997008973080757"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"98914196817061816"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
"10001994017946161515"
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1000198061604483526684"
) ;
2020-06-26 00:31:25 +08:00
// target status
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _TARGET_BASE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"10001994017946161515"
) ;
assert . equal (
await ctx . DODO . methods . _TARGET_QUOTE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"1000198061604483526684"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "buy when R is BELOW ONE and RStatus becomes ABOVE ONE" , async ( ) = > {
2020-09-17 15:04:11 +08:00
<< << << < Updated upstream
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
await logGas (
ctx . DODO . methods . buyBaseToken ( decimalStr ( "2" ) , decimalStr ( "220" ) , "0x" ) ,
ctx . sendParam ( trader ) ,
"buy when R is BELOW ONE and RStatus becomes ABOVE ONE [gas cost worst case]"
) ;
2020-09-17 15:04:11 +08:00
=== === =
await ctx . DODO . methods . sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "90" ) , "0x" ) . send ( ctx . sendParam ( trader ) )
await logGas ( ctx . DODO . methods . buyBaseToken ( decimalStr ( "2" ) , decimalStr ( "220" ) , "0x" ) , ctx . sendParam ( trader ) , "buy when R is BELOW ONE and RStatus becomes ABOVE ONE [gas cost worst case]" )
>>> >>> > Stashed changes
2020-06-26 00:31:25 +08:00
// R status
2020-09-08 20:25:59 +08:00
assert . equal ( await ctx . DODO . methods . _R_STATUS_ ( ) . call ( ) , "1" ) ;
2020-06-26 00:31:25 +08:00
// trader balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "11" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"897977789597854403796"
) ;
2020-06-26 00:31:25 +08:00
// maintainer balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . BASE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
decimalStr ( "0.002" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( ctx . Maintainer ) . call ( ) ,
"98914196817061816"
) ;
2020-06-26 00:31:25 +08:00
// dodo balances
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _BASE_BALANCE_ ( ) . call ( ) ,
decimalStr ( "8.998" )
) ;
assert . equal (
await ctx . DODO . methods . _QUOTE_BALANCE_ ( ) . call ( ) ,
"1101923296205328534388"
) ;
2020-06-26 00:31:25 +08:00
// target status
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . DODO . methods . _TARGET_BASE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"10004000000000000000"
) ;
assert . equal (
await ctx . DODO . methods . _TARGET_QUOTE_TOKEN_AMOUNT_ ( ) . call ( ) ,
"1000198061604483526684"
) ;
} ) ;
} ) ;
2020-06-26 00:31:25 +08:00
describe ( "Corner cases" , ( ) = > {
it ( "buy or sell 0" , async ( ) = > {
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "0" ) , decimalStr ( "0" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "10" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "1000" )
) ;
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "0" ) , decimalStr ( "0" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "10" )
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
decimalStr ( "1000" )
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "buy or sell a tiny amount" , async ( ) = > {
// no precision problem
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. sellBaseToken ( "1" , decimalStr ( "0" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
"9999999999999999999"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1000000000000000000100"
) ;
2020-06-26 00:31:25 +08:00
// have precision problem, charge 0
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( "1" , decimalStr ( "1" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
"10000000000000000000"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1000000000000000000100"
) ;
assert . equal ( await ctx . DODO . methods . _R_STATUS_ ( ) . call ( ) , "0" ) ;
2020-06-26 00:31:25 +08:00
// no precision problem if trading amount is extremely small
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( "10" , decimalStr ( "1" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
assert . equal (
await ctx . BASE . methods . balanceOf ( trader ) . call ( ) ,
"10000000000000000010"
) ;
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"999999999999999999100"
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "sell a huge amount of base token" , async ( ) = > {
2020-09-08 20:25:59 +08:00
await ctx . mintTestToken ( trader , decimalStr ( "10000" ) , "0" ) ;
await ctx . DODO . methods
. sellBaseToken ( decimalStr ( "10000" ) , "0" , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
2020-06-26 00:31:25 +08:00
// nearly drain out quote pool
// because the fee donated is greater than remaining quote pool
// quote lp earn a considerable profit
2020-09-08 20:25:59 +08:00
assert . equal (
await ctx . QUOTE . methods . balanceOf ( trader ) . call ( ) ,
"1996900220185135480813"
) ;
assert . equal (
await ctx . DODO . methods . getLpQuoteBalance ( lp ) . call ( ) ,
"4574057156329524019750"
) ;
} ) ;
} ) ;
2020-06-26 00:31:25 +08:00
describe ( "Revert cases" , ( ) = > {
it ( "price limit" , async ( ) = > {
await assert . rejects (
2020-09-08 20:25:59 +08:00
ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "100" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ,
2020-06-26 00:31:25 +08:00
/BUY_BASE_COST_TOO_MUCH/
2020-09-08 20:25:59 +08:00
) ;
2020-06-26 00:31:25 +08:00
await assert . rejects (
2020-09-08 20:25:59 +08:00
ctx . DODO . methods
. sellBaseToken ( decimalStr ( "1" ) , decimalStr ( "100" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ,
2020-06-26 00:31:25 +08:00
/SELL_BASE_RECEIVE_NOT_ENOUGH/
2020-09-08 20:25:59 +08:00
) ;
} ) ;
2020-06-26 00:31:25 +08:00
it ( "base balance limit" , async ( ) = > {
await assert . rejects (
2020-09-08 20:25:59 +08:00
ctx . DODO . methods
. buyBaseToken ( decimalStr ( "11" ) , decimalStr ( "10000" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ,
2020-07-12 15:30:32 +08:00
/DODO_BASE_BALANCE_NOT_ENOUGH/
2020-09-08 20:25:59 +08:00
) ;
2020-06-26 00:31:25 +08:00
2020-09-08 20:25:59 +08:00
await ctx . DODO . methods
. buyBaseToken ( decimalStr ( "1" ) , decimalStr ( "200" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ;
2020-06-26 00:31:25 +08:00
await assert . rejects (
2020-09-08 20:25:59 +08:00
ctx . DODO . methods
. buyBaseToken ( decimalStr ( "11" ) , decimalStr ( "10000" ) , "0x" )
. send ( ctx . sendParam ( trader ) ) ,
2020-07-12 15:30:32 +08:00
/DODO_BASE_BALANCE_NOT_ENOUGH/
2020-09-08 20:25:59 +08:00
) ;
} ) ;
} ) ;
} ) ;