Skip to content

Commit

Permalink
Tasks: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PatricioHenderson committed May 2, 2024
1 parent f1e234d commit aa6c587
Showing 1 changed file with 130 additions and 67 deletions.
197 changes: 130 additions & 67 deletions packages/tasks/test/swap/BalancerV2Swapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
deployProxy,
deployTokenMock,
getSigners,
NATIVE_TOKEN_ADDRESS,
ONES_ADDRESS,
ONES_BYTES32,
ZERO_ADDRESS,
} from '@mimic-fi/v3-helpers'
Expand All @@ -15,94 +17,155 @@ import { buildEmptyTaskConfig, deployEnvironment } from '../../src/setup'
import { itBehavesLikeBaseSwapTask } from './BaseSwapTask.behavior'

describe('BalancerV2Swapper', () => {
let task: Contract
let smartVault: Contract, authorizer: Contract, connector: Contract, owner: SignerWithAddress
context("when no pool id's are set", () => {
let task: Contract
let smartVault: Contract, authorizer: Contract, connector: Contract, owner: SignerWithAddress

before('setup', async () => {
// eslint-disable-next-line prettier/prettier
[, owner] = await getSigners()
;({ authorizer, smartVault } = await deployEnvironment(owner))
})
before('setup', async () => {
// eslint-disable-next-line prettier/prettier
[, owner] = await getSigners()
;({ authorizer, smartVault } = await deployEnvironment(owner))
})

before('deploy connector', async () => {
connector = await deploy('BalancerV2SwapConnectorMock', [ZERO_ADDRESS])
const overrideConnectorCheckRole = smartVault.interface.getSighash('overrideConnectorCheck')
await authorizer.connect(owner).authorize(owner.address, smartVault.address, overrideConnectorCheckRole, [])
await smartVault.connect(owner).overrideConnectorCheck(connector.address, true)
})
before('deploy connector', async () => {
connector = await deploy('BalancerV2SwapConnectorMock', [ZERO_ADDRESS])
const overrideConnectorCheckRole = smartVault.interface.getSighash('overrideConnectorCheck')
await authorizer.connect(owner).authorize(owner.address, smartVault.address, overrideConnectorCheckRole, [])
await smartVault.connect(owner).overrideConnectorCheck(connector.address, true)
})

beforeEach('deploy task', async () => {
task = await deployProxy(
'BalancerV2Swapper',
[],
[
{
balancerPoolIds: [],
baseSwapConfig: {
connector: connector.address,
tokenOut: ZERO_ADDRESS,
maxSlippage: 0,
customTokensOut: [],
customMaxSlippages: [],
taskConfig: buildEmptyTaskConfig(owner, smartVault),
beforeEach('deploy task', async () => {
task = await deployProxy(
'BalancerV2Swapper',
[],
[
{
balancerPoolIds: [],
baseSwapConfig: {
connector: connector.address,
tokenOut: ZERO_ADDRESS,
maxSlippage: 0,
customTokensOut: [],
customMaxSlippages: [],
taskConfig: buildEmptyTaskConfig(owner, smartVault),
},
},
},
]
)
})

describe('swapper', () => {
beforeEach('set params', async function () {
this.owner = owner
this.task = task
this.authorizer = authorizer
]
)
})

itBehavesLikeBaseSwapTask('BALANCER_V2_SWAPPER')
})

describe('setPoolId', () => {
let token: Contract
describe('swapper', () => {
beforeEach('set params', async function () {
this.owner = owner
this.task = task
this.authorizer = authorizer
})

before('deploy token mock', async () => {
token = await deployTokenMock('TKN')
itBehavesLikeBaseSwapTask('BALANCER_V2_SWAPPER')
})

context('when the sender is authorized', () => {
beforeEach('set sender', async () => {
const setPoolIdRole = task.interface.getSighash('setPoolId')
await authorizer.connect(owner).authorize(owner.address, task.address, setPoolIdRole, [])
task = task.connect(owner)
})
describe('setPoolId', () => {
let token: Contract

context('when the pool id is not zero', () => {
const poolId = ONES_BYTES32
before('deploy token mock', async () => {
token = await deployTokenMock('TKN')
})

it('emits an event', async () => {
const tx = await task.setPoolId(token.address, poolId)
await assertEvent(tx, 'BalancerPoolIdSet', { token: token.address, poolId })
context('when the sender is authorized', () => {
beforeEach('set sender', async () => {
const setPoolIdRole = task.interface.getSighash('setPoolId')
await authorizer.connect(owner).authorize(owner.address, task.address, setPoolIdRole, [])
task = task.connect(owner)
})

context('when modifying the pool id', () => {
beforeEach('set pool id', async () => {
await task.setPoolId(token.address, poolId)
})
context('when the pool id is not zero', () => {
const poolId = ONES_BYTES32

it('updates the pool id', async () => {
const poolId = '0x0000000000000000000000000000000000000000000000000000000000000001'
it('emits an event', async () => {
const tx = await task.setPoolId(token.address, poolId)
await assertEvent(tx, 'BalancerPoolIdSet', { token: token.address, poolId })
})

context('when modifying the pool id', () => {
beforeEach('set pool id', async () => {
await task.setPoolId(token.address, poolId)
})

it('updates the pool id', async () => {
const poolId = '0x0000000000000000000000000000000000000000000000000000000000000001'
const tx = await task.setPoolId(token.address, poolId)
await assertEvent(tx, 'BalancerPoolIdSet', { token: token.address, poolId })
})
})
})
})

context('when the token address is zero', () => {
it('reverts', async () => {
await expect(
task.setPoolId(ZERO_ADDRESS, '0x0000000000000000000000000000000000000000000000000000000000000001')
).to.be.revertedWith('TaskTokenZero')
context('when the token address is zero', () => {
it('reverts', async () => {
await expect(
task.setPoolId(ZERO_ADDRESS, '0x0000000000000000000000000000000000000000000000000000000000000001')
).to.be.revertedWith('TaskTokenZero')
})
})
})
})
})

context("when pool id's are set", () => {
let task: Contract
let smartVault: Contract, authorizer: Contract, connector: Contract, owner: SignerWithAddress
const balancerPoolIds = [
{ token: NATIVE_TOKEN_ADDRESS, poolId: ONES_BYTES32 },
{ token: ONES_ADDRESS, poolId: '0x2222222222222222222222222222222222222222222222222222222222222222' },
]

before('setup', async () => {
// eslint-disable-next-line prettier/prettier
[, owner] = await getSigners()
;({ authorizer, smartVault } = await deployEnvironment(owner))
})

before('deploy connector', async () => {
connector = await deploy('BalancerV2SwapConnectorMock', [ZERO_ADDRESS])
const overrideConnectorCheckRole = smartVault.interface.getSighash('overrideConnectorCheck')
await authorizer.connect(owner).authorize(owner.address, smartVault.address, overrideConnectorCheckRole, [])
await smartVault.connect(owner).overrideConnectorCheck(connector.address, true)
})

beforeEach('deploy task', async () => {
task = await deployProxy(
'BalancerV2Swapper',
[],
[
{
balancerPoolIds,
baseSwapConfig: {
connector: connector.address,
tokenOut: ZERO_ADDRESS,
maxSlippage: 0,
customTokensOut: [],
customMaxSlippages: [],
taskConfig: buildEmptyTaskConfig(owner, smartVault),
},
},
]
)
})

describe('swapper', () => {
beforeEach('set params', async function () {
this.owner = owner
this.task = task
this.authorizer = authorizer
})

itBehavesLikeBaseSwapTask('BALANCER_V2_SWAPPER')
})

it("Create the task with pool id's", async () => {
for (const { token, poolId } of balancerPoolIds) {
const actualPoolId = await task.balancerPoolId(token)
expect(actualPoolId).to.equal(poolId)
}
})
})
})

0 comments on commit aa6c587

Please sign in to comment.