import { AssetStandard, bytesToHex, Hex, hexToBytes, LockType, OpType, trimHexPrefix } from "js-moi-utils"; import { MAS2 } from "./mas2"; import { documentEncode, Schema } from "js-polo"; import { APPROVE_SCHEMA, BALANCEOF_SCHEMA, BURN_SCHEMA, GET_DYNAMIC_METADATA_SCHEMA, GET_DYNAMIC_TOKEN_METADATA_SCHEMA, GET_STATIC_METADATA_SCHEMA, GET_STATIC_TOKEN_METADATA_SCHEMA, LOCKUP_SCHEMA, MINT_SCHEMA, MINT_WITH_METADATA_SCHEMA, RELEASE_SCHEMA, REVOKE_SCHEMA, SET_DYNAMIC_METADATA_SCHEMA, SET_STATIC_METADATA_SCHEMA, SET_STATIC_TOKEN_METADATA_SCHEMA, TRANSFER_FROM_SCHEMA, TRANSFER_SCHEMA } from "./mas2-schema"; import { Signer } from "js-moi-signer"; import { AssetCreatePayload, IxParticipant } from "js-moi-providers"; import { SARGA_ADDRESS } from "js-moi-constants"; import { InteractionContext } from "js-moi-interactions"; import { SET_DYNAMIC_TOKEN_METADATA_SCHEMA } from "./mas1-schema"; export class MAS2AssetLogic { assetId: string signer: Signer constructor(assetId: string, signer: Signer) { this.assetId = assetId; this.signer = signer; } private polorize(payload: T, schema: Schema): Uint8Array { const document = documentEncode(payload, schema) return document.bytes() } static async newAsset( signer: Signer, symbol: string, supply: number | bigint, manager: string, enableEvents: boolean, ): Promise { const response = await this.create(signer, symbol, supply, manager, enableEvents).send() const results = await response.result() return new MAS2AssetLogic(results[0].asset_id, signer) } static create( signer: Signer, symbol: string, supply: number | bigint, manager: string, enableEvents: boolean, ): InteractionContext { const payload: AssetCreatePayload = { symbol: symbol, max_supply: supply, standard: AssetStandard.MAS2, dimension: 0, enable_events: enableEvents, manager: manager as Hex, logic_payload: { manifest: "0x", callsite: "Init" } } return new InteractionContext({ opType: OpType.ASSET_CREATE, payload: payload, participants: [], signer: signer, }) } public transfer(tokenId: number | bigint, beneficiary: string, amount: number | bigint): InteractionContext { const payload: MAS2.Transfer = { token_id: tokenId, beneficiary: hexToBytes(beneficiary), amount: amount, } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, } ] const rawPayload = this.polorize(payload, TRANSFER_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.TRANSFER, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public transferFrom(tokenId: number | bigint, benefactor: string, beneficiary: string, amount: number | bigint): InteractionContext { const payload: MAS2.TransferFrom = { token_id: tokenId, benefactor: hexToBytes(benefactor), beneficiary: hexToBytes(beneficiary), amount: amount, } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: benefactor as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, } ] const rawPayload = this.polorize(payload, TRANSFER_FROM_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.TRANSFERFROM, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public mint(beneficiary: string, amount: number | bigint): InteractionContext { const payload: MAS2.Mint = { beneficiary: hexToBytes(beneficiary), amount: amount, } const participants: IxParticipant[] = [ { id: this.assetId as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, } ] const rawPayload = this.polorize(payload, MINT_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.MINT, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public mintWithMetadata(beneficiary: string, amount: number | bigint, staticMetadata: Record): InteractionContext { const payload: MAS2.MintWithMetadata = { beneficiary: hexToBytes(beneficiary), amount: amount, static_metadata: new Map(Object.entries(staticMetadata)) } const participants: IxParticipant[] = [ { id: this.assetId as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, } ] const rawPayload = this.polorize(payload, MINT_WITH_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.MINTWITHMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public burn(tokenId: number | bigint, amount: number | bigint): InteractionContext { const payload: MAS2.Burn = { token_id: tokenId, amount: amount, } const participants: IxParticipant[] = [ { id: this.assetId as Hex, lock_type: LockType.MUTATE_LOCK, } ] const rawPayload = this.polorize(payload, BURN_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.BURN, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public approve(tokenId: number | bigint, beneficiary: string, amount: number | bigint, expiresAt: number): InteractionContext { const payload: MAS2.Approve = { token_id: tokenId, beneficiary: hexToBytes(beneficiary), amount: amount, expires_at: expiresAt } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, } ] const rawPayload = this.polorize(payload, APPROVE_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.APPROVE, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public revoke(tokenId: number | bigint, beneficiary: string): InteractionContext { const payload: MAS2.Revoke = { token_id: tokenId, beneficiary: hexToBytes(beneficiary), } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, } ] const rawPayload = this.polorize(payload, REVOKE_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.REVOKE, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public lockup(tokenId: number | bigint, beneficiary: string, amount: number | bigint): InteractionContext { const payload: MAS2.Lockup = { token_id: tokenId, beneficiary: hexToBytes(beneficiary), amount: amount } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, }, { id: SARGA_ADDRESS as Hex, lock_type: LockType.MUTATE_LOCK } ] const rawPayload = this.polorize(payload, LOCKUP_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.LOCKUP, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }) } public release(tokenId: number | bigint, benefactor: string, beneficiary: string, amount: number | bigint): InteractionContext { const payload: MAS2.Release = { token_id: tokenId, benefactor: hexToBytes(benefactor), beneficiary: hexToBytes(beneficiary), amount: amount } const participants: IxParticipant[] = [ { id: beneficiary as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: benefactor as Hex, lock_type: LockType.MUTATE_LOCK, }, { id: this.assetId as Hex, lock_type: LockType.NO_LOCK, } ] const rawPayload = this.polorize(payload, RELEASE_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.RELEASE, calldata: bytesToHex(rawPayload) as Hex, }, participants: participants, signer: this.signer, }); } public SetStaticMetadata(key: string, value: Uint8Array) { const payload: MAS2.SetStaticMetadata = { key: key, value: value } const rawPayload = this.polorize(payload, SET_STATIC_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.SETSTATICMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public SetDynamicMetadata(key: string, value: Uint8Array) { const payload: MAS2.SetDynamicMetadata = { key: key, value: value } const rawPayload = this.polorize(payload, SET_DYNAMIC_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.SETDYNAMICMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public SetStaticTokenMetadata(tokenId: number | bigint, key: string, value: Uint8Array) { const payload: MAS2.SetStaticTokenMetadata = { token_id: tokenId, key: key, value: value } const rawPayload = this.polorize(payload, SET_STATIC_TOKEN_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.SETSTATICTOKENMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public SetDynamicTokenMetadata(tokenId: number | bigint, key: string, value: Uint8Array) { const payload: MAS2.SetDynamicTokenMetadata = { token_id: tokenId, key: key, value: value } const rawPayload = this.polorize(payload, SET_DYNAMIC_TOKEN_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.SETDYNAMICTOKENMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } // Readonly routines public symbol() { return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.SYMBOL, }, participants: [], signer: this.signer, }) } public balanceOf(tokenId: number | bigint, address: string) { const payload: MAS2.BalanceOf = { token_id: tokenId, address: hexToBytes(address) } const rawPayload = this.polorize(payload, BALANCEOF_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.BALANCEOF, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public creator() { return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.CREATOR, }, participants: [], signer: this.signer, }) } public manager() { return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.MANAGER, }, participants: [], signer: this.signer, }) } public GetStaticMetadata(key: string) { const payload: MAS2.GetStaticMetadata = { key: key, } const rawPayload = this.polorize(payload, GET_STATIC_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.GETSTATICMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public GetDynamicMetadata(key: string) { const payload: MAS2.GetDynamicMetadata = { key: key, } const rawPayload = this.polorize(payload, GET_DYNAMIC_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.GETDYNAMICMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public GetStaticTokenMetadata(tokenId: number | bigint, key: string) { const payload: MAS2.GetStaticTokenMetadata = { token_id: tokenId, key: key, } const rawPayload = this.polorize(payload, GET_STATIC_TOKEN_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.GETSTATICTOKENMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } public GetDynamicTokenMetadata(tokenId: number | bigint, key: string) { const payload: MAS2.GetDynamicTokenMetadata = { token_id: tokenId, key: key } const rawPayload = this.polorize(payload, GET_DYNAMIC_TOKEN_METADATA_SCHEMA) return new InteractionContext({ opType: OpType.ASSET_INVOKE, payload: { asset_id: trimHexPrefix(this.assetId) as Hex, callsite: MAS2.Endpoint.GETDYNAMICTOKENMETADATA, calldata: bytesToHex(rawPayload) as Hex, }, participants: [], signer: this.signer, }) } }