Skip to content

Commit

Permalink
marge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinsoza committed Nov 6, 2024
2 parents 2788001 + 003c8e0 commit 32e9b44
Show file tree
Hide file tree
Showing 54 changed files with 1,566 additions and 695 deletions.
2 changes: 1 addition & 1 deletion apps/contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["strategies/*", "defindex", "factory"]
members = ["strategies/*", "vault", "factory"]
exclude = [
"strategies/external_wasms",
]
Expand Down
2 changes: 1 addition & 1 deletion apps/contracts/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUBDIRS = strategies defindex factory
SUBDIRS = strategies vault factory

default: build

Expand Down
2 changes: 1 addition & 1 deletion apps/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Make sure you have the hodl strategy deployed, if not, you can run:
yarn deploy-hodl <network>
```

to test the factory to deploy a DeFindex Vault
to test the factory to deploy a DeFindex Vault, and deposit there two times, you can run:

```bash
yarn test <network>
Expand Down
2 changes: 0 additions & 2 deletions apps/contracts/defindex/src/constants.rs

This file was deleted.

43 changes: 0 additions & 43 deletions apps/contracts/defindex/src/investment.rs

This file was deleted.

116 changes: 0 additions & 116 deletions apps/contracts/defindex/src/test/withdraw.rs

This file was deleted.

3 changes: 2 additions & 1 deletion apps/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"deploy-hodl": "tsc && node dist/deploy_hodl.js",
"publish-addresses": "tsc && node dist/publish_addresses.js",
"test": "tsc && node dist/test.js",
"test-vault": "tsc && node dist/tests/testOnlyVault.js"
"test-vault": "tsc && node dist/tests/testOnlyVault.js",
"test-dev": "tsc && node dist/tests/dev.js"
},
"type": "module",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion apps/contracts/src/deploy_hodl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function deployContracts(addressBook: AddressBook) {

const emptyVecScVal = xdr.ScVal.scvVec([]);

console.log("Initializing Soroswap Adapter");
console.log("Initializing DeFindex HODL Strategy");
await invokeContract(
"hodl_strategy",
addressBook,
Expand Down
116 changes: 112 additions & 4 deletions apps/contracts/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
nativeToScVal,
Networks,
scValToNative,
xdr
xdr,
Keypair
} from "@stellar/stellar-sdk";
import { randomBytes } from "crypto";
import { depositToVault } from "./tests/vault.js";
import { depositToVault, withdrawFromVault } from "./tests/vault.js";
import { AddressBook } from "./utils/address_book.js";
import { airdropAccount, invokeContract } from "./utils/contract.js";
import { config } from "./utils/env_config.js";
import { checkUserBalance, depositToStrategy, withdrawFromStrategy} from "./tests/strategy.js";


export async function test_factory(addressBook: AddressBook) {
Expand All @@ -26,6 +28,7 @@ export async function test_factory(addressBook: AddressBook) {
console.log("Testing Create DeFindex on Factory");
console.log("-------------------------------------------------------");

console.log("Setting Emergengy Manager, Fee Receiver and Manager accounts");
const emergencyManager = loadedConfig.getUser("DEFINDEX_EMERGENCY_MANAGER_SECRET_KEY");
if (network !== "mainnet") await airdropAccount(emergencyManager);

Expand Down Expand Up @@ -108,9 +111,114 @@ const passphrase = network === "mainnet" ? Networks.PUBLIC : network === "testne

const loadedConfig = config(network);

// Deposit directly on Strategy

const myUser = Keypair.random();
if (network !== "mainnet") await airdropAccount(myUser);
console.log("New user publicKey:", myUser.publicKey());
const{
user: strategyUser,
balanceBefore: balanceBeforeStrategyDeposit,
result: strategyDepositResult,
balanceAfter: balanceAfterStrategyDeposit}
= await depositToStrategy(addressBook.getContractId("hodl_strategy"), myUser, 1234567890);


console.log(" -- ")
console.log(" -- ")
console.log("Step 0: Deposited directly in Strategy, with balance before:", balanceBeforeStrategyDeposit, "and balance after:", balanceAfterStrategyDeposit);

console.log(" -- ")
console.log(" -- ")

// Withdraw directly from strategy
const {
user: strategyWithdrawUser,
balanceBefore: balanceBeforeStrategyWithdraw,
result: strategyWithdrawResult,
balanceAfter: balanceAfterStrategyWithdraw
} = await withdrawFromStrategy(addressBook.getContractId("hodl_strategy"), myUser, 567890);

console.log(" -- ")
console.log(" -- ")
console.log("Step 1: Withdrawn directly from Strategy, with balance before:", balanceBeforeStrategyWithdraw, "and balance after:", balanceAfterStrategyWithdraw);
console.log(" -- ")
console.log(" -- ")


// Step 0: Deploy the vault
const deployedVault = await test_factory(addressBook);
await depositToVault(deployedVault);
await depositToVault(deployedVault);
console.log(" -- ")
console.log(" -- ")
console.log("Step 0: Deployed Vault:", deployedVault);
console.log(" -- ")
console.log(" -- ")


// Step 1: Deposit to vault and capture initial balances
const { user, balanceBefore: depositBalanceBefore, result: depositResult, balanceAfter: depositBalanceAfter }
= await depositToVault(deployedVault, 987654321);
console.log(" -- ")
console.log(" -- ")
console.log("Step 1: Deposited to Vault using user:", user.publicKey(), "with balance before:", depositBalanceBefore, "and balance after:", depositBalanceAfter);
console.log(" -- ")
console.log(" -- ")

// Step 2: Check strategy balance after deposit
const strategyBalanceAfterDeposit = await checkUserBalance(addressBook.getContractId("hodl_strategy"), user.publicKey(), user);
console.log(" -- ")
console.log(" -- ")
console.log("Step 2: Strategy balance after deposit:", strategyBalanceAfterDeposit);
console.log(" -- ")
console.log(" -- ")

// check vault balance of XLM after deposit
let xlmContractId: string;
switch (network) {
case "testnet":
xlmContractId = xlm.contractId(Networks.TESTNET);
break;
case "mainnet":
xlmContractId = xlm.contractId(Networks.PUBLIC);
break;
default:
console.log("Invalid network:", network, "It should be either testnet or mainnet");
throw new Error("Invalid network");
}
const xlmAddress = new Address(xlmContractId);
const vaultBalanceAfterDeposit = await checkUserBalance(xlmContractId, deployedVault, user);
console.log(" -- ")
console.log(" -- ")
console.log("Step 2: Vault balance after deposit:", vaultBalanceAfterDeposit);
console.log(" -- ")
console.log(" -- ")

// Step 3: Withdraw from the vault
const { balanceBefore: withdrawBalanceBefore, result: withdrawResult, balanceAfter: withdrawBalanceAfter }
= await withdrawFromVault(deployedVault, 4321, user);

// Step 4: Check strategy balance after withdrawal
const strategyBalanceAfterWithdraw = await checkUserBalance(addressBook.getContractId("hodl_strategy"), user.publicKey(), user);

// Log a table with all balances
console.table([
{
Operation: "Deposit",
"Balance Before": depositBalanceBefore,
"Deposit Result": depositResult,
"Balance After": depositBalanceAfter,
"Strategy Balance After": strategyBalanceAfterDeposit
},
{
Operation: "Withdraw",
"Balance Before": withdrawBalanceBefore,
"Withdraw Result": withdrawResult,
"Balance After": withdrawBalanceAfter,
"Strategy Balance After": strategyBalanceAfterWithdraw
}
]);

await depositToVault(deployedVault, 98765421);

// await getDfTokenBalance("CCL54UEU2IGTCMIJOYXELIMVA46CLT3N5OG35XN45APXDZYHYLABF53N", "GDAMXOJUSW6O67UVI6U4LBHI5IIJFUKQVDHPKNFKOIYRLYB2LA6YDAFI", loadedConfig.admin)
// await depositToVault("CCIOE3BLPYOYDFB5KALLDXED2CZT3GJDZSHY453U4TTOIRZLAKMKZPLR");
Expand Down
15 changes: 15 additions & 0 deletions apps/contracts/src/tests/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { airdropAccount } from "../utils/contract.js";
import { withdrawFromVault } from "./vault.js";
import { Keypair } from "@stellar/stellar-sdk";

const user = Keypair.fromSecret("SA77N6PLHDFRYDNYE3YJQBPTRNODMVYP5WWF2SG42DXB52GW2FWOG2B3")
const contract = "CCNWF3D7FJCZKYCAD6FAO3JNPRHG6SVXHO5YTFDZRXSPOJXL6TIBWP3Y"
console.log("🚀 ~ file: dev.ts ~ line 6 ~ user", user.publicKey())
const withdrawResult =
await withdrawFromVault(contract, 10000, user)
// await withdrawFromVault(contract, BigInt(10000), user)

console.log('🚀 ~ withdrawResult:', withdrawResult);
// const badUser = Keypair.random()
// await airdropAccount(badUser);
// await withdrawFromVault(contract, BigInt(1000), badUser)
Loading

0 comments on commit 32e9b44

Please sign in to comment.