@aeternity/aepp-sdk 12.0.0 → 12.1.2
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/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -15
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +2 -1
- package/dist/aepp-sdk.browser.js.LICENSE.txt +1 -0
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +20765 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/AeSdk.d.ts +7 -7
- package/es/AeSdk.mjs.map +1 -1
- package/es/AeSdkAepp.d.ts +4 -3
- package/es/AeSdkAepp.mjs +2 -1
- package/es/AeSdkAepp.mjs.map +1 -1
- package/es/AeSdkBase.d.ts +22 -21
- package/es/AeSdkBase.mjs +25 -21
- package/es/AeSdkBase.mjs.map +1 -1
- package/es/AeSdkWallet.d.ts +4 -3
- package/es/AeSdkWallet.mjs +5 -1
- package/es/AeSdkWallet.mjs.map +1 -1
- package/es/Node.d.ts +7 -10
- package/es/Node.mjs +63 -57
- package/es/Node.mjs.map +1 -1
- package/es/account/Base.d.ts +7 -4
- package/es/account/Base.mjs +3 -3
- package/es/account/Base.mjs.map +1 -1
- package/es/account/Memory.d.ts +5 -4
- package/es/account/Memory.mjs +24 -2
- package/es/account/Memory.mjs.map +1 -1
- package/es/account/Rpc.d.ts +6 -6
- package/es/account/Rpc.mjs +3 -2
- package/es/account/Rpc.mjs.map +1 -1
- package/es/aens.d.ts +27 -14
- package/es/aens.mjs +27 -21
- package/es/aens.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/Browser.d.ts +2 -1
- package/es/aepp-wallet-communication/connection/Browser.mjs +5 -2
- package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +1 -0
- package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +1 -0
- package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +1 -0
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +5 -1
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection-proxy.d.ts +1 -0
- package/es/aepp-wallet-communication/connection-proxy.mjs +1 -0
- package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -1
- package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +1 -0
- package/es/aepp-wallet-communication/rpc/RpcClient.mjs +5 -5
- package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -1
- package/es/aepp-wallet-communication/rpc/types.d.ts +8 -8
- package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -1
- package/es/aepp-wallet-communication/schema.d.ts +17 -2
- package/es/aepp-wallet-communication/schema.mjs +54 -17
- package/es/aepp-wallet-communication/schema.mjs.map +1 -1
- package/es/aepp-wallet-communication/wallet-detector.d.ts +2 -1
- package/es/aepp-wallet-communication/wallet-detector.mjs +2 -3
- package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -1
- package/es/apis/node/models/index.d.ts +6 -2
- package/es/apis/node/models/index.mjs.map +1 -1
- package/es/apis/node/models/mappers.mjs +4 -2
- package/es/apis/node/models/mappers.mjs.map +1 -1
- package/es/chain.d.ts +92 -64
- package/es/chain.mjs +190 -154
- package/es/chain.mjs.map +1 -1
- package/es/channel/handlers.d.ts +3 -3
- package/es/channel/handlers.mjs +42 -44
- package/es/channel/handlers.mjs.map +1 -1
- package/es/channel/index.d.ts +43 -52
- package/es/channel/index.mjs +46 -49
- package/es/channel/index.mjs.map +1 -1
- package/es/channel/internal.d.ts +16 -15
- package/es/channel/internal.mjs +67 -62
- package/es/channel/internal.mjs.map +1 -1
- package/es/contract/Compiler.d.ts +1 -0
- package/es/contract/Compiler.mjs +5 -4
- package/es/contract/Compiler.mjs.map +1 -1
- package/es/contract/aci.d.ts +17 -15
- package/es/contract/aci.mjs +58 -45
- package/es/contract/aci.mjs.map +1 -1
- package/es/contract/ga.d.ts +16 -15
- package/es/contract/ga.mjs +12 -7
- package/es/contract/ga.mjs.map +1 -1
- package/es/contract/methods.d.ts +9 -6
- package/es/contract/methods.mjs +9 -6
- package/es/contract/methods.mjs.map +1 -1
- package/es/deprecated/index.d.ts +143 -0
- package/es/deprecated/index.mjs +162 -0
- package/es/deprecated/index.mjs.map +1 -0
- package/es/deprecated/methods.d.ts +18 -0
- package/es/deprecated/methods.mjs +26 -0
- package/es/deprecated/methods.mjs.map +1 -0
- package/es/index.d.ts +4 -1
- package/es/index.mjs +4 -1
- package/es/index.mjs.map +1 -1
- package/es/oracle.d.ts +69 -62
- package/es/oracle.mjs +121 -103
- package/es/oracle.mjs.map +1 -1
- package/es/spend.d.ts +16 -15
- package/es/spend.mjs +19 -30
- package/es/spend.mjs.map +1 -1
- package/es/tx/builder/address.d.ts +25 -0
- package/es/tx/builder/address.mjs +45 -0
- package/es/tx/builder/address.mjs.map +1 -0
- package/es/tx/builder/constants.d.ts +55 -26
- package/es/tx/builder/constants.mjs +60 -38
- package/es/tx/builder/constants.mjs.map +1 -1
- package/es/tx/builder/field-types/coin-amount.d.ts +13 -0
- package/es/tx/builder/field-types/coin-amount.mjs +26 -0
- package/es/tx/builder/field-types/coin-amount.mjs.map +1 -0
- package/es/tx/builder/field-types/deposit.d.ts +15 -0
- package/es/tx/builder/field-types/deposit.mjs +19 -0
- package/es/tx/builder/field-types/deposit.mjs.map +1 -0
- package/es/tx/builder/field-types/fee.d.ts +25 -0
- package/es/tx/{min-fee.mjs → builder/field-types/fee.mjs} +62 -60
- package/es/tx/builder/field-types/fee.mjs.map +1 -0
- package/es/tx/builder/field-types/field.d.ts +6 -0
- package/es/tx/builder/field-types/field.mjs +15 -0
- package/es/tx/builder/field-types/field.mjs.map +1 -0
- package/es/tx/builder/field-types/gas-limit.d.ts +12 -0
- package/es/tx/builder/field-types/gas-limit.mjs +33 -0
- package/es/tx/builder/field-types/gas-limit.mjs.map +1 -0
- package/es/tx/builder/field-types/gas-price.d.ts +8 -0
- package/es/tx/builder/field-types/gas-price.mjs +18 -0
- package/es/tx/builder/field-types/gas-price.mjs.map +1 -0
- package/es/tx/builder/field-types/index.d.ts +70 -0
- package/es/tx/builder/field-types/index.mjs +25 -0
- package/es/tx/builder/field-types/index.mjs.map +1 -0
- package/es/tx/builder/field-types/name-fee.d.ts +20 -0
- package/es/tx/builder/field-types/name-fee.mjs +24 -0
- package/es/tx/builder/field-types/name-fee.mjs.map +1 -0
- package/es/tx/builder/field-types/name-id.d.ts +14 -0
- package/es/tx/builder/field-types/name-id.mjs +19 -0
- package/es/tx/builder/field-types/name-id.mjs.map +1 -0
- package/es/tx/builder/field-types/name.d.ts +13 -0
- package/es/tx/builder/field-types/name.mjs +18 -0
- package/es/tx/builder/field-types/name.mjs.map +1 -0
- package/es/tx/builder/field-types/short-u-int.d.ts +6 -0
- package/es/tx/builder/field-types/short-u-int.mjs +12 -0
- package/es/tx/builder/field-types/short-u-int.mjs.map +1 -0
- package/es/tx/builder/field-types/u-int.d.ts +8 -0
- package/es/tx/builder/field-types/u-int.mjs +14 -0
- package/es/tx/builder/field-types/u-int.mjs.map +1 -0
- package/es/tx/builder/helpers.d.ts +27 -33
- package/es/tx/builder/helpers.mjs +41 -47
- package/es/tx/builder/helpers.mjs.map +1 -1
- package/es/tx/builder/index.d.ts +25 -12
- package/es/tx/builder/index.mjs +74 -83
- package/es/tx/builder/index.mjs.map +1 -1
- package/es/tx/builder/schema.d.ts +995 -144
- package/es/tx/builder/schema.mjs +121 -170
- package/es/tx/builder/schema.mjs.map +1 -1
- package/es/tx/index.d.ts +28 -27
- package/es/tx/index.mjs +97 -117
- package/es/tx/index.mjs.map +1 -1
- package/es/tx/validator.d.ts +4 -3
- package/es/tx/validator.mjs +79 -92
- package/es/tx/validator.mjs.map +1 -1
- package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -1
- package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -1
- package/es/typings/bip32-path/index.d.mjs.map +1 -1
- package/es/typings/blakejs/index.d.mjs.map +1 -1
- package/es/typings/sha-js/index.d.mjs +2 -0
- package/es/typings/sha-js/index.d.mjs.map +1 -0
- package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -1
- package/es/utils/amount-formatter.d.ts +9 -9
- package/es/utils/amount-formatter.mjs +17 -17
- package/es/utils/amount-formatter.mjs.map +1 -1
- package/es/utils/autorest.mjs +7 -7
- package/es/utils/autorest.mjs.map +1 -1
- package/es/utils/bignumber.mjs +2 -1
- package/es/utils/bignumber.mjs.map +1 -1
- package/es/utils/bytes.mjs +1 -1
- package/es/utils/bytes.mjs.map +1 -1
- package/es/utils/crypto.d.ts +8 -23
- package/es/utils/crypto.mjs +15 -41
- package/es/utils/crypto.mjs.map +1 -1
- package/es/utils/encoder-types.d.ts +61 -0
- package/es/utils/encoder-types.mjs +36 -0
- package/es/utils/encoder-types.mjs.map +1 -0
- package/es/utils/encoder.d.ts +6 -7
- package/es/utils/encoder.mjs +42 -26
- package/es/utils/encoder.mjs.map +1 -1
- package/es/utils/errors.d.ts +1 -11
- package/es/utils/errors.mjs +4 -20
- package/es/utils/errors.mjs.map +1 -1
- package/es/utils/hd-wallet.d.ts +18 -2
- package/es/utils/hd-wallet.mjs +64 -34
- package/es/utils/hd-wallet.mjs.map +1 -1
- package/es/utils/json-big.mjs +2 -2
- package/es/utils/json-big.mjs.map +1 -1
- package/es/utils/keystore.mjs +4 -2
- package/es/utils/keystore.mjs.map +1 -1
- package/es/utils/mptree.d.ts +2 -2
- package/es/utils/mptree.mjs +11 -6
- package/es/utils/mptree.mjs.map +1 -1
- package/es/utils/other.d.ts +8 -2
- package/es/utils/other.mjs +15 -3
- package/es/utils/other.mjs.map +1 -1
- package/es/utils/semver-satisfies.d.ts +1 -1
- package/es/utils/semver-satisfies.mjs +2 -2
- package/es/utils/semver-satisfies.mjs.map +1 -1
- package/es/utils/string.mjs.map +1 -1
- package/package.json +36 -34
- package/src/AeSdk.ts +30 -27
- package/src/AeSdkAepp.ts +93 -84
- package/src/AeSdkBase.ts +148 -130
- package/src/AeSdkWallet.ts +172 -156
- package/src/Node.ts +108 -102
- package/src/account/Base.ts +42 -32
- package/src/account/Memory.ts +45 -30
- package/src/account/Rpc.ts +32 -30
- package/src/aens.ts +124 -108
- package/src/aepp-wallet-communication/connection/Browser.ts +18 -15
- package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +22 -21
- package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +63 -52
- package/src/aepp-wallet-communication/connection-proxy.ts +8 -7
- package/src/aepp-wallet-communication/rpc/RpcClient.ts +67 -61
- package/src/aepp-wallet-communication/rpc/types.ts +32 -32
- package/src/aepp-wallet-communication/schema.ts +117 -74
- package/src/aepp-wallet-communication/wallet-detector.ts +29 -28
- package/src/apis/node/code-model-v4.yaml +54 -20
- package/src/apis/node/models/index.ts +6 -2
- package/src/apis/node/models/mappers.ts +4 -2
- package/src/chain.ts +286 -228
- package/src/channel/handlers.ts +424 -372
- package/src/channel/index.ts +285 -251
- package/src/channel/internal.ts +248 -232
- package/src/contract/Compiler.ts +22 -21
- package/src/contract/aci.ts +267 -258
- package/src/contract/ga.ts +93 -72
- package/src/contract/methods.ts +32 -27
- package/src/deprecated/index.ts +172 -0
- package/src/deprecated/methods.ts +25 -0
- package/src/index.ts +37 -32
- package/src/oracle.ts +211 -189
- package/src/spend.ts +82 -70
- package/src/tx/builder/address.ts +50 -0
- package/src/tx/builder/constants.ts +82 -52
- package/src/tx/builder/field-types/coin-amount.ts +27 -0
- package/src/tx/builder/field-types/deposit.ts +21 -0
- package/src/tx/builder/field-types/fee.ts +143 -0
- package/src/tx/builder/field-types/field.ts +9 -0
- package/src/tx/builder/field-types/gas-limit.ts +41 -0
- package/src/tx/builder/field-types/gas-price.ts +16 -0
- package/src/tx/builder/field-types/index.ts +45 -0
- package/src/tx/builder/field-types/name-fee.ts +32 -0
- package/src/tx/builder/field-types/name-id.ts +20 -0
- package/src/tx/builder/field-types/name.ts +18 -0
- package/src/tx/builder/field-types/short-u-int.ts +11 -0
- package/src/tx/builder/field-types/u-int.ts +16 -0
- package/src/tx/builder/helpers.ts +112 -117
- package/src/tx/builder/index.ts +203 -197
- package/src/tx/builder/schema.ts +421 -465
- package/src/tx/index.ts +128 -135
- package/src/tx/validator.ts +165 -161
- package/src/typings/@aeternity__argon2-browser/index.d.ts +1 -1
- package/src/typings/@aeternity__uuid/index.d.ts +3 -1
- package/src/typings/bip32-path/index.d.ts +6 -6
- package/src/typings/blakejs/index.d.ts +4 -32
- package/src/typings/sha-js/index.d.ts +5 -0
- package/src/typings/tweetnacl-auth/index.d.ts +2 -2
- package/src/utils/amount-formatter.ts +41 -41
- package/src/utils/autorest.ts +57 -54
- package/src/utils/bignumber.ts +7 -6
- package/src/utils/bytes.ts +19 -19
- package/src/utils/crypto.ts +82 -94
- package/src/utils/encoder-types.ts +63 -0
- package/src/utils/encoder.ts +104 -56
- package/src/utils/errors.ts +246 -264
- package/src/utils/hd-wallet.ts +114 -82
- package/src/utils/json-big.ts +16 -15
- package/src/utils/keystore.ts +65 -62
- package/src/utils/mptree.ts +59 -56
- package/src/utils/other.ts +26 -18
- package/src/utils/semver-satisfies.ts +15 -11
- package/src/utils/string.ts +10 -10
- package/es/tx/builder/field-types.d.ts +0 -62
- package/es/tx/builder/field-types.mjs +0 -126
- package/es/tx/builder/field-types.mjs.map +0 -1
- package/es/tx/min-fee.d.ts +0 -20
- package/es/tx/min-fee.mjs.map +0 -1
- package/src/tx/builder/field-types.ts +0 -111
- package/src/tx/min-fee.ts +0 -136
package/src/spend.ts
CHANGED
|
@@ -14,139 +14,151 @@
|
|
|
14
14
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
15
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
16
16
|
*/
|
|
17
|
-
import
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import
|
|
17
|
+
import BigNumber from 'bignumber.js';
|
|
18
|
+
import {
|
|
19
|
+
sendTransaction, getAccount, getBalance, resolveName,
|
|
20
|
+
} from './chain';
|
|
21
|
+
import { _buildTx, BuildTxOptions } from './tx';
|
|
22
|
+
import { buildTxHash, unpackTx } from './tx/builder';
|
|
23
|
+
import { ArgumentError } from './utils/errors';
|
|
24
|
+
import { Encoded, Encoding } from './utils/encoder';
|
|
25
|
+
import { Tag, AensName } from './tx/builder/constants';
|
|
26
|
+
import AccountBase from './account/Base';
|
|
26
27
|
|
|
27
28
|
/**
|
|
28
29
|
* Sign and post a transaction to the chain
|
|
30
|
+
* @category chain
|
|
29
31
|
* @param tx - Transaction
|
|
30
32
|
* @param options - Options
|
|
31
33
|
* @param options.verify - Verify transaction before broadcast, throw error if not
|
|
32
34
|
* valid
|
|
33
35
|
* @returns Transaction
|
|
34
36
|
*/
|
|
35
|
-
export async function send
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
export async function send(
|
|
38
|
+
tx: Encoded.Transaction,
|
|
39
|
+
options: SendOptions,
|
|
40
|
+
): Promise<SendReturnType> {
|
|
41
|
+
// TODO: detect authFun in AccountGa
|
|
42
|
+
const authFun = options.innerTx === true
|
|
43
|
+
? undefined
|
|
44
|
+
: (await getAccount(await options.onAccount.address(options), options)).authFun;
|
|
39
45
|
|
|
40
|
-
const signed =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
: await options.
|
|
46
|
+
const signed = await options.onAccount.signTransaction(tx, {
|
|
47
|
+
...options,
|
|
48
|
+
authFun,
|
|
49
|
+
networkId: (await options.onNode.getStatus()).networkId,
|
|
50
|
+
});
|
|
44
51
|
|
|
45
52
|
return options.innerTx === true
|
|
46
53
|
? { hash: buildTxHash(signed), rawTx: signed }
|
|
47
|
-
:
|
|
54
|
+
: sendTransaction(signed, options);
|
|
48
55
|
}
|
|
49
56
|
|
|
50
|
-
type SendOptionsType = Parameters<AccountBase['signTransaction']>[1]
|
|
51
|
-
&
|
|
52
|
-
& Pick<Parameters<typeof signUsingGA>[1], 'onAccount' | 'onCompiler'>
|
|
57
|
+
type SendOptionsType = Parameters<AccountBase['signTransaction']>[1]
|
|
58
|
+
& Parameters<typeof sendTransaction>[1] & { onAccount: AccountBase };
|
|
53
59
|
export interface SendOptions extends SendOptionsType {}
|
|
54
60
|
interface SendReturnType extends Awaited<ReturnType<typeof sendTransaction>> {}
|
|
55
61
|
|
|
56
|
-
export async function signUsingGA (
|
|
57
|
-
tx: EncodedData<'tx'>,
|
|
58
|
-
{ authData, authFun, ...options }: {
|
|
59
|
-
authData: Parameters<typeof createMetaTx>[1]
|
|
60
|
-
authFun: Parameters<typeof createMetaTx>[2]
|
|
61
|
-
} & Parameters<typeof createMetaTx>[3]
|
|
62
|
-
): Promise<EncodedData<'tx'>> {
|
|
63
|
-
return await createMetaTx(tx, authData, authFun, options)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
62
|
/**
|
|
67
63
|
* Send coins to another account
|
|
64
|
+
* @category chain
|
|
68
65
|
* @param amount - Amount to spend
|
|
69
66
|
* @param recipientIdOrName - Address or name of recipient account
|
|
70
67
|
* @param options - Options
|
|
71
68
|
* @returns Transaction
|
|
72
69
|
*/
|
|
73
|
-
export async function spend
|
|
70
|
+
export async function spend(
|
|
74
71
|
amount: number | string,
|
|
75
|
-
recipientIdOrName:
|
|
76
|
-
options: SpendOptions
|
|
72
|
+
recipientIdOrName: Encoded.AccountAddress | AensName,
|
|
73
|
+
options: SpendOptions,
|
|
77
74
|
): ReturnType<typeof send> {
|
|
78
|
-
return
|
|
79
|
-
await _buildTx(
|
|
75
|
+
return send(
|
|
76
|
+
await _buildTx(Tag.SpendTx, {
|
|
80
77
|
...options,
|
|
81
78
|
senderId: await options.onAccount.address(options),
|
|
82
|
-
recipientId: await resolveName(
|
|
83
|
-
|
|
79
|
+
recipientId: await resolveName<Encoding.AccountAddress>(
|
|
80
|
+
recipientIdOrName,
|
|
81
|
+
'account_pubkey',
|
|
82
|
+
options,
|
|
83
|
+
),
|
|
84
|
+
amount,
|
|
84
85
|
}),
|
|
85
|
-
options
|
|
86
|
-
)
|
|
86
|
+
options,
|
|
87
|
+
);
|
|
87
88
|
}
|
|
88
89
|
|
|
89
|
-
type SpendOptionsType = BuildTxOptions<
|
|
90
|
-
& Parameters<typeof resolveName>[2] & { onAccount: AccountBase } & SendOptions
|
|
90
|
+
type SpendOptionsType = BuildTxOptions<Tag.SpendTx, 'senderId' | 'recipientId' | 'amount'>
|
|
91
|
+
& Parameters<typeof resolveName>[2] & { onAccount: AccountBase } & SendOptions;
|
|
91
92
|
interface SpendOptions extends SpendOptionsType {}
|
|
92
93
|
|
|
93
94
|
// TODO: Rename to spendFraction
|
|
94
95
|
/**
|
|
95
96
|
* Send a fraction of coin balance to another account
|
|
97
|
+
* @category chain
|
|
96
98
|
* @param fraction - Fraction of balance to spend (between 0 and 1)
|
|
97
99
|
* @param recipientIdOrName - Address or name of recipient account
|
|
98
100
|
* @param options - Options
|
|
99
101
|
* @returns Transaction
|
|
100
102
|
*/
|
|
101
|
-
export async function transferFunds
|
|
103
|
+
export async function transferFunds(
|
|
102
104
|
fraction: number | string,
|
|
103
|
-
recipientIdOrName: AensName |
|
|
104
|
-
options: TransferFundsOptions
|
|
105
|
+
recipientIdOrName: AensName | Encoded.AccountAddress,
|
|
106
|
+
options: TransferFundsOptions,
|
|
105
107
|
): ReturnType<typeof send> {
|
|
106
108
|
if (fraction < 0 || fraction > 1) {
|
|
107
|
-
throw new ArgumentError('fraction', 'a number between 0 and 1', fraction)
|
|
109
|
+
throw new ArgumentError('fraction', 'a number between 0 and 1', fraction);
|
|
108
110
|
}
|
|
109
|
-
const recipientId = await resolveName<
|
|
110
|
-
|
|
111
|
+
const recipientId = await resolveName<Encoding.AccountAddress>(
|
|
112
|
+
recipientIdOrName,
|
|
113
|
+
'account_pubkey',
|
|
114
|
+
options,
|
|
115
|
+
);
|
|
116
|
+
const senderId = await options.onAccount.address(options);
|
|
111
117
|
const balance = new BigNumber(
|
|
112
|
-
await getBalance.bind(options.onAccount)(senderId, options)
|
|
113
|
-
)
|
|
114
|
-
const desiredAmount = balance.times(fraction).integerValue(BigNumber.ROUND_HALF_UP)
|
|
118
|
+
await getBalance.bind(options.onAccount)(senderId, options),
|
|
119
|
+
);
|
|
120
|
+
const desiredAmount = balance.times(fraction).integerValue(BigNumber.ROUND_HALF_UP);
|
|
115
121
|
const { tx: { fee } } = unpackTx(
|
|
116
|
-
await _buildTx(
|
|
117
|
-
|
|
118
|
-
|
|
122
|
+
await _buildTx(Tag.SpendTx, {
|
|
123
|
+
...options, senderId, recipientId, amount: desiredAmount,
|
|
124
|
+
}),
|
|
125
|
+
Tag.SpendTx,
|
|
126
|
+
);
|
|
119
127
|
// Reducing of the amount may reduce transaction fee, so this is not completely accurate
|
|
120
|
-
const amount = desiredAmount.plus(fee).gt(balance) ? balance.minus(fee) : desiredAmount
|
|
121
|
-
return
|
|
122
|
-
await _buildTx(
|
|
123
|
-
|
|
124
|
-
|
|
128
|
+
const amount = desiredAmount.plus(fee).gt(balance) ? balance.minus(fee) : desiredAmount;
|
|
129
|
+
return send(
|
|
130
|
+
await _buildTx(Tag.SpendTx, {
|
|
131
|
+
...options, senderId, recipientId, amount,
|
|
132
|
+
}),
|
|
133
|
+
options,
|
|
134
|
+
);
|
|
125
135
|
}
|
|
126
136
|
|
|
127
|
-
type TransferFundsOptionsType = BuildTxOptions<
|
|
128
|
-
& Parameters<typeof resolveName>[2] & { onAccount: AccountBase } & SendOptions
|
|
137
|
+
type TransferFundsOptionsType = BuildTxOptions<Tag.SpendTx, 'senderId' | 'recipientId' | 'amount'>
|
|
138
|
+
& Parameters<typeof resolveName>[2] & { onAccount: AccountBase } & SendOptions;
|
|
129
139
|
interface TransferFundsOptions extends TransferFundsOptionsType {}
|
|
130
140
|
|
|
131
141
|
/**
|
|
132
142
|
* Submit transaction of another account paying for it (fee and gas)
|
|
143
|
+
* @category chain
|
|
133
144
|
* @param transaction - tx_<base64>-encoded transaction
|
|
134
145
|
* @param options - Options
|
|
135
146
|
* @returns Object Transaction
|
|
136
147
|
*/
|
|
137
|
-
export async function payForTransaction
|
|
138
|
-
transaction:
|
|
148
|
+
export async function payForTransaction(
|
|
149
|
+
transaction: Encoded.Transaction,
|
|
150
|
+
options: PayForTransactionOptions,
|
|
139
151
|
): ReturnType<typeof send> {
|
|
140
|
-
return
|
|
152
|
+
return send(
|
|
141
153
|
await _buildTx(
|
|
142
|
-
|
|
143
|
-
{ ...options, payerId: await options.onAccount.address(options), tx: transaction }
|
|
154
|
+
Tag.PayingForTx,
|
|
155
|
+
{ ...options, payerId: await options.onAccount.address(options), tx: transaction },
|
|
144
156
|
),
|
|
145
|
-
options
|
|
146
|
-
)
|
|
157
|
+
options,
|
|
158
|
+
);
|
|
147
159
|
}
|
|
148
160
|
|
|
149
161
|
interface PayForTransactionOptions extends
|
|
150
|
-
BuildTxOptions<
|
|
151
|
-
onAccount: AccountBase
|
|
162
|
+
BuildTxOptions<Tag.PayingForTx, 'payerId' | 'tx'>, SendOptions {
|
|
163
|
+
onAccount: AccountBase;
|
|
152
164
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ArgumentError, PrefixNotFoundError, TagNotFoundError } from '../../utils/errors';
|
|
2
|
+
import { toBytes } from '../../utils/bytes';
|
|
3
|
+
import {
|
|
4
|
+
decode, encode, Encoded, Encoding,
|
|
5
|
+
} from '../../utils/encoder';
|
|
6
|
+
import { isItemOfArray } from '../../utils/other';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Map of prefix to ID tag constant
|
|
10
|
+
* @see {@link https://github.com/aeternity/protocol/blob/master/serializations.md#the-id-type}
|
|
11
|
+
* @see {@link https://github.com/aeternity/aeserialization/blob/eb68fe331bd476910394966b7f5ede7a74d37e35/src/aeser_id.erl#L97-L102}
|
|
12
|
+
* @see {@link https://github.com/aeternity/aeserialization/blob/eb68fe331bd476910394966b7f5ede7a74d37e35/src/aeser_api_encoder.erl#L163-L168}
|
|
13
|
+
*/
|
|
14
|
+
const idTagToEncoding = [
|
|
15
|
+
Encoding.AccountAddress,
|
|
16
|
+
Encoding.Name,
|
|
17
|
+
Encoding.Commitment,
|
|
18
|
+
Encoding.OracleAddress,
|
|
19
|
+
Encoding.ContractAddress,
|
|
20
|
+
Encoding.Channel,
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
type AddressEncodings = typeof idTagToEncoding[number];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Utility function to create and _id type
|
|
27
|
+
* @category transaction builder
|
|
28
|
+
* @param hashId - Encoded hash
|
|
29
|
+
* @returns Buffer Buffer with ID tag and decoded HASh
|
|
30
|
+
*/
|
|
31
|
+
export function writeId(hashId: Encoded.Generic<AddressEncodings>): Buffer {
|
|
32
|
+
if (typeof hashId !== 'string') throw new ArgumentError('hashId', 'a string', hashId);
|
|
33
|
+
const encoding = hashId.slice(0, 2);
|
|
34
|
+
if (!isItemOfArray(encoding, idTagToEncoding)) throw new TagNotFoundError(encoding);
|
|
35
|
+
const idTag = idTagToEncoding.indexOf(encoding) + 1;
|
|
36
|
+
return Buffer.from([...toBytes(idTag), ...decode(hashId)]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Utility function to read and _id type
|
|
41
|
+
* @category transaction builder
|
|
42
|
+
* @param buf - Data
|
|
43
|
+
* @returns Encoided hash string with prefix
|
|
44
|
+
*/
|
|
45
|
+
export function readId(buf: Buffer): Encoded.Generic<AddressEncodings> {
|
|
46
|
+
const idTag = Buffer.from(buf).readUIntBE(0, 1);
|
|
47
|
+
const encoding = idTagToEncoding[idTag - 1];
|
|
48
|
+
if (encoding == null) throw new PrefixNotFoundError(idTag);
|
|
49
|
+
return encode(buf.slice(1, buf.length), encoding);
|
|
50
|
+
}
|
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { mapObject } from '../../utils/other'
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { mapObject } from '../../utils/other';
|
|
4
3
|
|
|
5
4
|
// # AENS
|
|
6
|
-
export type AensName = `${string}.chain
|
|
7
|
-
export const NAME_TTL = 180000
|
|
5
|
+
export type AensName = `${string}.chain`;
|
|
6
|
+
export const NAME_TTL = 180000;
|
|
8
7
|
// # max number of block into the future that the name is going to be available
|
|
9
8
|
// # https://github.com/aeternity/protocol/blob/epoch-v0.22.0/AENS.md#update
|
|
10
9
|
// # https://github.com/aeternity/protocol/blob/44a93d3aab957ca820183c3520b9daf6b0fedff4/AENS.md#aens-entry
|
|
11
|
-
export const NAME_MAX_TTL = 36000
|
|
12
|
-
export const NAME_MAX_CLIENT_TTL = 84600
|
|
13
|
-
export const CLIENT_TTL = NAME_MAX_CLIENT_TTL
|
|
14
|
-
export const MIN_GAS_PRICE = 1e9
|
|
10
|
+
export const NAME_MAX_TTL = 36000;
|
|
11
|
+
export const NAME_MAX_CLIENT_TTL = 84600;
|
|
12
|
+
export const CLIENT_TTL = NAME_MAX_CLIENT_TTL;
|
|
13
|
+
export const MIN_GAS_PRICE = 1e9;
|
|
15
14
|
// # see https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L67
|
|
16
|
-
export const NAME_FEE_MULTIPLIER = 1e14 // 100000000000000
|
|
17
|
-
export const NAME_FEE_BID_INCREMENT = 0.05 // # the increment is in percentage
|
|
15
|
+
export const NAME_FEE_MULTIPLIER = 1e14; // 100000000000000
|
|
16
|
+
export const NAME_FEE_BID_INCREMENT = 0.05; // # the increment is in percentage
|
|
18
17
|
// # see https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L272
|
|
19
|
-
export const NAME_BID_TIMEOUT_BLOCKS = 480 // # ~1 day
|
|
18
|
+
export const NAME_BID_TIMEOUT_BLOCKS = 480; // # ~1 day
|
|
20
19
|
// # this is the max length for a domain that requires a base fee to be paid
|
|
21
|
-
export const NAME_MAX_LENGTH_FEE = 31
|
|
22
|
-
export const NAME_BID_MAX_LENGTH = 12 // # this is the max length for a domain to be part of a bid
|
|
23
|
-
export enum POINTER_KEY_BY_PREFIX {
|
|
24
|
-
ak = 'account_pubkey',
|
|
25
|
-
ok = 'oracle_pubkey',
|
|
26
|
-
ct = 'contract_pubkey',
|
|
27
|
-
ch = 'channel'
|
|
28
|
-
}
|
|
20
|
+
export const NAME_MAX_LENGTH_FEE = 31;
|
|
21
|
+
export const NAME_BID_MAX_LENGTH = 12; // # this is the max length for a domain to be part of a bid
|
|
29
22
|
// # https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L290
|
|
30
23
|
// # https://github.com/aeternity/protocol/blob/master/AENS.md#protocol-fees-and-protection-times
|
|
31
24
|
// # bid ranges:
|
|
@@ -60,37 +53,74 @@ export const NAME_BID_RANGES = mapObject({
|
|
|
60
53
|
4: 1346269,
|
|
61
54
|
3: 2178309,
|
|
62
55
|
2: 3524578,
|
|
63
|
-
1: 5702887
|
|
64
|
-
}, ([key, value]) => [key, new BigNumber(value).times(NAME_FEE_MULTIPLIER)])
|
|
65
|
-
|
|
66
|
-
// # Tag constant for ids (type uint8)
|
|
67
|
-
// # see https://github.com/aeternity/protocol/blob/master/serializations.md#the-id-type
|
|
68
|
-
// # <<Tag:1/unsigned-integer-unit:8, Hash:32/binary-unit:8>>
|
|
69
|
-
const ID_TAG_ACCOUNT = 1
|
|
70
|
-
const ID_TAG_NAME = 2
|
|
71
|
-
const ID_TAG_COMMITMENT = 3
|
|
72
|
-
const ID_TAG_ORACLE = 4
|
|
73
|
-
const ID_TAG_CONTRACT = 5
|
|
74
|
-
const ID_TAG_CHANNEL = 6
|
|
56
|
+
1: 5702887,
|
|
57
|
+
}, ([key, value]) => [key, new BigNumber(value).times(NAME_FEE_MULTIPLIER)]);
|
|
75
58
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Enum with tag types
|
|
61
|
+
* @category transaction builder
|
|
62
|
+
* @see {@link https://github.com/aeternity/protocol/blob/0f6dee3d9d1e8e2469816798f5c7587a6c918f94/serializations.md#binary-serialization}
|
|
63
|
+
* @see {@link https://github.com/aeternity/aeserialization/blob/eb68fe331bd476910394966b7f5ede7a74d37e35/src/aeser_chain_objects.erl#L39-L97}
|
|
64
|
+
*/
|
|
65
|
+
// TODO: implement serialisation for commented-out tags
|
|
66
|
+
export enum Tag {
|
|
67
|
+
Account = 10,
|
|
68
|
+
SignedTx = 11,
|
|
69
|
+
SpendTx = 12,
|
|
70
|
+
// Oracle = 20,
|
|
71
|
+
// OracleQuery = 21,
|
|
72
|
+
OracleRegisterTx = 22,
|
|
73
|
+
OracleQueryTx = 23,
|
|
74
|
+
OracleResponseTx = 24,
|
|
75
|
+
OracleExtendTx = 25,
|
|
76
|
+
// Name = 30,
|
|
77
|
+
// NameCommitment = 31,
|
|
78
|
+
NameClaimTx = 32,
|
|
79
|
+
NamePreclaimTx = 33,
|
|
80
|
+
NameUpdateTx = 34,
|
|
81
|
+
NameRevokeTx = 35,
|
|
82
|
+
NameTransferTx = 36,
|
|
83
|
+
// NameAuction = 37,
|
|
84
|
+
Contract = 40,
|
|
85
|
+
ContractCall = 41,
|
|
86
|
+
ContractCreateTx = 42,
|
|
87
|
+
ContractCallTx = 43,
|
|
88
|
+
ChannelCreateTx = 50,
|
|
89
|
+
// ChannelSetDelegatesTx = 501,
|
|
90
|
+
ChannelDepositTx = 51,
|
|
91
|
+
ChannelWithdrawTx = 52,
|
|
92
|
+
ChannelForceProgressTx = 521,
|
|
93
|
+
ChannelCloseMutualTx = 53,
|
|
94
|
+
ChannelCloseSoloTx = 54,
|
|
95
|
+
ChannelSlashTx = 55,
|
|
96
|
+
ChannelSettleTx = 56,
|
|
97
|
+
ChannelOffChainTx = 57,
|
|
98
|
+
ChannelOffChainUpdateTransfer = 570,
|
|
99
|
+
ChannelOffChainUpdateDeposit = 571,
|
|
100
|
+
ChannelOffChainUpdateWithdraw = 572,
|
|
101
|
+
ChannelOffChainUpdateCreateContract = 573,
|
|
102
|
+
ChannelOffChainUpdateCallContract = 574,
|
|
103
|
+
// ChannelOffChainUpdateMeta = 576,
|
|
104
|
+
ChannelClientReconnectTx = 575,
|
|
105
|
+
Channel = 58,
|
|
106
|
+
ChannelSnapshotSoloTx = 59,
|
|
107
|
+
TreesPoi = 60,
|
|
108
|
+
// TreesDb = 61,
|
|
109
|
+
StateTrees = 62,
|
|
110
|
+
Mtree = 63,
|
|
111
|
+
MtreeValue = 64,
|
|
112
|
+
ContractsMtree = 621,
|
|
113
|
+
CallsMtree = 622,
|
|
114
|
+
ChannelsMtree = 623,
|
|
115
|
+
NameserviceMtree = 624,
|
|
116
|
+
OraclesMtree = 625,
|
|
117
|
+
AccountsMtree = 626,
|
|
118
|
+
CompilerSophia = 70,
|
|
119
|
+
GaAttachTx = 80,
|
|
120
|
+
GaMetaTx = 81,
|
|
121
|
+
PayingForTx = 82,
|
|
122
|
+
// KeyBlock = 100,
|
|
123
|
+
// MicroBlock = 101,
|
|
124
|
+
// LightMicroBlock = 102,
|
|
125
|
+
// Pof = 200,
|
|
83
126
|
}
|
|
84
|
-
|
|
85
|
-
export const PREFIX_ID_TAG = {
|
|
86
|
-
ak: ID_TAG.account,
|
|
87
|
-
nm: ID_TAG.name,
|
|
88
|
-
cm: ID_TAG.commitment,
|
|
89
|
-
ok: ID_TAG.oracle,
|
|
90
|
-
ct: ID_TAG.contract,
|
|
91
|
-
ch: ID_TAG.channel
|
|
92
|
-
} as const
|
|
93
|
-
|
|
94
|
-
export const ID_TAG_PREFIX = mapObject(
|
|
95
|
-
PREFIX_ID_TAG, ([key, value]: [EncodingType, number]) => [value, key]
|
|
96
|
-
)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import uInt, { Int } from './u-int';
|
|
2
|
+
import { AE_AMOUNT_FORMATS, formatAmount } from '../../../utils/amount-formatter';
|
|
3
|
+
import { ArgumentError } from '../../../utils/errors';
|
|
4
|
+
|
|
5
|
+
export interface Options { denomination?: AE_AMOUNT_FORMATS }
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
...uInt,
|
|
9
|
+
|
|
10
|
+
serializeAettos(value: string | undefined): string {
|
|
11
|
+
if (value == null) throw new ArgumentError('value', 'provided', value);
|
|
12
|
+
return value;
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
serializeOptional(value: Int | undefined, { denomination, ...options }: Options): Buffer {
|
|
16
|
+
return uInt.serialize(
|
|
17
|
+
this.serializeAettos(
|
|
18
|
+
value != null ? formatAmount(value, { denomination }) : value,
|
|
19
|
+
options,
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
serialize(value: Int, options: Options): Buffer {
|
|
25
|
+
return this.serializeOptional(value, options);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import coinAmount from './coin-amount';
|
|
2
|
+
import { Int } from './u-int';
|
|
3
|
+
import { IllegalArgumentError } from '../../../utils/errors';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
...coinAmount,
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param value - Deposit value in string format. Should be equal to '0'.
|
|
10
|
+
* @param options - Options
|
|
11
|
+
* @returns Deposit value Buffer.
|
|
12
|
+
*/
|
|
13
|
+
serialize(
|
|
14
|
+
value: Int | undefined,
|
|
15
|
+
options: Parameters<typeof coinAmount['serialize']>[1],
|
|
16
|
+
): Buffer {
|
|
17
|
+
value ??= 0;
|
|
18
|
+
if (+value !== 0) throw new IllegalArgumentError(`Contract deposit is not refundable, so it should be equal 0, got ${value.toString()} instead`);
|
|
19
|
+
return coinAmount.serialize(value, options);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { IllegalArgumentError } from '../../../utils/errors';
|
|
3
|
+
import { MIN_GAS_PRICE, Tag } from '../constants';
|
|
4
|
+
import coinAmount from './coin-amount';
|
|
5
|
+
import { isKeyOfObject } from '../../../utils/other';
|
|
6
|
+
|
|
7
|
+
const BASE_GAS = 15000;
|
|
8
|
+
const GAS_PER_BYTE = 20;
|
|
9
|
+
const KEY_BLOCK_INTERVAL = 3;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculate the Base fee gas
|
|
13
|
+
* @see {@link https://github.com/aeternity/protocol/blob/master/consensus/README.md#gas}
|
|
14
|
+
* @param txType - The transaction type
|
|
15
|
+
* @returns The base fee
|
|
16
|
+
* @example
|
|
17
|
+
* ```js
|
|
18
|
+
* TX_FEE_BASE('channelForceProgress') => new BigNumber(30 * 15000)
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
const TX_FEE_BASE_GAS = (txType: Tag): BigNumber => {
|
|
22
|
+
const feeFactors = {
|
|
23
|
+
[Tag.ChannelForceProgressTx]: 30,
|
|
24
|
+
[Tag.ChannelOffChainTx]: 0,
|
|
25
|
+
[Tag.ChannelOffChainUpdateCallContract]: 0,
|
|
26
|
+
[Tag.ChannelOffChainUpdateCreateContract]: 0,
|
|
27
|
+
[Tag.ChannelOffChainUpdateDeposit]: 0,
|
|
28
|
+
[Tag.ChannelOffChainUpdateWithdraw]: 0,
|
|
29
|
+
[Tag.ChannelOffChainUpdateTransfer]: 0,
|
|
30
|
+
[Tag.ContractCreateTx]: 5,
|
|
31
|
+
[Tag.ContractCallTx]: 12,
|
|
32
|
+
[Tag.GaAttachTx]: 5,
|
|
33
|
+
[Tag.GaMetaTx]: 5,
|
|
34
|
+
[Tag.PayingForTx]: 1 / 5,
|
|
35
|
+
} as const;
|
|
36
|
+
const factor = feeFactors[txType as keyof typeof feeFactors] ?? 1;
|
|
37
|
+
return new BigNumber(factor * BASE_GAS);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Calculate fee for Other types of transactions
|
|
42
|
+
* @see {@link https://github.com/aeternity/protocol/blob/master/consensus/README.md#gas}
|
|
43
|
+
* @param txType - The transaction type
|
|
44
|
+
* @param txSize - The transaction size
|
|
45
|
+
* @returns parameters - The transaction parameters
|
|
46
|
+
* @returns parameters.relativeTtl - The relative ttl
|
|
47
|
+
* @returns parameters.innerTxSize - The size of the inner transaction
|
|
48
|
+
* @returns The Other fee
|
|
49
|
+
* @example
|
|
50
|
+
* ```js
|
|
51
|
+
* TX_FEE_OTHER_GAS('oracleResponse',10, { relativeTtl: 10, innerTxSize: 10 })
|
|
52
|
+
* => new BigNumber(10).times(20).plus(Math.ceil(32000 * 10 / Math.floor(60 * 24 * 365 / 2)))
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
const TX_FEE_OTHER_GAS = (
|
|
56
|
+
txType: Tag,
|
|
57
|
+
txSize: number,
|
|
58
|
+
{ relativeTtl, innerTxSize }: { relativeTtl: number; innerTxSize: number },
|
|
59
|
+
): BigNumber => {
|
|
60
|
+
switch (txType) {
|
|
61
|
+
case Tag.OracleRegisterTx:
|
|
62
|
+
case Tag.OracleExtendTx:
|
|
63
|
+
case Tag.OracleQueryTx:
|
|
64
|
+
case Tag.OracleResponseTx:
|
|
65
|
+
return new BigNumber(txSize)
|
|
66
|
+
.times(GAS_PER_BYTE)
|
|
67
|
+
.plus(
|
|
68
|
+
Math.ceil((32000 * relativeTtl) / Math.floor((60 * 24 * 365) / KEY_BLOCK_INTERVAL)),
|
|
69
|
+
);
|
|
70
|
+
case Tag.GaMetaTx:
|
|
71
|
+
case Tag.PayingForTx:
|
|
72
|
+
return new BigNumber(txSize).minus(innerTxSize).times(GAS_PER_BYTE);
|
|
73
|
+
default:
|
|
74
|
+
return new BigNumber(txSize).times(GAS_PER_BYTE);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function getOracleRelativeTtl(params: any, txType: Tag): number {
|
|
79
|
+
const ttlKeys = {
|
|
80
|
+
[Tag.OracleRegisterTx]: 'oracleTtlValue',
|
|
81
|
+
[Tag.OracleExtendTx]: 'oracleTtlValue',
|
|
82
|
+
[Tag.OracleQueryTx]: 'queryTtlValue',
|
|
83
|
+
[Tag.OracleResponseTx]: 'responseTtlValue',
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
if (!isKeyOfObject(txType, ttlKeys)) return 1;
|
|
87
|
+
return params[ttlKeys[txType]];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Calculate fee based on tx type and params
|
|
92
|
+
*/
|
|
93
|
+
export function buildFee(txType: Tag, buildTx: any): BigNumber {
|
|
94
|
+
const { rlpEncoded: { length }, txObject } = buildTx;
|
|
95
|
+
|
|
96
|
+
return TX_FEE_BASE_GAS(txType)
|
|
97
|
+
.plus(TX_FEE_OTHER_GAS(txType, length, {
|
|
98
|
+
relativeTtl: getOracleRelativeTtl(txObject, txType),
|
|
99
|
+
innerTxSize: [Tag.GaMetaTx, Tag.PayingForTx].includes(txType)
|
|
100
|
+
? txObject.tx.tx.encodedTx.rlpEncoded.length
|
|
101
|
+
: 0,
|
|
102
|
+
}))
|
|
103
|
+
.times(MIN_GAS_PRICE);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Calculate min fee
|
|
108
|
+
* @category transaction builder
|
|
109
|
+
* @param txType - Transaction type
|
|
110
|
+
* @param rebuildTx - Callback to get built transaction with specific fee
|
|
111
|
+
*/
|
|
112
|
+
export function calculateMinFee(
|
|
113
|
+
txType: Tag,
|
|
114
|
+
rebuildTx: (value: BigNumber) => any,
|
|
115
|
+
): BigNumber {
|
|
116
|
+
let fee = new BigNumber(0);
|
|
117
|
+
let previousFee;
|
|
118
|
+
do {
|
|
119
|
+
previousFee = fee;
|
|
120
|
+
fee = buildFee(txType, rebuildTx(fee));
|
|
121
|
+
} while (!fee.eq(previousFee));
|
|
122
|
+
return fee;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export default {
|
|
126
|
+
...coinAmount,
|
|
127
|
+
|
|
128
|
+
serializeAettos(
|
|
129
|
+
_value: string | undefined,
|
|
130
|
+
{ txType, rebuildTx, _computingMinFee }:
|
|
131
|
+
{ txType: Tag; rebuildTx: (params: any) => any; _computingMinFee?: string },
|
|
132
|
+
): string {
|
|
133
|
+
if (_computingMinFee != null) return _computingMinFee;
|
|
134
|
+
const minFee = calculateMinFee(txType, (fee) => rebuildTx({ _computingMinFee: fee }));
|
|
135
|
+
const value = new BigNumber(_value ?? minFee);
|
|
136
|
+
if (minFee.gt(value)) {
|
|
137
|
+
throw new IllegalArgumentError(`Fee ${value.toString()} must be bigger then ${minFee}`);
|
|
138
|
+
}
|
|
139
|
+
return value.toFixed();
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
serialize: coinAmount.serializeOptional,
|
|
143
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { IllegalArgumentError } from '../../../utils/errors';
|
|
2
|
+
import { MIN_GAS_PRICE, Tag } from '../constants';
|
|
3
|
+
import shortUInt from './short-u-int';
|
|
4
|
+
import { buildFee } from './fee';
|
|
5
|
+
|
|
6
|
+
function calculateGasLimitMax(
|
|
7
|
+
txType: Tag,
|
|
8
|
+
gasMax: number,
|
|
9
|
+
rebuildTx: (value: number) => any,
|
|
10
|
+
): number {
|
|
11
|
+
return gasMax - +buildFee(txType, rebuildTx(gasMax)).dividedBy(MIN_GAS_PRICE);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default {
|
|
15
|
+
...shortUInt,
|
|
16
|
+
|
|
17
|
+
serialize(
|
|
18
|
+
_value: number | undefined,
|
|
19
|
+
{
|
|
20
|
+
txType, rebuildTx, gasMax = 6e6, _computingGasLimit,
|
|
21
|
+
}: {
|
|
22
|
+
txType: Tag;
|
|
23
|
+
rebuildTx: (params: any) => any;
|
|
24
|
+
gasMax: number;
|
|
25
|
+
_computingGasLimit?: number;
|
|
26
|
+
},
|
|
27
|
+
): Buffer {
|
|
28
|
+
if (_computingGasLimit != null) return shortUInt.serialize(_computingGasLimit);
|
|
29
|
+
|
|
30
|
+
const gasLimitMax = calculateGasLimitMax(
|
|
31
|
+
txType,
|
|
32
|
+
gasMax,
|
|
33
|
+
(gasLimit) => rebuildTx({ _computingGasLimit: gasLimit }),
|
|
34
|
+
);
|
|
35
|
+
const value = _value ?? gasLimitMax;
|
|
36
|
+
if (value > gasLimitMax) {
|
|
37
|
+
throw new IllegalArgumentError(`Gas limit ${value} must be less or equal to ${gasLimitMax}`);
|
|
38
|
+
}
|
|
39
|
+
return shortUInt.serialize(value);
|
|
40
|
+
},
|
|
41
|
+
};
|