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"; export const packageName = "init"; export const moduleAddress = new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"); export const moduleName = "init"; export const ERR_NOT_ENOUGH_PERMISSIONS : U64 = u64("305"); export class CapabilityStorage { static moduleAddress = moduleAddress; static moduleName = moduleName; __app: $.AppType | null = null; static structName: string = "CapabilityStorage"; static typeParameters: TypeParamDeclType[] = [ ]; static fields: FieldDeclType[] = [ { name: "signer_cap", typeTag: new StructTag(new HexString("0x1"), "account", "SignerCapability", []) }]; signer_cap: Stdlib.Account.SignerCapability; constructor(proto: any, public typeTag: TypeTag) { this.signer_cap = proto['signer_cap'] as Stdlib.Account.SignerCapability; } static CapabilityStorageParser(data:any, typeTag: TypeTag, repo: AptosParserRepo) : CapabilityStorage { const proto = $.parseStructProto(data, typeTag, repo, CapabilityStorage); return new CapabilityStorage(proto, typeTag); } static async load(repo: AptosParserRepo, client: AptosClient, address: HexString, typeParams: TypeTag[]) { const result = await repo.loadResource(client, address, CapabilityStorage, typeParams); return result as unknown as CapabilityStorage; } static async loadByApp(app: $.AppType, address: HexString, typeParams: TypeTag[]) { const result = await app.repo.loadResource(app.client, address, CapabilityStorage, typeParams); await result.loadFullState(app) return result as unknown as CapabilityStorage; } static getTag(): StructTag { return new StructTag(moduleAddress, moduleName, "CapabilityStorage", []); } async loadFullState(app: $.AppType) { await this.signer_cap.loadFullState(app); this.__app = app; } } export function initialize_swap_ ( swap_admin: HexString, metadata: U8[], code: U8[], $c: AptosDataCache, ): void { let lp_acc, signer_cap; if (!((Stdlib.Signer.address_of_(swap_admin, $c)).hex() === (new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")).hex())) { throw $.abortCode($.copy(ERR_NOT_ENOUGH_PERMISSIONS)); } [lp_acc, signer_cap] = Stdlib.Account.create_resource_account_(swap_admin, [u8("115"), u8("119"), u8("97"), u8("112"), 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); Stdlib.Debug.print_(lp_acc, $c, [AtomicTypeTag.Signer]); Stdlib.Code.publish_package_txn_(lp_acc, $.copy(metadata), [$.copy(code)], $c); $c.move_to(new SimpleStructTag(CapabilityStorage), swap_admin, new CapabilityStorage({ signer_cap: signer_cap }, new SimpleStructTag(CapabilityStorage))); return; } export function buildPayload_initialize_swap ( metadata: U8[], code: U8[], isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { const typeParamStrings = [] as string[]; return $.buildPayload( new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41"), "init", "initialize_swap", typeParamStrings, [ metadata, code, ], isJSON, ); } export function retrieve_signer_cap_ ( swap_admin: HexString, $c: AptosDataCache, ): Stdlib.Account.SignerCapability { if (!((Stdlib.Signer.address_of_(swap_admin, $c)).hex() === (new HexString("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41")).hex())) { throw $.abortCode($.copy(ERR_NOT_ENOUGH_PERMISSIONS)); } let { signer_cap: signer_cap } = $c.move_from(new SimpleStructTag(CapabilityStorage), Stdlib.Signer.address_of_(swap_admin, $c)); return signer_cap; } export function loadParsers(repo: AptosParserRepo) { repo.addParser("0x870723e9a8f6d07c350e79d63655de673fb24d0695c702f479c201ab7b055f41::init::CapabilityStorage", CapabilityStorage.CapabilityStorageParser); } export class App { constructor( public client: AptosClient, public repo: AptosParserRepo, public cache: AptosLocalCache, ) { } get moduleAddress() {{ return moduleAddress; }} get moduleName() {{ return moduleName; }} get CapabilityStorage() { return CapabilityStorage; } async loadCapabilityStorage( owner: HexString, loadFull=true, fillCache=true, ) { const val = await CapabilityStorage.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; } payload_initialize_swap( metadata: U8[], code: U8[], isJSON = false, ): TxnBuilderTypes.TransactionPayloadEntryFunction | Types.TransactionPayload_EntryFunctionPayload { return buildPayload_initialize_swap(metadata, code, isJSON); } async initialize_swap( _account: AptosAccount, metadata: U8[], code: U8[], option?: OptionTransaction, _isJSON = false ) { const payload__ = buildPayload_initialize_swap(metadata, code, _isJSON); return $.sendPayloadTx(this.client, _account, payload__, option); } }