Skip to content

Commit

Permalink
Ledger building added, failure for deposits added
Browse files Browse the repository at this point in the history
  • Loading branch information
fulltimemike committed Aug 26, 2024
1 parent ea13edc commit 7254d1c
Show file tree
Hide file tree
Showing 11 changed files with 584 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pondo-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"stopDevnet": "tmux kill-session -t devnet",
"watchDevnet": "tmux attach-session -t devnet",
"cleanDevnet": "rm -rf .history-* .ledger-* .logs-* .current-proposal-cache-*",
"stopDevnetAtHeight": "./utils/stop-devnet-at-height.sh"
"stopDevnetAtHeight": "./utils/stop-devnet-at-height.sh",
"buildLedger": "node ./dist/buildLedger.js"
},
"repository": {
"type": "git",
Expand Down
18 changes: 18 additions & 0 deletions pondo-bot/src/aleo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,24 @@ export const airDropCredits = async (publicKey: string, amount: bigint) => {
);
}

export const transactionAcceptedBlockHeight = async (transactionResult: any, retriesRemaining: number = 25): Promise<number> => {
if (retriesRemaining <= 0) {
return -1;
}

const transaction = JSON.parse(transactionResult.transaction);
const transactionId = transaction.id;
console.log(`Checking block height for accepted or rejected with id ${transactionId}`);
const client = getClient();
try {
const foundTransaction = await client.request('transaction', { id: transactionId });
return foundTransaction.block_id;
} catch (e: any) {
await delay(2_000);
return await transactionAcceptedBlockHeight(transactionResult, retriesRemaining - 1);
}
}

export const isTransactionAccepted = async (transactionResult: any, retriesRemaining: number = 25): Promise<boolean> => {
if (retriesRemaining <= 0) {
return false;
Expand Down
29 changes: 21 additions & 8 deletions pondo-bot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getMTSPBalance, isTransactionAccepted } from './aleo/client';
import { deployAllProgramsIfNecessary } from './aleo/deploy';
import {
NETWORK,
Expand All @@ -8,14 +9,17 @@ import {
PRIVATE_KEY,
TEST,
RPC_URL,
BOT_DELAY
BOT_DELAY,
PALEO_TOKEN_ID
} from './constants';
import { initializeProgramsIfNecessary } from './protocol/initializePrograms';
import {
approveReferenceDelegatorsIfNecessary,
deployReferenceDelegatorsIfNecessary,
} from './protocol/referenceDelegators';
import { runOracleProtocol, runProtocol } from './protocol/runProtocol';
import { depositAsSigner } from './protocol/userActions';
import { distributeDeposits } from './protocol/validatorActions';
import { fundTestAccountsIfNecessary, runTests } from './tests/runTests';
import { delay } from './util';

Expand All @@ -27,20 +31,20 @@ async function main() {
// Initialize all programs if necessary
await initializeProgramsIfNecessary();
// Deploy reference delegators if necessary
await deployReferenceDelegatorsIfNecessary();
// await deployReferenceDelegatorsIfNecessary();

console.log('All programs have been deployed and initialized');
console.log('****************** All programs have been deployed and initialized ******************');

if (TEST) {
await fundTestAccountsIfNecessary();
}

while (true) {
try {
if (MULTI_SIG_PRIVATE_KEY_0 && MULTI_SIG_PRIVATE_KEY_1 && MULTI_SIG_PRIVATE_KEY_2) {
// Approve reference delegators if necessary
await approveReferenceDelegatorsIfNecessary();
}
// if (MULTI_SIG_PRIVATE_KEY_0 && MULTI_SIG_PRIVATE_KEY_1 && MULTI_SIG_PRIVATE_KEY_2) {
// // Approve reference delegators if necessary
// await approveReferenceDelegatorsIfNecessary();
// }
// Run the protocol
if (ORACLE_ONLY) {
await runOracleProtocol();
Expand All @@ -49,7 +53,16 @@ async function main() {
}

if (TEST) {
await runTests();
// deposit sum of 125_000_000_000 microcredits
const balance = await getMTSPBalance("aleo12ux3gdauck0v60westgcpqj7v8rrcr3v346e4jtq04q7kkt22czsh808v2", PALEO_TOKEN_ID);
console.log(`Balance: ${balance}`);
if (balance < BigInt(125_000_000_000)) {
// console.log('depositing into the protocol');
// const depositTx = await depositAsSigner(BigInt(125_000_000_000), PRIVATE_KEY);
// const isComplete = await isTransactionAccepted(depositTx);
}
// await runTests();s
// await distributeDeposits();
}

await delay(BOT_DELAY);
Expand Down
2 changes: 1 addition & 1 deletion pondo-bot/src/protocol/userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export const batchedWithdraw = async (
) => {
const resolvedImports = await resolveImports(CORE_PROTOCOL_PROGRAM_IMPORTS);

await submitTransaction(
return await submitTransaction(
NETWORK!,
privateKey || TEST_USER0_PRIVATE_KEY!,
CORE_PROTOCOL_PROGRAM_CODE,
Expand Down
131 changes: 131 additions & 0 deletions pondo-bot/src/tests/cases/depositNormalBonded.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { after, before, describe, it } from 'node:test';
import { NETWORK, PALEO_TOKEN_ID, PALEO_TOKEN_ID_DEFAULT, PRIVATE_KEY } from '../../constants';
import * as Aleo from '@demox-labs/aleo-sdk';
import { airDropCredits, getMappingValue, getMTSPBalance, getProgram, getPublicBalance, isTransactionAccepted } from '../../aleo/client';
import { killAuthorizePool } from '../../aleo/execute';
import { pondoPrograms } from '../../compiledPrograms';
import { calculatePaleoForDeposit, depositAsSigner, depositViaAllowance } from '../../protocol/userActions';
import assert from 'node:assert';

// run with the bonded state
describe('deposit', async () => {
let protocolProgram: string;
let paleoProgram: string;
let pondoTokenProgram: string;
let protocolState: string;

const protocolId: string = pondoPrograms.find((program) =>
program.includes('pondo_core_protocol')
)!;
const paleoId: string = pondoPrograms.find((program) =>
program.includes('pondo_staked_token')
)!;
const pondoTokenId: string = pondoPrograms.find((program) =>
program.includes('pondo_token')
)!;
const mtspId: string = pondoPrograms.find((program) =>
program.includes('multi_token_support_program')
)!;

let protocolProgramAddress: string;
let paleoMintedToCoreProtocol: BigInt;

const testUserPK1 = 'APrivateKey1zkp5fjQuBAeRnvPXFfde17Jm11S8wbF4iprqUBfG8AkLTkL';
const testUser1 = 'aleo1znrtvcdvxtjtw2ty9rex9yj0yf2cpxejktfmncj5et7ud9nunsqqczmczk';
const testUserPK2 = 'APrivateKey1zkpBBU1h6pHmUfGpq7gZF9ckRaSwoNxSrWJe26YQhC2mzfT';
const testUser2 = 'aleo1w7vqw25w4yktzlucuxzekxm8r5jee40z5p0wjq35qqdrq257mupqssgj7q';

before(async () => {
// assert that program states are as expected
// for aleo credits:
// need delegators + pondo protocol
protocolProgram = await getProgram(protocolId);
protocolProgramAddress = Aleo.Program.fromString(NETWORK!, protocolProgram).toAddress();
const protocolAccountBalance = await getPublicBalance(protocolProgramAddress);
console.log(`Protocol account balance: ${protocolAccountBalance}`);
const protocolDelegatedBalance = await getMappingValue('0u8', protocolId, 'balances');
console.log(`Protocol delegated balance: ${protocolDelegatedBalance}`);
const protocolBondedWithdrawals = await getMappingValue('1u8', protocolId, 'balances');
console.log(`Protocol bonded withdrawals: ${protocolBondedWithdrawals}`);
const protocolClaimableWithdrawals = await getMappingValue('2u8', protocolId, 'balances');
console.log(`Protocol claimable withdrawals: ${protocolClaimableWithdrawals}`);
const owedCommission = await getMappingValue('0u8', protocolId, 'owed_commission');
console.log(`Owed commission: ${owedCommission}`);
console.log(`Protocol program address: ${protocolProgramAddress}`);
console.log('expected minted address: aleo1f2feyx0alhy4tx0chs5f3z66u03nekzav90ry388jcrummghegxq0rcg7k');
paleoMintedToCoreProtocol = await getMTSPBalance(protocolProgramAddress, PALEO_TOKEN_ID, true);
console.log(`Paleo minted to core protocol: ${paleoMintedToCoreProtocol}`);
assert(paleoMintedToCoreProtocol === BigInt(101000000), 'Paleo minted to core protocol is not as expected');
protocolState = await getMappingValue('0u8', protocolId, 'protocol_state');
assert(protocolState === '0u8', 'Protocol state is not 0u8, or normal'); // normal
console.log(`Protocol state: ${protocolState}`);

const txResult1 = await airDropCredits(testUser2, BigInt(10_000_000_000));
const txResult = await airDropCredits(testUser1, BigInt(10_000_000_000));
const airDropComplete = await isTransactionAccepted(txResult);
console.log(`Airdrop complete: ${airDropComplete}`);
const testUserBalance = await getPublicBalance(testUser1);
console.log(`Test user balance: ${testUserBalance}`);
});

after(async () => {
await killAuthorizePool();
});

describe('normal state, earning rewards', async () => {
describe('deposit_public_as_signer', async () => {
it('should increase aleo pool and mint expected paleo to the test account', async () => {
const depositAmount = BigInt(500_000_000);
const expectedPaleoMinted = await calculatePaleoForDeposit(depositAmount);
console.log(`Expected paleo minted: ${expectedPaleoMinted}`);
const depositTx = await depositAsSigner(depositAmount, testUserPK1, expectedPaleoMinted);
const depositComplete = await isTransactionAccepted(depositTx);
const testUserPaleo = await getMTSPBalance(testUser1, PALEO_TOKEN_ID, true);
console.log(`Test user paleo balance: ${testUserPaleo}`);

assert(testUserPaleo === expectedPaleoMinted, 'Test user paleo balance is not as expected');
});

it('allows double deposits', async () => {
const currentPaleo = await getMTSPBalance(testUser1, PALEO_TOKEN_ID, true);
const depositAmount = BigInt(500_000_000);
const expectedPaleoMinted = await calculatePaleoForDeposit(depositAmount);
console.log(`Expected paleo minted: ${expectedPaleoMinted}`);
const depositTx = await depositAsSigner(depositAmount, testUserPK1, expectedPaleoMinted);
const depositComplete = await isTransactionAccepted(depositTx);
const testUserPaleo = await getMTSPBalance(testUser1, PALEO_TOKEN_ID, true);
console.log(`Test user paleo balance: ${testUserPaleo}`);

assert(testUserPaleo === expectedPaleoMinted + currentPaleo, 'Test user paleo balance is not as expected');
});
});

describe('deposit_public', async () => {
it('should increase aleo pool and mint expected paleo to the test account', async () => {
const depositAmount = BigInt(500_000_000);
const expectedPaleoMinted = await calculatePaleoForDeposit(depositAmount);
console.log(`Expected paleo minted: ${expectedPaleoMinted}`);
const depositAllowance = await depositViaAllowance(depositAmount, testUserPK2, expectedPaleoMinted);
const depositAllowanceComplete = await isTransactionAccepted(depositAllowance);
const testUserPaleo = await getMTSPBalance(testUser2, PALEO_TOKEN_ID, true);
console.log(`Test user paleo balance: ${testUserPaleo}`);

assert(testUserPaleo === expectedPaleoMinted, 'Test user paleo balance is not as expected');
});

it('allows double deposits', async () => {
const currentPaleo = await getMTSPBalance(testUser2, PALEO_TOKEN_ID, true);
const depositAmount = BigInt(500_000_000);
const expectedPaleoMinted = await calculatePaleoForDeposit(depositAmount);
console.log(`Expected paleo minted: ${expectedPaleoMinted}`);

const depositAllowance = await depositViaAllowance(depositAmount, testUserPK2, expectedPaleoMinted);
const depositAllowanceComplete = await isTransactionAccepted(depositAllowance);
const testUserPaleo = await getMTSPBalance(testUser2, PALEO_TOKEN_ID, true);
console.log(`Test user paleo balance: ${testUserPaleo}`);

assert(testUserPaleo === expectedPaleoMinted + currentPaleo, 'Test user paleo balance is not as expected');
});
});
});
});
Loading

0 comments on commit 7254d1c

Please sign in to comment.