Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : ExitFee Settings moved to contract! #42

Closed
wants to merge 16 commits into from
6 changes: 3 additions & 3 deletions .github/workflows/lb-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Build Docker images
run: docker-compose -f docker-compose.test.base.yml -f docker-compose.test.reg.yml -f docker-compose.test.par.yml build
run: docker compose -f docker-compose.test.base.yml -f docker-compose.test.reg.yml -f docker-compose.test.par.yml build

- uses: actions/checkout@v3
- name: Run unit & integration tests
run: docker-compose -f docker-compose.test.base.yml -f docker-compose.test.reg.yml up --abort-on-container-exit
run: docker compose -f docker-compose.test.base.yml -f docker-compose.test.reg.yml up --abort-on-container-exit

- uses: actions/checkout@v3
- name: Run parallel tests
run: docker container prune -f && docker volume prune -f && docker-compose -f docker-compose.test.base.yml -f docker-compose.test.par.yml up --abort-on-container-exit
run: docker container prune -f && docker volume prune -f && docker compose -f docker-compose.test.base.yml -f docker-compose.test.par.yml up --abort-on-container-exit
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn build && yarn lint && yarn depcheck
pnpm build && pnpm lint && pnpm depcheck
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.20.0
20.17.0
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM node:20
COPY package.json .
RUN npm i
RUN npm i -g pnpm
RUN pnpm install
COPY . .
CMD ["npm", "run", "start"]
CMD ["pnpm", "run", "start:prod"]

74 changes: 69 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,78 @@ All configuration is done via environment variables. See all variables at [.env.

## Building & Running

1. Make sure dependencies are installed - run `yarn` in the base directory
2. Build `yarn build`
3. Run `yarn start`
### Development & Production

## Postgresql
Make sure dependencies are installed - run script below in the base directory
```shell
pnpm install
```

Starts the project in development mode using ts-node to execute the script at ./src/exec/run.ts.
```shell
pnpm start
```


Starts the project in production mode by running the pre-built script at ./build/src/exec/run.js.
```shell
pnpm start:prod
```

### Build & Clean

Compiling smart contracts using Hardhat.
Compiling TypeScript files based on tsconfig.json.

```shell
pnpm build
```


Cleans the project by removing the dist/ folder and TypeScript build info files.
```shell
pnpm clean
```

### Linting

Runs both linting checks and applies automatic formatting fixes.
```shell
pnpm lint
```

Automatically formats TypeScript files in src, exec, and test directories based on Prettier configuration (.prettierrc.json).
```shell
pnpm lint:fix
```

Performs TSLint checks and displays output in a stylish format.
```shell
pnpm lint:check
```

### Database

Starts a PostgreSQL database in a Docker container with:
- Database Name: **lightbridge**
- Password: **abcdef**
- Exposed on port **5432**.

```shell
pnpm db:start
```

Connect to Postgres on CLI:
`psql --username postgres -d postgres --password`
```shell
psql --username postgres -d postgres --password
```

### Testing

Runs all unit and integration tests using Hardhat, with stack traces enabled for easier debugging.
```shell
pnpm test
```

## Deployments

Expand Down
72 changes: 67 additions & 5 deletions contracts/LightBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
/// @dev The total number of disbursements processed.
mapping(uint256 => uint256) public totalDisbursements;

// @dev depositId to failed status and disbursement info
/// @dev depositId to failed status and disbursement info
mapping(uint256 => FailedNativeDisbursement) public failedNativeDisbursements;

/// @dev exit fee (in percentage) to deduct fee while disbursement of asset.
/// where 1% = 100 and 100% = 10000
/// {sourceChainId} => {exitFee}
mapping(uint32 => uint256) public exitFee;

/// @dev amount of token collected as fee for disbursement of asset.
uint256 public feeCollected;

/********************
* Events *
********************/
Expand Down Expand Up @@ -152,6 +160,12 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
uint32 indexed toChainId,
bool supported
);

event ExitFeeSet(
uint256 previousFee,
uint256 exitFee,
uint32 sourceChainId
);

/**********************
* Function Modifiers *
Expand Down Expand Up @@ -180,12 +194,11 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
/********************
* Public Functions *
********************/

/// @dev Initialize this contract
function initialize() external onlyNotInitialized() initializer() {
disburser = msg.sender;
owner = msg.sender;

__Context_init_unchained();
__Pausable_init_unchained();
__Multicall_init_unchained();
Expand Down Expand Up @@ -271,6 +284,32 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
emit AssetReceived(_token, uint32(block.chainid), _toChainId, totalDeposits[_toChainId] - 1, msg.sender, _amount);
}

/**
* @dev get the fee for sourceChainId in case exitFee is 0 (not set or no fee for this chain),
* no fee is applied the exit fee is not set for source chain id will return amount as it is.
* if exitFee is set calculate the fee to deduct from amount and return amountAfterFee deduction.
*
* @param _amount A amount to be disburser
* @param _sourceChainId A sourceChainId from where withdrawal has been intiated.
*/
function calculateAmountAfterFee(uint256 _amount, uint32 _sourceChainId)
public
view
returns (uint256, uint256) {
uint256 fee = exitFee[_sourceChainId];

if (fee == 0) {
return (_amount, 0);
}

/// As fee is in basis points so dividing by 10^4 can give actual percentage of fee.
uint256 _fee = _amount * fee / 10000;
uint256 _amountAfterFee = _amount - _fee;

return (_amountAfterFee, _fee);
}


/**
* @dev Accepts a list of Disbursements and forwards the amount paid to
* the contract to each recipient. The method reverts if there are zero
Expand All @@ -295,7 +334,7 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
uint256 remainingValue = msg.value;
for (uint256 i = 0; i < _numDisbursements; i++) {

uint256 _amount = _disbursements[i].amount;
uint256 _amountBeforeFee = _disbursements[i].amount;
address _addr = _disbursements[i].addr;
uint32 _sourceChainId = _disbursements[i].sourceChainId;
uint256 _depositId = _disbursements[i].depositId;
Expand All @@ -307,7 +346,13 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {
// Ensure the depositId matches our expected value.
require(_depositId == totalDisbursements[_sourceChainId], "Unexpected next deposit id");
totalDisbursements[_sourceChainId] += 1;


// calculate amount after deducting fee
(uint256 _amount, uint256 fee) = calculateAmountAfterFee(_amountBeforeFee, _sourceChainId);

// update record of fee collected
feeCollected += fee;

// ensure amount sent in the tx is equal to disbursement (moved into loop to ensure token flexibility)
if (_token == address(0)) {
require(_amount <= remainingValue, "Disbursement total != amount sent");
Expand Down Expand Up @@ -497,4 +542,21 @@ contract LightBridge is PausableUpgradeable, MulticallUpgradeable {

emit MaxTransferAmountPerDaySet(_token, _toChainId, pastMaxTransferAmountPerDay, _maxTransferAmountPerDay);
}

/**
* @dev Sets exit fee (in basis points 1% = 100) used to calculate the fee to deduct while withdrawal of asset
*
* @param _exitFee The exit in the form of basis point (0.1)
* @param _sourceChainId sourceChainId for which the exitFee need to set.
*/
function setExitFee(uint256 _exitFee, uint32 _sourceChainId) external onlyOwner() {
require(_exitFee <= 10000, "Exit fee too high"); // Max 100%

uint256 previousFee = exitFee[_sourceChainId];

exitFee[_sourceChainId] = _exitFee;

emit ExitFeeSet(previousFee, _exitFee, _sourceChainId);
}

}
31 changes: 3 additions & 28 deletions docker-compose.test.base.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.8"
# version: "3.8"

services:
subgraph_deployer_eth:
Expand Down Expand Up @@ -115,6 +115,7 @@ services:
- "anvil --hardfork berlin --chain-id 31338" # use legacy tx to emulate boba network
environment:
- ANVIL_IP_ADDR=0.0.0.0
platform: linux/amd64
# ports:
# - "8546:8545"
anvil_eth:
Expand All @@ -123,35 +124,9 @@ services:
- "anvil --chain-id 31337" # use EIP 1559 tx to emulate new network (e.g. Boba Sepolia)
environment:
- ANVIL_IP_ADDR=0.0.0.0
platform: linux/amd64
# ports:
# - "8545:8545"

lightbridge:
depends_on:
- lightbridge_db
- kms
- anvil_eth
- anvil_bnb
- graph-node_eth
- graph-node_bnb
image: bobanetwork/lightbridge:latest
build:
context: .
dockerfile: ./Dockerfile
command: bash -c "sleep 60; npm test"
environment:
LIGHTBRIDGE_MODE: "testnets"
LIGHTBRIDGE_REJECT_UNAUTHORIZED: "true"
# KMS setup (incl. defaults)
LIGHTBRIDGE_ENV: "dev"
LIGHTBRIDGE_AWS_KMS_ACCESS_KEY: "1"
LIGHTBRIDGE_AWS_KMS_SECRET_KEY: "2"
LIGHTBRIDGE_AWS_KMS_KEY_ID: "lb_disburser_pk"
LIGHTBRIDGE_AWS_KMS_REGION: "us-east-1"
LIGHTBRIDGE_AWS_KMS_ENDPOINT: "http://kms:8888/"
# DB
LIGHTBRIDGE_POSTGRES_PASSWORD: "abcdef"
LIGHTBRIDGE_POSTGRES_DB_HOST: "lightbridge_db" # always use local instance for tests

lightbridge_db:
image: postgres
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test.reg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
build:
context: .
dockerfile: ./Dockerfile
command: bash -c "sleep 60; npm test"
command: bash -c "sleep 60; pnpm test:e2e"
environment:
LIGHTBRIDGE_MODE: "testnets"
LIGHTBRIDGE_REJECT_UNAUTHORIZED: "true"
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
"scripts": {
"start": "ts-node ./src/exec/run.ts",
"start:prod": "node ./build/src/exec/run.js",
"build": "yarn build:contracts && yarn build:typescript",
"build": "pnpm build:contracts && pnpm build:typescript",
"build:contracts": "hardhat compile",
"build:typescript": "tsc -p ./tsconfig.json",
"clean": "rimraf dist/ ./tsconfig.tsbuildinfo",
"lint": "yarn lint:fix && yarn lint:check",
"lint": "pnpm lint:fix && pnpm lint:check",
"lint:fix": "prettier --config .prettierrc.json --write \"{src,exec,test}/**/*.ts\"",
"lint:check": "tslint --format stylish --project .",
"db:start": "docker run -e POSTGRES_DB=lightbridge -e POSTGRES_PASSWORD=abcdef -p 5432:5432 postgres",
"test": "hardhat test test/lightbridge.integration.spec.ts test/lightbridge.unit.spec.ts --show-stack-traces",
"test:paralell": "hardhat test test/lightbridge.parallel.spec.ts --show-stack-traces",
"test:docker": "docker-compose -f docker-compose.test.base.yml -f docker-compose.test.reg.yml up --abort-on-container-exit",
"test:unit": "hardhat test test/lightbridge.integration.spec.ts test/lightbridge.unit.spec.ts --show-stack-traces",
"test:e2e": "hardhat test test/lightbridge.integration.spec.ts --show-stack-traces",
"test:paralell": "npx hardhat test test/lightbridge.parallel.spec.ts --show-stack-traces",
"test:docker": "docker-compose -f docker-compose.test.base.yml up --abort-on-container-exit",
"test:docker:paralell": "docker-compose -f docker-compose.test.base.yml -f docker-compose.test.par.yml up --abort-on-container-exit",
"test:coverage": "nyc hardhat test && nyc merge .nyc_output coverage.json",
"typeorm": "typeorm-ts-node-commonjs",
Expand Down Expand Up @@ -51,7 +52,7 @@
"dependencies": {
"@bobanetwork/aws-kms": "^1.0.1",
"@bobanetwork/core_contracts": "0.5.12",
"@bobanetwork/graphql-utils": "^1.1.6",
"@bobanetwork/graphql-utils": "1.1.10",
"@bobanetwork/light-bridge-chains": "^1.1.2",
"@eth-optimism/common-ts": "0.2.2",
"@openzeppelin/contracts": "4.3.2",
Expand All @@ -66,7 +67,7 @@
},
"husky": {
"hooks": {
"pre-commit": "yarn build && yarn lint && yarn depcheck"
"pre-commit": "pnpm build && pnpm lint && pnpm depcheck"
}
}
}
Loading
Loading