Skip to content

Commit

Permalink
merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Oct 25, 2024
2 parents 08ccd88 + 389a264 commit 8d38348
Show file tree
Hide file tree
Showing 10 changed files with 1,801 additions and 120 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ yarn-error.log
!.yarn/plugins
!.yarn/sdks
!.yarn/versions

.env
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@
"scripts": {
"dev": "tsc -w --importHelpers",
"build": "tsc --importHelpers",
"prepare": "yarn build"
"prepare": "yarn build",
"test": "mocha --loader=ts-node/esm --exit --timeout 60000 'src/**/*.test.ts'"
},
"dependencies": {
"eth-permit": "^0.2.3",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@types/chai": "^5.0.0",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.8",
"chai": "^5.1.1",
"dotenv": "^16.4.5",
"eslint": "^9.12.0",
"globals": "^15.11.0",
"mocha": "^10.7.3",
"npm-run-all": "4.1.5",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "~5.6.3",
"typescript-eslint": "^8.8.1",
"viem": "^2.21.25"
Expand Down
7 changes: 6 additions & 1 deletion src/Centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { doTransaction, isLocalAccount } from './utils/transaction.js'

export type Config = {
environment: 'mainnet' | 'demo' | 'dev'
rpcUrls?: Record<number | string, string>
subqueryUrl: string
}
type DerivedConfig = Config & {
Expand Down Expand Up @@ -107,9 +108,13 @@ export class Centrifuge {
chains
.filter((chain) => (this.#config.environment === 'mainnet' ? !chain.testnet : chain.testnet))
.forEach((chain) => {
const rpcUrl = this.#config.rpcUrls?.[`${chain.id}`] ?? undefined
if (!rpcUrl) {
console.warn(`No rpcUrl defined for chain ${chain.id}. Using public RPC endpoint.`)
}
this.#clients.set(
chain.id,
createPublicClient<any, Chain>({ chain, transport: http(), batch: { multicall: true } })
createPublicClient<any, Chain>({ chain, transport: http(rpcUrl), batch: { multicall: true } })
)
})
}
Expand Down
16 changes: 8 additions & 8 deletions src/abi/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { parseAbi } from 'viem'
import CentrifugeRouter from './CentrifugeRouter.abi.json'
import Currency from './Currency.abi.json'
import Gateway from './Gateway.abi.json'
import InvestmentManager from './InvestmentManager.abi.json'
import LiquidityPool from './LiquidityPool.abi.json'
import Permit from './Permit.abi.json'
import PoolManager from './PoolManager.abi.json'
import Router from './Router.abi.json'
import CentrifugeRouter from './CentrifugeRouter.abi.json' assert { type: 'json' }
import Currency from './Currency.abi.json' assert { type: 'json' }
import Gateway from './Gateway.abi.json' assert { type: 'json' }
import InvestmentManager from './InvestmentManager.abi.json' assert { type: 'json' }
import LiquidityPool from './LiquidityPool.abi.json' assert { type: 'json' }
import Permit from './Permit.abi.json' assert { type: 'json' }
import PoolManager from './PoolManager.abi.json' assert { type: 'json' }
import Router from './Router.abi.json' assert { type: 'json' }

export const ABI = {
CentrifugeRouter: parseAbi(CentrifugeRouter),
Expand Down
58 changes: 58 additions & 0 deletions src/tests/Centrifuge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from 'chai'
import { Centrifuge } from '../Centrifuge.js'
import { TenderlyFork } from './tenderly.js'
import { sepolia } from 'viem/chains'
import { parseEther } from 'viem'

describe('Centrifuge', () => {
let centrifuge: Centrifuge
let tenderlyFork: TenderlyFork

before(async () => {
tenderlyFork = await TenderlyFork.create(sepolia)
centrifuge = new Centrifuge({
environment: 'demo',
rpcUrls: {
11155111: tenderlyFork.rpcUrl,
},
})
centrifuge.setSigner(tenderlyFork.signer)
})
// TODO: don't remove if any test fails
// after(async () => {
// return await tenderlyFork.deleteTenderlyRpcEndpoint()
// })
it('should be connected to sepolia', async () => {
const client = centrifuge.getClient()
expect(client?.chain.id).to.equal(11155111)
const chains = centrifuge.chains
expect(chains).to.include(11155111)
})
it('should fetch account and balances', async () => {
const account = await centrifuge.account('0x423420Ae467df6e90291fd0252c0A8a637C1e03f')
const balances = await account.balances()
expect(balances).to.exist
})

it('should make a transfer', async () => {
await Promise.all([
tenderlyFork.fundAccountEth(tenderlyFork.account.address, parseEther('100')),
tenderlyFork.fundAccountERC20(tenderlyFork.account.address, parseEther('100')),
])
const account = await centrifuge.account(tenderlyFork.account.address)
const balances = await account.balances()
expect(Number(balances)).to.be.greaterThan(0)
// doesn't work: ERC20/insufficient-balance, the tenderly signer does not match the sender of the transfer
// const transfer = await account.transfer('0x423420Ae467df6e90291fd0252c0A8a637C1e03f', parseEther('10'))
// if ('receipt' in transfer) {
// expect(transfer.receipt.status).to.equal('success')
// } else {
// throw new Error('Transfer failed')
// }
})

it('should fetch a pool by id', async () => {
const pool = await centrifuge.pool('4139607887')
expect(pool).to.exist
})
})
52 changes: 52 additions & 0 deletions src/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Testing with Mocha and Tenderly

Tenderly allows us to create a local Ethereum network that is compatible with Ethereum Mainnet that we can use to run our tests against. A new fork will be created for each test suite.

### Local setup

1. Visit [Tenderly](https://dashboard.tenderly.co/) and create an account if you don't have one.

2. Create a new project.

3. Settings is where you will find the values for the environment variables `TENDERLY_ACCESS_KEY` and `TENDERLY_ACCOUNT_SLUG` and `TENDERLY_PROJECT_SLUG`

4. Copy `env.example` file to `.env` and add the values you found in the previous step.

Install dependencies:

```bash
yarn
```

Run the tests:

```bash
yarn test
```

### Usage

```ts
// create a new fork
const tenderlyFork = await TenderlyFork.create(sepolia)

// or use an existing fork
const tenderlyFork = new TenderlyFork(sepolia, '<vnet-id>')

// connect to centrifuge
const centrifuge = new Centrifuge({
environment: 'demo',
rpcUrls: {
11155111: tenderlyFork.rpcUrl,
},
})

// set the tenderly signer as the signer for centrifuge
centrifuge.setSigner(tenderlyFork.signer)

// fund the signer's account on the fork
await tenderlyFork.fundAccountEth(tenderlyFork.account.address, parseEther('100'))

// fund the signer's account with USDt ERC20 tokens
await tenderlyFork.fundAccountERC20(tenderlyFork.account.address, parseEther('100'))
```
3 changes: 3 additions & 0 deletions src/tests/env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TENDERLY_ACCESS_KEY=
PROJECT_SLUG=
ACCOUNT_SLUG=
Loading

0 comments on commit 8d38348

Please sign in to comment.