UNPKG

@mysten/suins

Version:
145 lines (143 loc) 4.29 kB
import { MoveStruct, MoveTuple, normalizeMoveArguments } from "../utils/index.mjs"; import { TypeName } from "./deps/std/type_name.mjs"; import { VecMap } from "./deps/sui/vec_map.mjs"; import { bcs } from "@mysten/sui/bcs"; //#region src/contracts/suins_payments/payments.ts /************************************************************** * THIS FILE IS GENERATED AND SHOULD NOT BE MANUALLY MODIFIED * **************************************************************/ const $moduleName = "@suins/payments::payments"; const PaymentsApp = new MoveTuple({ name: `${$moduleName}::PaymentsApp`, fields: [bcs.bool()] }); const CoinTypeData = new MoveStruct({ name: `${$moduleName}::CoinTypeData`, fields: { decimals: bcs.u8(), discount_percentage: bcs.u8(), price_feed_id: bcs.vector(bcs.u8()), type_name: TypeName } }); const PaymentsConfig = new MoveStruct({ name: `${$moduleName}::PaymentsConfig`, fields: { currencies: VecMap(TypeName, CoinTypeData), base_currency: TypeName, max_age: bcs.u64(), burn_bps: bcs.u64() } }); /** * This has to be called with our base payment currency. The payment has to be * equal to the base price of the domain. We do not need to check the price feed * for the base currency. */ function handleBasePayment(options) { const packageAddress = options.package ?? "@suins/payments"; const argumentsTypes = [ null, null, null, null ]; const parameterNames = [ "suins", "bbbVault", "intent", "payment" ]; return (tx) => tx.moveCall({ package: packageAddress, module: "payments", function: "handle_base_payment", arguments: normalizeMoveArguments(options.arguments, argumentsTypes, parameterNames), typeArguments: options.typeArguments }); } /** * Handles a payment done for a non-base currency payment. E.g. SUI, NS. * * The payment amount is derived from the base currency price and the Pyth price * feed. * * The `user_price_guard` is a value that the user expects to pay. If the payment * amount is higher than this value, the payment will be rejected. This is to * protect the user from paying more than they expected on their FEs. Ideally, this * number should be calculated on the FE based on the price that is being displayed * to the user (with a buffer determined by the FE). */ function handlePayment(options) { const packageAddress = options.package ?? "@suins/payments"; const argumentsTypes = [ null, null, null, null, "0x2::clock::Clock", null, "u64" ]; const parameterNames = [ "suins", "bbbVault", "intent", "payment", "priceInfoObject", "userPriceGuard" ]; return (tx) => tx.moveCall({ package: packageAddress, module: "payments", function: "handle_payment", arguments: normalizeMoveArguments(options.arguments, argumentsTypes, parameterNames), typeArguments: options.typeArguments }); } /** * Calculates the amount that has to be paid in the target currency. * * Can be used to split the payment amount in a single PTB. * * 1. const intent = function_to_get_intent(); * 2. const price = calculate_price<SUI>(suins, intent, ...); * 3. const coin = txb.splitCoins(baseCoin, [price]) * 4. handle_payment<SUI>(suins, intent, coin, ...); */ function calculatePrice(options) { const packageAddress = options.package ?? "@suins/payments"; const argumentsTypes = [ null, "u64", "0x2::clock::Clock", null ]; const parameterNames = [ "suins", "baseAmount", "priceInfoObject" ]; return (tx) => tx.moveCall({ package: packageAddress, module: "payments", function: "calculate_price", arguments: normalizeMoveArguments(options.arguments, argumentsTypes, parameterNames), typeArguments: options.typeArguments }); } function calculatePriceAfterDiscount(options) { const packageAddress = options.package ?? "@suins/payments"; const argumentsTypes = [null, null]; const parameterNames = ["suins", "intent"]; return (tx) => tx.moveCall({ package: packageAddress, module: "payments", function: "calculate_price_after_discount", arguments: normalizeMoveArguments(options.arguments, argumentsTypes, parameterNames), typeArguments: options.typeArguments }); } //#endregion export { PaymentsConfig, calculatePrice, calculatePriceAfterDiscount, handleBasePayment, handlePayment }; //# sourceMappingURL=payments.mjs.map