-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scripts to manage collector tokens (#125)
* feat: add hardhat scripts to add/remove/get tokens from the collector * doc: update readme to include task description * feat: change the removeToken script not to require the token index
- Loading branch information
Showing
10 changed files
with
362 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HardhatRuntimeEnvironment } from 'hardhat/types'; | ||
|
||
export type ManageCollectorTokenArgs = { | ||
collectorAddress: string; | ||
tokenAddress: string; | ||
}; | ||
|
||
export const addTokenToCollector = async ( | ||
{ collectorAddress, tokenAddress }: ManageCollectorTokenArgs, | ||
{ ethers }: HardhatRuntimeEnvironment | ||
) => { | ||
const collector = await ethers.getContractAt('Collector', collectorAddress); | ||
|
||
try { | ||
await collector.addToken(tokenAddress); | ||
} catch (error) { | ||
console.error( | ||
`Error adding token with address ${tokenAddress} to allowed tokens on Collector ${collectorAddress}` | ||
); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HardhatRuntimeEnvironment } from 'hardhat/types'; | ||
import { ManageCollectorTokenArgs } from './addToken'; | ||
|
||
export type GetCollectorTokensArgs = Omit< | ||
ManageCollectorTokenArgs, | ||
'tokenAddress' | ||
>; | ||
|
||
export const getCollectorTokens = async ( | ||
{ collectorAddress }: GetCollectorTokensArgs, | ||
{ ethers }: HardhatRuntimeEnvironment | ||
) => { | ||
const collector = await ethers.getContractAt('Collector', collectorAddress); | ||
|
||
try { | ||
const tokens = await collector.getTokens(); | ||
console.log('Allowed Tokens:', tokens); | ||
} catch (error) { | ||
console.error(`Error retrieving tokens from Collector ${collectorAddress}`); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { HardhatRuntimeEnvironment } from 'hardhat/types'; | ||
import { ManageCollectorTokenArgs } from './addToken'; | ||
|
||
export const removeTokenFromCollector = async ( | ||
{ collectorAddress, tokenAddress }: ManageCollectorTokenArgs, | ||
{ ethers }: HardhatRuntimeEnvironment | ||
) => { | ||
const collector = await ethers.getContractAt('Collector', collectorAddress); | ||
|
||
try { | ||
const tokens = await collector.getTokens(); | ||
const tokenIndex = tokens.findIndex((token) => token === tokenAddress); | ||
if (tokenIndex < 0) { | ||
throw new Error( | ||
`Token with address ${tokenAddress} not found. Please verify the tokens managed by the Collector ${collectorAddress}` | ||
); | ||
} | ||
console.log(`Token found with index ${tokenIndex}`); | ||
await collector.removeToken(tokenAddress, tokenIndex); | ||
} catch (error) { | ||
console.error( | ||
`Error removing token with address ${tokenAddress} from Collector ${collectorAddress}` | ||
); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import * as hre from 'hardhat'; | ||
import { ethers } from 'hardhat'; | ||
import sinon from 'sinon'; | ||
import { | ||
ManageCollectorTokenArgs, | ||
addTokenToCollector, | ||
} from '../../../tasks/collector/addToken'; | ||
import { Collector } from '../../../typechain-types'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('Script to add tokens to collector', function () { | ||
describe('addToken', function () { | ||
const taskArgs: ManageCollectorTokenArgs = { | ||
collectorAddress: '0x06c85B7EA1AA2d030E1a747B3d8d15D5845fd714', | ||
tokenAddress: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
}; | ||
|
||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should add a token when no tokens are managed', async function () { | ||
const addToken = sinon.spy(); | ||
const fakeCollector = { | ||
addToken, | ||
} as unknown as Collector; | ||
sinon.stub(ethers, 'getContractAt').resolves(fakeCollector); | ||
await expect( | ||
addTokenToCollector(taskArgs, hre), | ||
'addTokenToCollector rejected' | ||
).not.to.be.rejected; | ||
expect(addToken.called, 'Collector.addToken was not called').to.be.true; | ||
}); | ||
|
||
it('should fail if the token is already managed', async function () { | ||
const expectedError = new Error('Token already managed'); | ||
const addToken = sinon.spy(() => { | ||
throw expectedError; | ||
}); | ||
const fakeCollector = { | ||
addToken, | ||
} as unknown as Collector; | ||
sinon.stub(ethers, 'getContractAt').resolves(fakeCollector); | ||
await expect( | ||
addTokenToCollector(taskArgs, hre), | ||
'addTokenToCollector did not reject' | ||
).to.be.rejectedWith(expectedError); | ||
expect(addToken.called, 'Collector.addToken was not called').to.be.true; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import * as hre from 'hardhat'; | ||
import { ethers } from 'hardhat'; | ||
import sinon, { SinonSpy } from 'sinon'; | ||
import { | ||
getCollectorTokens, | ||
GetCollectorTokensArgs, | ||
} from '../../../tasks/collector/getTokens'; | ||
import { Collector } from '../../../typechain-types'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('Script to retrieve the collector tokens', function () { | ||
describe('getTokens', function () { | ||
const taskArgs: GetCollectorTokensArgs = { | ||
collectorAddress: '0x06c85B7EA1AA2d030E1a747B3d8d15D5845fd714', | ||
}; | ||
const tokens = ['0x123abc', '0xabc123']; | ||
let consoleLogSpy: SinonSpy; | ||
|
||
beforeEach(function () { | ||
consoleLogSpy = sinon.spy(console, 'log'); | ||
}); | ||
|
||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should get the tokens managed by the collector', async function () { | ||
const getTokens = sinon.stub().returns(Promise.resolve(tokens)); | ||
const fakeCollector = { | ||
getTokens, | ||
} as unknown as Collector; | ||
sinon.stub(ethers, 'getContractAt').resolves(fakeCollector); | ||
await expect( | ||
getCollectorTokens(taskArgs, hre), | ||
'getCollectorTokens rejected' | ||
).not.to.be.rejected; | ||
expect(getTokens.called, 'Collector.getTokens was not called').to.be.true; | ||
expect( | ||
consoleLogSpy.calledWithExactly('Allowed Tokens:', tokens), | ||
'Console.log was not called with the expected arguments' | ||
).to.be.true; | ||
}); | ||
|
||
it('should fail if the getTokens task raises an error', async function () { | ||
const expectedError = new Error('Token already managed'); | ||
const getTokens = sinon.spy(() => { | ||
throw expectedError; | ||
}); | ||
const stubContract = { | ||
getTokens, | ||
} as unknown as Collector; | ||
sinon.stub(ethers, 'getContractAt').resolves(stubContract); | ||
await expect( | ||
getCollectorTokens(taskArgs, hre), | ||
'getCollectorTokens did not reject' | ||
).to.be.rejectedWith(expectedError); | ||
expect(consoleLogSpy.called, 'Console.log was called').to.be.false; | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.