Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/contracts/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Chain } from '../chains/constants';
import { UnifiToken } from './tokens';

// Source of truth:
// https://github.com/PufferFinance/Deployments-and-ACL/tree/main/docs/deployments
Expand All @@ -12,10 +13,7 @@ export const CONTRACT_ADDRESSES = {
// L2RewardManager is not deployed to mainnet but base.
L2RewardManager: '0x0000000000000000000000000000000000000000',
PufferWithdrawalManager: '0xDdA0483184E75a5579ef9635ED14BacCf9d50283',
NucleusBoringVault: '0x196ead472583bc1e9af7a05f860d9857e1bd3dcc',
NucleusAccountant: '0xa9fb7e2922216debe3fd5e1bbe7591ee446dc21c',
NucleusTeller: '0x08eb2eccdf6ebd7aba601791f23ec5b5f68a1d53',
AtomicQueue: '0xc7287780bfa0C5D2dD74e3e51E238B1cd9B221ee',
NucleusAtomicQueue: '0xc7287780bfa0C5D2dD74e3e51E238B1cd9B221ee',
},
[Chain.Holesky]: {
PufferVault: '0x9196830bB4c05504E0A8475A0aD566AceEB6BeC9',
Expand All @@ -24,13 +22,33 @@ export const CONTRACT_ADDRESSES = {
PufLocker: '0xa58983ad0899a452b7420bc57228e329d7ba92b6',
L1RewardManager: '0x10f970bcb84B82B82a65eBCbF45F26dD26D69F12',
L2RewardManager: '0x58C046794f69A8830b0BE737022a45b4acd01dE5',
// Nucleus contracts are not deployed to holesky.
PufferWithdrawalManager: '0x5A3E1069B66800c0ecbc91bd81b1AE4D1804DBc4',
NucleusBoringVault: '0x0000000000000000000000000000000000000000',
NucleusAccountant: '0x0000000000000000000000000000000000000000',
NucleusTeller: '0x0000000000000000000000000000000000000000',
},
[Chain.Base]: {
L2RewardManager: '0xF9Dd335bF363b2E4ecFe3c94A86EBD7Dd3Dcf0e7',
},
};

export const NUCLEUS_CONTRACT_ADDRESSES = {
[UnifiToken.unifiETH]: {
[Chain.Mainnet]: {
NucleusBoringVault: '0x196ead472583bc1e9af7a05f860d9857e1bd3dcc',
NucleusAccountant: '0xa9fb7e2922216debe3fd5e1bbe7591ee446dc21c',
NucleusTeller: '0x08eb2eccdf6ebd7aba601791f23ec5b5f68a1d53',
},
},
[UnifiToken.unifiUSD]: {
[Chain.Mainnet]: {
NucleusBoringVault: '0x82c40e07277eBb92935f79cE92268F80dDc7caB4',
NucleusAccountant: '0xe0bDb7b9225A2CeB42998dc2E51D4D3CDeb7e3Be',
NucleusTeller: '0x5d3Fb47FE7f3F4Ce8fe55518f7E4F7D6061B54DD',
},
},
[UnifiToken.unifiBTC]: {
[Chain.Mainnet]: {
NucleusBoringVault: '0x170d847a8320f3b6a77ee15b0cae430e3ec933a0',
NucleusAccountant: '0x2afb28b0561d99b5e00829ec2ef54946a00a35f7',
NucleusTeller: '0x0743647a607822781f9d0a639454e76289182f0b',
},
},
};
20 changes: 20 additions & 0 deletions lib/contracts/handlers/nucleus-accountant-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { NucleusAccountantHandler } from './nucleus-accountant-handler';
import { mockAccount, testingUtils } from '../../../test/setup-tests';
import { generateAddress } from '../../../test/mocks/address';
import { Address } from 'viem';
import { UnifiToken } from '../tokens';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';

describe('NucleusAccountantHandler', () => {
const contractTestingUtils = testingUtils.generateContractUtils(
Expand Down Expand Up @@ -72,4 +74,22 @@ describe('NucleusAccountantHandler', () => {
const rateInQuoteSafe = await handler.getRateInQuoteSafe(tokenAddress);
expect(rateInQuoteSafe).toEqual(mockRate);
});

it('should use the given token to call contract functions', async () => {
const unifiBTCAccountant = handler
.withToken(UnifiToken.unifiBTC)
.getContract();
expect(unifiBTCAccountant.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiBTC][Chain.Mainnet]
.NucleusAccountant,
);

const unifiUSDAcountant = handler
.withToken(UnifiToken.unifiUSD)
.getContract();
expect(unifiUSDAcountant.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiUSD][Chain.Mainnet]
.NucleusAccountant,
);
});
});
24 changes: 21 additions & 3 deletions lib/contracts/handlers/nucleus-accountant-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import {
GetContractReturnType,
} from 'viem';
import { Chain } from '../../chains/constants';
import { CONTRACT_ADDRESSES } from '../addresses';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';
import { NUCLEUS_ACCOUNTANT_ABIS } from '../abis/nucleus-accountant-abis';
import { UnifiToken } from '../tokens';

/**
* Handler for the `Accountant` contract from nucleus.
*/
export class NucleusAccountantHandler {
private token: UnifiToken;

/**
* Create the handler for processing tokens.
*
Expand All @@ -28,7 +31,21 @@ export class NucleusAccountantHandler {
private chain: Chain,
private walletClient: WalletClient,
private publicClient: PublicClient,
) {}
) {
this.token = UnifiToken.unifiETH;
}

/**
* Set the UniFi token to use for executing transactions on the
* contract.
*
* @param token UniFi token to use for the handler.
* @returns The handler.
*/
public withToken(token: UnifiToken) {
this.token = token;
return this;
}

/**
* Get the contract. This is a method because the typings are complex
Expand All @@ -37,7 +54,8 @@ export class NucleusAccountantHandler {
* @returns The viem contract.
*/
public getContract() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility for runtime error in case NUCLEUS_CONTRACT_ADDRESSES doesn't exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if the contract doesn't exist for the selected chain. We can put checks in place and throw more sophisticated errors for all the handlers but that for now can be shelved I think as it's working fine at the moment.

const address = CONTRACT_ADDRESSES[this.chain].NucleusAccountant as Address;
const address = NUCLEUS_CONTRACT_ADDRESSES[this.token][this.chain]
.NucleusAccountant as Address;
const abi = NUCLEUS_ACCOUNTANT_ABIS[this.chain].Accountant;
const client = { public: this.publicClient, wallet: this.walletClient };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Account, isHash, PublicClient, WalletClient } from 'viem';
import {
setupTestPublicClient,
setupTestWalletClient,
Expand All @@ -7,7 +8,6 @@ import { mockAccount, testingUtils } from '../../../test/setup-tests';
import { NUCLEUS_ATOMIC_QUEUE_ABIS } from '../abis/nucleus-atomic-queue-abis';
import { NucleusAtomicQueueHandler } from './nucleus-atomic-queue-handler';
import { TOKENS_ADDRESSES, UnifiToken, Token } from '../tokens';
import { Account, isHash, PublicClient, WalletClient } from 'viem';

describe('NucleusAtomicQueueHandler', () => {
const contractTestingUtils = testingUtils.generateContractUtils(
Expand Down
3 changes: 2 additions & 1 deletion lib/contracts/handlers/nucleus-atomic-queue-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class NucleusAtomicQueueHandler {
}

public getContract() {
const address = CONTRACT_ADDRESSES[this.chain].AtomicQueue as Address;
const address = CONTRACT_ADDRESSES[this.chain]
.NucleusAtomicQueue as Address;
const abi = NUCLEUS_ATOMIC_QUEUE_ABIS[this.chain].AtomicQueue;
const client = { public: this.publicClient, wallet: this.walletClient };

Expand Down
24 changes: 22 additions & 2 deletions lib/contracts/handlers/nucleus-boring-vault-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isHash } from 'viem';
import {
setupTestPublicClient,
setupTestWalletClient,
Expand All @@ -7,7 +8,8 @@ import { mockAccount, testingUtils } from '../../../test/setup-tests';
import { NucleusBoringVaultHandler } from './nucleus-boring-vault-handler';
import { NUCLEUS_BORING_VAULT_ABIS } from '../abis/nucleus-boring-vault-abis';
import { generateAddress } from '../../../test/mocks/address';
import { isHash } from 'viem';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';
import { UnifiToken } from '../tokens';

describe('NucleusBoringVaultHandler', () => {
const contractTestingUtils = testingUtils.generateContractUtils(
Expand Down Expand Up @@ -42,7 +44,7 @@ describe('NucleusBoringVaultHandler', () => {
const mockTotalSupply = 100n;
contractTestingUtils.mockCall('totalSupply', [mockTotalSupply]);

const totalSupply = await handler.getTotalSupply();
const totalSupply = await handler.totalSupply();
expect(totalSupply).toEqual(mockTotalSupply);
});

Expand Down Expand Up @@ -105,4 +107,22 @@ describe('NucleusBoringVaultHandler', () => {
expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});

it('should use the given token to call contract functions', async () => {
const unifiBTCBoringVault = handler
.withToken(UnifiToken.unifiBTC)
.getContract();
expect(unifiBTCBoringVault.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiBTC][Chain.Mainnet]
.NucleusBoringVault,
);

const unifiUSDBoringVault = handler
.withToken(UnifiToken.unifiUSD)
.getContract();
expect(unifiUSDBoringVault.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiUSD][Chain.Mainnet]
.NucleusBoringVault,
);
});
});
28 changes: 17 additions & 11 deletions lib/contracts/handlers/nucleus-boring-vault-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { NUCLEUS_BORING_VAULT_ABIS } from '../abis/nucleus-boring-vault-abis';
import { CONTRACT_ADDRESSES } from '../addresses';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';
import { UnifiToken } from '../tokens';

export type PermitParams = {
account: Address;
Expand All @@ -28,6 +29,7 @@ export type PermitParams = {
*/
export class NucleusBoringVaultHandler {
private viemChain: ViemChain;
private token: UnifiToken;

/**
* Create the handler for processing UniFi tokens.
Expand All @@ -44,6 +46,19 @@ export class NucleusBoringVaultHandler {
private publicClient: PublicClient,
) {
this.viemChain = VIEM_CHAINS[chain];
this.token = UnifiToken.unifiETH;
}

/**
* Set the UniFi token to use for executing transactions on the
* contract.
*
* @param token UniFi token to use for the handler.
* @returns The handler.
*/
public withToken(token: UnifiToken) {
this.token = token;
return this;
}

/**
Expand All @@ -53,7 +68,7 @@ export class NucleusBoringVaultHandler {
* @returns The viem contract.
*/
public getContract() {
const address = CONTRACT_ADDRESSES[this.chain]
const address = NUCLEUS_CONTRACT_ADDRESSES[this.token][this.chain]
.NucleusBoringVault as Address;
const abi = NUCLEUS_BORING_VAULT_ABIS[this.chain].BoringVault;
const client = { public: this.publicClient, wallet: this.walletClient };
Expand All @@ -76,15 +91,6 @@ export class NucleusBoringVaultHandler {
return this.getContract().read.allowance([owner, spender]);
}

/**
* Get the total supply of the token.
*
* @returns Total supply of the token.
*/
public getTotalSupply() {
return this.getContract().read.totalSupply();
}

/**
* Check the token balance of the wallet.
*
Expand Down
15 changes: 15 additions & 0 deletions lib/contracts/handlers/nucleus-teller-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { generateAddress } from '../../../test/mocks/address';
import { Token, TOKENS_ADDRESSES, UnifiToken } from '../tokens';
import { isHash } from 'viem';
import { mockPermitSignature } from '../../../test/mocks/permit-signature';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';

describe('NucleusBoringVaultHandler', () => {
const contractTestingUtils = testingUtils.generateContractUtils(
Expand Down Expand Up @@ -109,4 +110,18 @@ describe('NucleusBoringVaultHandler', () => {
expect(typeof (await estimate())).toBe('bigint');
expect(isHash(await transact())).toBe(true);
});

it('should use the given token to call contract functions', async () => {
const unifiBTCTeller = handler.withToken(UnifiToken.unifiBTC).getContract();
expect(unifiBTCTeller.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiBTC][Chain.Mainnet]
.NucleusTeller,
);

const unifiUSDTeller = handler.withToken(UnifiToken.unifiUSD).getContract();
expect(unifiUSDTeller.address).toEqual(
NUCLEUS_CONTRACT_ADDRESSES[UnifiToken.unifiUSD][Chain.Mainnet]
.NucleusTeller,
);
});
});
19 changes: 17 additions & 2 deletions lib/contracts/handlers/nucleus-teller-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GetContractReturnType,
} from 'viem';
import { Chain, VIEM_CHAINS, ViemChain } from '../../chains/constants';
import { CONTRACT_ADDRESSES } from '../addresses';
import { NUCLEUS_CONTRACT_ADDRESSES } from '../addresses';
import { NUCLEUS_TELLER_ABIS } from '../abis/nucleus-teller-abis';
import { Token, TOKENS_ADDRESSES, UnifiToken } from '../tokens';
import { ERC20PermitHandler } from './erc20-permit-handler';
Expand Down Expand Up @@ -37,6 +37,7 @@ export type DepositWithPermitParams = {
export class NucleusTellerHandler {
private viemChain: ViemChain;
private erc20PermitHandler: ERC20PermitHandler;
private token: UnifiToken;

/**
* Create the handler for processing tokens.
Expand All @@ -58,6 +59,19 @@ export class NucleusTellerHandler {
walletClient,
publicClient,
);
this.token = UnifiToken.unifiETH;
}

/**
* Set the UniFi token to use for executing transactions on the
* contract.
*
* @param token UniFi token to use for the handler.
* @returns The handler.
*/
public withToken(token: UnifiToken) {
this.token = token;
return this;
}

/**
Expand All @@ -67,7 +81,8 @@ export class NucleusTellerHandler {
* @returns The viem contract.
*/
public getContract() {
const address = CONTRACT_ADDRESSES[this.chain].NucleusTeller as Address;
const address = NUCLEUS_CONTRACT_ADDRESSES[this.token][this.chain]
.NucleusTeller as Address;
const abi = NUCLEUS_TELLER_ABIS[this.chain].Teller;
const client = { public: this.publicClient, wallet: this.walletClient };

Expand Down
Loading
Loading