Skip to content

Commit

Permalink
chore: implement batching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
njokuScript committed Feb 6, 2024
1 parent 5daf9f4 commit 494c1ec
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
12 changes: 6 additions & 6 deletions src/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const approveUSDC = async (
const owner = await signer.getAddress();

// build transaction object
const to: any = getContractAddress('USDC')[chainId!];
const to: any = getContractAddress('USDC', chainId);
let iface = internal.getInterface(USDC__factory.abi);
const data = iface.encodeFunctionData('approve', [spender, amount]);

Expand All @@ -62,8 +62,8 @@ const waitTime = (seconds: number) => new Promise((resolve) => setTimeout(resolv
// const updateTestPrice = async (signer: Signer) => {
// const chainId = (await signer?.provider?.getNetwork())?.chainId.toString();

// const collateralAddress: any = getContractAddress('USDC')[chainId!];
// const feedContractAddress: any = getContractAddress('Feed')[chainId!];
// const collateralAddress: any = getContractAddress('USDC', chainId);
// const feedContractAddress: any = getContractAddress('Feed', chainId);
// const contract = new ContractManager(signer);

// await (await contract.getVaultContract()).updateFeedContract(feedContractAddress);
Expand All @@ -81,7 +81,7 @@ const waitTime = (seconds: number) => new Promise((resolve) => setTimeout(resolv
// const setMinterRole = async (signer: Signer, owner: string) => {
// const chainId = (await signer?.provider?.getNetwork())?.chainId.toString();

// const vaultContractAddress: any = getContractAddress('Vault')[chainId!];
// const vaultContractAddress: any = getContractAddress('Vault', chainId);
// const contract = new ContractManager(signer);

// await (await contract.getCurrencyContract()).setMinterRole(vaultContractAddress);
Expand All @@ -92,7 +92,7 @@ const waitTime = (seconds: number) => new Promise((resolve) => setTimeout(resolv
const getxNGNBalance = async (owner: any, signer?: Signer) => {
const chainId = (await signer?.provider?.getNetwork())?.chainId.toString();

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

const currencyContract = Contract(currencyContractAddress, Currency__factory.abi, signer);
const balance = await currencyContract.balanceOf(owner);
Expand All @@ -115,7 +115,7 @@ const approvexNGN = async (
const _amount = BigInt(amount) * BigInt(10e18);

// build transaction object
const to: any = getContractAddress('Currency')[chainId!];
const to: any = getContractAddress('Currency', chainId);
let iface = internal.getInterface(Currency__factory.abi);
const data = iface.encodeFunctionData('approve', [spender, _amount]);

Expand Down
45 changes: 34 additions & 11 deletions src/services/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
parseEther,
formatEther,
Signer,
AbiCoder,
} from 'ethers';
import { ICollateral, IContract } from '../types';
import { Transaction } from '../libs/transactions';
Expand All @@ -27,10 +28,19 @@ export enum VaultHealthFactor {
SAFE = 'SAFE',
}
export enum VaultOperations {
DepositCollateral = 0,
WithdrawCollateral = 1,
MintCurrency = 2,
BurnCurrency = 3,
Invalid = 0,
// Vault operations
DepositCollateral = 1,
WithdrawCollateral = 2,
MintCurrency = 3,
BurnCurrency = 4,
// Permit2 operations
Permit2_PermitTransferFrom = 5,
Permit2_Permit = 5,
Permit2_TransferFrom = 6,
/// ERC20 Operations
ERC20_Permit = 7,
ERC20_TransferFrom = 8,
}
const setupVault = async (
owner: string,
Expand Down Expand Up @@ -64,21 +74,34 @@ 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 data = iface.encodeFunctionData('multiInteract', [
[vaultContractAddress],
[VaultOperations.DepositCollateral],
[collateralAddress],
[ethers.ZeroAddress],
[_amount],
]);

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 data = iface.encodeFunctionData('multiInteract', [packedOperations, encodedParameters]);

const txConfig = await internal.getTransactionConfig({
from: owner,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/descent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { config } from 'dotenv';
import Descent from '../../dist/index.es';
import type { IDescentClass } from '../../dist/types/types';
import type { IDescentClass } from '../../dist/types';

config();

describe('Descent Protocol SDK Test', () => {
let descent: any;
let descent: IDescentClass;
let owner = '0x459D7FB72ac3dFB0666227B30F25A424A5583E9c';
let rpcUrl = 'https://goerli.base.org';

Expand Down
2 changes: 1 addition & 1 deletion test/utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config();
describe('Descent Protocol Utility Test', () => {
let descent: DescentClass;
let owner = '0x459D7FB72ac3dFB0666227B30F25A424A5583E9c';
let rpcUrl = 'https://goerli.base.org';
let rpcUrl = 'https://sepolia.base.org';

beforeAll(async () => {
descent = await Descent.create('https', {
Expand Down
36 changes: 18 additions & 18 deletions test/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Descent Protocol SDK Test', () => {
let descent: DescentClass;
let owner = '0x459D7FB72ac3dFB0666227B30F25A424A5583E9c';
let vault = '0xE2386C5eF4deC9d5815C60168e36c7153ba00D0C';
let rpcUrl = 'https://goerli.base.org';
let rpcUrl = 'https://sepolia.base.org';

let signer: Signer;

Expand All @@ -26,9 +26,9 @@ describe('Descent Protocol SDK Test', () => {

signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);

const response = await descent.setupVault();
// const response = await descent.setupVault();

console.log('Vault setup');
// console.log(response, 'Vault setup');
}, 120000);

it('should deposit usdc into a vault', async () => {
Expand All @@ -44,23 +44,23 @@ 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 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');
// 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 () => {
const response = await descent.repayCurrency('9000');
// it('should payback xNGN', async () => {
// const response = await descent.repayCurrency('9000');

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

0 comments on commit 494c1ec

Please sign in to comment.