stargaze-studio/contracts/badgeHub/contract.ts

656 lines
15 KiB
TypeScript
Raw Normal View History

2023-02-01 12:55:52 +00:00
/* eslint-disable eslint-comments/disable-enable-pair */
2023-02-10 07:46:41 +00:00
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2023-02-01 12:55:52 +00:00
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable camelcase */
import type { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
2023-02-10 07:46:41 +00:00
import { toUtf8 } from '@cosmjs/encoding'
2023-01-31 17:31:06 +00:00
import type { Coin } from '@cosmjs/proto-signing'
import { coin } from '@cosmjs/proto-signing'
import type { logs } from '@cosmjs/stargate'
2023-02-10 07:46:41 +00:00
import sizeof from 'object-sizeof'
2023-01-31 17:31:06 +00:00
export interface InstantiateResponse {
readonly contractAddress: string
readonly transactionHash: string
readonly logs: readonly logs.Log[]
}
export interface MigrateResponse {
readonly transactionHash: string
readonly logs: readonly logs.Log[]
}
2023-02-01 12:55:52 +00:00
export interface Rule {
by_key?: string
by_minter?: string
by_keys?: string[]
}
export interface Trait {
display_type?: string
trait_type: string
value: string
}
export interface Metadata {
name?: string
image?: string
2023-02-02 13:39:00 +00:00
image_data?: string
2023-02-01 12:55:52 +00:00
external_url?: string
description?: string
attributes?: Trait[]
background_color?: string
animation_url?: string
youtube_url?: string
}
export interface Badge {
manager: string
metadata: Metadata
transferrable: boolean
rule: Rule
2023-02-04 16:18:41 +00:00
expiry?: number
2023-02-01 12:55:52 +00:00
max_supply?: number
}
2023-01-31 17:31:06 +00:00
export interface BadgeHubInstance {
readonly contractAddress: string
//Query
getConfig: () => Promise<any>
2023-02-01 12:55:52 +00:00
getBadge: (id: number) => Promise<any>
getBadges: (start_after?: number, limit?: number) => Promise<any>
getKey: (id: number, pubkey: string) => Promise<any>
2023-02-14 08:26:22 +00:00
getKeys: (id: number, start_after?: string, limit?: number) => Promise<any>
2023-01-31 17:31:06 +00:00
//Execute
2023-02-01 12:55:52 +00:00
createBadge: (senderAddress: string, badge: Badge) => Promise<string>
editBadge: (senderAddress: string, id: number, metadata: Metadata) => Promise<string>
addKeys: (senderAddress: string, id: number, keys: string[]) => Promise<string>
purgeKeys: (senderAddress: string, id: number, limit?: number) => Promise<string>
purgeOwners: (senderAddress: string, id: number, limit?: number) => Promise<string>
mintByMinter: (senderAddress: string, id: number, owners: string[]) => Promise<string>
mintByKey: (senderAddress: string, id: number, owner: string, signature: string) => Promise<string>
mintByKeys: (senderAddress: string, id: number, owner: string, pubkey: string, signature: string) => Promise<string>
setNft: (senderAddress: string, nft: string) => Promise<string>
2023-01-31 17:31:06 +00:00
}
export interface BadgeHubMessages {
2023-02-01 12:55:52 +00:00
createBadge: (badge: Badge) => CreateBadgeMessage
editBadge: (id: number, metadata: Metadata) => EditBadgeMessage
addKeys: (id: number, keys: string[]) => AddKeysMessage
purgeKeys: (id: number, limit?: number) => PurgeKeysMessage
purgeOwners: (id: number, limit?: number) => PurgeOwnersMessage
mintByMinter: (id: number, owners: string[]) => MintByMinterMessage
mintByKey: (id: number, owner: string, signature: string) => MintByKeyMessage
mintByKeys: (id: number, owner: string, pubkey: string, signature: string) => MintByKeysMessage
setNft: (nft: string) => SetNftMessage
2023-01-31 17:31:06 +00:00
}
2023-02-01 12:55:52 +00:00
export interface CreateBadgeMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
create_badge: {
manager: string
metadata: Metadata
transferrable: boolean
rule: Rule
2023-02-04 16:18:41 +00:00
expiry?: number
2023-02-01 12:55:52 +00:00
max_supply?: number
2023-01-31 17:31:06 +00:00
}
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface EditBadgeMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
edit_badge: {
id: number
metadata: Metadata
2023-01-31 17:31:06 +00:00
}
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface AddKeysMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
add_keys: {
id: number
keys: string[]
}
2023-01-31 17:31:06 +00:00
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface PurgeKeysMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
purge_keys: {
id: number
limit?: number
2023-01-31 17:31:06 +00:00
}
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface PurgeOwnersMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
purge_owners: {
id: number
limit?: number
2023-01-31 17:31:06 +00:00
}
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface MintByMinterMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
mint_by_minter: {
id: number
owners: string[]
2023-01-31 17:31:06 +00:00
}
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface MintByKeyMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
mint_by_key: {
id: number
owner: string
signature: string
}
2023-01-31 17:31:06 +00:00
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface MintByKeysMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
mint_by_keys: {
id: number
owner: string
pubkey: string
signature: string
}
2023-01-31 17:31:06 +00:00
}
funds: Coin[]
}
2023-02-01 12:55:52 +00:00
export interface SetNftMessage {
2023-01-31 17:31:06 +00:00
sender: string
contract: string
msg: {
2023-02-01 12:55:52 +00:00
set_nft: {
nft: string
}
2023-01-31 17:31:06 +00:00
}
funds: Coin[]
}
export interface BadgeHubContract {
instantiate: (
senderAddress: string,
codeId: number,
initMsg: Record<string, unknown>,
label: string,
admin?: string,
funds?: Coin[],
) => Promise<InstantiateResponse>
migrate: (
senderAddress: string,
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
) => Promise<MigrateResponse>
use: (contractAddress: string) => BadgeHubInstance
messages: (contractAddress: string) => BadgeHubMessages
}
export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): BadgeHubContract => {
const use = (contractAddress: string): BadgeHubInstance => {
//Query
const getConfig = async (): Promise<any> => {
const res = await client.queryContractSmart(contractAddress, {
config: {},
})
return res
}
2023-02-01 12:55:52 +00:00
const getBadge = async (id: number): Promise<any> => {
2023-01-31 17:31:06 +00:00
const res = await client.queryContractSmart(contractAddress, {
2023-02-01 12:55:52 +00:00
badge: { id },
2023-01-31 17:31:06 +00:00
})
return res
}
2023-02-01 12:55:52 +00:00
const getBadges = async (start_after?: number, limit?: number): Promise<any> => {
2023-01-31 17:31:06 +00:00
const res = await client.queryContractSmart(contractAddress, {
2023-02-01 12:55:52 +00:00
badges: { start_after, limit },
2023-01-31 17:31:06 +00:00
})
return res
}
2023-02-14 08:26:22 +00:00
const getKey = async (id: number, pubkey: string): Promise<any> => {
2023-01-31 17:31:06 +00:00
const res = await client.queryContractSmart(contractAddress, {
2023-02-14 08:26:22 +00:00
key: { id, pubkey },
2023-01-31 17:31:06 +00:00
})
return res
}
2023-02-14 08:26:22 +00:00
const getKeys = async (id: number, start_after?: string, limit?: number): Promise<any> => {
2023-01-31 17:31:06 +00:00
const res = await client.queryContractSmart(contractAddress, {
2023-02-01 12:55:52 +00:00
keys: { id, start_after, limit },
2023-01-31 17:31:06 +00:00
})
return res
}
//Execute
2023-02-01 12:55:52 +00:00
const createBadge = async (senderAddress: string, badge: Badge): Promise<string> => {
2023-02-10 07:46:41 +00:00
const feeRateRaw = await client.queryContractRaw(
contractAddress,
toUtf8(Buffer.from(Buffer.from('fee_rate').toString('hex'), 'hex').toString()),
)
2023-02-13 16:58:45 +00:00
console.log('Fee Rate Raw: ', feeRateRaw)
2023-02-10 07:46:41 +00:00
const feeRate = JSON.parse(new TextDecoder().decode(feeRateRaw as Uint8Array))
console.log('Fee Rate:', feeRate)
console.log('badge size: ', sizeof(badge))
console.log('metadata size', sizeof(badge.metadata))
console.log('size of attributes ', sizeof(badge.metadata.attributes))
console.log('Total: ', Number(sizeof(badge)) + Number(sizeof(badge.metadata.attributes)))
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
create_badge: {
manager: badge.manager,
metadata: badge.metadata,
transferrable: badge.transferrable,
rule: badge.rule,
expiry: badge.expiry,
max_supply: badge.max_supply,
2023-01-31 17:31:06 +00:00
},
},
'auto',
'',
2023-02-10 07:46:41 +00:00
[
coin(
(Number(sizeof(badge)) + Number(sizeof(badge.metadata.attributes))) * Number(feeRate.metadata),
'ustars',
),
],
//[coin(1, 'ustars')],
2023-01-31 17:31:06 +00:00
)
2023-02-10 07:46:41 +00:00
const events = res.logs
.map((log) => log.events)
.flat()
.find(
(event) =>
event.attributes.findIndex((attr) => attr.key === 'action' && attr.value === 'badges/hub/create_badge') > 0,
)!
const id = Number(events.attributes.find((attr) => attr.key === 'id')!.value)
return res.transactionHash.concat(`:${id}`)
2023-01-31 17:31:06 +00:00
}
2023-02-01 12:55:52 +00:00
const editBadge = async (senderAddress: string, id: number, metadata: Metadata): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
edit_badge: {
id,
metadata,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const addKeys = async (senderAddress: string, id: number, keys: string[]): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
add_keys: {
id,
keys,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const purgeKeys = async (senderAddress: string, id: number, limit?: number): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
purge_keys: {
id,
limit,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const purgeOwners = async (senderAddress: string, id: number, limit?: number): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
purge_owners: {
id,
limit,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const mintByMinter = async (senderAddress: string, id: number, owners: string[]): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
mint_by_minter: {
id,
owners,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const mintByKey = async (senderAddress: string, id: number, owner: string, signature: string): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
mint_by_key: {
id,
owner,
signature,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const mintByKeys = async (
senderAddress: string,
id: number,
owner: string,
pubkey: string,
signature: string,
): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
mint_by_keys: {
id,
owner,
pubkey,
signature,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
2023-02-01 12:55:52 +00:00
const setNft = async (senderAddress: string, nft: string): Promise<string> => {
2023-01-31 17:31:06 +00:00
const res = await client.execute(
senderAddress,
contractAddress,
{
2023-02-01 12:55:52 +00:00
set_nft: {
nft,
},
2023-01-31 17:31:06 +00:00
},
'auto',
'',
)
return res.transactionHash
}
return {
contractAddress,
getConfig,
2023-02-01 12:55:52 +00:00
getBadge,
getBadges,
getKey,
getKeys,
createBadge,
editBadge,
addKeys,
purgeKeys,
purgeOwners,
mintByMinter,
mintByKey,
mintByKeys,
setNft,
2023-01-31 17:31:06 +00:00
}
}
const migrate = async (
senderAddress: string,
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
): Promise<MigrateResponse> => {
const result = await client.migrate(senderAddress, contractAddress, codeId, migrateMsg, 'auto')
return {
transactionHash: result.transactionHash,
logs: result.logs,
}
}
const instantiate = async (
senderAddress: string,
codeId: number,
initMsg: Record<string, unknown>,
label: string,
): Promise<InstantiateResponse> => {
2023-02-01 12:55:52 +00:00
const result = await client.instantiate(senderAddress, codeId, initMsg, label, 'auto')
2023-01-31 17:31:06 +00:00
return {
contractAddress: result.contractAddress,
transactionHash: result.transactionHash,
logs: result.logs,
}
}
const messages = (contractAddress: string) => {
2023-02-01 12:55:52 +00:00
const createBadge = (badge: Badge): CreateBadgeMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
create_badge: {
manager: badge.manager,
metadata: badge.metadata,
transferrable: badge.transferrable,
rule: badge.rule,
expiry: badge.expiry,
max_supply: badge.max_supply,
2023-01-31 17:31:06 +00:00
},
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const editBadge = (id: number, metadata: Metadata): EditBadgeMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
edit_badge: {
id,
metadata,
2023-01-31 17:31:06 +00:00
},
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const addKeys = (id: number, keys: string[]): AddKeysMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
add_keys: {
id,
keys,
},
2023-01-31 17:31:06 +00:00
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const purgeKeys = (id: number, limit?: number): PurgeKeysMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
purge_keys: {
id,
limit,
2023-01-31 17:31:06 +00:00
},
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const purgeOwners = (id: number, limit?: number): PurgeOwnersMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
purge_owners: {
id,
limit,
2023-01-31 17:31:06 +00:00
},
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const mintByMinter = (id: number, owners: string[]): MintByMinterMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
mint_by_minter: {
id,
owners,
2023-01-31 17:31:06 +00:00
},
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const mintByKey = (id: number, owner: string, signature: string): MintByKeyMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
mint_by_key: {
id,
owner,
signature,
},
2023-01-31 17:31:06 +00:00
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const mintByKeys = (id: number, owner: string, pubkey: string, signature: string): MintByKeysMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
mint_by_keys: {
id,
owner,
pubkey,
signature,
},
2023-01-31 17:31:06 +00:00
},
funds: [],
}
}
2023-02-01 12:55:52 +00:00
const setNft = (nft: string): SetNftMessage => {
2023-01-31 17:31:06 +00:00
return {
sender: txSigner,
contract: contractAddress,
msg: {
2023-02-01 12:55:52 +00:00
set_nft: {
nft,
},
2023-01-31 17:31:06 +00:00
},
funds: [],
}
}
return {
2023-02-01 12:55:52 +00:00
createBadge,
editBadge,
addKeys,
purgeKeys,
purgeOwners,
mintByMinter,
mintByKey,
mintByKeys,
setNft,
2023-01-31 17:31:06 +00:00
}
}
return { use, instantiate, migrate, messages }
}