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 Controller from "./controller"; import * as Implements from "./implements"; export const packageName = "swap"; export const moduleAddress = new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"); export const moduleName = "interface"; export const ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM : U64 = u64("106"); export const ERR_EMERGENCY : U64 = u64("102"); export const ERR_INSUFFICIENT_X_AMOUNT : U64 = u64("103"); export const ERR_INSUFFICIENT_Y_AMOUNT : U64 = u64("104"); export const ERR_MUST_BE_ORDER : U64 = u64("105"); export const ERR_NOT_COIN : U64 = u64("100"); export const ERR_THE_SAME_COIN : U64 = u64("101"); export function add_liquidity_ ( account: HexString, coin_x_val: U64, coin_x_val_min: U64, coin_y_val: U64, coin_y_val_min: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let account_addr, coin_x, coin_x_opt, coin_y, coin_y_opt, lp_coins, optimal_x, optimal_y; if (!!Controller.is_emergency_($c)) { throw $.abortCode($.copy(ERR_EMERGENCY)); } if (!($.copy(coin_x_val)).ge($.copy(coin_x_val_min))) { throw $.abortCode($.copy(ERR_INSUFFICIENT_X_AMOUNT)); } if (!($.copy(coin_y_val)).ge($.copy(coin_y_val_min))) { throw $.abortCode($.copy(ERR_INSUFFICIENT_Y_AMOUNT)); } if (!is_order_($c, [$p[0], $p[1]])) { throw $.abortCode($.copy(ERR_MUST_BE_ORDER)); } coin_x = Stdlib.Coin.withdraw_(account, $.copy(coin_x_val), $c, [$p[0]]); coin_y = Stdlib.Coin.withdraw_(account, $.copy(coin_y_val), $c, [$p[1]]); account_addr = Stdlib.Signer.address_of_(account, $c); if (!Stdlib.Coin.is_account_registered_($.copy(account_addr), $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])])) { Stdlib.Coin.register_(account, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); } else{ } [optimal_x, optimal_y] = calc_optimal_coin_values_($.copy(coin_x_val), $.copy(coin_y_val), $c, [$p[0], $p[1]]); coin_x_opt = Stdlib.Coin.extract_(coin_x, $.copy(optimal_x), $c, [$p[0]]); coin_y_opt = Stdlib.Coin.extract_(coin_y, $.copy(optimal_y), $c, [$p[1]]); lp_coins = Implements.mint_(coin_x_opt, coin_y_opt, $c, [$p[0], $p[1]]); Stdlib.Coin.deposit_($.copy(account_addr), lp_coins, $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); Stdlib.Coin.deposit_($.copy(account_addr), coin_x, $c, [$p[0]]); Stdlib.Coin.deposit_($.copy(account_addr), coin_y, $c, [$p[1]]); return; } export function buildPayload_add_liquidity ( coin_x_val: U64, coin_x_val_min: U64, coin_y_val: U64, coin_y_val_min: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = $p.map(t=>$.getTypeTagFullname(t)); return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "interface", "add_liquidity", typeParamStrings, [ coin_x_val, coin_x_val_min, coin_y_val, coin_y_val_min, ], isJSON, ); } export function calc_optimal_coin_values_ ( x_desired: U64, y_desired: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): [U64, U64] { let temp$1, reserves_x, reserves_y, x_returned, y_returned; [reserves_x, reserves_y] = Implements.get_reserves_size_($c, [$p[0], $p[1]]); if (($.copy(reserves_x)).eq((u64("0")))) { temp$1 = ($.copy(reserves_y)).eq((u64("0"))); } else{ temp$1 = false; } if (temp$1) { return [$.copy(x_desired), $.copy(y_desired)]; } else{ y_returned = (($.copy(reserves_y)).div($.copy(reserves_x))).mul($.copy(x_desired)); if (($.copy(y_returned)).le($.copy(y_desired))) { return [$.copy(x_desired), $.copy(y_returned)]; } else{ x_returned = (($.copy(reserves_x)).div($.copy(reserves_y))).mul($.copy(y_desired)); return [$.copy(x_returned), $.copy(y_desired)]; } } } export function compare_ ( $c: AptosDataCache, $p: TypeTag[], /* */ ): Stdlib.Comparator.Result { let temp$1, temp$2, temp$3, temp$4, comp, x_address, x_compare, x_info, y_address, y_compare, y_info; x_info = Stdlib.Type_info.type_of_($c, [$p[0]]); temp$1 = Stdlib.Type_info.struct_name_(x_info, $c); x_compare = temp$1; Stdlib.Vector.append_(x_compare, Stdlib.Type_info.module_name_(x_info, $c), $c, [AtomicTypeTag.U8]); y_info = Stdlib.Type_info.type_of_($c, [$p[1]]); temp$2 = Stdlib.Type_info.struct_name_(y_info, $c); y_compare = temp$2; Stdlib.Vector.append_(y_compare, Stdlib.Type_info.module_name_(y_info, $c), $c, [AtomicTypeTag.U8]); [temp$3, temp$4] = [x_compare, y_compare]; comp = Stdlib.Comparator.compare_(temp$3, temp$4, $c, [new VectorTag(AtomicTypeTag.U8)]); if (!Stdlib.Comparator.is_equal_(comp, $c)) { return comp; } else{ } x_address = Stdlib.Type_info.account_address_(x_info, $c); y_address = Stdlib.Type_info.account_address_(y_info, $c); return Stdlib.Comparator.compare_(x_address, y_address, $c, [AtomicTypeTag.Address]); } export function initialize_swap_ ( swap_admin: HexString, controller: HexString, beneficiary: HexString, $c: AptosDataCache, ): void { Implements.initialize_swap_(swap_admin, $.copy(controller), $.copy(beneficiary), $c); return; } export function buildPayload_initialize_swap ( controller: HexString, beneficiary: HexString, isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = [] as string[]; return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "interface", "initialize_swap", typeParamStrings, [ controller, beneficiary, ], isJSON, ); } export function is_order_ ( $c: AptosDataCache, $p: TypeTag[], /* */ ): boolean { let temp$1, comp; comp = compare_($c, [$p[0], $p[1]]); if (!!Stdlib.Comparator.is_equal_(comp, $c)) { throw $.abortCode($.copy(ERR_THE_SAME_COIN)); } if (Stdlib.Comparator.is_smaller_than_(comp, $c)) { temp$1 = true; } else{ temp$1 = false; } return temp$1; } export function register_pool_ ( account: HexString, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { if (!!Controller.is_emergency_($c)) { throw $.abortCode($.copy(ERR_EMERGENCY)); } if (!Stdlib.Coin.is_coin_initialized_($c, [$p[0]])) { throw $.abortCode($.copy(ERR_NOT_COIN)); } if (!Stdlib.Coin.is_coin_initialized_($c, [$p[1]])) { throw $.abortCode($.copy(ERR_NOT_COIN)); } if (is_order_($c, [$p[0], $p[1]])) { Implements.register_pool_(account, $c, [$p[0], $p[1]]); } else{ Implements.register_pool_(account, $c, [$p[1], $p[0]]); } return; } export function buildPayload_register_pool ( $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = $p.map(t=>$.getTypeTagFullname(t)); return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "interface", "register_pool", typeParamStrings, [], isJSON, ); } export function remove_liquidity_ ( account: HexString, lp_val: U64, min_x_out_val: U64, min_y_out_val: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let account_addr, coin_x, coin_y, lp_coins; if (!!Controller.is_emergency_($c)) { throw $.abortCode($.copy(ERR_EMERGENCY)); } if (!is_order_($c, [$p[0], $p[1]])) { throw $.abortCode($.copy(ERR_MUST_BE_ORDER)); } lp_coins = Stdlib.Coin.withdraw_(account, $.copy(lp_val), $c, [new StructTag(new HexString("0x2fe3016dd04ef69d5a440b5879a1cc8f1ea4297f6eeaceffc92d0161288a5045"), "lp_coin", "LP", [$p[0], $p[1]])]); [coin_x, coin_y] = Implements.burn_(lp_coins, $c, [$p[0], $p[1]]); if (!(Stdlib.Coin.value_(coin_x, $c, [$p[0]])).ge($.copy(min_x_out_val))) { throw $.abortCode($.copy(ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM)); } if (!(Stdlib.Coin.value_(coin_y, $c, [$p[1]])).ge($.copy(min_y_out_val))) { throw $.abortCode($.copy(ERR_COIN_OUT_NUM_LESS_THAN_EXPECTED_MINIMUM)); } account_addr = Stdlib.Signer.address_of_(account, $c); Stdlib.Coin.deposit_($.copy(account_addr), coin_x, $c, [$p[0]]); Stdlib.Coin.deposit_($.copy(account_addr), coin_y, $c, [$p[1]]); return; } export function buildPayload_remove_liquidity ( lp_val: U64, min_x_out_val: U64, min_y_out_val: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = $p.map(t=>$.getTypeTagFullname(t)); return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "interface", "remove_liquidity", typeParamStrings, [ lp_val, min_x_out_val, min_y_out_val, ], isJSON, ); } export function swap_ ( account: HexString, coin_val: U64, coin_out_min_val: U64, $c: AptosDataCache, $p: TypeTag[], /* */ ): void { let account_addr, coin_x, coin_y; if (!!Controller.is_emergency_($c)) { throw $.abortCode($.copy(ERR_EMERGENCY)); } coin_x = Stdlib.Coin.withdraw_(account, $.copy(coin_val), $c, [$p[0]]); if (is_order_($c, [$p[0], $p[1]])) { coin_y = Implements.swap_x_(coin_x, $.copy(coin_out_min_val), $c, [$p[0], $p[1]]); } else{ coin_y = Implements.swap_y_(coin_x, $.copy(coin_out_min_val), $c, [$p[1], $p[0]]); } account_addr = Stdlib.Signer.address_of_(account, $c); Stdlib.Coin.deposit_($.copy(account_addr), coin_y, $c, [$p[1]]); return; } export function buildPayload_swap ( coin_val: U64, coin_out_min_val: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = $p.map(t=>$.getTypeTagFullname(t)); return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "interface", "swap", typeParamStrings, [ coin_val, coin_out_min_val, ], isJSON, ); } export function loadParsers(repo: AptosParserRepo) { } export class App { constructor( public client: AptosClient, public repo: AptosParserRepo, public cache: AptosLocalCache, ) { } get moduleAddress() {{ return moduleAddress; }} get moduleName() {{ return moduleName; }} payload_add_liquidity( coin_x_val: U64, coin_x_val_min: U64, coin_y_val: U64, coin_y_val_min: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_add_liquidity(coin_x_val, coin_x_val_min, coin_y_val, coin_y_val_min, $p, isJSON); } async add_liquidity( _account: AptosAccount, coin_x_val: U64, coin_x_val_min: U64, coin_y_val: U64, coin_y_val_min: U64, $p: TypeTag[], /* */ option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_add_liquidity(coin_x_val, coin_x_val_min, coin_y_val, coin_y_val_min, $p, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } payload_initialize_swap( controller: HexString, beneficiary: HexString, isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_initialize_swap(controller, beneficiary, isJSON); } async initialize_swap( _account: AptosAccount, controller: HexString, beneficiary: HexString, option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_initialize_swap(controller, beneficiary, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } payload_register_pool( $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_register_pool($p, isJSON); } async register_pool( _account: AptosAccount, $p: TypeTag[], /* */ option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_register_pool($p, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } payload_remove_liquidity( lp_val: U64, min_x_out_val: U64, min_y_out_val: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_remove_liquidity(lp_val, min_x_out_val, min_y_out_val, $p, isJSON); } async remove_liquidity( _account: AptosAccount, lp_val: U64, min_x_out_val: U64, min_y_out_val: U64, $p: TypeTag[], /* */ option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_remove_liquidity(lp_val, min_x_out_val, min_y_out_val, $p, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } payload_swap( coin_val: U64, coin_out_min_val: U64, $p: TypeTag[], /* */ isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_swap(coin_val, coin_out_min_val, $p, isJSON); } async swap( _account: AptosAccount, coin_val: U64, coin_out_min_val: U64, $p: TypeTag[], /* */ option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_swap(coin_val, coin_out_min_val, $p, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } }