From d541fe7294e7dd7693c29d7d4d11333680523a31 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Sat, 7 Oct 2023 08:53:18 +0300 Subject: [PATCH] Init Royalty Registry contract helpers --- contracts/royaltyRegistry/contract.ts | 397 ++++++++++++++++++++++++++ 1 file changed, 397 insertions(+) create mode 100644 contracts/royaltyRegistry/contract.ts diff --git a/contracts/royaltyRegistry/contract.ts b/contracts/royaltyRegistry/contract.ts new file mode 100644 index 0000000..c4688a5 --- /dev/null +++ b/contracts/royaltyRegistry/contract.ts @@ -0,0 +1,397 @@ +import type { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' +import type { Coin } from '@cosmjs/proto-signing' +import type { logs } from '@cosmjs/stargate' + +export interface InstantiateResponse { + readonly contractAddress: string + readonly transactionHash: string +} + +export interface MigrateResponse { + readonly transactionHash: string + readonly logs: readonly logs.Log[] +} + +export interface RoyaltyRegistryInstance { + readonly contractAddress: string + //Query + config: () => Promise + collectionRoyaltyDefault: (collection: string) => Promise + collectionRoyaltyProtocol: (collection: string, protocol: string) => Promise + // RoyaltyProtocolByCollection: (collection: string, queryOptions: QqueryOptions) => Promise + royaltyPayment: (collection: string, protocol?: string) => Promise + + //Execute + initializeCollectionRoyalty: (collection: string) => Promise + setCollectionRoyaltyDefault: (collection: string, recipient: string, share: number) => Promise + updateCollectionRoyaltyDefault: ( + collection: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => Promise + setCollectionRoyaltyProtocol: ( + collection: string, + protocol: string, + recipient: string, + share: number, + ) => Promise + updateCollectionRoyaltyProtocol: ( + collection: string, + protocol?: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => Promise +} + +export interface RoyaltyRegistryMessages { + initializeCollectionRoyalty: (collection: string) => InitializeCollectionRoyaltyMessage + setCollectionRoyaltyDefault: ( + collection: string, + recipient: string, + share: number, + ) => SetCollectionRoyaltyDefaultMessage + updateCollectionRoyaltyDefault: ( + collection: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => UpdateCollectionRoyaltyDefaultMessage + setCollectionRoyaltyProtocol: ( + collection: string, + protocol: string, + recipient: string, + share: number, + ) => SetCollectionRoyaltyProtocolMessage + updateCollectionRoyaltyProtocol: ( + collection: string, + protocol?: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => UpdateCollectionRoyaltyProtocolMessage +} + +export interface InitializeCollectionRoyaltyMessage { + sender: string + contract: string + msg: { + initialize_collection_royalty: { collection: string } + } + funds: Coin[] +} + +export interface SetCollectionRoyaltyDefaultMessage { + sender: string + contract: string + msg: { + set_collection_royalty_default: { collection: string; recipient: string; share: number } + } + funds: Coin[] +} + +export interface UpdateCollectionRoyaltyDefaultMessage { + sender: string + contract: string + msg: { + update_collection_royalty_default: { + collection: string + recipient?: string + share_delta?: number + decrement?: boolean + } + } + funds: Coin[] +} + +export interface SetCollectionRoyaltyProtocolMessage { + sender: string + contract: string + msg: { + set_collection_royalty_protocol: { + collection: string + protocol: string + recipient: string + share: number + } + } + funds: Coin[] +} + +export interface UpdateCollectionRoyaltyProtocolMessage { + sender: string + contract: string + msg: { + update_collection_royalty_protocol: { + collection: string + protocol?: string + recipient?: string + share_delta?: number + decrement?: boolean + } + } + funds: Coin[] +} + +export interface RoyaltyRegistryContract { + instantiate: ( + codeId: number, + initMsg: Record, + label: string, + admin?: string, + ) => Promise + + use: (contractAddress: string) => RoyaltyRegistryInstance + + migrate: ( + senderAddress: string, + contractAddress: string, + codeId: number, + migrateMsg: Record, + ) => Promise + + messages: (contractAddress: string) => RoyaltyRegistryMessages +} + +export const RoyaltyRegistry = (client: SigningCosmWasmClient, txSigner: string): RoyaltyRegistryContract => { + const use = (contractAddress: string): RoyaltyRegistryInstance => { + ///QUERY + const config = async (): Promise => { + return client.queryContractSmart(contractAddress, { + config: {}, + }) + } + + const collectionRoyaltyDefault = async (collection: string): Promise => { + return client.queryContractSmart(contractAddress, { + collection_royalty_default: { collection }, + }) + } + + const collectionRoyaltyProtocol = async (collection: string, protocol: string): Promise => { + return client.queryContractSmart(contractAddress, { + collection_royalty_protocol: { collection, protocol }, + }) + } + + const royaltyPayment = async (collection: string, protocol?: string): Promise => { + return client.queryContractSmart(contractAddress, { + royalty_payment: { collection, protocol }, + }) + } + + /// EXECUTE + const initializeCollectionRoyalty = async (collection: string): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + initialize_collection_royalty: { collection }, + }, + 'auto', + ) + return res.transactionHash + } + + const setCollectionRoyaltyDefault = async ( + collection: string, + recipient: string, + share: number, + ): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + set_collection_royalty_default: { collection, recipient, share }, + }, + 'auto', + ) + return res.transactionHash + } + + const updateCollectionRoyaltyDefault = async ( + collection: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + update_collection_royalty_default: { collection, recipient, share_delta: shareDelta, decrement }, + }, + 'auto', + ) + return res.transactionHash + } + + const setCollectionRoyaltyProtocol = async ( + collection: string, + protocol: string, + recipient: string, + share: number, + ): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + set_collection_royalty_protocol: { collection, protocol, recipient, share }, + }, + 'auto', + ) + return res.transactionHash + } + + const updateCollectionRoyaltyProtocol = async ( + collection: string, + protocol?: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + update_collection_royalty_protocol: { + collection, + protocol, + recipient, + share_delta: shareDelta, + decrement, + }, + }, + 'auto', + ) + return res.transactionHash + } + + return { + contractAddress, + config, + collectionRoyaltyDefault, + collectionRoyaltyProtocol, + royaltyPayment, + initializeCollectionRoyalty, + setCollectionRoyaltyDefault, + updateCollectionRoyaltyDefault, + setCollectionRoyaltyProtocol, + updateCollectionRoyaltyProtocol, + } + } + + const instantiate = async ( + codeId: number, + initMsg: Record, + label: string, + admin?: string, + ): Promise => { + const result = await client.instantiate(txSigner, codeId, initMsg, label, 'auto', { + admin, + }) + + return { + contractAddress: result.contractAddress, + transactionHash: result.transactionHash, + } + } + + const migrate = async ( + senderAddress: string, + contractAddress: string, + codeId: number, + migrateMsg: Record, + ): Promise => { + const result = await client.migrate(senderAddress, contractAddress, codeId, migrateMsg, 'auto') + return { + transactionHash: result.transactionHash, + logs: result.logs, + } + } + + const messages = (contractAddress: string) => { + const initializeCollectionRoyalty = (collection: string) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + initialize_collection_royalty: { collection }, + }, + funds: [], + } + } + + const setCollectionRoyaltyDefault = (collection: string, recipient: string, share: number) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + set_collection_royalty_default: { collection, recipient, share }, + }, + funds: [], + } + } + + const updateCollectionRoyaltyDefault = ( + collection: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + update_collection_royalty_default: { collection, recipient, share_delta: shareDelta, decrement }, + }, + funds: [], + } + } + + const setCollectionRoyaltyProtocol = (collection: string, protocol: string, recipient: string, share: number) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + set_collection_royalty_protocol: { collection, protocol, recipient, share }, + }, + funds: [], + } + } + + const updateCollectionRoyaltyProtocol = ( + collection: string, + protocol?: string, + recipient?: string, + shareDelta?: number, + decrement?: boolean, + ) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + update_collection_royalty_protocol: { + collection, + protocol, + recipient, + share_delta: shareDelta, + decrement, + }, + }, + funds: [], + } + } + + return { + initializeCollectionRoyalty, + setCollectionRoyaltyDefault, + updateCollectionRoyaltyDefault, + setCollectionRoyaltyProtocol, + updateCollectionRoyaltyProtocol, + } + } + + return { use, instantiate, migrate, messages } +}