Calculate badge creation fee

This commit is contained in:
Serkan Reis 2023-02-10 10:46:41 +03:00
parent c626864f0b
commit 04781069b7
4 changed files with 49 additions and 7 deletions

View File

@ -1,11 +1,15 @@
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable camelcase */
import type { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { toUtf8 } from '@cosmjs/encoding'
import type { Coin } from '@cosmjs/proto-signing'
import { coin } from '@cosmjs/proto-signing'
import type { logs } from '@cosmjs/stargate'
import sizeof from 'object-sizeof'
export interface InstantiateResponse {
readonly contractAddress: string
@ -261,6 +265,19 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
//Execute
const createBadge = async (senderAddress: string, badge: Badge): Promise<string> => {
const feeRateRaw = await client.queryContractRaw(
contractAddress,
toUtf8(Buffer.from(Buffer.from('fee_rate').toString('hex'), 'hex').toString()),
)
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)))
const res = await client.execute(
senderAddress,
contractAddress,
@ -276,10 +293,25 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
},
'auto',
'',
[coin(912, 'ustars')],
[
coin(
(Number(sizeof(badge)) + Number(sizeof(badge.metadata.attributes))) * Number(feeRate.metadata),
'ustars',
),
],
//[coin(1, 'ustars')],
)
return res.transactionHash
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}`)
}
const editBadge = async (senderAddress: string, id: number, metadata: Metadata): Promise<string> => {

View File

@ -57,6 +57,7 @@
"autoprefixer": "^10",
"husky": "^7",
"lint-staged": "^12",
"object-sizeof": "^1.6.0",
"postcss": "^8",
"tailwindcss": "^3",
"typescript": "^4"
@ -83,4 +84,4 @@
},
"prettier": "@stargaze-studio/prettier-config",
"private": true
}
}

View File

@ -72,6 +72,7 @@ const BadgeHubExecutePage: NextPage = () => {
name: 'manager',
title: 'Manager',
subtitle: 'Badge Hub Manager',
defaultValue: wallet.address,
})
const nameState = useInputState({
@ -262,10 +263,11 @@ const BadgeHubExecutePage: NextPage = () => {
const txHash = await toast.promise(dispatchExecute(payload), {
error: `${type.charAt(0).toUpperCase() + type.slice(1)} execute failed!`,
loading: 'Executing message...',
success: (tx) => `Transaction ${tx} success!`,
success: (tx) => `Transaction ${tx.split(':')[0]} success!`,
})
if (txHash) {
setLastTx(txHash)
setLastTx(txHash.split(':')[0])
console.log(txHash.split(':')[1])
}
},
{
@ -279,11 +281,11 @@ const BadgeHubExecutePage: NextPage = () => {
let privKey: Buffer
do {
privKey = crypto.randomBytes(32)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
} while (!secp256k1.privateKeyVerify(privKey))
const privateKey = privKey.toString('hex')
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
console.log('Private Key: ', privateKey)
const publicKey = Buffer.from(secp256k1.publicKeyCreate(privKey)).toString('hex')
keyState.onChange(publicKey)

View File

@ -6343,6 +6343,13 @@ object-keys@^1.1.1:
resolved "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz"
integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==
object-sizeof@^1.6.0:
version "1.6.3"
resolved "https://registry.yarnpkg.com/object-sizeof/-/object-sizeof-1.6.3.tgz#6edbbf26825b971fd7a32125a800ed2a9895af95"
integrity sha512-LGtilAKuDGKCcvu1Xg3UvAhAeJJlFmblo3faltmOQ80xrGwAHxnauIXucalKdTEksHp/Pq9tZGz1hfyEmjFJPQ==
dependencies:
buffer "^5.6.0"
object.assign@^4.1.0, object.assign@^4.1.2:
version "4.1.2"
resolved "https://registry.npmjs.org/object.assign/-/object.assign-4.1.2.tgz"