Skip to content

Commit

Permalink
chore: call vault contracts directly
Browse files Browse the repository at this point in the history
  • Loading branch information
njokuScript committed Feb 11, 2024
1 parent 494c1ec commit 8fcce56
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 72 deletions.
8 changes: 4 additions & 4 deletions src/contracts/getContractAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContractName, SupportedNetwork } from './types';
const addresses: Record<ContractName, Partial<Record<SupportedNetwork | string, string>>> = {
Vault: {
[SupportedNetwork.BASE_GOERLI]: '0xE2386C5eF4deC9d5815C60168e36c7153ba00D0C',
[SupportedNetwork.BASE_SEPOLIA]: '0x18196CCaA8C2844c82B40a8bDCa27349C7466280',
[SupportedNetwork.BASE_SEPOLIA]: '0x3d35807343CbF4fDb16E42297F2214f62848D032',
},
MultiStaticcall: {
[SupportedNetwork.BASE_GOERLI]: '0x5226c32C94acdd44743FC5c376582d6782FA7592',
Expand All @@ -19,18 +19,18 @@ const addresses: Record<ContractName, Partial<Record<SupportedNetwork | string,
},
Currency: {
[SupportedNetwork.BASE_GOERLI]: '0xee2bDAE7896910c49BeA25106B9f8e9f4B671c82',
[SupportedNetwork.BASE_SEPOLIA]: '0x5d0583Ef20884C0b175046d515Ec227200C12C89',
[SupportedNetwork.BASE_SEPOLIA]: '0xB8747e5cce01AA5a51021989BA11aE33097db485',
},
Feed: {
[SupportedNetwork.BASE_GOERLI]: '0x970066EE55DF2134D1b52451afb49034AE5Fa29a',
[SupportedNetwork.BASE_SEPOLIA]: '0x94D80B2EA3cda86bF350DD7860e1171701F284c8',
[SupportedNetwork.BASE_SEPOLIA]: '0xFBD26B871D55ba56B7a780eF1fF243Db7A3E81f4',
},
USDC: {
[SupportedNetwork.BASE_GOERLI]: '0xF175520C52418dfE19C8098071a252da48Cd1C19',
[SupportedNetwork.BASE_SEPOLIA]: '0x036CbD53842c5426634e7929541eC2318f3dCF7e',
},
Rate: {
[SupportedNetwork.BASE_SEPOLIA]: '0x774843f6Baa4AAE62F026a8aF3c1C6FF3e55Ca39',
[SupportedNetwork.BASE_SEPOLIA]: '0x00A0BcB0e2099f4a0564c26e24eBfA866D3235D6',
},
};

Expand Down
73 changes: 23 additions & 50 deletions src/services/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,14 @@ const collateralizeVault = async (
transaction: Transaction,
internal: Internal,
) => {
const abiCoder = AbiCoder.defaultAbiCoder();
const collateralAddress: any = getContractAddress(collateral, chainId);
const vaultContractAddress: any = getContractAddress('Vault', chainId);

const _amount = BigInt(amount) * BigInt(1e6);

// encode
// build transaction object
const to: any = getContractAddress('VaultRouter', chainId);
let iface = internal.getInterface(VaultRouter__factory.abi);

const packedOperations = ethers.solidityPacked(
['uint8', 'uint8'],
[VaultOperations.ERC20_TransferFrom, VaultOperations.DepositCollateral],
);
let encodedParameters = [];

encodedParameters.push(
abiCoder.encode(
['address', 'address', 'uint256'],
[vaultContractAddress, collateralAddress, _amount],
),
);
encodedParameters.push(abiCoder.encode(['address', 'uint256'], [collateralAddress, _amount]));

console.log(encodedParameters, 'encoded parameters');
const to: any = getContractAddress('Vault', chainId);
let iface = internal.getInterface(Vault__factory.abi);

const data = iface.encodeFunctionData('multiInteract', [packedOperations, encodedParameters]);
const data = iface.encodeFunctionData('depositCollateral', [collateralAddress, owner, _amount]);

const txConfig = await internal.getTransactionConfig({
from: owner,
Expand Down Expand Up @@ -130,8 +110,8 @@ const withdrawCollateral = async (

const _amount = BigInt(amount) * BigInt(1e6);

const to: any = getContractAddress('VaultRouter', chainId);
let iface = internal.getInterface(VaultRouter__factory.abi);
const to: any = getContractAddress('Vault', chainId);
let iface = internal.getInterface(Vault__factory.abi);

const maxWithdrawable = await vaultGetterContract.getMaxWithdrawable(
vaultContractAddress,
Expand All @@ -144,14 +124,14 @@ const withdrawCollateral = async (
if (Number(amount) > Number(formattedMaxWithdrawable.toString())) {
throw new Error(' Withdrawal amount is more than available collateral balance');
}
const recepientAddress = owner;

// build transaction object
const data = iface.encodeFunctionData('multiInteract', [
[vaultContractAddress],
[VaultOperations.WithdrawCollateral],
[collateralAddress],
[owner],
[_amount],
const data = iface.encodeFunctionData('withdrawCollateral', [
collateralAddress,
owner,
recepientAddress,
_amount,
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
Expand Down Expand Up @@ -192,15 +172,16 @@ const mintCurrency = async (
throw new Error(' Borrow amount is more than available currency borrowable');
}

const recepientAddress = owner;

// build transaction object
const to: any = getContractAddress('VaultRouter', chainId);
let iface = internal.getInterface(VaultRouter__factory.abi);
const data = iface.encodeFunctionData('multiInteract', [
[vaultContractAddress],
[VaultOperations.MintCurrency],
[collateralAddress],
[owner],
[_amount],
const to: any = getContractAddress('Vault', chainId);
let iface = internal.getInterface(Vault__factory.abi);
const data = iface.encodeFunctionData('mintCurrency', [
collateralAddress,
owner,
recepientAddress,
_amount,
]);
const txConfig = await internal.getTransactionConfig({
from: owner,
Expand All @@ -223,7 +204,6 @@ const burnCurrency = async (
signer: Signer,
) => {
const collateralAddress: any = getContractAddress(collateral, chainId);
const vaultContractAddress: any = getContractAddress('Vault', chainId);

const currencyContractAddress: any = getContractAddress('Currency', chainId);

Expand All @@ -238,17 +218,10 @@ const burnCurrency = async (
if (Number(amount) > Number(formattedBalance.toString())) {
throw new Error('Payback xNGN: Insufficient funds');
}

// build transaction object
const to: any = getContractAddress('VaultRouter', chainId);
let iface = internal.getInterface(VaultRouter__factory.abi);
const data = iface.encodeFunctionData('multiInteract', [
[vaultContractAddress],
[VaultOperations.BurnCurrency],
[collateralAddress],
[owner],
[_amount],
]);
const to: any = getContractAddress('Vault', chainId);
let iface = internal.getInterface(Vault__factory.abi);
const data = iface.encodeFunctionData('burnCurrency', [collateralAddress, owner, _amount]);
const txConfig = await internal.getTransactionConfig({
from: owner,
to,
Expand Down
42 changes: 24 additions & 18 deletions test/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ config();
describe('Descent Protocol SDK Test', () => {
let descent: DescentClass;
let owner = '0x459D7FB72ac3dFB0666227B30F25A424A5583E9c';
let vault = '0xE2386C5eF4deC9d5815C60168e36c7153ba00D0C';
let vault = '0x3d35807343CbF4fDb16E42297F2214f62848D032';
let rpcUrl = 'https://sepolia.base.org';

let signer: Signer;
Expand Down Expand Up @@ -44,23 +44,29 @@ describe('Descent Protocol SDK Test', () => {
expect(response).not.toBeNull;
}, 500000);

// it('should withdraw usdc from a vault', async () => {
// const response = await descent.withdrawCollateral('50');
// await waitTime(60);
// expect(response).not.toBeNull;
// }, 200000);

// it('should mint xNGN from a vault to an address', async () => {
// const response = await descent.borrowCurrency('10000');

// await waitTime(60);
// expect(response).not.toBeNull;
// }, 200000);
it('should withdraw usdc from a vault', async () => {
const response = await descent.withdrawCollateral('50');
await waitTime(60);
expect(response).not.toBeNull;
}, 200000);

// it('should payback xNGN', async () => {
// const response = await descent.repayCurrency('9000');
it('should mint xNGN from a vault to an address', async () => {
const response = await descent.borrowCurrency('10000');

// await waitTime(60);
// expect(response).not.toBeNull;
// }, 200000);
await waitTime(60);
expect(response).not.toBeNull;
}, 200000);

it('should payback xNGN', async () => {
try {
await approvexNGN(vault, '30000', signer, descent.transaction, descent.internal);
const response = await descent.repayCurrency('30000');
console.log(response, 'res');

await waitTime(60);
expect(response).not.toBeNull;
} catch (err) {
console.log(err?.info, 'error');
}
}, 200000);
});

0 comments on commit 8fcce56

Please sign in to comment.