Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv committed Mar 20, 2024
0 parents commit eef7ea4
Show file tree
Hide file tree
Showing 29 changed files with 6,813 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# INFURA_API_KEY
# An Infura API Key is needed to setup the service and run the tests
INFURA_API_KEY=

# EOAs configuration
# WARNING: this data is highly sensitive. Make sure these keys don't get
# leaked, especially when using a non-testing network. Losing these keys
# would lead to a permanent loss of the funds they hold.
PRIVATE_KEY=
WALLET_ADDRESS=
SECOND_PRIVATE_KEY=
SECOND_WALLET_ADDRESS=
THIRD_PRIVATE_KEY=
THIRD_WALLET_ADDRESS=
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: ['plugin:@typescript-eslint/recommended', 'prettier'],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
};
66 changes: 66 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: CI

on:
push:
pull_request:
release:
types: [released]

jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.10.x'
cache: 'yarn'
- run: yarn install --immutable
- run: yarn run format-check

es-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.10.x'
cache: 'yarn'
- run: yarn install --immutable
- run: yarn run lint-check

init:
runs-on: ubuntu-latest
env:
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
WALLET_ADDRESS: ${{ secrets.WALLET_ADDRESS }}
SECOND_PRIVATE_KEY: ${{ secrets.SECOND_PRIVATE_KEY }}
SECOND_WALLET_ADDRESS: ${{ secrets.SECOND_WALLET_ADDRESS }}
THIRD_PRIVATE_KEY: ${{ secrets.THIRD_PRIVATE_KEY }}
THIRD_WALLET_ADDRESS: ${{ secrets.THIRD_WALLET_ADDRESS }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.10.x'
cache: 'yarn'
- run: yarn install --immutable
- run: yarn start

tests:
runs-on: ubuntu-latest
env:
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
WALLET_ADDRESS: ${{ secrets.WALLET_ADDRESS }}
SECOND_PRIVATE_KEY: ${{ secrets.SECOND_PRIVATE_KEY }}
SECOND_WALLET_ADDRESS: ${{ secrets.SECOND_WALLET_ADDRESS }}
THIRD_PRIVATE_KEY: ${{ secrets.THIRD_PRIVATE_KEY }}
THIRD_WALLET_ADDRESS: ${{ secrets.THIRD_WALLET_ADDRESS }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.10.x'
cache: 'yarn'
- run: yarn install --immutable
- run: yarn test
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# compiled output
/dist
/node_modules

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# Yarn No Zero Installs
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# env
.env
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn run lint
yarn run format
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.10.0
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
33 changes: 33 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "tsx",
"type": "node",
"request": "launch",
"program": "${file}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsx",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": ["<node_internals>/**", "${workspaceFolder}/node_modules/**"]
},
{
"type": "node",
"request": "launch",
"name": "Debug current test file",
"skipFiles": ["<node_internals>/**"],
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand",
"--coverage",
"false",
"${relativeFile}"
],
"console": "integratedTerminal"
}
]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"typescript.preferences.importModuleSpecifier": "non-relative"
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Héctor Gómez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Safe Backend Integration

Safe Backend integration tests runner.

## Requirements

- Node 20.11.1 – https://nodejs.org/en/

## Installation

**Optional:** If you have NVM installed, you can run `nvm use` in the root folder of the project to use the recommended
Node version set for this project.

We use Yarn as the package manager for this project. Yarn is bundled with the project so to use it run:

```bash
corepack enable && yarn install
```

## 1. Running the setup process

First, configure your private keys and public addresses for the three signer addresses used to run the tests by creating a `.env` file. You can use `.env.sample` as a template.

Then, run the setup application:

```bash
tsx src/index.ts
```

This would print 4 log messages in JSON format indicating the outcome of the execution. Given the provided data for the three signer addresses, the setup process will:

- Check if a Safe is deployed for the given signer addresses setup. i.e.: it would check the on-chain data to see whether a 2/3 Safe with with the provided signer addresses as signers exist on the Sepolia chain.

- If a Safe exists, it will print a `SAFE_ALREADY_DEPLOYED` message, and a link to Safe Wallet Web.
- If it doesn't exist, it would deploy a new Safe with the provided configuration, and it will print a `SAFE_DEPLOYED` message with the link to Safe Wallet Web.

- Check the ETH balances of the provided signer addresses in Sepolia.

- If any of the signer addresses has less than `0.01 ETH`, it will pick a signer with more than `0.01 ETH`, and try to send `0.01 ETH` to the first one. If none of the signer has more than `0.01 ETH`, an error will be thrown.

- Print 3 log lines more indicating the address and the balance of each of the signer addresses configured as Safe signers.

## 2. Running the tests

After the setup process is successfully completed, you can run the tests:

```bash
yarn test
```
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: '.*\\.spec\\.ts$',
testTimeout: 120_000,
};
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "safe-backend-integration",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"axios": "^1.6.2",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"tsx": "^4.7.1",
"typescript": "^5.3.3"
},
"scripts": {
"format": "prettier --write .",
"format-check": "prettier --check .",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint-check": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"start": "./node_modules/.bin/tsx src/index.ts",
"test": "jest",
"test:watch": "jest --watch"
},
"dependencies": {
"@safe-global/api-kit": "^2.1.0",
"@safe-global/protocol-kit": "^2.0.0",
"@safe-global/safe-core-sdk-types": "^3.0.1",
"dotenv": "^16.3.1",
"ethers": "^6.9.0",
"ts-node": "^10.9.2",
"winston": "^3.11.0"
}
}
19 changes: 19 additions & 0 deletions src/__tests__/blockchain/rpc-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Block, ethers } from 'ethers';

describe('Blockchain read-only', () => {
const provider = new ethers.InfuraProvider(
'sepolia',
process.env.INFURA_API_KEY,
);

it('should get the last block', async () => {
const block: Block | null = await provider.getBlock('latest');

expect(block).toMatchObject({
hash: expect.any(String),
number: expect.any(Number),
timestamp: expect.any(Number),
transactions: expect.arrayContaining([]),
});
});
});
16 changes: 16 additions & 0 deletions src/__tests__/blockchain/wallet-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { configuration } from '@/config/configuration';
import { InfuraProvider, Wallet } from 'ethers';

describe('Wallet actions', () => {
const provider = new InfuraProvider('sepolia', process.env.INFURA_API_KEY);
const wallets = configuration.privateKeys.map(
(pk) => new Wallet(pk, provider),
);

it('should build wallets from the configured addresses', async () => {
const [firstWallet, secondWallet, thirdWallet] = wallets;
expect(firstWallet.address).toEqual(configuration.walletAddresses[0]);
expect(secondWallet.address).toEqual(configuration.walletAddresses[1]);
expect(thirdWallet.address).toEqual(configuration.walletAddresses[2]);
});
});
Loading

0 comments on commit eef7ea4

Please sign in to comment.