import * as $ from "@manahippo/move-to-ts"; import {AptosDataCache, AptosParserRepo, DummyCache, AptosLocalCache} from "@manahippo/move-to-ts"; import {U8, U64, U128} from "@manahippo/move-to-ts"; import {u8, u64, u128} from "@manahippo/move-to-ts"; import {TypeParamDeclType, FieldDeclType} from "@manahippo/move-to-ts"; import {AtomicTypeTag, StructTag, TypeTag, VectorTag, SimpleStructTag} from "@manahippo/move-to-ts"; import {OptionTransaction} from "@manahippo/move-to-ts"; import {HexString, AptosClient, AptosAccount, TxnBuilderTypes, Types} from "aptos"; import * as Stdlib from "../stdlib"; import * as Event from "./event"; import * as Init from "./init"; import * as Math from "./math"; export const packageName = "swap"; export const moduleAddress = new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"); export const moduleName = "implements"; export const ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM : U64 = u64("304"); export const ERR_INCORRECT_BURN_VALUES : U64 = u64("303"); export const ERR_LIQUID_NOT_ENOUGH : U64 = u64("307"); export const ERR_NOT_ENOUGH_PERMISSIONS_TO_INITIALIZE : U64 = u64("306"); export const ERR_POOL_DOES_NOT_EXIST : U64 = u64("301"); export const ERR_POOL_EXISTS_FOR_PAIR : U64 = u64("300"); export const ERR_POOL_IS_LOCKED : U64 = u64("302"); export const ERR_SWAP_NOT_INITIALIZE : U64 = u64("308"); export const ERR_U64_OVERFLOW : U64 = u64("309"); export const FEE_MULTIPLIER : U64 = u64("30"); export const FEE_SCALE : U64 = u64("10000"); export const MINIMAL_LIQUIDITY : U64 = u64("310"); export const SYMBOL_PREFIX_LENGTH : U64 = u64("4"); export const U64_MAX : U64 = u64("18446744073709551615"); export class Config { static moduleAddress = moduleAddress; static moduleName = moduleName; __app: $.AppType | null = null; static structName: string = "Config"; static typeParameters: TypeParamDeclType[] = [ ]; static fields: FieldDeclType[] = [ { name: "pool_cap", typeTag: new StructTag(new HexString("0x1"), "account", "SignerCapability", []) }, { name: "fee_cap", typeTag: new StructTag(new HexString("0x1"), "account", "SignerCapability", []) }, { name: "controller", typeTag: AtomicTypeTag.Address }, { name: "beneficiary", typeTag: AtomicTypeTag.Address }]; pool_cap: Stdlib.Account.SignerCapability; fee_cap: Stdlib.Account.SignerCapability; controller: HexString; beneficiary: HexString; constructor(proto: any, public typeTag: TypeTag) { this.pool_cap = proto['pool_cap'] as Stdlib.Account.SignerCapability; this.fee_cap = proto['fee_cap'] as Stdlib.Account.SignerCapability; this.controller = proto['controller'] as HexString; this.beneficiary = proto['beneficiary'] as HexString; } static ConfigParser(data:any, typeTag: TypeTag, repo: AptosParserRepo) : Config { const proto = $.parseStructProto(data, typeTag, repo, Config); return new Config(proto, typeTag); } static async load(repo: AptosParserRepo, client: AptosClient, address: HexString, typeParams: TypeTag[]) { const result = await repo.loadResource(client, address, Config, typeParams); return result as unknown as Config; } static async loadByApp(app: $.AppType, address: HexString, typeParams: TypeTag[]) { const result = await app.repo.loadResource(app.client, address, Config, typeParams); await result.loadFullState(app) return result as unknown as Config; } static getTag(): StructTag { return new StructTag(moduleAddress, moduleName, "Config", []); } async loadFullState(app: $.AppType) { await this.pool_cap.loadFullState(app); await this.fee_cap.loadFullState(app); this.__app = app; } } export class LiquidityPool { static moduleAddress = moduleAddress; static moduleName = moduleName; __app: $.AppType | null = null; static structName: string = "LiquidityPool"; static typeParameters: TypeParamDeclType[] = [ { name: "X", isPhantom: true }, { name: "Y", isPhantom: true } ]; static fields: FieldDeclType[] = [ { name: "coin_x", typeTag: new StructTag(new HexString("0x1"), "coin", "Coin", [new $.TypeParamIdx(0)]) }, { name: "coin_y", typeTag: new StructTag(new HexString("0x1"), "coin", "Coin", [new $.TypeParamIdx(1)]) }, { name: "timestamp", typeTag: AtomicTypeTag.U64 }, { name: "x_cumulative", typeTag: AtomicTypeTag.U128 }, { name: "y_cumulative", typeTag: AtomicTypeTag.U128 }, { name: "lp_mint_cap", typeTag: new StructTag(new HexString("0x1"), "coin", "MintCapability", [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [new $.TypeParamIdx(0), new $.TypeParamIdx(1)])]) }, { name: "lp_burn_cap", typeTag: new StructTag(new HexString("0x1"), "coin", "BurnCapability", [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [new $.TypeParamIdx(0), new $.TypeParamIdx(1)])]) }]; coin_x: Stdlib.Coin.Coin; coin_y: Stdlib.Coin.Coin; timestamp: U64; x_cumulative: U128; y_cumulative: U128; lp_mint_cap: Stdlib.Coin.MintCapability; lp_burn_cap: Stdlib.Coin.BurnCapability; constructor(proto: any, public typeTag: TypeTag) { this.coin_x = proto['coin_x'] as Stdlib.Coin.Coin; this.coin_y = proto['coin_y'] as Stdlib.Coin.Coin; this.timestamp = proto['timestamp'] as U64; this.x_cumulative = proto['x_cumulative'] as U128; this.y_cumulative = proto['y_cumulative'] as U128; this.lp_mint_cap = proto['lp_mint_cap'] as Stdlib.Coin.MintCapability; this.lp_burn_cap = proto['lp_burn_cap'] as Stdlib.Coin.BurnCapability; } static LiquidityPoolParser(data:any, typeTag: TypeTag, repo: AptosParserRepo) : LiquidityPool { const proto = $.parseStructProto(data, typeTag, repo, LiquidityPool); return new LiquidityPool(proto, typeTag); } static async load(repo: AptosParserRepo, client: AptosClient, address: HexString, typeParams: TypeTag[]) { const result = await repo.loadResource(client, address, LiquidityPool, typeParams); return result as unknown as LiquidityPool; } static async loadByApp(app: $.AppType, address: HexString, typeParams: TypeTag[]) { const result = await app.repo.loadResource(app.client, address, LiquidityPool, typeParams); await result.loadFullState(app) return result as unknown as LiquidityPool; } static makeTag($p: TypeTag[]): StructTag { return new StructTag(moduleAddress, moduleName, "LiquidityPool", $p); } async loadFullState(app: $.AppType) { await this.coin_x.loadFullState(app); await this.coin_y.loadFullState(app); await this.lp_mint_cap.loadFullState(app); await this.lp_burn_cap.loadFullState(app); this.__app = app; } } export function beneficiary_ ( $c: AptosDataCache, ): HexString { if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } return $.copy($c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")).beneficiary); } export function burn_ ( lp_coins: Stdlib.Coin.Coin, $c: AptosDataCache, $p: TypeTag[], /* */ ): [Stdlib.Coin.Coin, Stdlib.Coin.Coin] { let temp$1, temp$2, burned_lp_coins_val, lp_coins_total, pool, pool_address, x_coin_to_return, x_reserve_val, x_tmp, x_to_return_val, y_coin_to_return, y_reserve_val, y_tmp, y_to_return_val; pool_address = pool_address_($c); if (!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_DOES_NOT_EXIST)); } pool = $c.borrow_global_mut(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address)); burned_lp_coins_val = Stdlib.Coin.value_(lp_coins, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); x_reserve_val = Stdlib.Coin.value_(pool.coin_x, $c, [$p[0]]); y_reserve_val = Stdlib.Coin.value_(pool.coin_y, $c, [$p[1]]); temp$1 = Stdlib.Coin.supply_($c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); lp_coins_total = Stdlib.Option.extract_(temp$1, $c, [AtomicTypeTag.U128]); x_tmp = u128(($.copy(x_reserve_val)).mul($.copy(burned_lp_coins_val))); x_to_return_val = u64(($.copy(x_tmp)).div($.copy(lp_coins_total))); y_tmp = u128(($.copy(y_reserve_val)).mul($.copy(burned_lp_coins_val))); y_to_return_val = u64(($.copy(y_tmp)).div($.copy(lp_coins_total))); if (($.copy(x_to_return_val)).gt(u64("0"))) { temp$2 = ($.copy(y_to_return_val)).gt(u64("0")); } else{ temp$2 = false; } if (!temp$2) { throw $.abortCode($.copy(ERR_INCORRECT_BURN_VALUES)); } x_coin_to_return = Stdlib.Coin.extract_(pool.coin_x, $.copy(x_to_return_val), $c, [$p[0]]); y_coin_to_return = Stdlib.Coin.extract_(pool.coin_y, $.copy(y_to_return_val), $c, [$p[1]]); Event.removed_event_($.copy(pool_address), $.copy(x_to_return_val), $.copy(y_to_return_val), $.copy(burned_lp_coins_val), $c, [$p[0], $p[1]]); update_oracle_($.copy(pool_address), pool, $c, [$p[0], $p[1]]); Stdlib.Coin.burn_(lp_coins, pool.lp_burn_cap, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); return [x_coin_to_return, y_coin_to_return]; } export function coin_symbol_prefix_ ( $c: AptosDataCache, $p: TypeTag[], /* */ ): Stdlib.String.String { let prefix_length, symbol; symbol = Stdlib.Coin.symbol_($c, [$p[0]]); prefix_length = $.copy(SYMBOL_PREFIX_LENGTH); if ((Stdlib.String.length_(symbol, $c)).lt($.copy(SYMBOL_PREFIX_LENGTH))) { prefix_length = Stdlib.String.length_(symbol, $c); } else{ } return Stdlib.String.sub_string_(symbol, u64("0"), $.copy(prefix_length), $c); } export function controller_ ( $c: AptosDataCache, ): HexString { if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } return $.copy($c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")).controller); } export function fee_account_ ( $c: AptosDataCache, ): HexString { let config; if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } config = $c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")); return Stdlib.Account.create_signer_with_capability_(config.fee_cap, $c); } export function fee_address_ ( $c: AptosDataCache, ): HexString { let config; if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } config = $c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")); return Stdlib.Account.get_signer_capability_address_(config.fee_cap, $c); } export function generate_lp_name_and_symbol_ ( $c: AptosDataCache, $p: TypeTag[], /* */ ): [Stdlib.String.String, Stdlib.String.String] { let lp_name, lp_symbol; lp_name = Stdlib.String.utf8_([], $c); Stdlib.String.append_utf8_(lp_name, [u8("76"), u8("80"), u8("45")], $c); Stdlib.String.append_(lp_name, Stdlib.Coin.symbol_($c, [$p[0]]), $c); Stdlib.String.append_utf8_(lp_name, [u8("45")], $c); Stdlib.String.append_(lp_name, Stdlib.Coin.symbol_($c, [$p[1]]), $c); lp_symbol = Stdlib.String.utf8_([], $c); Stdlib.String.append_(lp_symbol, coin_symbol_prefix_($c, [$p[0]]), $c); Stdlib.String.append_utf8_(lp_symbol, [u8("45")], $c); Stdlib.String.append_(lp_symbol, coin_symbol_prefix_($c, [$p[1]]), $c); return [$.copy(lp_name), $.copy(lp_symbol)]; } export function get_amout_out_ ( amout_in: U64, x: boolean, $c: AptosDataCache, $p: TypeTag[], /* */ ): U64 { let coin_in_val_after_fees, fee_multiplier, fee_pct, fee_scale, new_reserve_in, new_reserve_in__1, reserve_x, reserve_y; [reserve_x, reserve_y] = get_reserves_size_($c, [$p[0], $p[1]]); [fee_pct, fee_scale] = [$.copy(FEE_MULTIPLIER), $.copy(FEE_SCALE)]; fee_multiplier = ($.copy(fee_scale)).sub($.copy(fee_pct)); coin_in_val_after_fees = ($.copy(amout_in)).mul($.copy(fee_multiplier)); if (x) { new_reserve_in = (($.copy(reserve_x)).mul($.copy(fee_scale))).add($.copy(coin_in_val_after_fees)); return (($.copy(reserve_y)).mul($.copy(coin_in_val_after_fees))).div($.copy(new_reserve_in)); } else{ new_reserve_in__1 = (($.copy(reserve_y)).mul($.copy(fee_scale))).add($.copy(coin_in_val_after_fees)); return (($.copy(reserve_x)).mul($.copy(coin_in_val_after_fees))).div($.copy(new_reserve_in__1)); } } export function get_reserves_size_ ( $c: AptosDataCache, $p: TypeTag[], /* */ ): [U64, U64] { let pool, pool_address, x_reserve, y_reserve; pool_address = pool_address_($c); if (!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_DOES_NOT_EXIST)); } pool = $c.borrow_global(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address)); x_reserve = Stdlib.Coin.value_(pool.coin_x, $c, [$p[0]]); y_reserve = Stdlib.Coin.value_(pool.coin_y, $c, [$p[1]]); return [$.copy(x_reserve), $.copy(y_reserve)]; } export function initialize_swap_ ( swap_admin: HexString, controller: HexString, beneficiary: HexString, $c: AptosDataCache, ): void { let _signer, fee_cap, pool_account, pool_cap; if (!((Stdlib.Signer.address_of_(swap_admin, $c)).hex() === (new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")).hex())) { throw $.abortCode($.copy(ERR_NOT_ENOUGH_PERMISSIONS_TO_INITIALIZE)); } pool_cap = Init.retrieve_signer_cap_(swap_admin, $c); pool_account = Stdlib.Account.create_signer_with_capability_(pool_cap, $c); [_signer, fee_cap] = Stdlib.Account.create_resource_account_(swap_admin, [u8("99"), u8("111"), u8("110"), u8("116"), u8("114"), u8("111"), u8("108"), u8("108"), u8("101"), u8("114"), u8("95"), u8("97"), u8("99"), u8("99"), u8("111"), u8("117"), u8("110"), u8("116"), u8("95"), u8("115"), u8("101"), u8("101"), u8("100")], $c); $c.move_to(new SimpleStructTag(Config), swap_admin, new Config({ pool_cap: pool_cap, fee_cap: fee_cap, controller: $.copy(controller), beneficiary: $.copy(beneficiary) }, new SimpleStructTag(Config))); Event.initialize_(pool_account, $c); return; } export function mint_ ( coin_x: Stdlib.Coin.Coin, coin_y: Stdlib.Coin.Coin, $c: AptosDataCache, $p: TypeTag[], /* */ ): Stdlib.Coin.Coin { let temp$1, temp$2, temp$3, initial_liq, lp_coins, lp_coins_total, pool, pool_address, provided_liq, reserve_x, reserve_y, x_liq, x_provided_val, y_liq, y_provided_val; pool_address = pool_address_($c); if (!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_DOES_NOT_EXIST)); } x_provided_val = Stdlib.Coin.value_(coin_x, $c, [$p[0]]); y_provided_val = Stdlib.Coin.value_(coin_y, $c, [$p[1]]); temp$1 = Stdlib.Coin.supply_($c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); lp_coins_total = Stdlib.Option.extract_(temp$1, $c, [AtomicTypeTag.U128]); if ((u128("0")).eq(($.copy(lp_coins_total)))) { initial_liq = (Math.sqrt_($.copy(x_provided_val), $c)).mul(Math.sqrt_($.copy(y_provided_val), $c)); if (!($.copy(initial_liq)).gt($.copy(MINIMAL_LIQUIDITY))) { throw $.abortCode($.copy(ERR_LIQUID_NOT_ENOUGH)); } temp$3 = ($.copy(initial_liq)).sub($.copy(MINIMAL_LIQUIDITY)); } else{ [reserve_x, reserve_y] = get_reserves_size_($c, [$p[0], $p[1]]); x_liq = ((u128($.copy(lp_coins_total))).mul(u128($.copy(x_provided_val)))).div(u128($.copy(reserve_x))); y_liq = ((u128($.copy(lp_coins_total))).mul(u128($.copy(y_provided_val)))).div(u128($.copy(reserve_y))); if (($.copy(x_liq)).lt($.copy(y_liq))) { if (!($.copy(x_liq)).lt(u128($.copy(U64_MAX)))) { throw $.abortCode($.copy(ERR_U64_OVERFLOW)); } temp$2 = u64($.copy(x_liq)); } else{ if (!($.copy(y_liq)).lt(u128($.copy(U64_MAX)))) { throw $.abortCode($.copy(ERR_U64_OVERFLOW)); } temp$2 = u64($.copy(y_liq)); } temp$3 = temp$2; } provided_liq = temp$3; pool = $c.borrow_global_mut(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address)); Stdlib.Coin.merge_(pool.coin_x, coin_x, $c, [$p[0]]); Stdlib.Coin.merge_(pool.coin_y, coin_y, $c, [$p[1]]); Event.added_event_($.copy(pool_address), $.copy(x_provided_val), $.copy(y_provided_val), $.copy(provided_liq), $c, [$p[0], $p[1]]); update_oracle_($.copy(pool_address), pool, $c, [$p[0], $p[1]]); lp_coins = Stdlib.Coin.mint_($.copy(provided_liq), pool.lp_mint_cap, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); return lp_coins; } export function pool_account_ ( $c: AptosDataCache, ): HexString { let config; if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } config = $c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")); return Stdlib.Account.create_signer_with_capability_(config.pool_cap, $c); } export function pool_address_ ( $c: AptosDataCache, ): HexString { let config; if (!$c.exists(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"))) { throw $.abortCode($.copy(ERR_SWAP_NOT_INITIALIZE)); } config = $c.borrow_global(new SimpleStructTag(Config), new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")); return Stdlib.Account.get_signer_capability_address_(config.pool_cap, $c); } export function register_pool_ ( account: HexString, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let fee_account, fee_address, lp_burn_cap, lp_freeze_cap, lp_mint_cap, lp_name, lp_symbol, pool, pool_account, pool_address; pool_account = pool_account_($c); pool_address = Stdlib.Signer.address_of_(pool_account, $c); fee_account = fee_account_($c); fee_address = Stdlib.Signer.address_of_(fee_account, $c); if (!!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_EXISTS_FOR_PAIR)); } [lp_name, lp_symbol] = generate_lp_name_and_symbol_($c, [$p[0], $p[1]]); [lp_burn_cap, lp_freeze_cap, lp_mint_cap] = Stdlib.Coin.initialize_(pool_account, $.copy(lp_name), $.copy(lp_symbol), u8("8"), true, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); Stdlib.Coin.destroy_freeze_cap_($.copy(lp_freeze_cap), $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); if (!Stdlib.Coin.is_account_registered_($.copy(fee_address), $c, [$p[0]])) { Stdlib.Coin.register_(fee_account, $c, [$p[0]]); } else{ } if (!Stdlib.Coin.is_account_registered_($.copy(fee_address), $c, [$p[1]])) { Stdlib.Coin.register_(fee_account, $c, [$p[1]]); } else{ } pool = new LiquidityPool({ coin_x: Stdlib.Coin.zero_($c, [$p[0]]), coin_y: Stdlib.Coin.zero_($c, [$p[1]]), timestamp: u64("0"), x_cumulative: u128("0"), y_cumulative: u128("0"), lp_mint_cap: $.copy(lp_mint_cap), lp_burn_cap: $.copy(lp_burn_cap) }, new SimpleStructTag(LiquidityPool, [$p[0], $p[1]])); $c.move_to(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), pool_account, pool); Event.created_event_($.copy(pool_address), Stdlib.Signer.address_of_(account, $c), $c, [$p[0], $p[1]]); return; } export function swap_x_ ( coin_in: Stdlib.Coin.Coin, coin_out_min_val: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): Stdlib.Coin.Coin { let coin_in_val, coin_out_val, fee_multiplier, pool, pool_address, x_fee_val, x_in, y_swapped; coin_in_val = Stdlib.Coin.value_(coin_in, $c, [$p[0]]); coin_out_val = get_amout_out_($.copy(coin_in_val), true, $c, [$p[0], $p[1]]); if (!($.copy(coin_out_val)).ge($.copy(coin_out_min_val))) { throw $.abortCode($.copy(ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM)); } pool_address = pool_address_($c); if (!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_DOES_NOT_EXIST)); } pool = $c.borrow_global_mut(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address)); Stdlib.Coin.merge_(pool.coin_x, coin_in, $c, [$p[0]]); y_swapped = Stdlib.Coin.extract_(pool.coin_y, $.copy(coin_out_val), $c, [$p[1]]); fee_multiplier = ($.copy(FEE_MULTIPLIER)).div(u64("5")); x_fee_val = (($.copy(coin_in_val)).mul($.copy(fee_multiplier))).div($.copy(FEE_SCALE)); x_in = Stdlib.Coin.extract_(pool.coin_x, $.copy(x_fee_val), $c, [$p[0]]); Stdlib.Coin.deposit_(beneficiary_($c), x_in, $c, [$p[0]]); Event.swapped_event_($.copy(pool_address), $.copy(coin_in_val), u64("0"), u64("0"), $.copy(coin_out_val), $c, [$p[0], $p[1]]); update_oracle_($.copy(pool_address), pool, $c, [$p[0], $p[1]]); return y_swapped; } export function swap_y_ ( coin_in: Stdlib.Coin.Coin, coin_out_min_val: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): Stdlib.Coin.Coin { let coin_in_val, coin_out_val, fee_multiplier, pool, pool_address, x_swapped, y_fee_val, y_in; coin_in_val = Stdlib.Coin.value_(coin_in, $c, [$p[1]]); coin_out_val = get_amout_out_($.copy(coin_in_val), false, $c, [$p[0], $p[1]]); if (!($.copy(coin_out_val)).ge($.copy(coin_out_min_val))) { throw $.abortCode($.copy(ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM)); } pool_address = pool_address_($c); if (!$c.exists(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address))) { throw $.abortCode($.copy(ERR_POOL_DOES_NOT_EXIST)); } pool = $c.borrow_global_mut(new SimpleStructTag(LiquidityPool, [$p[0], $p[1]]), $.copy(pool_address)); Stdlib.Coin.merge_(pool.coin_y, coin_in, $c, [$p[1]]); x_swapped = Stdlib.Coin.extract_(pool.coin_x, $.copy(coin_out_val), $c, [$p[0]]); fee_multiplier = ($.copy(FEE_MULTIPLIER)).div(u64("5")); y_fee_val = (($.copy(coin_in_val)).mul($.copy(fee_multiplier))).div($.copy(FEE_SCALE)); y_in = Stdlib.Coin.extract_(pool.coin_y, $.copy(y_fee_val), $c, [$p[1]]); Stdlib.Coin.deposit_(beneficiary_($c), y_in, $c, [$p[1]]); Event.swapped_event_($.copy(pool_address), u64("0"), $.copy(coin_out_val), $.copy(coin_in_val), u64("0"), $c, [$p[0], $p[1]]); update_oracle_($.copy(pool_address), pool, $c, [$p[0], $p[1]]); return x_swapped; } export function update_oracle_ ( pool_address: HexString, pool: LiquidityPool, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let temp$1, temp$2, block_timestamp, last_block_timestamp, time_elapsed, x_reserve, y_reserve; x_reserve = Stdlib.Coin.value_(pool.coin_x, $c, [$p[0]]); y_reserve = Stdlib.Coin.value_(pool.coin_y, $c, [$p[1]]); last_block_timestamp = $.copy(pool.timestamp); block_timestamp = Stdlib.Timestamp.now_seconds_($c); time_elapsed = ($.copy(block_timestamp)).sub($.copy(last_block_timestamp)); if (($.copy(time_elapsed)).gt(u64("0"))) { temp$1 = ($.copy(x_reserve)).neq(u64("0")); } else{ temp$1 = false; } if (temp$1) { temp$2 = ($.copy(y_reserve)).neq(u64("0")); } else{ temp$2 = false; } if (temp$2) { pool.x_cumulative = ((u128($.copy(time_elapsed))).mul(u128($.copy(x_reserve)))).div(u128($.copy(y_reserve))); pool.y_cumulative = ((u128($.copy(time_elapsed))).mul(u128($.copy(y_reserve)))).div(u128($.copy(x_reserve))); Event.update_oracle_event_($.copy(pool_address), $.copy(pool.x_cumulative), $.copy(pool.y_cumulative), $c, [$p[0], $p[1]]); } else{ } pool.timestamp = $.copy(block_timestamp); return; } export function withdraw_fee_ ( account: HexString, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let fee_account, fee_address, total; fee_account = fee_account_($c); fee_address = Stdlib.Signer.address_of_(fee_account, $c); total = Stdlib.Coin.balance_($.copy(fee_address), $c, [$p[0]]); Stdlib.Coin.transfer_(fee_account, $.copy(account), $.copy(total), $c, [$p[0]]); return Event.withdrew_event_(pool_address_($c), $.copy(total), $c, [$p[0]]); } export function loadParsers(repo: AptosParserRepo) { repo.addParser("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41::implements::Config", Config.ConfigParser); repo.addParser("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41::implements::LiquidityPool", LiquidityPool.LiquidityPoolParser); } export class App { constructor( public client: AptosClient, public repo: AptosParserRepo, public cache: AptosLocalCache, ) { } get moduleAddress() {{ return moduleAddress; }} get moduleName() {{ return moduleName; }} get Config() { return Config; } async loadConfig( owner: HexString, loadFull=true, fillCache=true, ) { const val = await Config.load(this.repo, this.client, owner, [] as TypeTag[]); if (loadFull) { await val.loadFullState(this); } if (fillCache) { this.cache.set(val.typeTag, owner, val); } return val; } get LiquidityPool() { return LiquidityPool; } async loadLiquidityPool( owner: HexString, $p: TypeTag[], /* */ loadFull=true, fillCache=true, ) { const val = await LiquidityPool.load(this.repo, this.client, owner, $p); if (loadFull) { await val.loadFullState(this); } if (fillCache) { this.cache.set(val.typeTag, owner, val); } return val; } }