diff --git a/programs/aqua/src/lib.rs b/programs/aqua/src/lib.rs index d121ce8..01ab20b 100644 --- a/programs/aqua/src/lib.rs +++ b/programs/aqua/src/lib.rs @@ -26,6 +26,7 @@ struct PairsTable { token_b: ScVal, reserve_a: ScVal, reserve_b: ScVal, + fee: ScVal } #[no_mangle] @@ -37,7 +38,6 @@ pub extern "C" fn on_close() { let lp_router_contract = stellar_strkey::Contract::from_string(&contract_address_str).unwrap().0; let rows = env.read::(); - env.log().debug(format!("rows: {:?}", &rows), None); let lp_router_contract_events: Vec = { let events = env.reader().pretty().soroban_events(); @@ -54,8 +54,6 @@ pub extern "C" fn on_close() { }; for t_event in lp_router_contract_events { - env.log().debug(format!("event: {:?}", &t_event), None); - let action: Symbol = env.from_scval(&t_event.topics[0]); let tokens: SorobanVec
= env.from_scval(&t_event.topics[1]); @@ -88,6 +86,7 @@ pub extern "C" fn on_close() { reserve_b: env.to_scval(0), token_a: env.to_scval(tokens.first()), token_b: env.to_scval(tokens.last()), + fee: env.to_scval(0) }; env.log().debug(format!("New action Table: {:?}", table), None); @@ -119,6 +118,7 @@ pub(crate) fn handle_add(env: &EnvClient, event: &PrettyContractEvent, data_leng let mut reserve_a: u128 = 0; let mut reserve_b: u128 = 0; + let mut fee_fraction: u32 = 0; if let Some(instance) = instance_storage { if let LedgerEntryData::ContractData(contract_data_entry) = &instance.entry.data { @@ -126,7 +126,6 @@ pub(crate) fn handle_add(env: &EnvClient, event: &PrettyContractEvent, data_leng if let Some(storage_map) = &contract_instance.storage { for entry in storage_map.iter() { - if let ScVal::Vec(Some(vec)) = &entry.key { if let Some(ScVal::Symbol(symbol)) = vec.first() { @@ -140,8 +139,12 @@ pub(crate) fn handle_add(env: &EnvClient, event: &PrettyContractEvent, data_leng env.log().debug(format!("ReseveB: {:?}", entry), None); reserve_b = parts.lo.into() } + } else if symbol.to_string() == "FeeFraction" { + if let ScVal::U32(fee) = &entry.val { + env.log().debug(format!("FeeFraction: {:?}", entry), None); + fee_fraction = fee.clone() + } } - } } } @@ -159,6 +162,7 @@ pub(crate) fn handle_add(env: &EnvClient, event: &PrettyContractEvent, data_leng reserve_b: env.to_scval(reserve_b), token_a: env.to_scval(tokens.first()), token_b: env.to_scval(tokens.last()), + fee: env.to_scval(fee_fraction) }; env.log().debug(format!("New action Table: {:?}", table), None); diff --git a/programs/aqua/zephyr.toml b/programs/aqua/zephyr.toml index 2e9fb7e..b55a7de 100644 --- a/programs/aqua/zephyr.toml +++ b/programs/aqua/zephyr.toml @@ -24,4 +24,8 @@ col_type = "BYTEA" [[tables.columns]] name = "reserve_b" +col_type = "BYTEA" + +[[tables.columns]] +name = "fee" col_type = "BYTEA" \ No newline at end of file diff --git a/programs/phoenix/src/lib.rs b/programs/phoenix/src/lib.rs index 1f96a6e..cd488e9 100644 --- a/programs/phoenix/src/lib.rs +++ b/programs/phoenix/src/lib.rs @@ -9,6 +9,7 @@ struct PairsTable { token_b: ScVal, reserve_a: ScVal, reserve_b: ScVal, + fee: ScVal } //Phoenix Factory @@ -91,6 +92,7 @@ pub extern "C" fn on_close() { reserve_b: env.to_scval(0), token_a: env.to_scval("0"), token_b: env.to_scval("0"), + fee: env.to_scval(0) }; for entry in pair_entries { @@ -101,6 +103,7 @@ pub extern "C" fn on_close() { table.token_a = env.to_scval(config_data.token_a.to_string()); table.token_b = env.to_scval(config_data.token_b.to_string()); + table.fee = env.to_scval(config_data.total_fee_bps); } if entry.key == env.to_scval(PairDataKey::ReserveA){ @@ -118,14 +121,9 @@ pub extern "C" fn on_close() { }else{ table.put(&env); } - }); } - } - - - } diff --git a/programs/phoenix/zephyr.toml b/programs/phoenix/zephyr.toml index 963eb38..f6c2df1 100644 --- a/programs/phoenix/zephyr.toml +++ b/programs/phoenix/zephyr.toml @@ -24,4 +24,8 @@ col_type = "BYTEA" [[tables.columns]] name = "reserve_b" -col_type = "BYTEA" \ No newline at end of file +col_type = "BYTEA" + +[[tables.columns]] +name = "fee" +col_type = "BYTEA" diff --git a/scripts/__tests__/pairs.test.ts b/scripts/__tests__/pairs.test.ts index 5083627..5419003 100644 --- a/scripts/__tests__/pairs.test.ts +++ b/scripts/__tests__/pairs.test.ts @@ -1,9 +1,9 @@ // import fs from "fs"; import { zephyrTableToGraphQLParser} from "mercury-sdk"; -import { getPairs } from "../utils/get-pairs"; +import { getAquaPairs, getPairs } from "../utils/get-pairs"; import { getTotalPairs } from "../utils/get-total-pairs"; import { getZephyrTable } from "../utils/get-table"; -import fs from "fs"; +import { getFeesFromPair } from "../utils/get-fees-from-pair"; test("soroswap pairs in MAINNET return non empty array", async () => { @@ -54,6 +54,18 @@ test.todo("aqua pairs in TESTNET return non empty array"); test.todo("aqua pairs in TESTNET is equal to Factory all_pairs_length()"); test.todo("aqua pairs in MAINNET return non empty array"); +test("aqua pairs in MAINNET amount is greater than 0", async () => { + const aquaPairsTable = getZephyrTable('aqua_pairs', "MAINNET") + const zephyrTableGraphQL = zephyrTableToGraphQLParser(aquaPairsTable); + const pairs = await getAquaPairs(zephyrTableGraphQL.address, 'MAINNET'); + for(let pair of pairs){ + const address = pair.address; + const fee = pair.fee; + const blockchainFee = await getFeesFromPair('aqua', 'MAINNET', address) + console.log('🟡blockchainFee', blockchainFee) + //To do: check if blockchainFee are equal to fee + } +}); test.todo("aqua pairs in MAINNET is equal to Factory all_pairs_length()"); diff --git a/scripts/utils/get-fees-from-pair.ts b/scripts/utils/get-fees-from-pair.ts new file mode 100644 index 0000000..266c563 --- /dev/null +++ b/scripts/utils/get-fees-from-pair.ts @@ -0,0 +1,35 @@ +import {scValToNative} from "@stellar/stellar-sdk" +import {invokeCustomContract} from "./contract"; +import { config } from './env_config'; + + +// import * as StellarSdk from "@stellar/stellar-sdk"; + +export const getFeesFromPair = async ( + protocolName: string, + network: "MAINNET" | "TESTNET", + pairAddress: string) => { + + let loadedConfig; + if (network === "MAINNET") { + loadedConfig = config('mainnet'); + } else { + loadedConfig = config('testnet'); + } + + const reserves = await invokeCustomContract( + pairAddress, + "get_fee_fraction", + [], + loadedConfig.user, + loadedConfig, + true); + // console.log("🚀 ~ getFeesFromPair ~ reserves", reserves.result.retval) + const reservesNative=scValToNative(reserves.result.retval) + console.log(reservesNative) + // console.log("🚀 ~ reservesNative:", reservesNative) + // const totalPairsNative=scValToNative(totalPairs.result.retval) + + return reservesNative; + +}; diff --git a/scripts/utils/get-pairs.ts b/scripts/utils/get-pairs.ts index af0ea0d..1b2870c 100644 --- a/scripts/utils/get-pairs.ts +++ b/scripts/utils/get-pairs.ts @@ -5,6 +5,7 @@ interface Pair { tokenA: string; tokenB: string; address: string; + fee?: number; } const parseValue = (value: any) => { @@ -46,3 +47,37 @@ export const getPairs = async (tableName: string, network: "MAINNET" | "TESTNET" return []; }; +export const getAquaPairs = async (tableName: string, network: "MAINNET" | "TESTNET") => { + + const mercuryInstance = getMercuryInstance(network); + const res = await mercuryInstance.getCustomQuery({ + request: `query Query { + events: allZephyr3B2F44Dfcda9Ebf57D4Deb257E6D4E90S { + data: nodes { + address + tokenA + tokenB + reserveA + reserveB + fee + } + } + }`, + }); + + if (res.ok) { + const pairs = res.data.events.data.map((d: any) => { + let n: any = {}; + + for (let key in d) { + n[key] = parseValue(d[key]); + } + + return n; + }); + + return pairs as Pair[]; + } + + return []; +}; diff --git a/scripts/utils/tx.ts b/scripts/utils/tx.ts index 817a6bc..298938a 100644 --- a/scripts/utils/tx.ts +++ b/scripts/utils/tx.ts @@ -1,5 +1,6 @@ import { Account, Keypair, SorobanRpc, Transaction, TransactionBuilder, xdr } from '@stellar/stellar-sdk'; import { EnvConfig } from './env_config.js'; +import { AxiosClient } from '@stellar/stellar-sdk/rpc'; type txResponse = SorobanRpc.Api.SendTransactionResponse | SorobanRpc.Api.GetTransactionResponse; type txStatus = SorobanRpc.Api.SendTransactionStatus | SorobanRpc.Api.GetTransactionStatus;