Skip to content

Commit

Permalink
MMerge branch 'main' of https://github.com/dydxprotocol/v4-chain into…
Browse files Browse the repository at this point in the history
… wl/qc1
  • Loading branch information
dydxwill committed Dec 7, 2023
2 parents 5fbe8fe + 834c19c commit 1fecda9
Show file tree
Hide file tree
Showing 24 changed files with 712 additions and 144 deletions.
1 change: 1 addition & 0 deletions e2e-testing/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ETH_RPC_ENDPOINT=https://eth-sepolia.g.alchemy.com/v2/demo
229 changes: 229 additions & 0 deletions e2e-testing/docker-compose-e2e-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
version: '3'
services:
kafka:
image: blacktop/kafka:2.6
ports:
- 9092:9092
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka
KAFKA_CREATE_TOPICS:
"to-ender:1:1,\
to-vulcan:1:1,\
to-websockets-orderbooks:1:1,\
to-websockets-subaccounts:1:1,\
to-websockets-trades:1:1,\
to-websockets-markets:1:1,\
to-websockets-candles:1:1"
KAFKA_LISTENERS: INTERNAL://:9092,EXTERNAL_SAME_HOST://:29092
KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:9092,EXTERNAL_SAME_HOST://localhost:29092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL_SAME_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
DD_AGENT_HOST: datadog-agent
healthcheck:
test: ["CMD-SHELL", "kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --topic to-websockets-candles --describe"]
interval: 5s
timeout: 20s
retries: 50
postgres:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.postgres.local
ports:
- 5435:5432
environment:
POSTGRES_PASSWORD: dydxserver123
POSTGRES_USER: dydx_dev
DATADOG_POSTGRES_PASSWORD: dydxserver123
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dydx_dev"]
interval: 5s
timeout: 20s
retries: 10
redis:
image: redis:5.0.6-alpine
ports:
- 6382:6379
dydxprotocold0:
image: local:e2etest-dydxprotocol
entrypoint:
- cosmovisor
- run
- start
- --log_level
# Note that only this validator has a log-level of `info`; other validators use `error` by default.
# Change to `debug` for more verbose log-level.
- info
- --home
- /dydxprotocol/chain/.alice
- --p2p.persistent_peers
- "17e5e45691f0d01449c84fd4ae87279578cdd7ec@dydxprotocold0:26656,47539956aaa8e624e0f1d926040e54908ad0eb44@dydxprotocold2:26656,5882428984d83b03d0c907c1f0af343534987052@dydxprotocold3:26656"
- --bridge-daemon-eth-rpc-endpoint
- "${ETH_RPC_ENDPOINT}"
environment:
- DAEMON_HOME=/dydxprotocol/chain/.alice
volumes:
- ../protocol/localnet/dydxprotocol0:/dydxprotocol/chain/.alice/data
ports:
- "26657:26657"
- "9090:9090"
- "1317:1317"

# This is the Indexer connected node.
# TODO: remove stake and make this a full node.
dydxprotocold1:
image: local:e2etest-dydxprotocol
entrypoint:
- cosmovisor
- run
- start
- --log_level
- error
- --home
- /dydxprotocol/chain/.bob
- --p2p.persistent_peers
- "17e5e45691f0d01449c84fd4ae87279578cdd7ec@dydxprotocold0:26656,b69182310be02559483e42c77b7b104352713166@dydxprotocold1:26656,47539956aaa8e624e0f1d926040e54908ad0eb44@dydxprotocold2:26656,5882428984d83b03d0c907c1f0af343534987052@dydxprotocold3:26656"
- --non-validating-full-node=true
- --bridge-daemon-eth-rpc-endpoint
- "${ETH_RPC_ENDPOINT}"
- --indexer-kafka-conn-str
- "kafka:9092"
environment:
- DAEMON_HOME=/dydxprotocol/chain/.bob
volumes:
- ../protocol/localnet/dydxprotocol1:/dydxprotocol/chain/.bob/data
ports:
- "26658:26657"
depends_on:
kafka:
condition: service_healthy

dydxprotocold2:
image: local:e2etest-dydxprotocol
entrypoint:
- cosmovisor
- run
- start
- --log_level
- error
- --home
- /dydxprotocol/chain/.carl
- --p2p.persistent_peers
- "17e5e45691f0d01449c84fd4ae87279578cdd7ec@dydxprotocold0:26656,47539956aaa8e624e0f1d926040e54908ad0eb44@dydxprotocold2:26656,5882428984d83b03d0c907c1f0af343534987052@dydxprotocold3:26656"
- --bridge-daemon-eth-rpc-endpoint
- "${ETH_RPC_ENDPOINT}"
environment:
- DAEMON_HOME=/dydxprotocol/chain/.carl
volumes:
- ../protocol/localnet/dydxprotocol2:/dydxprotocol/chain/.carl/data

dydxprotocold3:
image: local:e2etest-dydxprotocol
entrypoint:
- cosmovisor
- run
- start
- --log_level
- error
- --home
- /dydxprotocol/chain/.dave
- --p2p.persistent_peers
- "17e5e45691f0d01449c84fd4ae87279578cdd7ec@dydxprotocold0:26656,47539956aaa8e624e0f1d926040e54908ad0eb44@dydxprotocold2:26656,5882428984d83b03d0c907c1f0af343534987052@dydxprotocold3:26656"
- --bridge-daemon-eth-rpc-endpoint
- "${ETH_RPC_ENDPOINT}"
environment:
- DAEMON_HOME=/dydxprotocol/chain/.dave
volumes:
- ../protocol/localnet/dydxprotocol3:/dydxprotocol/chain/.dave/data

postgres-package:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.postgres-package.local
links:
- postgres
depends_on:
postgres:
condition: service_healthy
ender:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.service.local
args:
service: ender
ports:
- 3001:3001
links:
- postgres
environment:
- REDIS_URL=redis://redis:6379
depends_on:
kafka:
condition: service_healthy
postgres-package:
condition: service_completed_successfully
comlink:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.service.local
args:
service: comlink
environment:
- PORT=3002
- REDIS_URL=redis://redis:6379
- RATE_LIMIT_REDIS_URL=redis://redis:6379
- RATE_LIMIT_ENABLED=false
- INDEXER_LEVEL_GEOBLOCKING_ENABLED=false
- COMPLIANCE_DATA_CLIENT=PLACEHOLDER
ports:
- 3002:3002
links:
- postgres
depends_on:
postgres-package:
condition: service_completed_successfully
socks:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.service.local
args:
service: socks
ports:
- 3003:3003
links:
- postgres
environment:
- WS_PORT=3003
- COMLINK_URL=host.docker.internal:3002
depends_on:
kafka:
condition: service_healthy
postgres-package:
condition: service_completed_successfully
roundtable:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.service.local
args:
service: roundtable
ports:
- 3004:3004
links:
- postgres
depends_on:
kafka:
condition: service_healthy
postgres-package:
condition: service_completed_successfully
vulcan:
build:
context: ../indexer
dockerfile: ../indexer/Dockerfile.service.local
args:
service: vulcan
environment:
- REDIS_URL=redis://redis:6379
ports:
- 3005:3005
depends_on:
kafka:
condition: service_healthy
4 changes: 4 additions & 0 deletions e2e-testing/run-containerized-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd ../protocol
make e2etest-build-image
cd ../e2e-testing
docker compose -f docker-compose-e2e-test.yml up
9 changes: 7 additions & 2 deletions indexer/docker-compose-local-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ services:
- DD_PROFILING_ENABLED=true
- DD_ENV=localnet_${USER}
- DD_AGENT_HOST=datadog-agent
- REDIS_URL=redis://redis:6379
labels:
com.datadoghq.ad.logs: '[{"source": "indexer", "service": "ender"}]'
depends_on:
Expand All @@ -113,15 +114,18 @@ services:
dockerfile: Dockerfile.service.local
args:
service: comlink
NPM_TOKEN: ${NPM_TOKEN}
environment:
# See https://docs.datadoghq.com/profiler/enabling/nodejs/ for DD_ specific environment variables.
# Note that DD_SERVICE and DD_VERSION are read by default from package.json
- DD_PROFILING_ENABLED=true
- DD_ENV=localnet_${USER}
- DD_AGENT_HOST=datadog-agent
- TENDERMINT_WS_URL=host.docker.internal:26657 # connects to localhost:26657 on host machine
- REDIS_URL=redis://redis:6379
- RATE_LIMIT_REDIS_URL=redis://redis:6379
- PORT=3002
- RATE_LIMIT_ENABLED=false
- INDEXER_LEVEL_GEOBLOCKING_ENABLED=false
- COMPLIANCE_DATA_CLIENT=PLACEHOLDER
labels:
com.datadoghq.ad.logs: '[{"source": "indexer", "service": "comlink"}]'
ports:
Expand Down Expand Up @@ -191,6 +195,7 @@ services:
- DD_PROFILING_ENABLED=true
- DD_ENV=localnet_${USER}
- DD_AGENT_HOST=datadog-agent
- REDIS_URL=redis://redis:6379
labels:
com.datadoghq.ad.logs: '[{"source": "indexer", "service": "vulcan"}]'
ports:
Expand Down
6 changes: 3 additions & 3 deletions indexer/packages/postgres/__tests__/db/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('helpers', () => {
[defaultFundingIndexUpdate.perpetualId]: Big('10050'),
},
),
).toEqual(Big('20000')); // 10 * (12050-10050)
).toEqual(Big('-20000')); // 10 * (10050-12050). longs pay shorts when funding index is increasing.
});

it('compute unsettled funding for short position', () => {
Expand All @@ -64,7 +64,7 @@ describe('helpers', () => {
[defaultFundingIndexUpdate.perpetualId]: Big('10050'),
},
),
).toEqual(Big('-20000')); // -10 * (12050-10050)
).toEqual(Big('20000')); // -10 * (10050-12050). longs pay shorts when funding index is increasing.
});

it('compute unsettled funding for decimal position', () => {
Expand All @@ -82,7 +82,7 @@ describe('helpers', () => {
[defaultFundingIndexUpdate.perpetualId]: Big('10050'),
},
),
).toEqual(Big('2700.1674')); // 1.35 * (12050.124-10050)
).toEqual(Big('-2700.1674')); // 1.35 * (10050-12050.124). longs pay shorts when funding index is increasing.
});
});

Expand Down
11 changes: 8 additions & 3 deletions indexer/packages/postgres/src/db/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ export function getMaintenanceMarginPpm(
* Computes the unsettled funding for a position.
*
* To compute the net USDC balance for a subaccount, sum the result of this function for all
* open perpetual positions, and subtract the sum from the latest USDC asset position for
* open perpetual positions, and add it to the latest USDC asset position for
* this subaccount.
*
* When funding index is increasing, shorts get paid & unsettled funding for shorts should
* be positive, vice versa for longs.
* When funding index is decreasing, longs get paid & unsettled funding for longs should
* be positive, vice versa for shorts.
*
* @param position
* @param latestFundingIndex
* @param lastUpdateFundingIndex
Expand All @@ -53,8 +58,8 @@ export function getUnsettledFunding(
lastUpdateFundingIndexMap: FundingIndexMap,
): Big {
return Big(position.size).times(
latestFundingIndexMap[position.perpetualId].minus(
lastUpdateFundingIndexMap[position.perpetualId],
lastUpdateFundingIndexMap[position.perpetualId].minus(
latestFundingIndexMap[position.perpetualId],
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ describe('addresses-controller#V4', () => {
testConstants.defaultPerpetualPosition.entryPrice!,
),
maxSize: testConstants.defaultPerpetualPosition.maxSize,
// 200000 + 10*(10050-10000)=200500
netFunding: getFixedRepresentation('200500'),
// 200000 + 10*(10000-10050)=199500
netFunding: getFixedRepresentation('199500'),
// sumClose=0, so realized Pnl is the same as the net funding of the position.
// Unsettled funding is funding payments that already "happened" but not reflected
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
realizedPnl: getFixedRepresentation('200500'),
realizedPnl: getFixedRepresentation('199500'),
// size * (index-entry) = 10*(15000-20000) = -50000
unrealizedPnl: getFixedRepresentation(-50000),
status: testConstants.defaultPerpetualPosition.status,
Expand Down Expand Up @@ -246,12 +246,12 @@ describe('addresses-controller#V4', () => {
testConstants.defaultPerpetualPosition.entryPrice!,
),
maxSize: testConstants.defaultPerpetualPosition.maxSize,
// 200000 + 10*(10050-10000)=200500
netFunding: getFixedRepresentation('200500'),
// 200000 + 10*(10000-10050)=199500
netFunding: getFixedRepresentation('199500'),
// sumClose=0, so realized Pnl is the same as the net funding of the position.
// Unsettled funding is funding payments that already "happened" but not reflected
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
realizedPnl: getFixedRepresentation('200500'),
realizedPnl: getFixedRepresentation('199500'),
// size * (index-entry) = 10*(15000-20000) = -50000
unrealizedPnl: getFixedRepresentation(-50000),
status: testConstants.defaultPerpetualPosition.status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ describe('perpetual-positions-controller#V4', () => {
// For the calculation of the net funding (long position):
// settled funding on position = 200_000, size = 10, latest funding index = 10050
// last updated funding index = 10000
// total funding = 200_000 + (10 * (10050 - 10000)) = 200_500
netFunding: getFixedRepresentation('200500'),
// total funding = 200_000 + (10 * (10000 - 10050)) = 199_500
netFunding: getFixedRepresentation('199500'),
// sumClose=0, so realized Pnl is the same as the net funding of the position.
// Unsettled funding is funding payments that already "happened" but not reflected
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
realizedPnl: getFixedRepresentation('200500'),
realizedPnl: getFixedRepresentation('199500'),
// For the calculation of the unrealized pnl (long position):
// index price = 15_000, entry price = 20_000, size = 10
// unrealizedPnl = size * (index price - entry price)
Expand Down Expand Up @@ -126,12 +126,12 @@ describe('perpetual-positions-controller#V4', () => {
// For the calculation of the net funding (short position):
// settled funding on position = 200_000, size = -10, latest funding index = 10050
// last updated funding index = 10000
// total funding = 200_000 + (-10 * (10050 - 10000)) = 199_500
netFunding: getFixedRepresentation('199500'),
// total funding = 200_000 + (-10 * (10000 - 10050)) = 200_500
netFunding: getFixedRepresentation('200500'),
// sumClose=0, so realized Pnl is the same as the net funding of the position.
// Unsettled funding is funding payments that already "happened" but not reflected
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
realizedPnl: getFixedRepresentation('199500'),
realizedPnl: getFixedRepresentation('200500'),
// For the calculation of the unrealized pnl (short position):
// index price = 15_000, entry price = 20_000, size = -10
// unrealizedPnl = size * (index price - entry price)
Expand Down
Loading

0 comments on commit 1fecda9

Please sign in to comment.