From 04781069b7910f5af4701a0be009a5139b4a5c29 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Fri, 10 Feb 2023 10:46:41 +0300 Subject: [PATCH] Calculate badge creation fee --- contracts/badgeHub/contract.ts | 36 ++++++++++++++++++++++++++-- package.json | 3 ++- pages/contracts/badgeHub/execute.tsx | 10 ++++---- yarn.lock | 7 ++++++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/contracts/badgeHub/contract.ts b/contracts/badgeHub/contract.ts index 468a3b4..91dd77b 100644 --- a/contracts/badgeHub/contract.ts +++ b/contracts/badgeHub/contract.ts @@ -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 => { + 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 => { diff --git a/package.json b/package.json index 26e8567..2455bd8 100644 --- a/package.json +++ b/package.json @@ -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 -} \ No newline at end of file +} diff --git a/pages/contracts/badgeHub/execute.tsx b/pages/contracts/badgeHub/execute.tsx index 934051c..304351c 100644 --- a/pages/contracts/badgeHub/execute.tsx +++ b/pages/contracts/badgeHub/execute.tsx @@ -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) diff --git a/yarn.lock b/yarn.lock index b245db7..e2bca6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"