Skip to content

Commit

Permalink
budget from preflight
Browse files Browse the repository at this point in the history
  • Loading branch information
mn13 committed Sep 21, 2023
1 parent 38c103c commit 69acf80
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 44 deletions.
12 changes: 12 additions & 0 deletions integration-tests/artifacts/budget_utilization.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"borrow": {
"cpuInsns": "37577012",
"memBytes": "4027722"
}
}
{
"withdraw": {
"cpuInsns": "37631489",
"memBytes": "4031271"
}
}
114 changes: 80 additions & 34 deletions integration-tests/tests/pool.sut.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, Keypair, SorobanRpc } from "soroban-client";
import { SorobanClient } from "./soroban.client";
import { SendTransactionResult, SorobanClient } from "./soroban.client";
import { adminKeys, contractsFilename, setEnv, treasuryKeys } from "./soroban.config";
import {
convertToScvAddress,
Expand All @@ -14,6 +14,9 @@ import {
parseScvToJs
} from "./soroban.converter";
import { exec } from "child_process";
import * as fs from 'fs';

export const BUDGET_SNAPSHOT_FILE = 'artifacts/budget_utilization.snap';

export type SlenderAsset = "XLM" | "XRP" | "USDC";

Expand Down Expand Up @@ -78,9 +81,10 @@ export async function mintUnderlyingTo(
process.env[`SLENDER_TOKEN_${asset}`],
"mint",
adminKeys,
false,
convertToScvAddress(to),
convertToScvI128(amount)
)
) as Promise<SorobanRpc.GetTransactionResponse>
);
}

Expand All @@ -93,9 +97,10 @@ export async function mintBurn(
mintsBurns[i].asset_balance.get("asset"),
mintsBurns[i].mint ? "mint" : "clawback",
adminKeys,
false,
convertToScvAddress(mintsBurns[i].who.toString()),
convertToScvI128(mintsBurns[i].asset_balance.get("balance"))
);
) as SorobanRpc.GetTransactionResponse;

if (response.status != "SUCCESS") {
throw Error("Failed to transfer tokens!");
Expand Down Expand Up @@ -161,12 +166,14 @@ export async function accountPosition(
export async function setPrice(
client: SorobanClient,
asset: SlenderAsset,
amount: bigint
): Promise<void> {
await client.sendTransaction(
amount: bigint,
withBudget = false
): Promise<SendTransactionResult> {
return client.sendTransaction(
process.env.SLENDER_POOL,
"set_price",
adminKeys,
withBudget,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvI128(amount)
);
Expand Down Expand Up @@ -213,111 +220,131 @@ export async function borrow(
client: SorobanClient,
signer: Keypair,
asset: SlenderAsset,
amount: bigint
): Promise<void> {
amount: bigint,
withBudget = false,
): Promise<SendTransactionResult> {
const response = await client.sendTransaction(
process.env.SLENDER_POOL,
"borrow",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvI128(amount)
);

const result = parseMetaXdrToJs<Array<MintBurn>>(
response.resultMetaXdr
withBudget ? response[0].resultMetaXdr : (response as SorobanRpc.GetSuccessfulTransactionResponse).resultMetaXdr
);

await mintBurn(client, result);

return response;
}

export async function deposit(
client: SorobanClient,
signer: Keypair,
asset: SlenderAsset,
amount: bigint
): Promise<void> {
amount: bigint,
withBudget = false,
): Promise<SendTransactionResult> {
const response = await client.sendTransaction(
process.env.SLENDER_POOL,
"deposit",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvI128(amount)
);

const result = parseMetaXdrToJs<Array<MintBurn>>(
response.resultMetaXdr
withBudget ? response[0].resultMetaXdr : (response as SorobanRpc.GetSuccessfulTransactionResponse).resultMetaXdr
);

await mintBurn(client, result);

return response;
}

export async function repay(
client: SorobanClient,
signer: Keypair,
asset: SlenderAsset,
amount: bigint
): Promise<void> {
amount: bigint,
withBudget = false,
): Promise<SendTransactionResult> {
const response = await client.sendTransaction(
process.env.SLENDER_POOL,
"repay",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvI128(amount)
);

const result = parseMetaXdrToJs<Array<MintBurn>>(
response.resultMetaXdr
withBudget ? response[0].resultMetaXdr : (response as SorobanRpc.GetSuccessfulTransactionResponse).resultMetaXdr
);

await mintBurn(client, result);

return response;
}

export async function withdraw(
client: SorobanClient,
signer: Keypair,
asset: SlenderAsset,
amount: bigint
): Promise<void> {
amount: bigint,
withBudget = false,
): Promise<SendTransactionResult> {
const response = await client.sendTransaction(
process.env.SLENDER_POOL,
"withdraw",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvI128(amount),
convertToScvAddress(signer.publicKey())
);

const result = parseMetaXdrToJs<Array<MintBurn>>(
response.resultMetaXdr
withBudget ? response[0].resultMetaXdr : (response as SorobanRpc.GetSuccessfulTransactionResponse).resultMetaXdr
);

await mintBurn(client, result);

return response;
}

export async function liquidate(
client: SorobanClient,
signer: Keypair,
who: string,
receiveStoken: boolean
): Promise<void> {
receiveStoken: boolean,
withBudget = false,
): Promise<SendTransactionResult> {
const response = await client.sendTransaction(
process.env.SLENDER_POOL,
"liquidate",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(who),
convertToScvBool(receiveStoken)
);

const result = parseMetaXdrToJs<Array<MintBurn>>(
response.resultMetaXdr
withBudget ? response[0].resultMetaXdr : (response as SorobanRpc.GetSuccessfulTransactionResponse).resultMetaXdr
);

await mintBurn(client, result);

return response;
}

export async function collatCoeff(
Expand All @@ -338,12 +365,14 @@ export async function transferStoken(
asset: SlenderAsset,
signer: Keypair,
to: string,
amount: bigint
): Promise<void> {
await client.sendTransaction(
amount: bigint,
withBudget = false,
): Promise<SendTransactionResult> {
return client.sendTransaction(
process.env[`SLENDER_S_TOKEN_${asset}`],
"transfer",
signer,
withBudget,
convertToScvAddress(signer.publicKey()),
convertToScvAddress(to),
convertToScvI128(amount)
Expand Down Expand Up @@ -388,6 +417,7 @@ export async function finalizeTransfer(
process.env["SLENDER_POOL"],
"finalize_transfer",
signer,
false,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvAddress(signer.publicKey()),
convertToScvAddress(to),
Expand All @@ -398,6 +428,13 @@ export async function finalizeTransfer(
);
}

export function writeBudgetSnapshot(name: string, transactionResult: SendTransactionResult) {
if (Array.isArray(transactionResult)) {
const budget = transactionResult[1];
fs.writeFileSync(BUDGET_SNAPSHOT_FILE, `${JSON.stringify({ [name]: budget }, null, 2)}\n`, { flag: 'a' });
}
}

async function initContract<T>(
name: string,
callback: () => Promise<SorobanRpc.GetTransactionResponse>,
Expand All @@ -424,11 +461,12 @@ async function initToken(client: SorobanClient, asset: SlenderAsset, name: strin
process.env[`SLENDER_TOKEN_${asset}`],
"initialize",
adminKeys,
false,
convertToScvAddress(adminKeys.publicKey()),
convertToScvU32(9),
convertToScvString(name),
convertToScvString(asset)
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}

Expand All @@ -439,13 +477,14 @@ async function initSToken(client: SorobanClient, asset: SlenderAsset, salt: stri
process.env.SLENDER_DEPLOYER,
"deploy_s_token",
adminKeys,
false,
convertToScvBytes(salt, "hex"),
convertToScvBytes(process.env.SLENDER_S_TOKEN_HASH, "hex"),
convertToScvString(`SToken ${asset}`),
convertToScvString(`S${asset}`),
convertToScvAddress(process.env.SLENDER_POOL),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
),
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>,
result => result[0]
);
}
Expand All @@ -457,13 +496,14 @@ async function initDToken(client: SorobanClient, asset: SlenderAsset, salt: stri
process.env.SLENDER_DEPLOYER,
"deploy_debt_token",
adminKeys,
false,
convertToScvBytes(salt, "hex"),
convertToScvBytes(process.env.SLENDER_DEBT_TOKEN_HASH, "hex"),
convertToScvString(`DToken ${asset}`),
convertToScvString(`D${asset}`),
convertToScvAddress(process.env.SLENDER_POOL),
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
),
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>,
result => result[0]);
}

Expand All @@ -474,6 +514,7 @@ async function initPool(client: SorobanClient, salt: string): Promise<void> {
process.env.SLENDER_DEPLOYER,
"deploy_pool",
adminKeys,
false,
convertToScvBytes(salt, "hex"),
convertToScvBytes(process.env.SLENDER_POOL_HASH, "hex"),
convertToScvAddress(adminKeys.publicKey()),
Expand All @@ -485,7 +526,7 @@ async function initPool(client: SorobanClient, salt: string): Promise<void> {
"max_rate": convertToScvU32(50_000),
"scaling_coeff": convertToScvU32(9_000)
})
),
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>,
result => result[0]
);
}
Expand All @@ -497,13 +538,14 @@ async function initPoolReserve(client: SorobanClient, asset: SlenderAsset, decim
process.env.SLENDER_POOL,
"init_reserve",
adminKeys,
false,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvMap({
"debt_token_address": convertToScvAddress(process.env[`SLENDER_DEBT_TOKEN_${asset}`]),
// "decimals": convertToScvU32(9),
"s_token_address": convertToScvAddress(process.env[`SLENDER_S_TOKEN_${asset}`]),
})
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}

Expand All @@ -514,14 +556,15 @@ async function initPoolCollateral(client: SorobanClient, asset: SlenderAsset): P
process.env.SLENDER_POOL,
"configure_as_collateral",
adminKeys,
false,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvMap({
"discount": convertToScvU32(6000),
"liq_bonus": convertToScvU32(11000),
"liq_cap": convertToScvI128(1000000000000000n),
"util_cap": convertToScvU32(9000)
})
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}

Expand All @@ -532,9 +575,10 @@ async function initPoolPriceFeed(client: SorobanClient, feed: string, assets: st
process.env.SLENDER_POOL,
"set_price_feed",
adminKeys,
false,
convertToScvAddress(feed),
convertToScvVec(assets.map(asset => convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`])))
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}

Expand All @@ -545,9 +589,10 @@ async function initPoolBorrowing(client: SorobanClient, asset: SlenderAsset): Pr
process.env.SLENDER_POOL,
"enable_borrowing_on_reserve",
adminKeys,
false,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvBool(true)
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}

Expand All @@ -558,8 +603,9 @@ async function initBaseAsset(client: SorobanClient, asset: SlenderAsset): Promis
process.env.SLENDER_POOL,
"set_base_asset",
adminKeys,
false,
convertToScvAddress(process.env[`SLENDER_TOKEN_${asset}`]),
convertToScvBool(true)
)
) as Promise<SorobanRpc.GetSuccessfulTransactionResponse>
);
}
Loading

0 comments on commit 69acf80

Please sign in to comment.