From 74a7f5a263d8707ff7df8d6edca43370f7bafa72 Mon Sep 17 00:00:00 2001 From: aelf-lxy Date: Mon, 26 Aug 2024 16:02:02 +0800 Subject: [PATCH] feat: add did-ui-react as peerDependencies --- packages/base/package.json | 5 ++- packages/bridge/package.json | 14 ++++-- packages/react/package.json | 2 +- packages/starter/src/Demo/ContractDemo.tsx | 12 ++++++ packages/utils/package.json | 5 ++- packages/utils/src/contract/callViewMethod.ts | 43 +++++++++++++++++++ packages/utils/src/contract/index.ts | 1 + packages/wallets/night-elf/package.json | 2 +- packages/wallets/portkey-aa/package.json | 7 ++- .../wallets/portkey-discover/package.json | 2 +- .../portkey-discover/src/detectProvider.ts | 15 ++++--- 11 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 packages/utils/src/contract/callViewMethod.ts diff --git a/packages/base/package.json b/packages/base/package.json index 1e926aed..134dbfd6 100644 --- a/packages/base/package.json +++ b/packages/base/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/wallet-adapter-base", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", @@ -43,6 +43,9 @@ "jsdom": "^23.0.1", "typescript": "^5.3.3" }, + "peerDependencies": { + "@portkey/did-ui-react": "^2.7.2" + }, "publishConfig": { "registry": "https://registry.npmjs.org", "access": "public" diff --git a/packages/bridge/package.json b/packages/bridge/package.json index 8d9ae94d..7ccfd90a 100755 --- a/packages/bridge/package.json +++ b/packages/bridge/package.json @@ -1,14 +1,17 @@ { "name": "@aelf-web-login/wallet-adapter-bridge", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", "typings": "dist/esm/index.d.ts", "exports": { - "import": "./dist/esm/index.js", - "require": "./dist/lib/index.js", - "types": "./dist/esm/index.d.ts" + ".": { + "import": "./dist/esm/index.js", + "require": "./dist/lib/index.js", + "types": "./dist/esm/index.d.ts" + }, + "./css": "./dist/esm/ui.css" }, "files": [ "dist", @@ -39,6 +42,9 @@ "father": "^4.3.8", "typescript": "^5.3.3" }, + "peerDependencies": { + "@portkey/did-ui-react": "^2.7.2" + }, "publishConfig": { "registry": "https://registry.npmjs.org", "access": "public" diff --git a/packages/react/package.json b/packages/react/package.json index f3aefa1d..ab863223 100755 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/wallet-adapter-react", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", diff --git a/packages/starter/src/Demo/ContractDemo.tsx b/packages/starter/src/Demo/ContractDemo.tsx index 84654011..87e6ab49 100644 --- a/packages/starter/src/Demo/ContractDemo.tsx +++ b/packages/starter/src/Demo/ContractDemo.tsx @@ -3,6 +3,7 @@ import { Button } from 'antd'; import { useConnectWallet } from '@aelf-web-login/wallet-adapter-react'; import configJson from './contract/config.json'; import configTdvwJson from './contract/config.tdvw.json'; +import { callViewMethod as callViewMethodOfUtils } from '@aelf-web-login/utils'; function useExampleCall(name: string, func: () => any) { const [result, setResult] = useState({}); @@ -55,6 +56,17 @@ const ContractDemo: React.FC = () => { console.log('ContractDemo init----------'); const examples = [ + useExampleCall('call getBalance in callViewMethodOfUtils', async () => { + return callViewMethodOfUtils({ + endPoint: 'https://tdvw-test-node.aelf.io', + contractAddress: configTdvwJson.multiToken, + methodName: 'GetBalance', + args: { + symbol: 'ELF', + owner: 'rRZCro3wsAk2mW1s4CvM66wCe8cYgKKBCUFGuBhF6rUtoQNyk', + }, + }); + }), useExampleCall('call getBalance', async () => { return callViewMethod({ contractAddress: configJson.multiToken, diff --git a/packages/utils/package.json b/packages/utils/package.json index e5f9d4f6..508ae7f3 100755 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/utils", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", @@ -62,6 +62,9 @@ "ts-node": "^10.9.2", "typescript": "^5.3.3" }, + "peerDependencies": { + "@portkey/did-ui-react": "^2.7.2" + }, "publishConfig": { "registry": "https://registry.npmjs.org", "access": "public" diff --git a/packages/utils/src/contract/callViewMethod.ts b/packages/utils/src/contract/callViewMethod.ts new file mode 100644 index 00000000..3b303778 --- /dev/null +++ b/packages/utils/src/contract/callViewMethod.ts @@ -0,0 +1,43 @@ +import AElf from 'aelf-sdk'; + +const CacheViewContracts: { [key: string]: any } = {}; +const httpProviders: any = {}; +function getAElf(rpc: string) { + if (!httpProviders[rpc]) httpProviders[rpc] = new AElf(new AElf.providers.HttpProvider(rpc)); + return httpProviders[rpc]; +} +const getContract = async ( + endPoint: string, + contractAddress: string, + wallet?: any, +): Promise => { + const key = endPoint + contractAddress; + + if (!CacheViewContracts[key]) { + if (!wallet) wallet = AElf.wallet.createNewWallet(); + const aelf = getAElf(endPoint); + const contract = await aelf.chain.contractAt(contractAddress, wallet); + CacheViewContracts[endPoint + contractAddress] = contract; + return contract; + } + + return CacheViewContracts[key]; +}; + +export async function callViewMethod({ + endPoint, + contractAddress, + methodName, + args, + wallet, +}: { + endPoint: string; + contractAddress: string; + methodName: string; + args: T; + wallet?: any; +}) { + const contract = await getContract(endPoint, contractAddress, wallet); + const rs = contract[methodName].call(args); + return rs as R; +} diff --git a/packages/utils/src/contract/index.ts b/packages/utils/src/contract/index.ts index ce0d66d5..e2852406 100644 --- a/packages/utils/src/contract/index.ts +++ b/packages/utils/src/contract/index.ts @@ -2,3 +2,4 @@ export * from './useCheckAllowanceAndApprove'; export * from './useGetBalance'; export * from './getRawTransaction'; export * from './getTxResultRetry'; +export * from './callViewMethod'; diff --git a/packages/wallets/night-elf/package.json b/packages/wallets/night-elf/package.json index b63a6dd6..738ca450 100755 --- a/packages/wallets/night-elf/package.json +++ b/packages/wallets/night-elf/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/wallet-adapter-night-elf", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", diff --git a/packages/wallets/portkey-aa/package.json b/packages/wallets/portkey-aa/package.json index dedf0f3c..ca259446 100755 --- a/packages/wallets/portkey-aa/package.json +++ b/packages/wallets/portkey-aa/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/wallet-adapter-portkey-aa", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", @@ -27,14 +27,17 @@ "dependencies": { "@aelf-web-login/wallet-adapter-base": "workspace:*", "@portkey/contracts": "^2.6.2", - "@portkey/did-ui-react": "^2.7.2", "@portkey/utils": "^2.4.5" }, "devDependencies": { + "@portkey/did-ui-react": "^2.7.2", "@portkey/types": "^2.7.2", "father": "^4.3.8", "typescript": "^5.3.3" }, + "peerDependencies": { + "@portkey/did-ui-react": "^2.7.2" + }, "publishConfig": { "registry": "https://registry.npmjs.org", "access": "public" diff --git a/packages/wallets/portkey-discover/package.json b/packages/wallets/portkey-discover/package.json index 725c696d..f27b4974 100755 --- a/packages/wallets/portkey-discover/package.json +++ b/packages/wallets/portkey-discover/package.json @@ -1,6 +1,6 @@ { "name": "@aelf-web-login/wallet-adapter-portkey-discover", - "version": "0.1.3-alpha.19", + "version": "0.1.3-alpha.21", "type": "module", "main": "dist/esm/index.js", "module": "dist/esm/index.js", diff --git a/packages/wallets/portkey-discover/src/detectProvider.ts b/packages/wallets/portkey-discover/src/detectProvider.ts index 101242a8..970ba4d0 100644 --- a/packages/wallets/portkey-discover/src/detectProvider.ts +++ b/packages/wallets/portkey-discover/src/detectProvider.ts @@ -7,9 +7,14 @@ export default async function detectDiscoverProvider(): Promise