From 7ec5d0c2a2aafec36cc22f48d13ec721845b33ce Mon Sep 17 00:00:00 2001 From: AnhVAR Date: Tue, 23 Jul 2024 15:22:56 +0700 Subject: [PATCH 1/3] Add MsgNoop for sponsor service --- packages/adena-extension/package.json | 2 +- .../common/validation/validation-message.ts | 16 ++++++++++ .../src/inject/executor/executor.ts | 6 ++++ .../src/inject/message/methods/transaction.ts | 2 +- .../mapper/transaction-history-mapper.ts | 31 +++++++++++++++++++ .../response/transaction-history-response.ts | 4 +++ .../src/services/transaction/message/vm/vm.ts | 14 +++++++++ packages/adena-module/package.json | 2 +- packages/adena-module/src/utils/messages.ts | 20 +++++++++++- 9 files changed, 93 insertions(+), 4 deletions(-) diff --git a/packages/adena-extension/package.json b/packages/adena-extension/package.json index 4612b795e..144707c3c 100644 --- a/packages/adena-extension/package.json +++ b/packages/adena-extension/package.json @@ -74,7 +74,7 @@ "webpack-merge": "^5.10.0" }, "dependencies": { - "@gnolang/gno-js-client": "1.3.0", + "@gnolang/gno-js-client": "git+https://github.com/VAR-META-Tech/gno-js-client.git#msg_noop", "@gnolang/tm2-js-client": "1.2.1", "@tanstack/react-query": "^4.36.1", "@vespaiach/axios-fetch-adapter": "^0.3.1", diff --git a/packages/adena-extension/src/common/validation/validation-message.ts b/packages/adena-extension/src/common/validation/validation-message.ts index df703207c..a8a054ee5 100644 --- a/packages/adena-extension/src/common/validation/validation-message.ts +++ b/packages/adena-extension/src/common/validation/validation-message.ts @@ -134,3 +134,19 @@ export const validateTransactionMessageOfRun = (message: { [key in string]: any } return true; }; + +export const validateTransactionMessageOfVmNoop = (message: { [key in string]: any }): boolean => { + if (!message.type || !message.value) { + return false; + } + if (message.type !== '/vm.m_noop') { + return false; + } + if (typeof message.value !== 'object') { + return false; + } + if (Object.keys(message.value).indexOf('caller') === -1) { + return false; + } + return true; +}; diff --git a/packages/adena-extension/src/inject/executor/executor.ts b/packages/adena-extension/src/inject/executor/executor.ts index f9ae41e19..0a23f2d65 100644 --- a/packages/adena-extension/src/inject/executor/executor.ts +++ b/packages/adena-extension/src/inject/executor/executor.ts @@ -6,6 +6,7 @@ import { validateTransactionMessageOfBankSend, validateTransactionMessageOfRun, validateTransactionMessageOfVmCall, + validateTransactionMessageOfVmNoop, } from '@common/validation/validation-message'; type Params = { [key in string]: any }; @@ -133,6 +134,11 @@ export class AdenaExecutor { return InjectionMessageInstance.failure('INVALID_FORMAT'); } break; + case '/vm.m_noop': + if (!validateTransactionMessageOfVmNoop(message)) { + return InjectionMessageInstance.failure('INVALID_FORMAT'); + } + break; default: return InjectionMessageInstance.failure('UNSUPPORTED_TYPE'); } diff --git a/packages/adena-extension/src/inject/message/methods/transaction.ts b/packages/adena-extension/src/inject/message/methods/transaction.ts index 8af5194d4..dcb5f0378 100644 --- a/packages/adena-extension/src/inject/message/methods/transaction.ts +++ b/packages/adena-extension/src/inject/message/methods/transaction.ts @@ -100,7 +100,7 @@ export const validateInjectionAddress = (currentAccountAddress: string): boolean }; export const validateInjectionTransactionType = (requestData: InjectionMessage): any => { - const messageTypes = ['/bank.MsgSend', '/vm.m_call', '/vm.m_addpkg', '/vm.m_run']; + const messageTypes = ['/bank.MsgSend', '/vm.m_call', '/vm.m_addpkg', '/vm.m_run', '/vm.m_noop']; return requestData.data?.messages.every((message: any) => messageTypes.includes(message?.type)); }; diff --git a/packages/adena-extension/src/repositories/transaction/mapper/transaction-history-mapper.ts b/packages/adena-extension/src/repositories/transaction/mapper/transaction-history-mapper.ts index 0f81be3ab..7238be86a 100644 --- a/packages/adena-extension/src/repositories/transaction/mapper/transaction-history-mapper.ts +++ b/packages/adena-extension/src/repositories/transaction/mapper/transaction-history-mapper.ts @@ -4,6 +4,7 @@ import { HistoryItemBankMsgSend, HistoryItemVmMAddPkg, HistoryItemVmMCall, + HistoryItemVmMNoop, TransactionHistoryResponse, } from '../response/transaction-history-response'; @@ -53,6 +54,10 @@ function isHistoryItemVmMAddPkg(historyItem: HistoryItem): historyItem is Histor return historyItem.type === '/vm.m_addpkg'; } +function isHistoryItemVmMNoop(historyItem: HistoryItem): historyItem is HistoryItemVmMNoop { + return historyItem.type === '/vm.m_noop'; +} + function isVmAddPkgType(func?: string): boolean { return func === 'AddPkg'; } @@ -113,6 +118,9 @@ export class TransactionHistoryMapper { if (isHistoryItemVmMAddPkg(historyItem)) { return TransactionHistoryMapper.mappedHistoryItemVmMAddPkg(historyItem); } + if (isHistoryItemVmMNoop(historyItem)) { + return TransactionHistoryMapper.mappedHistoryItemVmMNoop(historyItem); + } return TransactionHistoryMapper.mappedHistoryItemDefault(historyItem); } @@ -221,6 +229,29 @@ export class TransactionHistoryMapper { }; } + private static mappedHistoryItemVmMNoop(historyItem: HistoryItemVmMNoop): TransactionInfo { + const { hash, result, func, transfer, date, fee } = historyItem; + const valueType = result.status === 'Fail' ? 'BLUR' : func === 'Receive' ? 'ACTIVE' : 'DEFAULT'; + return { + hash, + logo: '', + type: 'CONTRACT_CALL', + typeName: 'Msg for sponsor service', + status: result.status === 'Success' ? 'SUCCESS' : 'FAIL', + title: func ?? '', + amount: { + value: `${transfer.amount || '0'}`, + denom: transfer.denom || 'GNOT', + }, + valueType, + date: dateToLocal(date).value, + networkFee: { + value: `${fee.amount || '0'}`, + denom: `${fee.denom}`, + }, + }; + } + private static mappedHistoryItemDefault(historyItem: HistoryItem): TransactionInfo { const { hash, result, func, transfer, date, fee } = historyItem; const valueType = result.status === 'Fail' ? 'BLUR' : func === 'Receive' ? 'ACTIVE' : 'DEFAULT'; diff --git a/packages/adena-extension/src/repositories/transaction/response/transaction-history-response.ts b/packages/adena-extension/src/repositories/transaction/response/transaction-history-response.ts index fdc330b89..707384f43 100644 --- a/packages/adena-extension/src/repositories/transaction/response/transaction-history-response.ts +++ b/packages/adena-extension/src/repositories/transaction/response/transaction-history-response.ts @@ -52,3 +52,7 @@ export interface HistoryItemVmMAddPkg extends HistoryItem { }[]; }; } + +export interface HistoryItemVmMNoop extends HistoryItem { + caller: string; +} \ No newline at end of file diff --git a/packages/adena-extension/src/services/transaction/message/vm/vm.ts b/packages/adena-extension/src/services/transaction/message/vm/vm.ts index fb3643aaa..5e5c4e3ba 100644 --- a/packages/adena-extension/src/services/transaction/message/vm/vm.ts +++ b/packages/adena-extension/src/services/transaction/message/vm/vm.ts @@ -61,3 +61,17 @@ export const createMessageOfVmRun = (info: { }, }; }; + +export const createMessageOfVmNoop = (info: { + caller: string; +}): { + type: string; + value: { caller: string;}; +} => { + return { + type: '/vm.m_noop', + value: { + caller: info.caller, + }, + }; +}; \ No newline at end of file diff --git a/packages/adena-module/package.json b/packages/adena-module/package.json index db4241e59..ba89a322b 100644 --- a/packages/adena-module/package.json +++ b/packages/adena-module/package.json @@ -43,7 +43,7 @@ }, "dependencies": { "@cosmjs/ledger-amino": "^0.32.4", - "@gnolang/gno-js-client": "1.3.0", + "@gnolang/gno-js-client": "git+https://github.com/VAR-META-Tech/gno-js-client.git#msg_noop", "@gnolang/tm2-js-client": "1.2.1", "@ledgerhq/hw-transport": "^6.30.4", "@ledgerhq/hw-transport-mocker": "^6.28.4", diff --git a/packages/adena-module/src/utils/messages.ts b/packages/adena-module/src/utils/messages.ts index 2d5664c4b..ed42a450f 100644 --- a/packages/adena-module/src/utils/messages.ts +++ b/packages/adena-module/src/utils/messages.ts @@ -1,5 +1,5 @@ import { Any, PubKeySecp256k1, Tx, TxFee, TxSignature } from '@gnolang/tm2-js-client'; -import { MsgCall, MsgAddPackage, MsgSend, MsgEndpoint } from '@gnolang/gno-js-client'; +import { MsgCall, MsgAddPackage, MsgSend, MsgEndpoint, MsgNoop } from '@gnolang/gno-js-client'; import { MemPackage, MemFile, MsgRun } from '@gnolang/gno-js-client/bin/proto/gno/vm'; import { fromBase64 } from '../encoding'; @@ -59,6 +59,14 @@ export const decodeTxMessages = (messages: Any[]): any[] => { ...messageJson, }; } + case MsgEndpoint.MSG_NOOP: { + const decodedMessage = MsgNoop.decode(m.value); + const messageJson = MsgNoop.toJSON(decodedMessage) as any; + return { + '@type': m.typeUrl, + ...messageJson, + }; + } default: throw new Error(`unsupported message type ${m.typeUrl}`); } @@ -128,6 +136,16 @@ function encodeMessageValue(message: { type: string; value: any }) { value: MsgRun.encode(msgRun).finish(), }); } + case MsgEndpoint.MSG_NOOP: { + const value = message.value; + const result = MsgNoop.create({ + caller: value.caller, + }); + return Any.create({ + typeUrl: MsgEndpoint.MSG_NOOP, + value: MsgNoop.encode(result).finish(), + }); + } default: { return Any.create({ typeUrl: MsgEndpoint.MSG_CALL, From f706d3f03559d88bcbf69e354188e7537c1aedd3 Mon Sep 17 00:00:00 2001 From: AnhVAR Date: Mon, 29 Jul 2024 11:21:13 +0700 Subject: [PATCH 2/3] Skip check balance in case Sponsor msg --- packages/adena-extension/package.json | 2 +- .../src/pages/popup/wallet/approve-transaction-main/index.tsx | 4 ++++ packages/adena-module/package.json | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/adena-extension/package.json b/packages/adena-extension/package.json index 144707c3c..e91cfb7c0 100644 --- a/packages/adena-extension/package.json +++ b/packages/adena-extension/package.json @@ -75,7 +75,7 @@ }, "dependencies": { "@gnolang/gno-js-client": "git+https://github.com/VAR-META-Tech/gno-js-client.git#msg_noop", - "@gnolang/tm2-js-client": "1.2.1", + "@gnolang/tm2-js-client": "git+https://github.com/VAR-META-Tech/tm2-js-client.git#msg_noop", "@tanstack/react-query": "^4.36.1", "@vespaiach/axios-fetch-adapter": "^0.3.1", "adena-module": "*", diff --git a/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx b/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx index d1d23dd29..3db18c8c4 100644 --- a/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx +++ b/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx @@ -96,6 +96,10 @@ const ApproveTransactionContainer: React.FC = () => { }, [document]); const isErrorNetworkFee = useMemo(() => { + //skip for case sign msg for sponsor service + if (document?.msgs[0].type === '/vm.m_noop') { + return false; + } return BigNumber(currentBalance).shiftedBy(-6).isLessThan(networkFee.amount); }, [currentBalance, networkFee]); diff --git a/packages/adena-module/package.json b/packages/adena-module/package.json index ba89a322b..078215494 100644 --- a/packages/adena-module/package.json +++ b/packages/adena-module/package.json @@ -44,7 +44,7 @@ "dependencies": { "@cosmjs/ledger-amino": "^0.32.4", "@gnolang/gno-js-client": "git+https://github.com/VAR-META-Tech/gno-js-client.git#msg_noop", - "@gnolang/tm2-js-client": "1.2.1", + "@gnolang/tm2-js-client": "git+https://github.com/VAR-META-Tech/tm2-js-client.git#msg_noop", "@ledgerhq/hw-transport": "^6.30.4", "@ledgerhq/hw-transport-mocker": "^6.28.4", "@ledgerhq/hw-transport-webhid": "^6.28.4", From 764ad3e5bfc0458907955be914bc01b091e22859 Mon Sep 17 00:00:00 2001 From: AnhVAR Date: Tue, 30 Jul 2024 15:35:05 +0700 Subject: [PATCH 3/3] separate type-checking isSponsorService --- .../pages/popup/wallet/approve-transaction-main/index.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx b/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx index 3db18c8c4..f266379ca 100644 --- a/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx +++ b/packages/adena-extension/src/pages/popup/wallet/approve-transaction-main/index.tsx @@ -95,9 +95,12 @@ const ApproveTransactionContainer: React.FC = () => { }; }, [document]); + const isSponsorService = useMemo(() => { + return (document?.msgs?.length ?? 0) > 1 && document?.msgs[0].type === '/vm.m_noop'; + }, [document]); + const isErrorNetworkFee = useMemo(() => { - //skip for case sign msg for sponsor service - if (document?.msgs[0].type === '/vm.m_noop') { + if (isSponsorService) { return false; } return BigNumber(currentBalance).shiftedBy(-6).isLessThan(networkFee.amount);