robonomics-interface-vue 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/account/index.js +3 -0
- package/dist/account/useAccount.js +70 -0
- package/dist/account/useBalance.js +89 -0
- package/dist/account/useSend.js +177 -0
- package/dist/datalog/index.js +3 -0
- package/dist/datalog/useAction.js +38 -0
- package/dist/datalog/useDatalog.js +60 -0
- package/dist/datalog/useQuery.js +95 -0
- package/dist/devices/index.js +3 -0
- package/dist/devices/useAction.js +119 -0
- package/dist/devices/useDevices.js +55 -0
- package/dist/devices/useQuery.js +98 -0
- package/dist/index.js +34 -21
- package/dist/launch/index.js +3 -0
- package/dist/launch/useAction.js +39 -0
- package/dist/launch/useLaunch.js +58 -0
- package/dist/launch/useQuery.js +96 -0
- package/dist/subscription/index.js +4 -0
- package/dist/subscription/useAction.js +70 -0
- package/dist/subscription/useAvailableSubscriptions.js +56 -0
- package/dist/subscription/useQuery.js +177 -0
- package/dist/subscription/useSubscription.js +57 -0
- package/dist/tools/index.js +2 -0
- package/dist/tools/useAction.js +49 -0
- package/dist/tools/useLoader.js +114 -0
- package/dist/twin/index.js +4 -0
- package/dist/twin/useAction.js +60 -0
- package/dist/twin/useQuery.js +155 -0
- package/dist/twin/useTwin.js +57 -0
- package/dist/twin/useTwins.js +55 -0
- package/dist/usePolkadotApi.js +76 -0
- package/package.json +22 -9
- package/dist/account.js +0 -6
- package/dist/chunks/decode-CWwgypFR.js +0 -3781
- package/dist/chunks/encode-D8r8uM3V.js +0 -17
- package/dist/chunks/index-B3M8eakN.js +0 -150
- package/dist/chunks/index-B86Fg97r.js +0 -21
- package/dist/chunks/index-BRgmmMra.js +0 -67
- package/dist/chunks/index-C0GVyO9T.js +0 -72
- package/dist/chunks/index-DYftgLJ4.js +0 -155
- package/dist/chunks/index-DwDjkKOQ.js +0 -87
- package/dist/chunks/index-V4bU3GI2.js +0 -133
- package/dist/chunks/useLoader-CINj3kKp.js +0 -47
- package/dist/chunks/usePolkadotApi-D1EyagBA.js +0 -30
- package/dist/datalog.js +0 -6
- package/dist/devices.js +0 -6
- package/dist/launch.js +0 -6
- package/dist/subscription.js +0 -7
- package/dist/tools.js +0 -6
- package/dist/twin.js +0 -7
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
3
|
+
|
|
4
|
+
/** @type {import('vue').Ref<string|null>} Current account address */
|
|
5
|
+
const account = ref(null);
|
|
6
|
+
/** @type {import('vue').Ref|null} Current account key pair */
|
|
7
|
+
let pair = ref(null);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} AccountComposable
|
|
11
|
+
* @property {import('vue').Ref<string|null>} account - Current account address
|
|
12
|
+
* @property {import('vue').Ref|null} pair - Current account key pair
|
|
13
|
+
* @property {Function} setSender - Function for setting sender
|
|
14
|
+
* @property {Function} setSubscription - Function for setting subscription
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Vue сomposable for account management
|
|
18
|
+
* @description Provides functionality for setting transaction sender and subscription
|
|
19
|
+
* @returns {AccountComposable} Object with methods and properties for working with account
|
|
20
|
+
* @example
|
|
21
|
+
* const { account, pair, setSender, setSubscription } = useAccount()
|
|
22
|
+
*
|
|
23
|
+
* setSender(keypair, { subscription: 'sub', signer: 'sr25519' })
|
|
24
|
+
* setSubscription('subscription_id')
|
|
25
|
+
*/
|
|
26
|
+
const useAccount = () => {
|
|
27
|
+
const { isConnected, instance } = usePolkadotApi();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Sets transaction sender
|
|
31
|
+
* @description Sets key pair and subscription for sending transactions
|
|
32
|
+
* @param {Object} senderPair - Sender key pair
|
|
33
|
+
* @param {Object} options - Options for setting
|
|
34
|
+
* @param {string} options.subscription - Subscription ID
|
|
35
|
+
* @param {string} options.signer - Signing type (e.g., 'sr25519')
|
|
36
|
+
* @throws {Error} Throws error if not connected to network
|
|
37
|
+
* @example
|
|
38
|
+
* setSender(keypair, { subscription: 'sub_id', signer: 'sr25519' })
|
|
39
|
+
*/
|
|
40
|
+
const setSender = (senderPair, { subscription, signer }) => {
|
|
41
|
+
if (!isConnected.value) {
|
|
42
|
+
throw new Error("Not connected");
|
|
43
|
+
}
|
|
44
|
+
instance.account.setSender(senderPair, {
|
|
45
|
+
signer,
|
|
46
|
+
subscription
|
|
47
|
+
});
|
|
48
|
+
account.value = senderPair.address;
|
|
49
|
+
pair.value = senderPair;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Sets subscription for account
|
|
54
|
+
* @description Sets subscription for working with account
|
|
55
|
+
* @param {string} subscription - Subscription ID
|
|
56
|
+
* @throws {Error} Throws error if not connected to network
|
|
57
|
+
* @example
|
|
58
|
+
* setSubscription('subscription_id')
|
|
59
|
+
*/
|
|
60
|
+
const setSubscription = (subscription) => {
|
|
61
|
+
if (!isConnected.value) {
|
|
62
|
+
throw new Error("Not connected");
|
|
63
|
+
}
|
|
64
|
+
instance.account.useSubscription(subscription);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return { account, pair, setSender, setSubscription };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export { useAccount };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { account } from 'robonomics-interface';
|
|
2
|
+
import { isRef, ref, watch, onUnmounted } from 'vue';
|
|
3
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} BalanceComposable
|
|
7
|
+
* @property {import('vue').Ref<number|undefined>} balance - Current account balance
|
|
8
|
+
* @property {import('vue').Ref<string>} address - Address of tracked account
|
|
9
|
+
* @property {import('vue').Ref<Error|null>} error - Error when loading balance
|
|
10
|
+
* @property {Function} unsubscribe - Function for unsubscribing from changes
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Vue сomposable for tracking account balance
|
|
14
|
+
* @description Provides reactive account balance with automatic updates when address or connection status changes
|
|
15
|
+
* @param {string|import('vue').Ref<string>} addressInit - Account address for balance tracking
|
|
16
|
+
* @param {boolean} [isWatch] - Whether to enable automatic balance change tracking
|
|
17
|
+
* @returns {BalanceComposable} Object with balance and management methods
|
|
18
|
+
* @example
|
|
19
|
+
* const { balance, address, error } = useBalance('5F...', true)
|
|
20
|
+
*
|
|
21
|
+
* // With reactive address
|
|
22
|
+
* const address = ref('5F...')
|
|
23
|
+
* const { balance } = useBalance(address)
|
|
24
|
+
*/
|
|
25
|
+
function useBalance(addressInit, isWatch = true) {
|
|
26
|
+
const address = isRef(addressInit) ? addressInit : ref(addressInit);
|
|
27
|
+
const { isConnected, instance } = usePolkadotApi();
|
|
28
|
+
/** @type {import('vue').Ref<number|undefined>} Current account balance */
|
|
29
|
+
const balance = ref(0);
|
|
30
|
+
/** @type {import('vue').Ref<Error|null>} Error when loading balance */
|
|
31
|
+
const error = ref(null);
|
|
32
|
+
/** @type {Function|undefined} Function for unsubscribing from changes */
|
|
33
|
+
let unsubscriber;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Unsubscribes from balance changes
|
|
37
|
+
* @description Clears current subscription and resets unsubscriber
|
|
38
|
+
*/
|
|
39
|
+
const unsubscribe = () => {
|
|
40
|
+
if (unsubscriber) {
|
|
41
|
+
unsubscriber();
|
|
42
|
+
unsubscriber = undefined;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Loads account balance
|
|
48
|
+
* @description Loads current balance or subscribes to changes depending on isWatch parameter
|
|
49
|
+
* @async
|
|
50
|
+
*/
|
|
51
|
+
const loadBalance = async () => {
|
|
52
|
+
if (!isConnected.value) return;
|
|
53
|
+
unsubscribe();
|
|
54
|
+
try {
|
|
55
|
+
if (!isWatch) {
|
|
56
|
+
balance.value = await account.query.getBalance(
|
|
57
|
+
instance.api,
|
|
58
|
+
address.value
|
|
59
|
+
);
|
|
60
|
+
} else {
|
|
61
|
+
unsubscriber = await account.query.getBalance(
|
|
62
|
+
instance.api,
|
|
63
|
+
address.value,
|
|
64
|
+
(r) => {
|
|
65
|
+
balance.value = r;
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
error.value = null;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
error.value = e;
|
|
72
|
+
balance.value = undefined;
|
|
73
|
+
unsubscriber = undefined;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
watch([address, isConnected], loadBalance, { immediate: true });
|
|
78
|
+
|
|
79
|
+
onUnmounted(unsubscribe);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
balance,
|
|
83
|
+
address,
|
|
84
|
+
error,
|
|
85
|
+
unsubscribe
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { useBalance };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
3
|
+
import { useAccount } from './useAccount.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} SendComposable
|
|
7
|
+
* @property {Object} [tx] - Transaction object (if autoCreate = true)
|
|
8
|
+
* @property {Function} create - Function for creating transaction object
|
|
9
|
+
* @property {Function} send - Function for sending transaction
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Vue сomposable for sending transactions
|
|
13
|
+
* @description Provides functionality for creating and sending transactions in the Polkadot network
|
|
14
|
+
* @param {Object} options - Composable options
|
|
15
|
+
* @param {boolean} [options.autoCreate] - Automatically create transaction object
|
|
16
|
+
* @returns {SendComposable} Object with methods for working with transactions
|
|
17
|
+
* @example
|
|
18
|
+
* const { tx, create, send } = useSend({ autoCreate: true })
|
|
19
|
+
*
|
|
20
|
+
* // Sending simple transaction
|
|
21
|
+
* await tx.send(() => api.tx.system.remark('Hello'))
|
|
22
|
+
*
|
|
23
|
+
* // Creating custom transaction object
|
|
24
|
+
* const customTx = create()
|
|
25
|
+
* await customTx.send(() => api.tx.balances.transfer(address, amount))
|
|
26
|
+
*/
|
|
27
|
+
const useSend = ({ autoCreate = true } = {}) => {
|
|
28
|
+
const { isConnected, instance } = usePolkadotApi();
|
|
29
|
+
const { setSender } = useAccount();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Sends transaction to the network
|
|
33
|
+
* @description Performs signing and sending of transaction with automatic nonce management and subscription
|
|
34
|
+
* @param {Object} tx - Transaction object
|
|
35
|
+
* @param {Function|any} call - Callable function or transaction instance
|
|
36
|
+
* @param {Object} options - Options for sending
|
|
37
|
+
* @param {Object} options.pair - Key pair for signing (optional)
|
|
38
|
+
* @param {string} options.signer - Signing type (optional)
|
|
39
|
+
* @param {string} options.subscription - Subscription ID (optional)
|
|
40
|
+
* @returns {Promise<any>} Result of transaction sending
|
|
41
|
+
* @async
|
|
42
|
+
* @example
|
|
43
|
+
* await send(tx, () => api.tx.system.remark('Hello'), {
|
|
44
|
+
* pair: keypair,
|
|
45
|
+
* signer: 'sr25519'
|
|
46
|
+
* })
|
|
47
|
+
*/
|
|
48
|
+
const send = async (tx, call, { pair, signer, subscription }) => {
|
|
49
|
+
if (!call) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
tx.start();
|
|
54
|
+
|
|
55
|
+
let currentSubscription = false;
|
|
56
|
+
|
|
57
|
+
if (!instance.account.pair && !pair) {
|
|
58
|
+
tx.setError(new Error("Not sender"));
|
|
59
|
+
} else if (!isConnected.value) {
|
|
60
|
+
tx.setError(new Error("Not connected"));
|
|
61
|
+
} else {
|
|
62
|
+
if (call instanceof Function) {
|
|
63
|
+
try {
|
|
64
|
+
if (call.constructor.name === "AsyncFunction") {
|
|
65
|
+
call = await call();
|
|
66
|
+
} else {
|
|
67
|
+
call = call();
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
tx.setError(error);
|
|
71
|
+
tx.setPropcess(false);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
if (pair) {
|
|
78
|
+
setSender(pair, { subscription, signer });
|
|
79
|
+
} else if (
|
|
80
|
+
subscription &&
|
|
81
|
+
subscription !== instance.account.subscription
|
|
82
|
+
) {
|
|
83
|
+
currentSubscription = instance.account.subscription;
|
|
84
|
+
instance.account.useSubscription(subscription);
|
|
85
|
+
}
|
|
86
|
+
const nonce = await instance.api.rpc.system.accountNextIndex(
|
|
87
|
+
instance.account.pair.address
|
|
88
|
+
);
|
|
89
|
+
tx.result.value = await instance.account.signAndSend(call, {
|
|
90
|
+
nonce
|
|
91
|
+
});
|
|
92
|
+
// console.log("tx", tx.result.value.block, tx.result.value.tx);
|
|
93
|
+
} catch (e) {
|
|
94
|
+
tx.setError(e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
tx.setPropcess(false);
|
|
99
|
+
|
|
100
|
+
if (subscription && currentSubscription !== false) {
|
|
101
|
+
instance.account.useSubscription(currentSubscription);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return tx.result;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @typedef {Object} Tx
|
|
109
|
+
* @property {import('vue').Ref<string|null>} error - Transaction error
|
|
110
|
+
* @property {import('vue').Ref<boolean|null>} process - Processing status
|
|
111
|
+
* @property {import('vue').Ref<any>} result - Transaction result
|
|
112
|
+
* @property {Function} send - Function for sending transaction
|
|
113
|
+
* @property {Function} start - Function for starting transaction
|
|
114
|
+
* @property {Function} setPropcess - Function for setting processing status
|
|
115
|
+
* @property {Function} setError - Function for setting error
|
|
116
|
+
*/
|
|
117
|
+
/**
|
|
118
|
+
* Creates transaction object
|
|
119
|
+
* @description Creates new transaction object with methods for state management
|
|
120
|
+
* @returns {Tx} Transaction object with management methods
|
|
121
|
+
* @example
|
|
122
|
+
* const tx = create()
|
|
123
|
+
* tx.start()
|
|
124
|
+
* await tx.send(() => api.tx.system.remark('Hello'))
|
|
125
|
+
*/
|
|
126
|
+
const create = () => {
|
|
127
|
+
const tx = {
|
|
128
|
+
/** @type {import('vue').Ref<string|null>} Transaction error */
|
|
129
|
+
error: ref(null),
|
|
130
|
+
/** @type {import('vue').Ref<boolean|null>} Transaction processing status */
|
|
131
|
+
process: ref(null),
|
|
132
|
+
/** @type {import('vue').Ref<any>} Transaction result */
|
|
133
|
+
result: ref(false),
|
|
134
|
+
/**
|
|
135
|
+
* Sends transaction
|
|
136
|
+
* @param {Function|any} call - Callable function or transaction instance
|
|
137
|
+
* @param {Object} options - Options for sending
|
|
138
|
+
* @returns {Promise<any>} Result of sending
|
|
139
|
+
*/
|
|
140
|
+
send: (call, options = {}) => {
|
|
141
|
+
return send(tx, call, options);
|
|
142
|
+
},
|
|
143
|
+
/**
|
|
144
|
+
* Starts transaction
|
|
145
|
+
* @description Resets transaction state and sets processing flag
|
|
146
|
+
*/
|
|
147
|
+
start: () => {
|
|
148
|
+
tx.result.value = null;
|
|
149
|
+
tx.error.value = null;
|
|
150
|
+
tx.process.value = true;
|
|
151
|
+
},
|
|
152
|
+
/**
|
|
153
|
+
* Sets transaction processing status
|
|
154
|
+
* @param {boolean} process - New processing status
|
|
155
|
+
*/
|
|
156
|
+
setPropcess: (process) => {
|
|
157
|
+
tx.process.value = process;
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* Sets transaction error
|
|
161
|
+
* @param {Error} e - Error object
|
|
162
|
+
*/
|
|
163
|
+
setError: (e) => {
|
|
164
|
+
tx.error.value = e.message;
|
|
165
|
+
tx.process.value = false;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
return tx;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
if (autoCreate) {
|
|
172
|
+
return { tx: create(), create, send };
|
|
173
|
+
}
|
|
174
|
+
return { create, send };
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export { useSend };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { datalog } from 'robonomics-interface';
|
|
2
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {Object} DatalogActionComposable
|
|
6
|
+
* @property {Function} write - Function for writing data to datalog
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Vue сomposable for performing datalog actions
|
|
10
|
+
* @description Provides functions for writing data to datalog
|
|
11
|
+
* @returns {DatalogActionComposable} Object with methods for working with datalog
|
|
12
|
+
* @example
|
|
13
|
+
* const { write } = useAction()
|
|
14
|
+
*
|
|
15
|
+
* // Writing data to datalog
|
|
16
|
+
* const tx = write('Hello World')
|
|
17
|
+
* await tx.signAndSend(keypair)
|
|
18
|
+
*/
|
|
19
|
+
function useAction() {
|
|
20
|
+
const { instance, composeWithCheckConnection } = usePolkadotApi();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates transaction for writing data to datalog
|
|
24
|
+
* @description Creates transaction instance for writing data to datalog
|
|
25
|
+
* @param {string} data - Data to write to datalog
|
|
26
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
27
|
+
* @example
|
|
28
|
+
* const tx = write('Sensor data: 25.5°C')
|
|
29
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
30
|
+
*/
|
|
31
|
+
const write = (data) => {
|
|
32
|
+
return datalog.action.write(instance.api, data);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return { write: composeWithCheckConnection(write) };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { useAction };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { tools } from 'robonomics-interface';
|
|
2
|
+
import { isRef, ref, watch, onUnmounted, toRefs } from 'vue';
|
|
3
|
+
import { useLoader } from '../tools/useLoader.js';
|
|
4
|
+
import { useQuery } from './useQuery.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} DatalogComposable
|
|
8
|
+
* @property {import('vue').Ref<any>} data - Datalog data
|
|
9
|
+
* @property {import('vue').Ref<boolean>} loading - Loading status
|
|
10
|
+
* @property {import('vue').Ref<Error|null>} error - Loading error
|
|
11
|
+
* @property {Function} load - Function for forced data loading
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Vue сomposable for working with datalog
|
|
15
|
+
* @description Provides functionality for loading and tracking changes in datalog with automatic subscription management
|
|
16
|
+
* @param {string|import('vue').Ref<string>} addressInit - Datalog address for tracking
|
|
17
|
+
* @param {Object} options - Composable options
|
|
18
|
+
* @param {boolean} [options.immediate] - Whether to start loading immediately
|
|
19
|
+
* @returns {DatalogComposable} Object with datalog state and management methods
|
|
20
|
+
* @example
|
|
21
|
+
* const { data, loading, error, load } = useDatalog('5F...', { immediate: true })
|
|
22
|
+
*
|
|
23
|
+
* // With reactive address
|
|
24
|
+
* const address = ref('5F...')
|
|
25
|
+
* const { data } = useDatalog(address)
|
|
26
|
+
*/
|
|
27
|
+
function useDatalog(addressInit, { immediate = true } = {}) {
|
|
28
|
+
const address = isRef(addressInit) ? addressInit : ref(addressInit);
|
|
29
|
+
const { loader, setter, state } = useLoader();
|
|
30
|
+
const { load, listen } = useQuery();
|
|
31
|
+
const watcher = tools.createWatcher();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Starts the process of loading and subscribing to changes
|
|
35
|
+
* @description Stops previous subscription, loads data and subscribes to new changes
|
|
36
|
+
*/
|
|
37
|
+
const run = () => {
|
|
38
|
+
loader(async () => {
|
|
39
|
+
watcher.stop();
|
|
40
|
+
const data = await load(address.value);
|
|
41
|
+
setter(data);
|
|
42
|
+
const unsubscribe = await listen(address.value, (newData) => {
|
|
43
|
+
data.push(newData);
|
|
44
|
+
setter([...data]);
|
|
45
|
+
});
|
|
46
|
+
watcher.set(unsubscribe);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
watch(address, run, { immediate });
|
|
51
|
+
|
|
52
|
+
onUnmounted(watcher.stop);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
...toRefs(state),
|
|
56
|
+
load: run
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { useDatalog };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { encodeAddress } from '@polkadot/util-crypto';
|
|
2
|
+
import { tools, datalog } from 'robonomics-interface';
|
|
3
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} DatalogQueryComposable
|
|
7
|
+
* @property {Function} load - Function for loading data from datalog
|
|
8
|
+
* @property {Function} listen - Function for subscribing to datalog events
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Vue сomposable for datalog queries
|
|
12
|
+
* @description Provides functions for loading data from datalog and subscribing to events
|
|
13
|
+
* @returns {DatalogQueryComposable} Object with methods for working with datalog
|
|
14
|
+
* @example
|
|
15
|
+
* const { load, listen } = useQuery()
|
|
16
|
+
*
|
|
17
|
+
* // Loading data
|
|
18
|
+
* const data = await load('5F...')
|
|
19
|
+
*
|
|
20
|
+
* // Subscribing to events
|
|
21
|
+
* const unsubscribe = await listen('5F...', (newData) => {
|
|
22
|
+
* console.log('New datalog entry:', newData)
|
|
23
|
+
* })
|
|
24
|
+
*/
|
|
25
|
+
function useQuery() {
|
|
26
|
+
const { instance, composeWithCheckConnection } = usePolkadotApi();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Converts raw data to human-readable format
|
|
30
|
+
* @description Converts block and data to human-readable format
|
|
31
|
+
* @param {Array} raw - Raw data [block, data]
|
|
32
|
+
* @returns {Array} Converted data [blockNumber, humanReadableData]
|
|
33
|
+
* @example
|
|
34
|
+
* const humanData = toHuman([12345, '0x1234...'])
|
|
35
|
+
* // Result: [12345, 'decoded data']
|
|
36
|
+
*/
|
|
37
|
+
const toHuman = (raw) => {
|
|
38
|
+
return [raw[0].toNumber(), raw[1].toHuman()];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Loads data from datalog by address
|
|
43
|
+
* @description Loads all datalog entries for specified address
|
|
44
|
+
* @param {string} address - Datalog address for loading
|
|
45
|
+
* @returns {Promise<Array>} Array of datalog entries in human-readable format
|
|
46
|
+
* @async
|
|
47
|
+
* @example
|
|
48
|
+
* const data = await load('5F...')
|
|
49
|
+
* // Result: [[12345, 'data1'], [12346, 'data2']]
|
|
50
|
+
*/
|
|
51
|
+
const load = async (address) => {
|
|
52
|
+
const data = await datalog.query.read(instance.api, address);
|
|
53
|
+
return data.map(toHuman);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Subscribes to datalog events for specified address
|
|
58
|
+
* @description Creates subscription to datalog events and filters them by address
|
|
59
|
+
* @param {string} address - Datalog address for tracking
|
|
60
|
+
* @param {Function} cb - Callback function for processing new data
|
|
61
|
+
* @returns {Promise<Function>} Function for unsubscribing
|
|
62
|
+
* @async
|
|
63
|
+
* @example
|
|
64
|
+
* const unsubscribe = await listen('5F...', (newData) => {
|
|
65
|
+
* console.log('New entry:', newData)
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // Unsubscribe
|
|
69
|
+
* unsubscribe()
|
|
70
|
+
*/
|
|
71
|
+
const listen = async (address, cb) => {
|
|
72
|
+
return await tools.events.on(instance.api, { section: "datalog" }, (r) => {
|
|
73
|
+
r = address
|
|
74
|
+
? r.filter(
|
|
75
|
+
(item) => encodeAddress(item.data[0]) === encodeAddress(address)
|
|
76
|
+
)
|
|
77
|
+
: r;
|
|
78
|
+
if (r.length) {
|
|
79
|
+
for (const item of r) {
|
|
80
|
+
cb({
|
|
81
|
+
sender: encodeAddress(item.data[0]),
|
|
82
|
+
data: toHuman([item.data[1], item.data[2]])
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
load: composeWithCheckConnection(load),
|
|
91
|
+
listen: composeWithCheckConnection(listen)
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { useQuery };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { devices } from 'robonomics-interface';
|
|
2
|
+
import { usePolkadotApi } from '../usePolkadotApi.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {Object} DevicesActionComposable
|
|
6
|
+
* @property {Function} add - Function for adding new devices
|
|
7
|
+
* @property {Function} remove - Function for removing devices
|
|
8
|
+
* @property {Function} replace - Function for replacing devices
|
|
9
|
+
* @property {Function} replaceAll - Function for completely replacing devices list
|
|
10
|
+
* @property {Function} clear - Function for clearing devices list
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Vue сomposable for performing device actions
|
|
14
|
+
* @description Provides functions for managing devices list (adding, removing, replacing)
|
|
15
|
+
* @returns {DevicesActionComposable} Object with methods for working with devices
|
|
16
|
+
* @example
|
|
17
|
+
* const { add, remove, replace, replaceAll, clear } = useAction()
|
|
18
|
+
*
|
|
19
|
+
* // Adding devices
|
|
20
|
+
* const tx = add(['5F...'], ['5F...', '5F...'])
|
|
21
|
+
* await tx.signAndSend(keypair)
|
|
22
|
+
*
|
|
23
|
+
* // Removing devices
|
|
24
|
+
* const tx = remove(['5F...', '5F...'], ['5F...'])
|
|
25
|
+
* await tx.signAndSend(keypair)
|
|
26
|
+
*/
|
|
27
|
+
function useAction() {
|
|
28
|
+
const { composeWithCheckConnection, instance } = usePolkadotApi();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Adds new devices to existing list
|
|
32
|
+
* @description Creates transaction for adding new device addresses to current list
|
|
33
|
+
* @param {Array<string>} current - Current list of device addresses
|
|
34
|
+
* @param {Array<string>} newAddresses - New device addresses to add
|
|
35
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
36
|
+
* @example
|
|
37
|
+
* const tx = add(['5F...'], ['5F...', '5F...'])
|
|
38
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
39
|
+
*/
|
|
40
|
+
const add = (current, newAddresses) => {
|
|
41
|
+
return devices.action.setDevices(
|
|
42
|
+
instance.api,
|
|
43
|
+
devices.tools.add(current, newAddresses)
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes devices from existing list
|
|
49
|
+
* @description Creates transaction for removing specified device addresses from current list
|
|
50
|
+
* @param {Array<string>} current - Current list of device addresses
|
|
51
|
+
* @param {Array<string>} addresses - Device addresses to remove
|
|
52
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
53
|
+
* @example
|
|
54
|
+
* const tx = remove(['5F...', '5F...'], ['5F...'])
|
|
55
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
56
|
+
*/
|
|
57
|
+
const remove = (current, addresses) => {
|
|
58
|
+
return devices.action.setDevices(
|
|
59
|
+
instance.api,
|
|
60
|
+
devices.tools.remove(current, addresses)
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Replaces devices in existing list
|
|
66
|
+
* @description Creates transaction for replacing specified device addresses with new ones
|
|
67
|
+
* @param {Array<string>} current - Current list of device addresses
|
|
68
|
+
* @param {Array<string>} removeAddresses - Device addresses to remove
|
|
69
|
+
* @param {Array<string>} newAddresses - New device addresses to add
|
|
70
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
71
|
+
* @example
|
|
72
|
+
* const tx = replace(['5F...', '5F...'], ['5F...'], ['5F...'])
|
|
73
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
74
|
+
*/
|
|
75
|
+
const replace = (current, removeAddresses, newAddresses) => {
|
|
76
|
+
return devices.action.setDevices(
|
|
77
|
+
instance.api,
|
|
78
|
+
devices.tools.replace(current, removeAddresses, newAddresses)
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Completely replaces devices list
|
|
84
|
+
* @description Creates transaction for completely replacing current devices list with new one
|
|
85
|
+
* @param {Array<string>} addresses - New list of device addresses
|
|
86
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
87
|
+
* @example
|
|
88
|
+
* const tx = replaceAll(['5F...', '5F...', '5F...'])
|
|
89
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
90
|
+
*/
|
|
91
|
+
const replaceAll = (addresses) => {
|
|
92
|
+
return devices.action.setDevices(
|
|
93
|
+
instance.api,
|
|
94
|
+
devices.tools.replaceAll(addresses)
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Clears devices list
|
|
100
|
+
* @description Creates transaction for completely clearing devices list
|
|
101
|
+
* @returns {Object} Transaction instance for signing and sending
|
|
102
|
+
* @example
|
|
103
|
+
* const tx = clear()
|
|
104
|
+
* await tx.signAndSend(keypair, { nonce })
|
|
105
|
+
*/
|
|
106
|
+
const clear = () => {
|
|
107
|
+
return devices.action.setDevices(instance.api, devices.tools.clear());
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
add: composeWithCheckConnection(add),
|
|
112
|
+
remove: composeWithCheckConnection(remove),
|
|
113
|
+
replace: composeWithCheckConnection(replace),
|
|
114
|
+
replaceAll: composeWithCheckConnection(replaceAll),
|
|
115
|
+
clear: composeWithCheckConnection(clear)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { useAction };
|