@alephium/web3 2.0.10 → 3.0.0-test.1
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/LICENSE +165 -0
- package/README.md +11 -1
- package/dist/{src → _cjs}/address/address.js +60 -77
- package/dist/_cjs/address/address.js.map +1 -0
- package/dist/{src → _cjs}/address/index.js +1 -17
- package/dist/_cjs/address/index.js.map +1 -0
- package/dist/_cjs/api/api-alephium.d.ts +1152 -0
- package/dist/{src → _cjs}/api/api-alephium.js +5 -756
- package/dist/_cjs/api/api-alephium.js.map +1 -0
- package/dist/_cjs/api/api-explorer.d.ts +586 -0
- package/dist/{src → _cjs}/api/api-explorer.js +14 -454
- package/dist/_cjs/api/api-explorer.js.map +1 -0
- package/dist/{src → _cjs}/api/explorer-provider.js +1 -18
- package/dist/_cjs/api/explorer-provider.js.map +1 -0
- package/dist/{src → _cjs}/api/index.js +18 -24
- package/dist/_cjs/api/index.js.map +1 -0
- package/dist/{src → _cjs}/api/node-provider.d.ts +1 -1
- package/dist/{src → _cjs}/api/node-provider.js +4 -26
- package/dist/_cjs/api/node-provider.js.map +1 -0
- package/dist/{src → _cjs}/api/types.js +25 -43
- package/dist/_cjs/api/types.js.map +1 -0
- package/dist/{src → _cjs}/api/utils.d.ts +1 -1
- package/dist/{src → _cjs}/api/utils.js +9 -21
- package/dist/_cjs/api/utils.js.map +1 -0
- package/dist/{src → _cjs}/block/block.js +20 -26
- package/dist/_cjs/block/block.js.map +1 -0
- package/dist/_cjs/block/index.d.ts +2 -0
- package/dist/_cjs/block/index.js +6 -0
- package/dist/_cjs/block/index.js.map +1 -0
- package/dist/{src → _cjs}/codec/array-codec.js +1 -17
- package/dist/_cjs/codec/array-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/asset-output-codec.js +1 -17
- package/dist/_cjs/codec/asset-output-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/bigint-codec.js +2 -33
- package/dist/_cjs/codec/bigint-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/bytestring-codec.d.ts +1 -1
- package/dist/{src → _cjs}/codec/bytestring-codec.js +1 -17
- package/dist/_cjs/codec/bytestring-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/checksum-codec.js +1 -17
- package/dist/_cjs/codec/checksum-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/codec.js +3 -19
- package/dist/_cjs/codec/codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/compact-int-codec.js +2 -18
- package/dist/_cjs/codec/compact-int-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/contract-codec.js +1 -17
- package/dist/_cjs/codec/contract-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/contract-output-codec.js +1 -17
- package/dist/_cjs/codec/contract-output-codec.js.map +1 -0
- package/dist/_cjs/codec/contract-output-ref-codec.js +12 -0
- package/dist/_cjs/codec/contract-output-ref-codec.js.map +1 -0
- package/dist/_cjs/codec/either-codec.js +8 -0
- package/dist/_cjs/codec/either-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/hash.d.ts +1 -1
- package/dist/_cjs/codec/hash.js +20 -0
- package/dist/_cjs/codec/hash.js.map +1 -0
- package/dist/{src → _cjs}/codec/index.js +18 -24
- package/dist/_cjs/codec/index.js.map +1 -0
- package/dist/{src → _cjs}/codec/input-codec.js +1 -0
- package/dist/_cjs/codec/input-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/instr-codec.js +4 -21
- package/dist/_cjs/codec/instr-codec.js.map +1 -0
- package/dist/_cjs/codec/int-as-4bytes-codec.js +16 -0
- package/dist/_cjs/codec/int-as-4bytes-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/lockup-script-codec.d.ts +1 -1
- package/dist/{src → _cjs}/codec/lockup-script-codec.js +1 -17
- package/dist/_cjs/codec/lockup-script-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/method-codec.js +1 -17
- package/dist/_cjs/codec/method-codec.js.map +1 -0
- package/dist/_cjs/codec/option-codec.js +19 -0
- package/dist/_cjs/codec/option-codec.js.map +1 -0
- package/dist/_cjs/codec/output-codec.js +10 -0
- package/dist/_cjs/codec/output-codec.js.map +1 -0
- package/dist/_cjs/codec/public-key-like-codec.js +14 -0
- package/dist/_cjs/codec/public-key-like-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/reader.js +1 -17
- package/dist/_cjs/codec/reader.js.map +1 -0
- package/dist/_cjs/codec/script-codec.js +18 -0
- package/dist/_cjs/codec/script-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/signature-codec.d.ts +1 -1
- package/dist/_cjs/codec/signature-codec.js +8 -0
- package/dist/_cjs/codec/signature-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/timestamp-codec.js +1 -17
- package/dist/_cjs/codec/timestamp-codec.js.map +1 -0
- package/dist/_cjs/codec/token-codec.js +12 -0
- package/dist/_cjs/codec/token-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/transaction-codec.js +1 -17
- package/dist/_cjs/codec/transaction-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/unlock-script-codec.d.ts +1 -1
- package/dist/{src → _cjs}/codec/unlock-script-codec.js +1 -17
- package/dist/_cjs/codec/unlock-script-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/unsigned-tx-codec.js +1 -0
- package/dist/_cjs/codec/unsigned-tx-codec.js.map +1 -0
- package/dist/{src → _cjs}/codec/val.js +1 -17
- package/dist/_cjs/codec/val.js.map +1 -0
- package/dist/{src → _cjs}/constants.d.ts +0 -3
- package/dist/{src → _cjs}/constants.js +1 -20
- package/dist/_cjs/constants.js.map +1 -0
- package/dist/{src → _cjs}/contract/contract.js +60 -78
- package/dist/_cjs/contract/contract.js.map +1 -0
- package/dist/{src → _cjs}/contract/dapp-tx-builder.js +3 -19
- package/dist/_cjs/contract/dapp-tx-builder.js.map +1 -0
- package/dist/_cjs/contract/deployment.js +3 -0
- package/dist/_cjs/contract/deployment.js.map +1 -0
- package/dist/{src → _cjs}/contract/events.js +20 -26
- package/dist/_cjs/contract/events.js.map +1 -0
- package/dist/{src → _cjs}/contract/index.js +1 -17
- package/dist/_cjs/contract/index.js.map +1 -0
- package/dist/{src → _cjs}/contract/ralph.js +35 -69
- package/dist/_cjs/contract/ralph.js.map +1 -0
- package/dist/{src → _cjs}/contract/script-simulator.d.ts +0 -12
- package/dist/{src → _cjs}/contract/script-simulator.js +23 -64
- package/dist/_cjs/contract/script-simulator.js.map +1 -0
- package/dist/_cjs/debug.js +29 -0
- package/dist/_cjs/debug.js.map +1 -0
- package/dist/{src → _cjs}/error.js +1 -17
- package/dist/_cjs/error.js.map +1 -0
- package/dist/{src → _cjs}/exchange/exchange.js +8 -26
- package/dist/_cjs/exchange/exchange.js.map +1 -0
- package/dist/_cjs/exchange/index.d.ts +2 -0
- package/dist/{src → _cjs}/exchange/index.js +1 -17
- package/dist/_cjs/exchange/index.js.map +1 -0
- package/dist/{src → _cjs}/global.js +7 -26
- package/dist/_cjs/global.js.map +1 -0
- package/dist/{src → _cjs}/index.js +18 -27
- package/dist/_cjs/index.js.map +1 -0
- package/dist/_cjs/package.json +1 -0
- package/dist/{src → _cjs}/signer/index.js +1 -17
- package/dist/_cjs/signer/index.js.map +1 -0
- package/dist/{src → _cjs}/signer/signer.js +16 -63
- package/dist/_cjs/signer/signer.js.map +1 -0
- package/dist/{src → _cjs}/signer/tx-builder.js +4 -21
- package/dist/_cjs/signer/tx-builder.js.map +1 -0
- package/dist/{src → _cjs}/signer/types.js +6 -22
- package/dist/_cjs/signer/types.js.map +1 -0
- package/dist/{src → _cjs}/token/index.js +1 -17
- package/dist/_cjs/token/index.js.map +1 -0
- package/dist/{src → _cjs}/token/nft.d.ts +0 -1
- package/dist/{src → _cjs}/token/nft.js +5 -24
- package/dist/_cjs/token/nft.js.map +1 -0
- package/dist/{src → _cjs}/transaction/index.js +1 -17
- package/dist/_cjs/transaction/index.js.map +1 -0
- package/dist/{src → _cjs}/transaction/sign-verify.js +20 -27
- package/dist/_cjs/transaction/sign-verify.js.map +1 -0
- package/dist/{src → _cjs}/transaction/status.js +20 -26
- package/dist/_cjs/transaction/status.js.map +1 -0
- package/dist/{src → _cjs}/transaction/utils.js +3 -20
- package/dist/_cjs/transaction/utils.js.map +1 -0
- package/dist/{src → _cjs}/utils/bs58.d.ts +0 -1
- package/dist/{src → _cjs}/utils/bs58.js +4 -21
- package/dist/_cjs/utils/bs58.js.map +1 -0
- package/dist/_cjs/utils/djb2.js +11 -0
- package/dist/_cjs/utils/djb2.js.map +1 -0
- package/dist/{src → _cjs}/utils/index.js +1 -17
- package/dist/_cjs/utils/index.js.map +1 -0
- package/dist/{src → _cjs}/utils/number.d.ts +0 -2
- package/dist/{src → _cjs}/utils/number.fixture.js +1 -17
- package/dist/_cjs/utils/number.fixture.js.map +1 -0
- package/dist/{src → _cjs}/utils/number.js +10 -44
- package/dist/_cjs/utils/number.js.map +1 -0
- package/dist/_cjs/utils/sign.js +89 -0
- package/dist/_cjs/utils/sign.js.map +1 -0
- package/dist/{src → _cjs}/utils/subscription.js +1 -17
- package/dist/_cjs/utils/subscription.js.map +1 -0
- package/dist/{src → _cjs}/utils/utils.d.ts +7 -6
- package/dist/{src → _cjs}/utils/utils.js +60 -57
- package/dist/_cjs/utils/utils.js.map +1 -0
- package/dist/{src → _cjs}/utils/webcrypto.d.ts +1 -3
- package/dist/_cjs/utils/webcrypto.js +18 -0
- package/dist/_cjs/utils/webcrypto.js.map +1 -0
- package/dist/_esm/address/address.d.ts +36 -0
- package/dist/_esm/address/address.js +309 -0
- package/dist/_esm/address/address.js.map +1 -0
- package/dist/_esm/address/index.d.ts +1 -0
- package/dist/_esm/address/index.js +2 -0
- package/dist/_esm/address/index.js.map +1 -0
- package/dist/_esm/api/api-alephium.d.ts +1152 -0
- package/dist/_esm/api/api-alephium.js +797 -0
- package/dist/_esm/api/api-alephium.js.map +1 -0
- package/dist/_esm/api/api-explorer.d.ts +586 -0
- package/dist/_esm/api/api-explorer.js +577 -0
- package/dist/_esm/api/api-explorer.js.map +1 -0
- package/dist/_esm/api/explorer-provider.d.ts +21 -0
- package/dist/_esm/api/explorer-provider.js +48 -0
- package/dist/_esm/api/explorer-provider.js.map +1 -0
- package/dist/_esm/api/index.d.ts +6 -0
- package/dist/_esm/api/index.js +7 -0
- package/dist/_esm/api/index.js.map +1 -0
- package/dist/_esm/api/node-provider.d.ts +46 -0
- package/dist/_esm/api/node-provider.js +178 -0
- package/dist/_esm/api/node-provider.js.map +1 -0
- package/dist/_esm/api/types.d.ts +71 -0
- package/dist/_esm/api/types.js +219 -0
- package/dist/_esm/api/types.js.map +1 -0
- package/dist/_esm/api/utils.d.ts +10 -0
- package/dist/_esm/api/utils.js +37 -0
- package/dist/_esm/api/utils.js.map +1 -0
- package/dist/_esm/block/block.d.ts +27 -0
- package/dist/_esm/block/block.js +128 -0
- package/dist/_esm/block/block.js.map +1 -0
- package/dist/_esm/block/index.d.ts +2 -0
- package/dist/_esm/block/index.js +2 -0
- package/dist/_esm/block/index.js.map +1 -0
- package/dist/_esm/codec/array-codec.d.ts +8 -0
- package/dist/_esm/codec/array-codec.js +25 -0
- package/dist/_esm/codec/array-codec.js.map +1 -0
- package/dist/_esm/codec/asset-output-codec.d.ts +21 -0
- package/dist/_esm/codec/asset-output-codec.js +74 -0
- package/dist/_esm/codec/asset-output-codec.js.map +1 -0
- package/dist/_esm/codec/bigint-codec.d.ts +5 -0
- package/dist/_esm/codec/bigint-codec.js +62 -0
- package/dist/_esm/codec/bigint-codec.js.map +1 -0
- package/dist/_esm/codec/bytestring-codec.d.ts +10 -0
- package/dist/_esm/codec/bytestring-codec.js +16 -0
- package/dist/_esm/codec/bytestring-codec.js.map +1 -0
- package/dist/_esm/codec/checksum-codec.d.ts +8 -0
- package/dist/_esm/codec/checksum-codec.js +27 -0
- package/dist/_esm/codec/checksum-codec.js.map +1 -0
- package/dist/_esm/codec/codec.d.ts +57 -0
- package/dist/_esm/codec/codec.js +114 -0
- package/dist/_esm/codec/codec.js.map +1 -0
- package/dist/_esm/codec/compact-int-codec.d.ts +68 -0
- package/dist/_esm/codec/compact-int-codec.js +287 -0
- package/dist/_esm/codec/compact-int-codec.js.map +1 -0
- package/dist/_esm/codec/contract-codec.d.ts +19 -0
- package/dist/_esm/codec/contract-codec.js +47 -0
- package/dist/_esm/codec/contract-codec.js.map +1 -0
- package/dist/_esm/codec/contract-output-codec.d.ts +14 -0
- package/dist/_esm/codec/contract-output-codec.js +40 -0
- package/dist/_esm/codec/contract-output-codec.js.map +1 -0
- package/dist/_esm/codec/contract-output-ref-codec.d.ts +8 -0
- package/dist/_esm/codec/contract-output-ref-codec.js +9 -0
- package/dist/_esm/codec/contract-output-ref-codec.js.map +1 -0
- package/dist/_esm/codec/either-codec.d.ts +9 -0
- package/dist/_esm/codec/either-codec.js +5 -0
- package/dist/_esm/codec/either-codec.js.map +1 -0
- package/dist/_esm/codec/hash.d.ts +3 -0
- package/dist/_esm/codec/hash.js +15 -0
- package/dist/_esm/codec/hash.js.map +1 -0
- package/dist/_esm/codec/index.d.ts +24 -0
- package/dist/_esm/codec/index.js +25 -0
- package/dist/_esm/codec/index.js.map +1 -0
- package/dist/_esm/codec/input-codec.d.ts +15 -0
- package/dist/_esm/codec/input-codec.js +33 -0
- package/dist/_esm/codec/input-codec.js.map +1 -0
- package/dist/_esm/codec/instr-codec.d.ts +857 -0
- package/dist/_esm/codec/instr-codec.js +1138 -0
- package/dist/_esm/codec/instr-codec.js.map +1 -0
- package/dist/_esm/codec/int-as-4bytes-codec.d.ts +7 -0
- package/dist/_esm/codec/int-as-4bytes-codec.js +12 -0
- package/dist/_esm/codec/int-as-4bytes-codec.js.map +1 -0
- package/dist/_esm/codec/lockup-script-codec.d.ts +42 -0
- package/dist/_esm/codec/lockup-script-codec.js +28 -0
- package/dist/_esm/codec/lockup-script-codec.js.map +1 -0
- package/dist/_esm/codec/method-codec.d.ts +20 -0
- package/dist/_esm/codec/method-codec.js +54 -0
- package/dist/_esm/codec/method-codec.js.map +1 -0
- package/dist/_esm/codec/option-codec.d.ts +9 -0
- package/dist/_esm/codec/option-codec.js +16 -0
- package/dist/_esm/codec/option-codec.js.map +1 -0
- package/dist/_esm/codec/output-codec.d.ts +7 -0
- package/dist/_esm/codec/output-codec.js +7 -0
- package/dist/_esm/codec/output-codec.js.map +1 -0
- package/dist/_esm/codec/public-key-like-codec.d.ts +17 -0
- package/dist/_esm/codec/public-key-like-codec.js +11 -0
- package/dist/_esm/codec/public-key-like-codec.js.map +1 -0
- package/dist/_esm/codec/reader.d.ts +10 -0
- package/dist/_esm/codec/reader.js +38 -0
- package/dist/_esm/codec/reader.js.map +1 -0
- package/dist/_esm/codec/script-codec.d.ts +12 -0
- package/dist/_esm/codec/script-codec.js +14 -0
- package/dist/_esm/codec/script-codec.js.map +1 -0
- package/dist/_esm/codec/signature-codec.d.ts +5 -0
- package/dist/_esm/codec/signature-codec.js +5 -0
- package/dist/_esm/codec/signature-codec.js.map +1 -0
- package/dist/_esm/codec/timestamp-codec.d.ts +8 -0
- package/dist/_esm/codec/timestamp-codec.js +19 -0
- package/dist/_esm/codec/timestamp-codec.js.map +1 -0
- package/dist/_esm/codec/token-codec.d.ts +8 -0
- package/dist/_esm/codec/token-codec.js +9 -0
- package/dist/_esm/codec/token-codec.js.map +1 -0
- package/dist/_esm/codec/transaction-codec.d.ts +21 -0
- package/dist/_esm/codec/transaction-codec.js +82 -0
- package/dist/_esm/codec/transaction-codec.js.map +1 -0
- package/dist/_esm/codec/unlock-script-codec.d.ts +44 -0
- package/dist/_esm/codec/unlock-script-codec.js +49 -0
- package/dist/_esm/codec/unlock-script-codec.js.map +1 -0
- package/dist/_esm/codec/unsigned-tx-codec.d.ts +23 -0
- package/dist/_esm/codec/unsigned-tx-codec.js +57 -0
- package/dist/_esm/codec/unsigned-tx-codec.js.map +1 -0
- package/dist/_esm/codec/val.d.ts +27 -0
- package/dist/_esm/codec/val.js +14 -0
- package/dist/_esm/codec/val.js.map +1 -0
- package/dist/_esm/constants.d.ts +14 -0
- package/dist/_esm/constants.js +15 -0
- package/dist/_esm/constants.js.map +1 -0
- package/dist/_esm/contract/contract.d.ts +328 -0
- package/dist/_esm/contract/contract.js +1449 -0
- package/dist/_esm/contract/contract.js.map +1 -0
- package/dist/_esm/contract/dapp-tx-builder.d.ts +26 -0
- package/dist/_esm/contract/dapp-tx-builder.js +166 -0
- package/dist/_esm/contract/dapp-tx-builder.js.map +1 -0
- package/dist/_esm/contract/deployment.d.ts +21 -0
- package/dist/_esm/contract/deployment.js +2 -0
- package/dist/_esm/contract/deployment.js.map +1 -0
- package/dist/_esm/contract/events.d.ts +17 -0
- package/dist/_esm/contract/events.js +59 -0
- package/dist/_esm/contract/events.js.map +1 -0
- package/dist/_esm/contract/index.d.ts +6 -0
- package/dist/_esm/contract/index.js +7 -0
- package/dist/_esm/contract/index.js.map +1 -0
- package/dist/_esm/contract/ralph.d.ts +73 -0
- package/dist/_esm/contract/ralph.js +372 -0
- package/dist/_esm/contract/ralph.js.map +1 -0
- package/dist/_esm/contract/script-simulator.d.ts +15 -0
- package/dist/_esm/contract/script-simulator.js +712 -0
- package/dist/_esm/contract/script-simulator.js.map +1 -0
- package/dist/_esm/debug.d.ts +6 -0
- package/dist/_esm/debug.js +21 -0
- package/dist/_esm/debug.js.map +1 -0
- package/dist/_esm/error.d.ts +4 -0
- package/dist/_esm/error.js +16 -0
- package/dist/_esm/error.js.map +1 -0
- package/dist/_esm/exchange/exchange.d.ts +21 -0
- package/dist/_esm/exchange/exchange.js +137 -0
- package/dist/_esm/exchange/exchange.js.map +1 -0
- package/dist/_esm/exchange/index.d.ts +2 -0
- package/dist/_esm/exchange/index.js +2 -0
- package/dist/_esm/exchange/index.js.map +1 -0
- package/dist/_esm/global.d.ts +10 -0
- package/dist/_esm/global.js +49 -0
- package/dist/_esm/global.js.map +1 -0
- package/dist/_esm/index.d.ts +15 -0
- package/dist/_esm/index.js +16 -0
- package/dist/_esm/index.js.map +1 -0
- package/dist/_esm/package.json +1 -0
- package/dist/_esm/signer/index.d.ts +3 -0
- package/dist/_esm/signer/index.js +4 -0
- package/dist/_esm/signer/index.js.map +1 -0
- package/dist/_esm/signer/signer.d.ts +63 -0
- package/dist/_esm/signer/signer.js +279 -0
- package/dist/_esm/signer/signer.js.map +1 -0
- package/dist/_esm/signer/tx-builder.d.ts +22 -0
- package/dist/_esm/signer/tx-builder.js +270 -0
- package/dist/_esm/signer/tx-builder.js.map +1 -0
- package/dist/_esm/signer/types.d.ts +167 -0
- package/dist/_esm/signer/types.js +27 -0
- package/dist/_esm/signer/types.js.map +1 -0
- package/dist/_esm/token/index.d.ts +1 -0
- package/dist/_esm/token/index.js +2 -0
- package/dist/_esm/token/index.js.map +1 -0
- package/dist/_esm/token/nft.d.ts +8 -0
- package/dist/_esm/token/nft.js +95 -0
- package/dist/_esm/token/nft.js.map +1 -0
- package/dist/_esm/transaction/index.d.ts +3 -0
- package/dist/_esm/transaction/index.js +4 -0
- package/dist/_esm/transaction/index.js.map +1 -0
- package/dist/_esm/transaction/sign-verify.d.ts +3 -0
- package/dist/_esm/transaction/sign-verify.js +8 -0
- package/dist/_esm/transaction/sign-verify.js.map +1 -0
- package/dist/_esm/transaction/status.d.ts +12 -0
- package/dist/_esm/transaction/status.js +33 -0
- package/dist/_esm/transaction/status.js.map +1 -0
- package/dist/_esm/transaction/utils.d.ts +4 -0
- package/dist/_esm/transaction/utils.js +35 -0
- package/dist/_esm/transaction/utils.js.map +1 -0
- package/dist/_esm/utils/bs58.d.ts +5 -0
- package/dist/_esm/utils/bs58.js +25 -0
- package/dist/_esm/utils/bs58.js.map +1 -0
- package/dist/_esm/utils/djb2.d.ts +1 -0
- package/dist/_esm/utils/djb2.js +8 -0
- package/dist/_esm/utils/djb2.js.map +1 -0
- package/dist/_esm/utils/index.d.ts +7 -0
- package/dist/_esm/utils/index.js +8 -0
- package/dist/_esm/utils/index.js.map +1 -0
- package/dist/_esm/utils/number.d.ts +17 -0
- package/dist/_esm/utils/number.fixture.d.ts +12 -0
- package/dist/_esm/utils/number.fixture.js +170 -0
- package/dist/_esm/utils/number.fixture.js.map +1 -0
- package/dist/_esm/utils/number.js +104 -0
- package/dist/_esm/utils/number.js.map +1 -0
- package/dist/_esm/utils/sign.d.ts +3 -0
- package/dist/_esm/utils/sign.js +52 -0
- package/dist/_esm/utils/sign.js.map +1 -0
- package/dist/_esm/utils/subscription.d.ts +22 -0
- package/dist/_esm/utils/subscription.js +31 -0
- package/dist/_esm/utils/subscription.js.map +1 -0
- package/dist/_esm/utils/utils.d.ts +40 -0
- package/dist/_esm/utils/utils.js +116 -0
- package/dist/_esm/utils/utils.js.map +1 -0
- package/dist/_esm/utils/webcrypto.d.ts +4 -0
- package/dist/_esm/utils/webcrypto.js +14 -0
- package/dist/_esm/utils/webcrypto.js.map +1 -0
- package/package.json +58 -34
- package/src/address/address.ts +12 -17
- package/src/api/api-alephium.ts +4 -5
- package/src/api/api-explorer.ts +4 -5
- package/src/api/types.ts +3 -2
- package/src/api/utils.ts +13 -1
- package/src/block/index.ts +2 -1
- package/src/codec/hash.ts +2 -2
- package/src/contract/contract.ts +14 -12
- package/src/exchange/index.ts +1 -3
- package/src/index.ts +0 -4
- package/src/signer/signer.ts +17 -11
- package/src/token/nft.ts +0 -1
- package/src/utils/sign.ts +19 -20
- package/src/utils/utils.ts +9 -20
- package/src/utils/webcrypto.ts +4 -11
- package/tsconfig.build.json +9 -0
- package/dist/alephium-web3.min.js +0 -3
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -12
- package/dist/alephium-web3.min.js.map +0 -1
- package/dist/scripts/check-versions.d.ts +0 -1
- package/dist/scripts/check-versions.js +0 -39
- package/dist/scripts/header.d.ts +0 -0
- package/dist/scripts/header.js +0 -18
- package/dist/src/api/api-alephium.d.ts +0 -2433
- package/dist/src/api/api-explorer.d.ts +0 -1472
- package/dist/src/block/index.d.ts +0 -1
- package/dist/src/block/index.js +0 -22
- package/dist/src/codec/contract-output-ref-codec.js +0 -28
- package/dist/src/codec/either-codec.js +0 -25
- package/dist/src/codec/hash.js +0 -40
- package/dist/src/codec/int-as-4bytes-codec.js +0 -32
- package/dist/src/codec/option-codec.js +0 -36
- package/dist/src/codec/output-codec.js +0 -26
- package/dist/src/codec/public-key-like-codec.js +0 -30
- package/dist/src/codec/script-codec.js +0 -34
- package/dist/src/codec/signature-codec.js +0 -24
- package/dist/src/codec/token-codec.js +0 -28
- package/dist/src/contract/deployment.js +0 -19
- package/dist/src/debug.js +0 -46
- package/dist/src/exchange/index.d.ts +0 -1
- package/dist/src/utils/djb2.js +0 -27
- package/dist/src/utils/sign.js +0 -96
- package/dist/src/utils/webcrypto.js +0 -42
- package/webpack.config.js +0 -56
- /package/dist/{src → _cjs}/address/address.d.ts +0 -0
- /package/dist/{src → _cjs}/address/index.d.ts +0 -0
- /package/dist/{src → _cjs}/api/explorer-provider.d.ts +0 -0
- /package/dist/{src → _cjs}/api/index.d.ts +0 -0
- /package/dist/{src → _cjs}/api/types.d.ts +0 -0
- /package/dist/{src → _cjs}/block/block.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/array-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/asset-output-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/bigint-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/checksum-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/compact-int-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/contract-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/contract-output-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/contract-output-ref-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/either-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/index.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/input-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/instr-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/int-as-4bytes-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/method-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/option-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/output-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/public-key-like-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/reader.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/script-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/timestamp-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/token-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/transaction-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/unsigned-tx-codec.d.ts +0 -0
- /package/dist/{src → _cjs}/codec/val.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/contract.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/dapp-tx-builder.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/deployment.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/events.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/index.d.ts +0 -0
- /package/dist/{src → _cjs}/contract/ralph.d.ts +0 -0
- /package/dist/{src → _cjs}/debug.d.ts +0 -0
- /package/dist/{src → _cjs}/error.d.ts +0 -0
- /package/dist/{src → _cjs}/exchange/exchange.d.ts +0 -0
- /package/dist/{src → _cjs}/global.d.ts +0 -0
- /package/dist/{src → _cjs}/index.d.ts +0 -0
- /package/dist/{src → _cjs}/signer/index.d.ts +0 -0
- /package/dist/{src → _cjs}/signer/signer.d.ts +0 -0
- /package/dist/{src → _cjs}/signer/tx-builder.d.ts +0 -0
- /package/dist/{src → _cjs}/signer/types.d.ts +0 -0
- /package/dist/{src → _cjs}/token/index.d.ts +0 -0
- /package/dist/{src → _cjs}/transaction/index.d.ts +0 -0
- /package/dist/{src → _cjs}/transaction/sign-verify.d.ts +0 -0
- /package/dist/{src → _cjs}/transaction/status.d.ts +0 -0
- /package/dist/{src → _cjs}/transaction/utils.d.ts +0 -0
- /package/dist/{src → _cjs}/utils/djb2.d.ts +0 -0
- /package/dist/{src → _cjs}/utils/index.d.ts +0 -0
- /package/dist/{src → _cjs}/utils/number.fixture.d.ts +0 -0
- /package/dist/{src → _cjs}/utils/sign.d.ts +0 -0
- /package/dist/{src → _cjs}/utils/subscription.d.ts +0 -0
|
@@ -0,0 +1,1449 @@
|
|
|
1
|
+
import { fromApiNumber256, toApiNumber256, toApiToken, toApiVal, fromApiTokens, getDefaultPrimitiveValue, PrimitiveTypes, decodeArrayType, fromApiPrimitiveVal, tryGetCallResult, decodeTupleType, stringify } from '../api';
|
|
2
|
+
import * as ralph from './ralph';
|
|
3
|
+
import { bs58, binToHex, assertType, WebCrypto, hexToBinUnsafe, isDevnet, isHexString, hexToString } from '../utils';
|
|
4
|
+
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId, isGrouplessAddress, isValidAddress, addressWithoutExplicitGroupIndex } from '../address';
|
|
5
|
+
import { getCurrentNodeProvider } from '../global';
|
|
6
|
+
import { subscribeToEvents } from './events';
|
|
7
|
+
import { MINIMAL_CONTRACT_DEPOSIT, ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants';
|
|
8
|
+
import { blake2b } from '@noble/hashes/blake2b';
|
|
9
|
+
import { isContractDebugMessageEnabled } from '../debug';
|
|
10
|
+
import { contract, LoadLocal, LoadImmFieldByIndex, LoadMutFieldByIndex, CallerContractId, LoadImmField, ByteVecEq, Assert, StoreMutFieldByIndex, DestroySelf, Pop, StoreLocal, instrCodec, U256Const, ApproveToken, ApproveAlph, CallExternal, Dup, CallerAddress, i32Codec, BytesConst } from '../codec';
|
|
11
|
+
import { TraceableError } from '../error';
|
|
12
|
+
const crypto = new WebCrypto();
|
|
13
|
+
export const StdIdFieldName = '__stdInterfaceId';
|
|
14
|
+
export const DEFAULT_NODE_COMPILER_OPTIONS = {
|
|
15
|
+
ignoreUnusedConstantsWarnings: false,
|
|
16
|
+
ignoreUnusedVariablesWarnings: false,
|
|
17
|
+
ignoreUnusedFieldsWarnings: false,
|
|
18
|
+
ignoreUnusedPrivateFunctionsWarnings: false,
|
|
19
|
+
ignoreUpdateFieldsCheckWarnings: false,
|
|
20
|
+
ignoreCheckExternalCallerWarnings: false,
|
|
21
|
+
ignoreUnusedFunctionReturnWarnings: false,
|
|
22
|
+
skipAbstractContractCheck: false,
|
|
23
|
+
skipTests: false
|
|
24
|
+
};
|
|
25
|
+
export const DEFAULT_COMPILER_OPTIONS = { errorOnWarnings: true, ...DEFAULT_NODE_COMPILER_OPTIONS };
|
|
26
|
+
export class Struct {
|
|
27
|
+
constructor(name, fieldNames, fieldTypes, isMutable) {
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.fieldNames = fieldNames;
|
|
30
|
+
this.fieldTypes = fieldTypes;
|
|
31
|
+
this.isMutable = isMutable;
|
|
32
|
+
}
|
|
33
|
+
static fromJson(json) {
|
|
34
|
+
if (json.name === null || json.fieldNames === null || json.fieldTypes === null || json.isMutable === null) {
|
|
35
|
+
throw Error('The JSON for struct is incomplete');
|
|
36
|
+
}
|
|
37
|
+
return new Struct(json.name, json.fieldNames, json.fieldTypes, json.isMutable);
|
|
38
|
+
}
|
|
39
|
+
static fromStructSig(sig) {
|
|
40
|
+
return new Struct(sig.name, sig.fieldNames, sig.fieldTypes, sig.isMutable);
|
|
41
|
+
}
|
|
42
|
+
toJson() {
|
|
43
|
+
return {
|
|
44
|
+
name: this.name,
|
|
45
|
+
fieldNames: this.fieldNames,
|
|
46
|
+
fieldTypes: this.fieldTypes,
|
|
47
|
+
isMutable: this.isMutable
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class Artifact {
|
|
52
|
+
constructor(version, name, functions) {
|
|
53
|
+
this.version = version;
|
|
54
|
+
this.name = name;
|
|
55
|
+
this.functions = functions;
|
|
56
|
+
}
|
|
57
|
+
async isDevnet(signer) {
|
|
58
|
+
if (!signer.nodeProvider) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
const chainParams = await signer.nodeProvider.infos.getInfosChainParams();
|
|
62
|
+
return isDevnet(chainParams.networkId);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function fromFunctionSig(sig) {
|
|
66
|
+
return {
|
|
67
|
+
name: sig.name,
|
|
68
|
+
paramNames: sig.paramNames,
|
|
69
|
+
paramTypes: sig.paramTypes,
|
|
70
|
+
paramIsMutable: sig.paramIsMutable,
|
|
71
|
+
returnTypes: sig.returnTypes
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export class Contract extends Artifact {
|
|
75
|
+
constructor(version, name, bytecode, bytecodeDebugPatch, codeHash, codeHashDebug, fieldsSig, eventsSig, functions, constants, enums, structs, mapsSig, stdInterfaceId) {
|
|
76
|
+
super(version, name, functions);
|
|
77
|
+
this.bytecode = bytecode;
|
|
78
|
+
this.bytecodeDebugPatch = bytecodeDebugPatch;
|
|
79
|
+
this.codeHash = codeHash;
|
|
80
|
+
this.fieldsSig = fieldsSig;
|
|
81
|
+
this.eventsSig = eventsSig;
|
|
82
|
+
this.constants = constants;
|
|
83
|
+
this.enums = enums;
|
|
84
|
+
this.structs = structs;
|
|
85
|
+
this.mapsSig = mapsSig;
|
|
86
|
+
this.stdInterfaceId = stdInterfaceId;
|
|
87
|
+
this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch);
|
|
88
|
+
this.codeHashDebug = codeHashDebug;
|
|
89
|
+
this.decodedContract = contract.contractCodec.decodeContract(hexToBinUnsafe(this.bytecode));
|
|
90
|
+
this.bytecodeForTesting = undefined;
|
|
91
|
+
this.decodedTestingContract = undefined;
|
|
92
|
+
this.codeHashForTesting = undefined;
|
|
93
|
+
}
|
|
94
|
+
isInlineFunc(index) {
|
|
95
|
+
if (index >= this.functions.length) {
|
|
96
|
+
throw new Error(`Invalid function index ${index}, function size: ${this.functions.length}`);
|
|
97
|
+
}
|
|
98
|
+
const inlineFuncFromIndex = this.decodedContract.methods.length;
|
|
99
|
+
return index >= inlineFuncFromIndex;
|
|
100
|
+
}
|
|
101
|
+
getByteCodeForTesting() {
|
|
102
|
+
if (this.bytecodeForTesting !== undefined)
|
|
103
|
+
return this.bytecodeForTesting;
|
|
104
|
+
const hasInlineFunction = this.functions.length > this.decodedContract.methods.length;
|
|
105
|
+
if (!hasInlineFunction && this.publicFunctions().length == this.functions.length) {
|
|
106
|
+
this.bytecodeForTesting = this.bytecodeDebug;
|
|
107
|
+
this.codeHashForTesting = this.codeHashDebug;
|
|
108
|
+
return this.bytecodeForTesting;
|
|
109
|
+
}
|
|
110
|
+
const decodedDebugContract = contract.contractCodec.decodeContract(hexToBinUnsafe(this.bytecodeDebug));
|
|
111
|
+
const methods = decodedDebugContract.methods.map((method) => ({ ...method, isPublic: true }));
|
|
112
|
+
const bytecodeForTesting = contract.contractCodec.encodeContract({
|
|
113
|
+
fieldLength: decodedDebugContract.fieldLength,
|
|
114
|
+
methods: methods
|
|
115
|
+
});
|
|
116
|
+
const codeHashForTesting = blake2b(bytecodeForTesting, { dkLen: 32 });
|
|
117
|
+
this.bytecodeForTesting = binToHex(bytecodeForTesting);
|
|
118
|
+
this.codeHashForTesting = binToHex(codeHashForTesting);
|
|
119
|
+
return this.bytecodeForTesting;
|
|
120
|
+
}
|
|
121
|
+
getDecodedTestingContract() {
|
|
122
|
+
if (this.decodedTestingContract !== undefined)
|
|
123
|
+
return this.decodedTestingContract;
|
|
124
|
+
const bytecodeForTesting = hexToBinUnsafe(this.getByteCodeForTesting());
|
|
125
|
+
this.decodedTestingContract = contract.contractCodec.decodeContract(bytecodeForTesting);
|
|
126
|
+
return this.decodedTestingContract;
|
|
127
|
+
}
|
|
128
|
+
hasCodeHash(hash) {
|
|
129
|
+
return this.codeHash === hash || this.codeHashDebug === hash || this.codeHashForTesting === hash;
|
|
130
|
+
}
|
|
131
|
+
getDecodedMethod(methodIndex) {
|
|
132
|
+
return this.decodedContract.methods[`${methodIndex}`];
|
|
133
|
+
}
|
|
134
|
+
publicFunctions() {
|
|
135
|
+
return this.functions.filter((_, index) => this.getDecodedMethod(index).isPublic);
|
|
136
|
+
}
|
|
137
|
+
usingPreapprovedAssetsFunctions() {
|
|
138
|
+
return this.functions.filter((_, index) => this.getDecodedMethod(index).usePreapprovedAssets);
|
|
139
|
+
}
|
|
140
|
+
usingAssetsInContractFunctions() {
|
|
141
|
+
return this.functions.filter((_, index) => this.getDecodedMethod(index).useContractAssets);
|
|
142
|
+
}
|
|
143
|
+
isMethodUsePreapprovedAssets(isDevnet, methodIndex) {
|
|
144
|
+
if (!isDevnet || !this.isInlineFunc(methodIndex))
|
|
145
|
+
return this.getDecodedMethod(methodIndex).usePreapprovedAssets;
|
|
146
|
+
const contract = this.getDecodedTestingContract();
|
|
147
|
+
return contract.methods[`${methodIndex}`].usePreapprovedAssets;
|
|
148
|
+
}
|
|
149
|
+
static fromJson(artifact, bytecodeDebugPatch = '', codeHashDebug = '', structs = []) {
|
|
150
|
+
if (artifact.version == null ||
|
|
151
|
+
artifact.name == null ||
|
|
152
|
+
artifact.bytecode == null ||
|
|
153
|
+
artifact.codeHash == null ||
|
|
154
|
+
artifact.fieldsSig == null ||
|
|
155
|
+
artifact.eventsSig == null ||
|
|
156
|
+
artifact.constants == null ||
|
|
157
|
+
artifact.enums == null ||
|
|
158
|
+
artifact.functions == null) {
|
|
159
|
+
throw Error('The artifact JSON for contract is incomplete');
|
|
160
|
+
}
|
|
161
|
+
const contract = new Contract(artifact.version, artifact.name, artifact.bytecode, bytecodeDebugPatch, artifact.codeHash, codeHashDebug ? codeHashDebug : artifact.codeHash, artifact.fieldsSig, artifact.eventsSig, artifact.functions, artifact.constants, artifact.enums, structs, artifact.mapsSig === null ? undefined : artifact.mapsSig, artifact.stdInterfaceId === null ? undefined : artifact.stdInterfaceId);
|
|
162
|
+
return contract;
|
|
163
|
+
}
|
|
164
|
+
static fromCompileResult(result, structs = []) {
|
|
165
|
+
return new Contract(result.version, result.name, result.bytecode, result.bytecodeDebugPatch, result.codeHash, result.codeHashDebug, result.fields, result.events, result.functions.map(fromFunctionSig), result.constants, result.enums, structs, result.maps, result.stdInterfaceId);
|
|
166
|
+
}
|
|
167
|
+
static async fromArtifactFile(path, bytecodeDebugPatch, codeHashDebug, structs = []) {
|
|
168
|
+
const fs = await import('fs');
|
|
169
|
+
const content = await fs.promises.readFile(path);
|
|
170
|
+
const artifact = JSON.parse(content.toString());
|
|
171
|
+
return Contract.fromJson(artifact, bytecodeDebugPatch, codeHashDebug, structs);
|
|
172
|
+
}
|
|
173
|
+
toString() {
|
|
174
|
+
const object = {
|
|
175
|
+
version: this.version,
|
|
176
|
+
name: this.name,
|
|
177
|
+
bytecode: this.bytecode,
|
|
178
|
+
codeHash: this.codeHash,
|
|
179
|
+
fieldsSig: this.fieldsSig,
|
|
180
|
+
eventsSig: this.eventsSig,
|
|
181
|
+
functions: this.functions,
|
|
182
|
+
constants: this.constants,
|
|
183
|
+
enums: this.enums
|
|
184
|
+
};
|
|
185
|
+
if (this.mapsSig !== undefined) {
|
|
186
|
+
object.mapsSig = this.mapsSig;
|
|
187
|
+
}
|
|
188
|
+
if (this.stdInterfaceId !== undefined) {
|
|
189
|
+
object.stdInterfaceId = this.stdInterfaceId;
|
|
190
|
+
}
|
|
191
|
+
return stringify(object, null, 2);
|
|
192
|
+
}
|
|
193
|
+
getInitialFieldsWithDefaultValues() {
|
|
194
|
+
const fields = this.stdInterfaceId === undefined
|
|
195
|
+
? this.fieldsSig
|
|
196
|
+
: {
|
|
197
|
+
names: this.fieldsSig.names.slice(0, -1),
|
|
198
|
+
types: this.fieldsSig.types.slice(0, -1),
|
|
199
|
+
isMutable: this.fieldsSig.isMutable.slice(0, -1)
|
|
200
|
+
};
|
|
201
|
+
return getDefaultValue(fields, this.structs);
|
|
202
|
+
}
|
|
203
|
+
toState(fields, asset, address) {
|
|
204
|
+
const addressDef = typeof address !== 'undefined' ? address : Contract.randomAddress();
|
|
205
|
+
return {
|
|
206
|
+
address: addressDef,
|
|
207
|
+
contractId: binToHex(contractIdFromAddress(addressDef)),
|
|
208
|
+
bytecode: this.bytecode,
|
|
209
|
+
codeHash: this.codeHash,
|
|
210
|
+
fields: fields,
|
|
211
|
+
fieldsSig: this.fieldsSig,
|
|
212
|
+
asset: asset
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
static randomAddress() {
|
|
216
|
+
const bytes = new Uint8Array(33);
|
|
217
|
+
crypto.getRandomValues(bytes);
|
|
218
|
+
bytes[0] = 3;
|
|
219
|
+
return bs58.encode(bytes);
|
|
220
|
+
}
|
|
221
|
+
printDebugMessages(funcName, messages) {
|
|
222
|
+
if (isContractDebugMessageEnabled() && messages.length != 0) {
|
|
223
|
+
console.log(`Testing ${this.name}.${funcName}:`);
|
|
224
|
+
messages.forEach((m) => printDebugMessage(m));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
toApiFields(fields) {
|
|
228
|
+
if (typeof fields === 'undefined') {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
return toApiFields(fields, this.fieldsSig, this.structs);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
toApiArgs(funcName, args) {
|
|
236
|
+
if (args) {
|
|
237
|
+
const func = this.functions.find((func) => func.name == funcName);
|
|
238
|
+
if (func == null) {
|
|
239
|
+
throw new Error(`Invalid function name: ${funcName}`);
|
|
240
|
+
}
|
|
241
|
+
return toApiArgs(args, func, this.structs);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
getMethodIndex(funcName) {
|
|
248
|
+
return this.functions.findIndex((func) => func.name === funcName);
|
|
249
|
+
}
|
|
250
|
+
toApiContractStates(states) {
|
|
251
|
+
return typeof states != 'undefined' ? states.map((state) => toApiContractState(state, this.structs)) : undefined;
|
|
252
|
+
}
|
|
253
|
+
toApiTestContractParams(funcName, params) {
|
|
254
|
+
const allFields = params.initialFields === undefined
|
|
255
|
+
? []
|
|
256
|
+
: ralph.flattenFields(params.initialFields, this.fieldsSig.names, this.fieldsSig.types, this.fieldsSig.isMutable, this.structs);
|
|
257
|
+
const immFields = allFields.filter((f) => !f.isMutable).map((f) => toApiVal(f.value, f.type));
|
|
258
|
+
const mutFields = allFields.filter((f) => f.isMutable).map((f) => toApiVal(f.value, f.type));
|
|
259
|
+
const methodIndex = this.getMethodIndex(funcName);
|
|
260
|
+
return {
|
|
261
|
+
group: params.group,
|
|
262
|
+
blockHash: params.blockHash,
|
|
263
|
+
blockTimeStamp: params.blockTimeStamp,
|
|
264
|
+
txId: params.txId,
|
|
265
|
+
address: params.contractAddress,
|
|
266
|
+
callerContractAddress: params.callerContractAddress,
|
|
267
|
+
bytecode: this.isInlineFunc(methodIndex) ? this.getByteCodeForTesting() : this.bytecodeDebug,
|
|
268
|
+
initialImmFields: immFields,
|
|
269
|
+
initialMutFields: mutFields,
|
|
270
|
+
initialAsset: typeof params.initialAsset !== 'undefined' ? toApiAsset(params.initialAsset) : undefined,
|
|
271
|
+
methodIndex,
|
|
272
|
+
args: this.toApiArgs(funcName, params.args),
|
|
273
|
+
existingContracts: this.toApiContractStates(params.existingContracts),
|
|
274
|
+
inputAssets: toApiInputAssets(params.inputAssets),
|
|
275
|
+
dustAmount: params.dustAmount?.toString()
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
fromApiContractState(state) {
|
|
279
|
+
return {
|
|
280
|
+
address: state.address,
|
|
281
|
+
contractId: binToHex(contractIdFromAddress(state.address)),
|
|
282
|
+
bytecode: state.bytecode,
|
|
283
|
+
initialStateHash: state.initialStateHash,
|
|
284
|
+
codeHash: state.codeHash,
|
|
285
|
+
fields: fromApiFields(state.immFields, state.mutFields, this.fieldsSig, this.structs),
|
|
286
|
+
fieldsSig: this.fieldsSig,
|
|
287
|
+
asset: fromApiAsset(state.asset)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
static fromApiContractState(state, getContractByCodeHash) {
|
|
291
|
+
const contract = getContractByCodeHash(state.codeHash);
|
|
292
|
+
return contract.fromApiContractState(state);
|
|
293
|
+
}
|
|
294
|
+
static fromApiEvent(event, codeHash, txId, getContractByCodeHash) {
|
|
295
|
+
let fields;
|
|
296
|
+
let name;
|
|
297
|
+
if (event.eventIndex == Contract.ContractCreatedEventIndex) {
|
|
298
|
+
fields = toContractCreatedEventFields(fromApiEventFields(event.fields, Contract.ContractCreatedEvent, true));
|
|
299
|
+
name = Contract.ContractCreatedEvent.name;
|
|
300
|
+
}
|
|
301
|
+
else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
|
|
302
|
+
fields = fromApiEventFields(event.fields, Contract.ContractDestroyedEvent, true);
|
|
303
|
+
name = Contract.ContractDestroyedEvent.name;
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
const contract = getContractByCodeHash(codeHash);
|
|
307
|
+
const eventSig = contract.eventsSig[event.eventIndex];
|
|
308
|
+
fields = fromApiEventFields(event.fields, eventSig);
|
|
309
|
+
name = eventSig.name;
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
txId: txId,
|
|
313
|
+
blockHash: event.blockHash,
|
|
314
|
+
contractAddress: event.contractAddress,
|
|
315
|
+
name: name,
|
|
316
|
+
eventIndex: event.eventIndex,
|
|
317
|
+
fields: fields
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
fromApiTestContractResult(methodName, result, txId, getContractByCodeHash) {
|
|
321
|
+
const methodIndex = this.functions.findIndex((sig) => sig.name === methodName);
|
|
322
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
323
|
+
const rawReturn = fromApiArray(result.returns, returnTypes, this.structs);
|
|
324
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
325
|
+
const addressToCodeHash = new Map();
|
|
326
|
+
addressToCodeHash.set(result.address, result.codeHash);
|
|
327
|
+
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
328
|
+
return {
|
|
329
|
+
contractId: binToHex(contractIdFromAddress(result.address)),
|
|
330
|
+
contractAddress: result.address,
|
|
331
|
+
returns: returns,
|
|
332
|
+
gasUsed: result.gasUsed,
|
|
333
|
+
contracts: result.contracts.map((contract) => Contract.fromApiContractState(contract, getContractByCodeHash)),
|
|
334
|
+
txOutputs: result.txOutputs.map(fromApiOutput),
|
|
335
|
+
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId, getContractByCodeHash),
|
|
336
|
+
debugMessages: result.debugMessages
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
async txParamsForDeployment(signer, params, group) {
|
|
340
|
+
const isDevnet = await this.isDevnet(signer);
|
|
341
|
+
const initialFields = params.initialFields ?? {};
|
|
342
|
+
const bytecode = this.buildByteCodeToDeploy(addStdIdToFields(this, initialFields), isDevnet, params.exposePrivateFunctions ?? false);
|
|
343
|
+
const selectedAccount = await signer.getSelectedAccount();
|
|
344
|
+
if (selectedAccount.keyType === 'gl-secp256k1') {
|
|
345
|
+
if (group === undefined) {
|
|
346
|
+
throw new Error('Groupless address requires explicit group number for contract deployment');
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const signerParams = {
|
|
350
|
+
signerAddress: selectedAccount.address,
|
|
351
|
+
signerKeyType: selectedAccount.keyType,
|
|
352
|
+
bytecode: bytecode,
|
|
353
|
+
initialAttoAlphAmount: params?.initialAttoAlphAmount,
|
|
354
|
+
issueTokenAmount: params?.issueTokenAmount,
|
|
355
|
+
issueTokenTo: params?.issueTokenTo,
|
|
356
|
+
initialTokenAmounts: params?.initialTokenAmounts,
|
|
357
|
+
gasAmount: params?.gasAmount,
|
|
358
|
+
gasPrice: params?.gasPrice,
|
|
359
|
+
group: group
|
|
360
|
+
};
|
|
361
|
+
return signerParams;
|
|
362
|
+
}
|
|
363
|
+
buildByteCodeToDeploy(initialFields, isDevnet, exposePrivateFunctions = false) {
|
|
364
|
+
if (exposePrivateFunctions && !isDevnet) {
|
|
365
|
+
throw new Error('Cannot expose private functions in non-devnet environment');
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
const bytecode = exposePrivateFunctions && isDevnet
|
|
369
|
+
? this.getByteCodeForTesting()
|
|
370
|
+
: isDevnet
|
|
371
|
+
? this.bytecodeDebug
|
|
372
|
+
: this.bytecode;
|
|
373
|
+
return ralph.buildContractByteCode(bytecode, initialFields, this.fieldsSig, this.structs);
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
throw new TraceableError(`Failed to build bytecode for contract ${this.name}`, error);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
static fromApiEvents(events, addressToCodeHash, txId, getContractByCodeHash) {
|
|
380
|
+
return events.map((event) => {
|
|
381
|
+
const contractAddress = event.contractAddress;
|
|
382
|
+
const codeHash = addressToCodeHash.get(contractAddress);
|
|
383
|
+
if (typeof codeHash !== 'undefined' || event.eventIndex < 0) {
|
|
384
|
+
return Contract.fromApiEvent(event, codeHash, txId, getContractByCodeHash);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
throw Error(`Cannot find codeHash for the contract address: ${contractAddress}`);
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
toApiCallContract(params, groupIndex, contractAddress, methodIndex) {
|
|
392
|
+
const functionSig = this.functions[`${methodIndex}`];
|
|
393
|
+
const args = toApiArgs(params.args ?? {}, functionSig, this.structs);
|
|
394
|
+
return {
|
|
395
|
+
...params,
|
|
396
|
+
group: groupIndex,
|
|
397
|
+
address: contractAddress,
|
|
398
|
+
methodIndex: methodIndex,
|
|
399
|
+
args: args,
|
|
400
|
+
inputAssets: toApiInputAssets(params.inputAssets)
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash) {
|
|
404
|
+
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
405
|
+
const callResult = tryGetCallResult(result);
|
|
406
|
+
return fromCallResult(callResult, txId, returnTypes, this.structs, getContractByCodeHash);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
Contract.ContractCreatedEventIndex = -1;
|
|
410
|
+
Contract.ContractCreatedEvent = {
|
|
411
|
+
name: 'ContractCreated',
|
|
412
|
+
fieldNames: ['address', 'parentAddress', 'stdInterfaceId'],
|
|
413
|
+
fieldTypes: ['Address', 'Address', 'ByteVec']
|
|
414
|
+
};
|
|
415
|
+
Contract.ContractDestroyedEventIndex = -2;
|
|
416
|
+
Contract.ContractDestroyedEvent = {
|
|
417
|
+
name: 'ContractDestroyed',
|
|
418
|
+
fieldNames: ['address'],
|
|
419
|
+
fieldTypes: ['Address']
|
|
420
|
+
};
|
|
421
|
+
Contract.DebugEventIndex = -3;
|
|
422
|
+
function fromCallResult(callResult, txId, returnTypes, structs, getContractByCodeHash) {
|
|
423
|
+
const rawReturn = fromApiArray(callResult.returns, returnTypes, structs);
|
|
424
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
425
|
+
const addressToCodeHash = new Map();
|
|
426
|
+
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
427
|
+
return {
|
|
428
|
+
returns: returns,
|
|
429
|
+
gasUsed: callResult.gasUsed,
|
|
430
|
+
contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
431
|
+
txInputs: callResult.txInputs,
|
|
432
|
+
txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
|
|
433
|
+
events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash),
|
|
434
|
+
debugMessages: callResult.debugMessages
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
export class Script extends Artifact {
|
|
438
|
+
constructor(version, name, bytecodeTemplate, bytecodeDebugPatch, fieldsSig, functions, structs) {
|
|
439
|
+
super(version, name, functions);
|
|
440
|
+
this.bytecodeTemplate = bytecodeTemplate;
|
|
441
|
+
this.bytecodeDebugPatch = bytecodeDebugPatch;
|
|
442
|
+
this.fieldsSig = fieldsSig;
|
|
443
|
+
this.structs = structs;
|
|
444
|
+
}
|
|
445
|
+
static fromCompileResult(result, structs = []) {
|
|
446
|
+
return new Script(result.version, result.name, result.bytecodeTemplate, result.bytecodeDebugPatch, result.fields, result.functions.map(fromFunctionSig), structs);
|
|
447
|
+
}
|
|
448
|
+
static fromJson(artifact, bytecodeDebugPatch = '', structs = []) {
|
|
449
|
+
if (artifact.version == null ||
|
|
450
|
+
artifact.name == null ||
|
|
451
|
+
artifact.bytecodeTemplate == null ||
|
|
452
|
+
artifact.fieldsSig == null ||
|
|
453
|
+
artifact.functions == null) {
|
|
454
|
+
throw Error('The artifact JSON for script is incomplete');
|
|
455
|
+
}
|
|
456
|
+
return new Script(artifact.version, artifact.name, artifact.bytecodeTemplate, bytecodeDebugPatch, artifact.fieldsSig, artifact.functions, structs);
|
|
457
|
+
}
|
|
458
|
+
static async fromArtifactFile(path, bytecodeDebugPatch, structs = []) {
|
|
459
|
+
const fs = await import('fs');
|
|
460
|
+
const content = await fs.promises.readFile(path);
|
|
461
|
+
const artifact = JSON.parse(content.toString());
|
|
462
|
+
return this.fromJson(artifact, bytecodeDebugPatch, structs);
|
|
463
|
+
}
|
|
464
|
+
toString() {
|
|
465
|
+
const object = {
|
|
466
|
+
version: this.version,
|
|
467
|
+
name: this.name,
|
|
468
|
+
bytecodeTemplate: this.bytecodeTemplate,
|
|
469
|
+
fieldsSig: this.fieldsSig,
|
|
470
|
+
functions: this.functions
|
|
471
|
+
};
|
|
472
|
+
return stringify(object, null, 2);
|
|
473
|
+
}
|
|
474
|
+
async txParamsForExecution(params) {
|
|
475
|
+
const selectedAccount = await params.signer.getSelectedAccount();
|
|
476
|
+
const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
|
|
477
|
+
const signerParams = {
|
|
478
|
+
signerAddress: selectedAccount.address,
|
|
479
|
+
signerKeyType: selectedAccount.keyType,
|
|
480
|
+
bytecode,
|
|
481
|
+
attoAlphAmount: params.attoAlphAmount,
|
|
482
|
+
tokens: params.tokens,
|
|
483
|
+
gasAmount: params.gasAmount,
|
|
484
|
+
gasPrice: params.gasPrice,
|
|
485
|
+
dustAmount: params.dustAmount
|
|
486
|
+
};
|
|
487
|
+
return signerParams;
|
|
488
|
+
}
|
|
489
|
+
buildByteCodeToDeploy(initialFields) {
|
|
490
|
+
try {
|
|
491
|
+
return ralph.buildScriptByteCode(this.bytecodeTemplate, initialFields, this.fieldsSig, this.structs);
|
|
492
|
+
}
|
|
493
|
+
catch (error) {
|
|
494
|
+
throw new TraceableError(`Failed to build bytecode for script ${this.name}`, error);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
export function fromApiFields(immFields, mutFields, fieldsSig, structs) {
|
|
499
|
+
let [immIndex, mutIndex] = [0, 0];
|
|
500
|
+
const func = (type, isMutable) => {
|
|
501
|
+
const nodeVal = isMutable ? mutFields[mutIndex++] : immFields[immIndex++];
|
|
502
|
+
return fromApiPrimitiveVal(nodeVal, type);
|
|
503
|
+
};
|
|
504
|
+
return fieldsSig.names.reduce((acc, name, index) => {
|
|
505
|
+
const fieldType = fieldsSig.types[`${index}`];
|
|
506
|
+
const isMutable = fieldsSig.isMutable[`${index}`];
|
|
507
|
+
acc[`${name}`] = buildVal(isMutable, fieldType, structs, func);
|
|
508
|
+
return acc;
|
|
509
|
+
}, {});
|
|
510
|
+
}
|
|
511
|
+
function buildVal(isMutable, type, structs, func) {
|
|
512
|
+
if (type.startsWith('[')) {
|
|
513
|
+
const [baseType, size] = decodeArrayType(type);
|
|
514
|
+
return Array.from(Array(size).keys()).map(() => buildVal(isMutable, baseType, structs, func));
|
|
515
|
+
}
|
|
516
|
+
if (type.startsWith('(')) {
|
|
517
|
+
const tuple = decodeTupleType(type);
|
|
518
|
+
return tuple.reduce((acc, fieldType) => {
|
|
519
|
+
acc.push(buildVal(isMutable, fieldType, structs, func));
|
|
520
|
+
return acc;
|
|
521
|
+
}, []);
|
|
522
|
+
}
|
|
523
|
+
const struct = structs.find((s) => s.name === type);
|
|
524
|
+
if (struct !== undefined) {
|
|
525
|
+
return struct.fieldNames.reduce((acc, name, index) => {
|
|
526
|
+
const fieldType = struct.fieldTypes[`${index}`];
|
|
527
|
+
const isFieldMutable = isMutable && struct.isMutable[`${index}`];
|
|
528
|
+
acc[`${name}`] = buildVal(isFieldMutable, fieldType, structs, func);
|
|
529
|
+
return acc;
|
|
530
|
+
}, {});
|
|
531
|
+
}
|
|
532
|
+
const primitiveType = PrimitiveTypes.includes(type) ? type : 'ByteVec';
|
|
533
|
+
return func(primitiveType, isMutable);
|
|
534
|
+
}
|
|
535
|
+
export function getDefaultValue(fieldsSig, structs) {
|
|
536
|
+
return fieldsSig.names.reduce((acc, name, index) => {
|
|
537
|
+
const type = fieldsSig.types[`${index}`];
|
|
538
|
+
acc[`${name}`] = buildVal(false, type, structs, getDefaultPrimitiveValue);
|
|
539
|
+
return acc;
|
|
540
|
+
}, {});
|
|
541
|
+
}
|
|
542
|
+
function fromApiVal(iter, type, structs, systemEvent = false) {
|
|
543
|
+
const func = (primitiveType) => {
|
|
544
|
+
const currentValue = iter.next();
|
|
545
|
+
if (currentValue.done)
|
|
546
|
+
throw Error('Not enough vals');
|
|
547
|
+
return fromApiPrimitiveVal(currentValue.value, primitiveType, systemEvent);
|
|
548
|
+
};
|
|
549
|
+
return buildVal(false, type, structs, func);
|
|
550
|
+
}
|
|
551
|
+
export function fromApiArray(values, types, structs) {
|
|
552
|
+
const iter = values.values();
|
|
553
|
+
return types.map((type) => fromApiVal(iter, type, structs));
|
|
554
|
+
}
|
|
555
|
+
export function fromApiEventFields(vals, eventSig, systemEvent = false) {
|
|
556
|
+
const iter = vals.values();
|
|
557
|
+
return eventSig.fieldNames.reduce((acc, name, index) => {
|
|
558
|
+
const type = eventSig.fieldTypes[`${index}`];
|
|
559
|
+
acc[`${name}`] = fromApiVal(iter, type, [], systemEvent);
|
|
560
|
+
return acc;
|
|
561
|
+
}, {});
|
|
562
|
+
}
|
|
563
|
+
function toApiAsset(asset) {
|
|
564
|
+
return {
|
|
565
|
+
attoAlphAmount: toApiNumber256(asset.alphAmount),
|
|
566
|
+
tokens: typeof asset.tokens !== 'undefined' ? asset.tokens.map(toApiToken) : []
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
function fromApiAsset(asset) {
|
|
570
|
+
return {
|
|
571
|
+
alphAmount: fromApiNumber256(asset.attoAlphAmount),
|
|
572
|
+
tokens: fromApiTokens(asset.tokens)
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function toApiContractState(state, structs) {
|
|
576
|
+
const stateFields = state.fields ?? {};
|
|
577
|
+
const fieldsSig = state.fieldsSig;
|
|
578
|
+
const allFields = ralph.flattenFields(stateFields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
|
|
579
|
+
const immFields = allFields.filter((f) => !f.isMutable).map((f) => toApiVal(f.value, f.type));
|
|
580
|
+
const mutFields = allFields.filter((f) => f.isMutable).map((f) => toApiVal(f.value, f.type));
|
|
581
|
+
return {
|
|
582
|
+
address: state.address,
|
|
583
|
+
bytecode: state.bytecode,
|
|
584
|
+
codeHash: state.codeHash,
|
|
585
|
+
initialStateHash: state.initialStateHash,
|
|
586
|
+
immFields,
|
|
587
|
+
mutFields,
|
|
588
|
+
asset: toApiAsset(state.asset)
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
function toApiFields(fields, fieldsSig, structs) {
|
|
592
|
+
return ralph
|
|
593
|
+
.flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs)
|
|
594
|
+
.map((f) => toApiVal(f.value, f.type));
|
|
595
|
+
}
|
|
596
|
+
function toApiArgs(args, funcSig, structs) {
|
|
597
|
+
return ralph
|
|
598
|
+
.flattenFields(args, funcSig.paramNames, funcSig.paramTypes, funcSig.paramIsMutable, structs)
|
|
599
|
+
.map((f) => toApiVal(f.value, f.type));
|
|
600
|
+
}
|
|
601
|
+
function toApiInputAsset(inputAsset) {
|
|
602
|
+
return { address: inputAsset.address, asset: toApiAsset(inputAsset.asset) };
|
|
603
|
+
}
|
|
604
|
+
function toApiInputAssets(inputAssets) {
|
|
605
|
+
return typeof inputAssets !== 'undefined' ? inputAssets.map(toApiInputAsset) : undefined;
|
|
606
|
+
}
|
|
607
|
+
function fromApiOutput(output) {
|
|
608
|
+
if (output.type === 'AssetOutput') {
|
|
609
|
+
const asset = output;
|
|
610
|
+
return {
|
|
611
|
+
type: 'AssetOutput',
|
|
612
|
+
address: asset.address,
|
|
613
|
+
alphAmount: fromApiNumber256(asset.attoAlphAmount),
|
|
614
|
+
tokens: fromApiTokens(asset.tokens),
|
|
615
|
+
lockTime: asset.lockTime,
|
|
616
|
+
message: asset.message
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
else if (output.type === 'ContractOutput') {
|
|
620
|
+
const asset = output;
|
|
621
|
+
return {
|
|
622
|
+
type: 'ContractOutput',
|
|
623
|
+
address: asset.address,
|
|
624
|
+
alphAmount: fromApiNumber256(asset.attoAlphAmount),
|
|
625
|
+
tokens: fromApiTokens(asset.tokens)
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
throw new Error(`Unknown output type: ${output}`);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
export function randomTxId() {
|
|
633
|
+
const bytes = new Uint8Array(32);
|
|
634
|
+
crypto.getRandomValues(bytes);
|
|
635
|
+
return binToHex(bytes);
|
|
636
|
+
}
|
|
637
|
+
assertType;
|
|
638
|
+
export class ContractFactory {
|
|
639
|
+
constructor(contract) {
|
|
640
|
+
this.contract = contract;
|
|
641
|
+
}
|
|
642
|
+
async deploy(signer, deployParams, group) {
|
|
643
|
+
const signerParams = await this.contract.txParamsForDeployment(signer, {
|
|
644
|
+
...deployParams,
|
|
645
|
+
initialFields: addStdIdToFields(this.contract, deployParams.initialFields)
|
|
646
|
+
}, group);
|
|
647
|
+
const result = await signer.signAndSubmitDeployContractTx(signerParams);
|
|
648
|
+
if ('transferTxs' in result) {
|
|
649
|
+
return {
|
|
650
|
+
...result,
|
|
651
|
+
contractInstance: this.at(result.contractAddress)
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
return {
|
|
656
|
+
...result,
|
|
657
|
+
contractInstance: this.at(result.contractAddress)
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
async deployTemplate(signer) {
|
|
662
|
+
return this.deploy(signer, {
|
|
663
|
+
initialFields: this.contract.getInitialFieldsWithDefaultValues()
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
stateForTest_(initFields, asset, address, maps) {
|
|
667
|
+
const newAsset = {
|
|
668
|
+
alphAmount: asset?.alphAmount ?? MINIMAL_CONTRACT_DEPOSIT,
|
|
669
|
+
tokens: asset?.tokens
|
|
670
|
+
};
|
|
671
|
+
const state = this.contract.toState(addStdIdToFields(this.contract, initFields), newAsset, address);
|
|
672
|
+
return {
|
|
673
|
+
...state,
|
|
674
|
+
bytecode: this.contract.bytecodeDebug,
|
|
675
|
+
codeHash: this.contract.codeHash,
|
|
676
|
+
maps: maps
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
export class ExecutableScript {
|
|
681
|
+
constructor(script, getContractByCodeHash) {
|
|
682
|
+
this.script = script;
|
|
683
|
+
this.getContractByCodeHash = getContractByCodeHash;
|
|
684
|
+
}
|
|
685
|
+
async execute(params) {
|
|
686
|
+
const signerParams = await this.script.txParamsForExecution(params);
|
|
687
|
+
return await params.signer.signAndSubmitExecuteScriptTx(signerParams);
|
|
688
|
+
}
|
|
689
|
+
async call(params) {
|
|
690
|
+
const mainFunc = this.script.functions.find((f) => f.name === 'main');
|
|
691
|
+
if (mainFunc === undefined) {
|
|
692
|
+
throw new Error(`There is no main function in script ${this.script.name}`);
|
|
693
|
+
}
|
|
694
|
+
const bytecode = this.script.buildByteCodeToDeploy(params.initialFields);
|
|
695
|
+
const txId = params.txId ?? randomTxId();
|
|
696
|
+
const provider = getCurrentNodeProvider();
|
|
697
|
+
const callResult = await provider.contracts.postContractsCallTxScript({
|
|
698
|
+
...params,
|
|
699
|
+
group: params.groupIndex ?? 0,
|
|
700
|
+
bytecode: bytecode,
|
|
701
|
+
inputAssets: toApiInputAssets(params.inputAssets)
|
|
702
|
+
});
|
|
703
|
+
const returnTypes = mainFunc.returnTypes;
|
|
704
|
+
const result = fromCallResult(callResult, txId, returnTypes, this.script.structs, this.getContractByCodeHash);
|
|
705
|
+
return result;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
function specialContractAddress(eventIndex, groupIndex) {
|
|
709
|
+
const bytes = new Uint8Array(32).fill(0);
|
|
710
|
+
bytes[30] = eventIndex;
|
|
711
|
+
bytes[31] = groupIndex;
|
|
712
|
+
return addressFromContractId(binToHex(bytes));
|
|
713
|
+
}
|
|
714
|
+
export const CreateContractEventAddresses = Array.from(Array(TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) => specialContractAddress(Contract.ContractCreatedEventIndex, groupIndex));
|
|
715
|
+
export const DestroyContractEventAddresses = Array.from(Array(TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) => specialContractAddress(Contract.ContractDestroyedEventIndex, groupIndex));
|
|
716
|
+
function decodeSystemEvent(event, eventSig, eventIndex) {
|
|
717
|
+
if (event.eventIndex !== eventIndex) {
|
|
718
|
+
throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
|
|
719
|
+
}
|
|
720
|
+
return fromApiEventFields(event.fields, eventSig, true);
|
|
721
|
+
}
|
|
722
|
+
function toContractCreatedEventFields(fields) {
|
|
723
|
+
const parentAddress = fields['parentAddress'];
|
|
724
|
+
const stdInterfaceId = fields['stdInterfaceId'];
|
|
725
|
+
return {
|
|
726
|
+
address: fields['address'],
|
|
727
|
+
parentAddress: parentAddress === '' ? undefined : parentAddress,
|
|
728
|
+
stdInterfaceIdGuessed: stdInterfaceId === '' ? undefined : stdInterfaceId
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
export function decodeContractCreatedEvent(event) {
|
|
732
|
+
const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
|
|
733
|
+
return {
|
|
734
|
+
blockHash: event.blockHash,
|
|
735
|
+
txId: event.txId,
|
|
736
|
+
eventIndex: event.eventIndex,
|
|
737
|
+
name: Contract.ContractCreatedEvent.name,
|
|
738
|
+
fields: toContractCreatedEventFields(fields)
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
export function decodeContractDestroyedEvent(event) {
|
|
742
|
+
const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
|
|
743
|
+
return {
|
|
744
|
+
blockHash: event.blockHash,
|
|
745
|
+
txId: event.txId,
|
|
746
|
+
eventIndex: event.eventIndex,
|
|
747
|
+
name: Contract.ContractDestroyedEvent.name,
|
|
748
|
+
fields: { address: fields['address'] }
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
export function subscribeEventsFromContract(options, address, eventIndex, decodeFunc, fromCount) {
|
|
752
|
+
const messageCallback = (event) => {
|
|
753
|
+
if (event.eventIndex !== eventIndex) {
|
|
754
|
+
return Promise.resolve();
|
|
755
|
+
}
|
|
756
|
+
return options.messageCallback(decodeFunc(event));
|
|
757
|
+
};
|
|
758
|
+
const errorCallback = (err, subscription) => {
|
|
759
|
+
return options.errorCallback(err, subscription);
|
|
760
|
+
};
|
|
761
|
+
const opt = {
|
|
762
|
+
pollingInterval: options.pollingInterval,
|
|
763
|
+
messageCallback: messageCallback,
|
|
764
|
+
errorCallback: errorCallback,
|
|
765
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
766
|
+
parallel: options.parallel
|
|
767
|
+
};
|
|
768
|
+
return subscribeToEvents(opt, address, fromCount);
|
|
769
|
+
}
|
|
770
|
+
export function addStdIdToFields(contract, fields) {
|
|
771
|
+
const stdInterfaceIdPrefix = '414c5048';
|
|
772
|
+
return contract.stdInterfaceId === undefined
|
|
773
|
+
? fields
|
|
774
|
+
: { ...fields, __stdInterfaceId: stdInterfaceIdPrefix + contract.stdInterfaceId };
|
|
775
|
+
}
|
|
776
|
+
function calcWrapperContractId(parentContractId, mapIndex, key, keyType, group) {
|
|
777
|
+
const prefix = ralph.encodeMapPrefix(mapIndex);
|
|
778
|
+
const encodedKey = ralph.encodeMapKey(key, keyType);
|
|
779
|
+
const path = binToHex(prefix) + binToHex(encodedKey);
|
|
780
|
+
return subContractId(parentContractId, path, group);
|
|
781
|
+
}
|
|
782
|
+
function genCodeForType(type, structs) {
|
|
783
|
+
const { immFields, mutFields } = ralph.calcFieldSize(type, true, structs);
|
|
784
|
+
const loadImmFieldByIndex = {
|
|
785
|
+
isPublic: true,
|
|
786
|
+
usePreapprovedAssets: false,
|
|
787
|
+
useContractAssets: false,
|
|
788
|
+
usePayToContractOnly: false,
|
|
789
|
+
argsLength: 1,
|
|
790
|
+
localsLength: 1,
|
|
791
|
+
returnLength: 1,
|
|
792
|
+
instrs: [LoadLocal(0), LoadImmFieldByIndex]
|
|
793
|
+
};
|
|
794
|
+
const loadMutFieldByIndex = {
|
|
795
|
+
...loadImmFieldByIndex,
|
|
796
|
+
instrs: [LoadLocal(0), LoadMutFieldByIndex]
|
|
797
|
+
};
|
|
798
|
+
const parentContractIdIndex = immFields;
|
|
799
|
+
const storeMutFieldByIndex = {
|
|
800
|
+
...loadImmFieldByIndex,
|
|
801
|
+
argsLength: 2,
|
|
802
|
+
localsLength: 2,
|
|
803
|
+
returnLength: 0,
|
|
804
|
+
instrs: [
|
|
805
|
+
CallerContractId,
|
|
806
|
+
LoadImmField(parentContractIdIndex),
|
|
807
|
+
ByteVecEq,
|
|
808
|
+
Assert,
|
|
809
|
+
LoadLocal(0),
|
|
810
|
+
LoadLocal(1),
|
|
811
|
+
StoreMutFieldByIndex
|
|
812
|
+
]
|
|
813
|
+
};
|
|
814
|
+
const destroy = {
|
|
815
|
+
isPublic: true,
|
|
816
|
+
usePreapprovedAssets: false,
|
|
817
|
+
useContractAssets: true,
|
|
818
|
+
usePayToContractOnly: false,
|
|
819
|
+
argsLength: 1,
|
|
820
|
+
localsLength: 1,
|
|
821
|
+
returnLength: 0,
|
|
822
|
+
instrs: [CallerContractId, LoadImmField(parentContractIdIndex), ByteVecEq, Assert, LoadLocal(0), DestroySelf]
|
|
823
|
+
};
|
|
824
|
+
const c = {
|
|
825
|
+
fieldLength: immFields + mutFields + 1,
|
|
826
|
+
methods: [loadImmFieldByIndex, loadMutFieldByIndex, storeMutFieldByIndex, destroy]
|
|
827
|
+
};
|
|
828
|
+
const bytecode = contract.contractCodec.encodeContract(c);
|
|
829
|
+
const codeHash = blake2b(bytecode, { dkLen: 32 });
|
|
830
|
+
return { bytecode: binToHex(bytecode), codeHash: binToHex(codeHash) };
|
|
831
|
+
}
|
|
832
|
+
function getContractFieldsSig(mapValueType) {
|
|
833
|
+
return {
|
|
834
|
+
names: ['value', 'parentContractId'],
|
|
835
|
+
types: [mapValueType, 'ByteVec'],
|
|
836
|
+
isMutable: [true, false]
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
function mapToExistingContracts(contract, parentContractId, group, map, mapIndex, type) {
|
|
840
|
+
const [keyType, valueType] = ralph.parseMapType(type);
|
|
841
|
+
const generatedContract = genCodeForType(valueType, contract.structs);
|
|
842
|
+
return Array.from(map.entries()).map(([key, value]) => {
|
|
843
|
+
const fields = { value, parentContractId };
|
|
844
|
+
const contractId = calcWrapperContractId(parentContractId, mapIndex, key, keyType, group);
|
|
845
|
+
return {
|
|
846
|
+
...generatedContract,
|
|
847
|
+
address: addressFromContractId(contractId),
|
|
848
|
+
contractId: contractId,
|
|
849
|
+
fieldsSig: getContractFieldsSig(valueType),
|
|
850
|
+
fields,
|
|
851
|
+
asset: { alphAmount: ONE_ALPH }
|
|
852
|
+
};
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
function mapsToExistingContracts(contract, parentContractId, group, initialMaps) {
|
|
856
|
+
const mapsSig = contract.mapsSig;
|
|
857
|
+
if (mapsSig === undefined)
|
|
858
|
+
return [];
|
|
859
|
+
const contractStates = [];
|
|
860
|
+
Object.keys(initialMaps).forEach((name) => {
|
|
861
|
+
const index = mapsSig.names.findIndex((n) => n === name);
|
|
862
|
+
if (index === -1)
|
|
863
|
+
throw new Error(`Map var ${name} does not exist in contract ${contract.name}`);
|
|
864
|
+
const mapType = mapsSig.types[`${index}`];
|
|
865
|
+
const states = mapToExistingContracts(contract, parentContractId, group, initialMaps[`${name}`], index, mapType);
|
|
866
|
+
contractStates.push(...states);
|
|
867
|
+
});
|
|
868
|
+
return contractStates;
|
|
869
|
+
}
|
|
870
|
+
function hasMap(state) {
|
|
871
|
+
return state.maps !== undefined;
|
|
872
|
+
}
|
|
873
|
+
function getTestExistingContracts(selfContract, selfContractId, group, params, getContractByCodeHash) {
|
|
874
|
+
const selfMaps = params.initialMaps ?? {};
|
|
875
|
+
const selfMapEntries = mapsToExistingContracts(selfContract, selfContractId, group, selfMaps);
|
|
876
|
+
const existingContracts = params.existingContracts ?? [];
|
|
877
|
+
const existingMapEntries = existingContracts.flatMap((contractState) => {
|
|
878
|
+
return hasMap(contractState)
|
|
879
|
+
? mapsToExistingContracts(getContractByCodeHash(contractState.codeHash), contractState.contractId, group, contractState.maps ?? {})
|
|
880
|
+
: [];
|
|
881
|
+
});
|
|
882
|
+
return existingContracts.concat(selfMapEntries, existingMapEntries);
|
|
883
|
+
}
|
|
884
|
+
function getNewCreatedContractExceptMaps(result, getContractByCodeHash) {
|
|
885
|
+
const isMapContract = (codeHash) => {
|
|
886
|
+
try {
|
|
887
|
+
getContractByCodeHash(codeHash);
|
|
888
|
+
return false;
|
|
889
|
+
}
|
|
890
|
+
catch (error) {
|
|
891
|
+
if (error instanceof Error && error.message.includes('Unknown code with code hash')) {
|
|
892
|
+
return true;
|
|
893
|
+
}
|
|
894
|
+
throw error;
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
const states = [];
|
|
898
|
+
result.events.forEach((event) => {
|
|
899
|
+
if (event.eventIndex === Contract.ContractCreatedEventIndex) {
|
|
900
|
+
const contractAddress = event.fields[0].value;
|
|
901
|
+
const contractState = result.contracts.find((c) => c.address === contractAddress);
|
|
902
|
+
if (contractState !== undefined && !isMapContract(contractState.codeHash)) {
|
|
903
|
+
states.push(contractState);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
return states;
|
|
908
|
+
}
|
|
909
|
+
export function extractMapsFromApiResult(selfAddress, params, group, apiResult, getContractByCodeHash) {
|
|
910
|
+
const selfMaps = params.initialMaps ?? {};
|
|
911
|
+
const existingContracts = params.existingContracts ?? [];
|
|
912
|
+
const updatedExistingContracts = apiResult.contracts.filter((c) => c.address === selfAddress || existingContracts.find((s) => s.address === c.address) !== undefined);
|
|
913
|
+
const newCreateContracts = getNewCreatedContractExceptMaps(apiResult, getContractByCodeHash);
|
|
914
|
+
const allMaps = [];
|
|
915
|
+
updatedExistingContracts.concat(newCreateContracts).forEach((state) => {
|
|
916
|
+
const artifact = getContractByCodeHash(state.codeHash);
|
|
917
|
+
if (artifact.mapsSig !== undefined) {
|
|
918
|
+
const originMaps = state.address === selfAddress ? selfMaps : existingContracts.find((s) => s.address === state.address)?.maps;
|
|
919
|
+
const maps = existingContractsToMaps(artifact, state.address, group, apiResult, originMaps ?? {});
|
|
920
|
+
allMaps.push({ address: state.address, maps });
|
|
921
|
+
}
|
|
922
|
+
});
|
|
923
|
+
return allMaps;
|
|
924
|
+
}
|
|
925
|
+
export async function testMethod(factory, methodName, params, getContractByCodeHash) {
|
|
926
|
+
const txId = params?.txId ?? randomTxId();
|
|
927
|
+
const selfContract = factory.contract;
|
|
928
|
+
const selfAddress = params.contractAddress ?? addressFromContractId(binToHex(crypto.getRandomValues(new Uint8Array(32))));
|
|
929
|
+
const selfContractId = binToHex(contractIdFromAddress(selfAddress));
|
|
930
|
+
const group = params.group ?? 0;
|
|
931
|
+
const existingContracts = getTestExistingContracts(selfContract, selfContractId, group, params, getContractByCodeHash);
|
|
932
|
+
const apiParams = selfContract.toApiTestContractParams(methodName, {
|
|
933
|
+
...params,
|
|
934
|
+
contractAddress: selfAddress,
|
|
935
|
+
txId: txId,
|
|
936
|
+
initialFields: addStdIdToFields(selfContract, params.initialFields ?? {}),
|
|
937
|
+
args: params.args === undefined ? {} : params.args,
|
|
938
|
+
existingContracts
|
|
939
|
+
});
|
|
940
|
+
const apiResult = await getCurrentNodeProvider().contracts.postContractsTestContract(apiParams);
|
|
941
|
+
const allMaps = extractMapsFromApiResult(selfAddress, params, group, apiResult, getContractByCodeHash);
|
|
942
|
+
const testResult = selfContract.fromApiTestContractResult(methodName, apiResult, txId, getContractByCodeHash);
|
|
943
|
+
testResult.contracts.forEach((c) => {
|
|
944
|
+
const maps = allMaps.find((v) => v.address === c.address)?.maps;
|
|
945
|
+
if (maps !== undefined)
|
|
946
|
+
c['maps'] = maps;
|
|
947
|
+
});
|
|
948
|
+
selfContract.printDebugMessages(methodName, testResult.debugMessages);
|
|
949
|
+
return {
|
|
950
|
+
...testResult,
|
|
951
|
+
maps: allMaps.find((v) => v.address === selfAddress)?.maps
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
function printDebugMessage(m) {
|
|
955
|
+
console.log(`> Contract @ ${m.contractAddress} - ${m.message}`);
|
|
956
|
+
}
|
|
957
|
+
export async function getDebugMessagesFromTx(txId, provider) {
|
|
958
|
+
if (isHexString(txId) && txId.length === 64) {
|
|
959
|
+
const nodeProvider = provider ?? getCurrentNodeProvider();
|
|
960
|
+
const events = await nodeProvider.events.getEventsTxIdTxid(txId);
|
|
961
|
+
return events.events
|
|
962
|
+
.filter((e) => e.eventIndex === Contract.DebugEventIndex)
|
|
963
|
+
.map((e) => {
|
|
964
|
+
if (e.fields.length === 1 && e.fields[0].type === 'ByteVec') {
|
|
965
|
+
return {
|
|
966
|
+
contractAddress: e.contractAddress,
|
|
967
|
+
message: hexToString(e.fields[0].value)
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
throw new Error(`Invalid debug log: ${stringify(e.fields)}`);
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
else {
|
|
976
|
+
throw new Error(`Invalid tx id: ${txId}`);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
export async function printDebugMessagesFromTx(txId, provider) {
|
|
980
|
+
const messages = await getDebugMessagesFromTx(txId, provider);
|
|
981
|
+
if (messages.length > 0) {
|
|
982
|
+
messages.forEach((m) => printDebugMessage(m));
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
export class RalphMap {
|
|
986
|
+
constructor(parentContract, parentContractId, mapName) {
|
|
987
|
+
this.parentContract = parentContract;
|
|
988
|
+
this.parentContractId = parentContractId;
|
|
989
|
+
this.mapName = mapName;
|
|
990
|
+
this.groupIndex = groupOfAddress(addressFromContractId(parentContractId));
|
|
991
|
+
}
|
|
992
|
+
async get(key) {
|
|
993
|
+
return getMapItem(this.parentContract, this.parentContractId, this.groupIndex, this.mapName, key);
|
|
994
|
+
}
|
|
995
|
+
async contains(key) {
|
|
996
|
+
return this.get(key).then((v) => v !== undefined);
|
|
997
|
+
}
|
|
998
|
+
toJSON() {
|
|
999
|
+
return {
|
|
1000
|
+
parentContractId: this.parentContractId,
|
|
1001
|
+
mapName: this.mapName,
|
|
1002
|
+
groupIndex: this.groupIndex
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
export async function getMapItem(parentContract, parentContractId, groupIndex, mapName, key) {
|
|
1007
|
+
const index = parentContract.mapsSig?.names.findIndex((name) => name === mapName);
|
|
1008
|
+
const mapType = index === undefined ? undefined : parentContract.mapsSig?.types[`${index}`];
|
|
1009
|
+
if (mapType === undefined) {
|
|
1010
|
+
throw new Error(`Map ${mapName} does not exist in contract ${parentContract.name}`);
|
|
1011
|
+
}
|
|
1012
|
+
const [keyType, valueType] = ralph.parseMapType(mapType);
|
|
1013
|
+
const mapItemContractId = calcWrapperContractId(parentContractId, index, key, keyType, groupIndex);
|
|
1014
|
+
const mapItemAddress = addressFromContractId(mapItemContractId);
|
|
1015
|
+
try {
|
|
1016
|
+
const state = await getCurrentNodeProvider().contracts.getContractsAddressState(mapItemAddress);
|
|
1017
|
+
const fieldsSig = getContractFieldsSig(valueType);
|
|
1018
|
+
const fields = fromApiFields(state.immFields, state.mutFields, fieldsSig, parentContract.structs);
|
|
1019
|
+
return fields['value'];
|
|
1020
|
+
}
|
|
1021
|
+
catch (error) {
|
|
1022
|
+
if (error instanceof Error && error.message.includes('KeyNotFound')) {
|
|
1023
|
+
return undefined;
|
|
1024
|
+
}
|
|
1025
|
+
throw new TraceableError(`Failed to get value from map ${mapName}, key: ${key}, parent contract id: ${parentContractId}`, error);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
function buildMapInfo(contract, fields) {
|
|
1029
|
+
const mapsSig = contract.mapsSig;
|
|
1030
|
+
if (mapsSig === undefined)
|
|
1031
|
+
return [];
|
|
1032
|
+
return mapsSig.names.map((name, index) => {
|
|
1033
|
+
const mapType = mapsSig.types[`${index}`];
|
|
1034
|
+
const value = (fields[`${name}`] ?? new Map());
|
|
1035
|
+
const [keyType, valueType] = ralph.parseMapType(mapType);
|
|
1036
|
+
return { name, value, keyType, valueType, index };
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
function extractFromEventLog(contract, result, allMaps, address, group) {
|
|
1040
|
+
const parentContractId = binToHex(contractIdFromAddress(address));
|
|
1041
|
+
const newInserted = [];
|
|
1042
|
+
result.debugMessages.forEach((message) => {
|
|
1043
|
+
if (message.contractAddress !== address)
|
|
1044
|
+
return;
|
|
1045
|
+
const decoded = ralph.tryDecodeMapDebugLog(message.message);
|
|
1046
|
+
if (decoded === undefined)
|
|
1047
|
+
return;
|
|
1048
|
+
const map = allMaps[`${decoded.mapIndex}`];
|
|
1049
|
+
const decodedKey = ralph.decodePrimitive(decoded.encodedKey, map.keyType);
|
|
1050
|
+
const contractId = subContractId(parentContractId, decoded.path, group);
|
|
1051
|
+
if (!decoded.isInsert) {
|
|
1052
|
+
map.value.delete(decodedKey);
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
const state = result.contracts.find((s) => s.address === addressFromContractId(contractId));
|
|
1056
|
+
if (state === undefined) {
|
|
1057
|
+
throw new Error(`Cannot find contract state for map value, map field: ${map.name}, value type: ${map.valueType}`);
|
|
1058
|
+
}
|
|
1059
|
+
newInserted.push(state.address);
|
|
1060
|
+
const fieldsSig = getContractFieldsSig(map.valueType);
|
|
1061
|
+
const fields = fromApiFields(state.immFields, state.mutFields, fieldsSig, contract.structs);
|
|
1062
|
+
map.value.set(decodedKey, fields['value']);
|
|
1063
|
+
});
|
|
1064
|
+
return newInserted;
|
|
1065
|
+
}
|
|
1066
|
+
function updateMaps(contract, result, allMaps, address, group) {
|
|
1067
|
+
const parentContractId = binToHex(contractIdFromAddress(address));
|
|
1068
|
+
const updated = [];
|
|
1069
|
+
allMaps.forEach((map) => {
|
|
1070
|
+
Array.from(map.value.keys()).forEach((key) => {
|
|
1071
|
+
const contractId = calcWrapperContractId(parentContractId, map.index, key, map.keyType, group);
|
|
1072
|
+
const updatedState = result.contracts.find((s) => s.address === addressFromContractId(contractId));
|
|
1073
|
+
if (updatedState === undefined)
|
|
1074
|
+
return;
|
|
1075
|
+
updated.push(updatedState.address);
|
|
1076
|
+
const fieldsSig = getContractFieldsSig(map.valueType);
|
|
1077
|
+
const fields = fromApiFields(updatedState.immFields, updatedState.mutFields, fieldsSig, contract.structs);
|
|
1078
|
+
map.value.set(key, fields['value']);
|
|
1079
|
+
});
|
|
1080
|
+
});
|
|
1081
|
+
return updated;
|
|
1082
|
+
}
|
|
1083
|
+
function existingContractsToMaps(contract, address, group, result, maps) {
|
|
1084
|
+
const allMaps = buildMapInfo(contract, maps);
|
|
1085
|
+
const updated = updateMaps(contract, result, allMaps, address, group);
|
|
1086
|
+
const newInserted = extractFromEventLog(contract, result, allMaps, address, group);
|
|
1087
|
+
const mapEntries = updated.concat(newInserted);
|
|
1088
|
+
const remainContracts = result.contracts.filter((c) => mapEntries.find((addr) => c.address === addr) === undefined);
|
|
1089
|
+
result.contracts = remainContracts;
|
|
1090
|
+
return allMaps.reduce((acc, map) => {
|
|
1091
|
+
acc[`${map.name}`] = map.value;
|
|
1092
|
+
return acc;
|
|
1093
|
+
}, {});
|
|
1094
|
+
}
|
|
1095
|
+
export class ContractInstance {
|
|
1096
|
+
constructor(address) {
|
|
1097
|
+
this.address = address;
|
|
1098
|
+
this.contractId = binToHex(contractIdFromAddress(address));
|
|
1099
|
+
this.groupIndex = groupOfAddress(address);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
export async function fetchContractState(contract, instance) {
|
|
1103
|
+
const contractState = await getCurrentNodeProvider().contracts.getContractsAddressState(instance.address);
|
|
1104
|
+
const state = contract.contract.fromApiContractState(contractState);
|
|
1105
|
+
return {
|
|
1106
|
+
...state,
|
|
1107
|
+
fields: state.fields
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
function checkGroupIndex(groupIndex) {
|
|
1111
|
+
if (groupIndex < 0 || groupIndex >= TOTAL_NUMBER_OF_GROUPS) {
|
|
1112
|
+
throw new Error(`Invalid group index ${groupIndex}, expected a value within the range [0, ${TOTAL_NUMBER_OF_GROUPS})`);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
export function subscribeContractCreatedEvent(options, fromGroup, fromCount) {
|
|
1116
|
+
checkGroupIndex(fromGroup);
|
|
1117
|
+
const contractAddress = CreateContractEventAddresses[`${fromGroup}`];
|
|
1118
|
+
return subscribeEventsFromContract(options, contractAddress, Contract.ContractCreatedEventIndex, (event) => {
|
|
1119
|
+
return {
|
|
1120
|
+
...decodeContractCreatedEvent(event),
|
|
1121
|
+
contractAddress: contractAddress
|
|
1122
|
+
};
|
|
1123
|
+
}, fromCount);
|
|
1124
|
+
}
|
|
1125
|
+
export function subscribeContractDestroyedEvent(options, fromGroup, fromCount) {
|
|
1126
|
+
checkGroupIndex(fromGroup);
|
|
1127
|
+
const contractAddress = DestroyContractEventAddresses[`${fromGroup}`];
|
|
1128
|
+
return subscribeEventsFromContract(options, contractAddress, Contract.ContractDestroyedEventIndex, (event) => {
|
|
1129
|
+
return {
|
|
1130
|
+
...decodeContractDestroyedEvent(event),
|
|
1131
|
+
contractAddress: contractAddress
|
|
1132
|
+
};
|
|
1133
|
+
}, fromCount);
|
|
1134
|
+
}
|
|
1135
|
+
export function decodeEvent(contract, instance, event, targetEventIndex) {
|
|
1136
|
+
if (event.eventIndex !== targetEventIndex &&
|
|
1137
|
+
!(targetEventIndex >= 0 && targetEventIndex < contract.eventsSig.length)) {
|
|
1138
|
+
throw new Error('Invalid event index: ' + event.eventIndex + ', expected: ' + targetEventIndex);
|
|
1139
|
+
}
|
|
1140
|
+
const eventSig = contract.eventsSig[`${targetEventIndex}`];
|
|
1141
|
+
const fields = fromApiEventFields(event.fields, eventSig);
|
|
1142
|
+
return {
|
|
1143
|
+
contractAddress: instance.address,
|
|
1144
|
+
blockHash: event.blockHash,
|
|
1145
|
+
txId: event.txId,
|
|
1146
|
+
eventIndex: event.eventIndex,
|
|
1147
|
+
name: eventSig.name,
|
|
1148
|
+
fields: fields
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
export function subscribeContractEvent(contract, instance, options, eventName, fromCount) {
|
|
1152
|
+
const eventIndex = contract.eventsSig.findIndex((sig) => sig.name === eventName);
|
|
1153
|
+
return subscribeEventsFromContract(options, instance.address, eventIndex, (event) => decodeEvent(contract, instance, event, eventIndex), fromCount);
|
|
1154
|
+
}
|
|
1155
|
+
export function subscribeContractEvents(contract, instance, options, fromCount) {
|
|
1156
|
+
const messageCallback = (event) => {
|
|
1157
|
+
return options.messageCallback({
|
|
1158
|
+
...decodeEvent(contract, instance, event, event.eventIndex),
|
|
1159
|
+
contractAddress: instance.address
|
|
1160
|
+
});
|
|
1161
|
+
};
|
|
1162
|
+
const errorCallback = (err, subscription) => {
|
|
1163
|
+
return options.errorCallback(err, subscription);
|
|
1164
|
+
};
|
|
1165
|
+
const opt = {
|
|
1166
|
+
pollingInterval: options.pollingInterval,
|
|
1167
|
+
messageCallback: messageCallback,
|
|
1168
|
+
errorCallback: errorCallback,
|
|
1169
|
+
onEventCountChanged: options.onEventCountChanged,
|
|
1170
|
+
parallel: options.parallel
|
|
1171
|
+
};
|
|
1172
|
+
return subscribeToEvents(opt, instance.address, fromCount);
|
|
1173
|
+
}
|
|
1174
|
+
export async function callMethod(contract, instance, methodName, params, getContractByCodeHash) {
|
|
1175
|
+
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1176
|
+
const txId = params?.txId ?? randomTxId();
|
|
1177
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1178
|
+
const callParams = contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1179
|
+
const result = await getCurrentNodeProvider().contracts.postContractsCallContract(callParams);
|
|
1180
|
+
const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash);
|
|
1181
|
+
contract.contract.printDebugMessages(methodName, callResult.debugMessages);
|
|
1182
|
+
return callResult;
|
|
1183
|
+
}
|
|
1184
|
+
function tryGetCallMethodAddressArg(maybeAddress, groupIndex) {
|
|
1185
|
+
if (!isValidAddress(maybeAddress))
|
|
1186
|
+
return maybeAddress;
|
|
1187
|
+
if (isGrouplessAddress(maybeAddress)) {
|
|
1188
|
+
const rawAddress = addressWithoutExplicitGroupIndex(maybeAddress);
|
|
1189
|
+
return `${rawAddress}:${groupIndex}`;
|
|
1190
|
+
}
|
|
1191
|
+
return maybeAddress;
|
|
1192
|
+
}
|
|
1193
|
+
function getCallMethodArgsFromVal(val, groupIndex) {
|
|
1194
|
+
if (typeof val === 'string') {
|
|
1195
|
+
return tryGetCallMethodAddressArg(val, groupIndex);
|
|
1196
|
+
}
|
|
1197
|
+
if (Array.isArray(val)) {
|
|
1198
|
+
return val.map((v) => getCallMethodArgsFromVal(v, groupIndex));
|
|
1199
|
+
}
|
|
1200
|
+
return val;
|
|
1201
|
+
}
|
|
1202
|
+
function getCallMethodArgs(args, groupIndex) {
|
|
1203
|
+
const newArgs = {};
|
|
1204
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1205
|
+
newArgs[`${key}`] = getCallMethodArgsFromVal(value, groupIndex);
|
|
1206
|
+
}
|
|
1207
|
+
return newArgs;
|
|
1208
|
+
}
|
|
1209
|
+
export async function signExecuteMethod(contract, instance, methodName, params) {
|
|
1210
|
+
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1211
|
+
const functionSig = contract.contract.functions[methodIndex];
|
|
1212
|
+
const isDevnet = await contract.contract.isDevnet(params.signer);
|
|
1213
|
+
const methodUsePreapprovedAssets = contract.contract.isMethodUsePreapprovedAssets(isDevnet, methodIndex);
|
|
1214
|
+
const bytecodeTemplate = getBytecodeTemplate(methodIndex, methodUsePreapprovedAssets, functionSig, contract.contract.structs, params.attoAlphAmount, params.tokens);
|
|
1215
|
+
const fieldsSig = toFieldsSig(contract.contract.name, functionSig);
|
|
1216
|
+
const bytecode = ralph.buildScriptByteCode(bytecodeTemplate, { __contract__: instance.contractId, ...params.args }, fieldsSig, contract.contract.structs);
|
|
1217
|
+
const signer = params.signer;
|
|
1218
|
+
const selectedAccount = await signer.getSelectedAccount();
|
|
1219
|
+
const signerParams = {
|
|
1220
|
+
signerAddress: selectedAccount.address,
|
|
1221
|
+
signerKeyType: selectedAccount.keyType,
|
|
1222
|
+
bytecode: bytecode,
|
|
1223
|
+
attoAlphAmount: params.attoAlphAmount,
|
|
1224
|
+
tokens: params.tokens,
|
|
1225
|
+
gasAmount: params.gasAmount,
|
|
1226
|
+
gasPrice: params.gasPrice,
|
|
1227
|
+
group: instance.groupIndex,
|
|
1228
|
+
dustAmount: params.dustAmount
|
|
1229
|
+
};
|
|
1230
|
+
const result = (await signer.signAndSubmitExecuteScriptTx(signerParams));
|
|
1231
|
+
if (isContractDebugMessageEnabled() && isDevnet) {
|
|
1232
|
+
await printDebugMessagesFromTx(result.txId, signer.nodeProvider);
|
|
1233
|
+
}
|
|
1234
|
+
return result;
|
|
1235
|
+
}
|
|
1236
|
+
function getBytecodeTemplate(methodIndex, methodUsePreapprovedAssets, functionSig, structs, attoAlphAmount, tokens) {
|
|
1237
|
+
const numberOfMethods = '01';
|
|
1238
|
+
const isPublic = '01';
|
|
1239
|
+
const scriptUseApprovedAssets = attoAlphAmount !== undefined || tokens !== undefined;
|
|
1240
|
+
const modifier = scriptUseApprovedAssets ? '03' : '00';
|
|
1241
|
+
const argsLength = '00';
|
|
1242
|
+
const returnsLength = '00';
|
|
1243
|
+
if (methodUsePreapprovedAssets && !scriptUseApprovedAssets) {
|
|
1244
|
+
throw new Error('The contract call requires preapproved assets but none are provided');
|
|
1245
|
+
}
|
|
1246
|
+
const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs);
|
|
1247
|
+
const approveAlphInstrs = getApproveAlphInstrs(methodUsePreapprovedAssets ? attoAlphAmount : undefined);
|
|
1248
|
+
const approveTokensInstrs = getApproveTokensInstrs(methodUsePreapprovedAssets ? tokens : undefined);
|
|
1249
|
+
const callerInstrs = getCallAddressInstrs(approveAlphInstrs.length / 2 + approveTokensInstrs.length / 3);
|
|
1250
|
+
const functionArgsNum = encodeU256Const(BigInt(templateVarsLength - 1));
|
|
1251
|
+
const localsLength = encodeI32(templateVarStoreLocalInstrs.length / 2);
|
|
1252
|
+
const templateVarLoadLocalInstrs = getTemplateVarLoadLocalInstrs(functionSig, structs);
|
|
1253
|
+
const functionReturnTypesLength = functionSig.returnTypes.reduce((acc, returnType) => acc + ralph.typeLength(returnType, structs), 0);
|
|
1254
|
+
const functionReturnPopInstrs = encodeInstr(Pop).repeat(functionReturnTypesLength);
|
|
1255
|
+
const functionReturnNum = encodeU256Const(BigInt(functionReturnTypesLength));
|
|
1256
|
+
const contractTemplateVar = '{0}';
|
|
1257
|
+
const externalCallInstr = encodeInstr(CallExternal(methodIndex));
|
|
1258
|
+
const numberOfInstrs = encodeI32(callerInstrs.length +
|
|
1259
|
+
approveAlphInstrs.length +
|
|
1260
|
+
approveTokensInstrs.length +
|
|
1261
|
+
templateVarStoreLocalInstrs.length +
|
|
1262
|
+
templateVarLoadLocalInstrs.length +
|
|
1263
|
+
functionReturnTypesLength +
|
|
1264
|
+
4);
|
|
1265
|
+
return (numberOfMethods +
|
|
1266
|
+
isPublic +
|
|
1267
|
+
modifier +
|
|
1268
|
+
argsLength +
|
|
1269
|
+
localsLength +
|
|
1270
|
+
returnsLength +
|
|
1271
|
+
numberOfInstrs +
|
|
1272
|
+
callerInstrs.join('') +
|
|
1273
|
+
approveAlphInstrs.join('') +
|
|
1274
|
+
approveTokensInstrs.join('') +
|
|
1275
|
+
templateVarStoreLocalInstrs.join('') +
|
|
1276
|
+
templateVarLoadLocalInstrs.join('') +
|
|
1277
|
+
functionArgsNum +
|
|
1278
|
+
functionReturnNum +
|
|
1279
|
+
contractTemplateVar +
|
|
1280
|
+
externalCallInstr +
|
|
1281
|
+
functionReturnPopInstrs);
|
|
1282
|
+
}
|
|
1283
|
+
function getApproveAlphInstrs(attoAlphAmount) {
|
|
1284
|
+
const approveAlphInstrs = [];
|
|
1285
|
+
if (attoAlphAmount) {
|
|
1286
|
+
const approvedAttoAlphAmount = encodeU256Const(BigInt(attoAlphAmount));
|
|
1287
|
+
approveAlphInstrs.push(approvedAttoAlphAmount);
|
|
1288
|
+
approveAlphInstrs.push(encodeInstr(ApproveAlph));
|
|
1289
|
+
}
|
|
1290
|
+
return approveAlphInstrs;
|
|
1291
|
+
}
|
|
1292
|
+
function getApproveTokensInstrs(tokens) {
|
|
1293
|
+
const approveTokensInstrs = [];
|
|
1294
|
+
if (tokens) {
|
|
1295
|
+
tokens.forEach((token) => {
|
|
1296
|
+
const tokenIdBin = hexToBinUnsafe(token.id);
|
|
1297
|
+
approveTokensInstrs.push(encodeInstr(BytesConst(tokenIdBin)));
|
|
1298
|
+
approveTokensInstrs.push(encodeU256Const(BigInt(token.amount)));
|
|
1299
|
+
approveTokensInstrs.push(encodeInstr(ApproveToken));
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
return approveTokensInstrs;
|
|
1303
|
+
}
|
|
1304
|
+
function getCallAddressInstrs(approveAssetsNum) {
|
|
1305
|
+
const callerInstrs = [];
|
|
1306
|
+
if (approveAssetsNum > 0) {
|
|
1307
|
+
callerInstrs.push(encodeInstr(CallerAddress));
|
|
1308
|
+
const dup = encodeInstr(Dup);
|
|
1309
|
+
if (approveAssetsNum > 1) {
|
|
1310
|
+
callerInstrs.push(...new Array(approveAssetsNum - 1).fill(dup));
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
return callerInstrs;
|
|
1314
|
+
}
|
|
1315
|
+
function getTemplateVarStoreLocalInstrs(functionSig, structs) {
|
|
1316
|
+
let templateVarIndex = 1;
|
|
1317
|
+
let localsLength = 0;
|
|
1318
|
+
const templateVarStoreInstrs = [];
|
|
1319
|
+
functionSig.paramTypes.forEach((paramType) => {
|
|
1320
|
+
const fieldsLength = ralph.typeLength(paramType, structs);
|
|
1321
|
+
if (fieldsLength > 1) {
|
|
1322
|
+
for (let i = 0; i < fieldsLength; i++) {
|
|
1323
|
+
templateVarStoreInstrs.push(`{${templateVarIndex + i}}`);
|
|
1324
|
+
}
|
|
1325
|
+
for (let i = 0; i < fieldsLength; i++) {
|
|
1326
|
+
templateVarStoreInstrs.push(encodeStoreLocalInstr(localsLength + (fieldsLength - i - 1)));
|
|
1327
|
+
}
|
|
1328
|
+
localsLength = localsLength + fieldsLength;
|
|
1329
|
+
}
|
|
1330
|
+
templateVarIndex = templateVarIndex + fieldsLength;
|
|
1331
|
+
});
|
|
1332
|
+
return [templateVarStoreInstrs, templateVarIndex];
|
|
1333
|
+
}
|
|
1334
|
+
function getTemplateVarLoadLocalInstrs(functionSig, structs) {
|
|
1335
|
+
let templateVarIndex = 1;
|
|
1336
|
+
let loadIndex = 0;
|
|
1337
|
+
const templateVarLoadInstrs = [];
|
|
1338
|
+
functionSig.paramTypes.forEach((paramType) => {
|
|
1339
|
+
const fieldsLength = ralph.typeLength(paramType, structs);
|
|
1340
|
+
if (fieldsLength === 1) {
|
|
1341
|
+
templateVarLoadInstrs.push(`{${templateVarIndex}}`);
|
|
1342
|
+
}
|
|
1343
|
+
if (fieldsLength > 1) {
|
|
1344
|
+
for (let i = 0; i < fieldsLength; i++) {
|
|
1345
|
+
templateVarLoadInstrs.push(encodeLoadLocalInstr(loadIndex + i));
|
|
1346
|
+
}
|
|
1347
|
+
loadIndex = loadIndex + fieldsLength;
|
|
1348
|
+
}
|
|
1349
|
+
templateVarIndex = templateVarIndex + fieldsLength;
|
|
1350
|
+
});
|
|
1351
|
+
return templateVarLoadInstrs;
|
|
1352
|
+
}
|
|
1353
|
+
function encodeStoreLocalInstr(index) {
|
|
1354
|
+
if (index < 0 || index > 0xff) {
|
|
1355
|
+
throw new Error(`StoreLocal index ${index} must be between 0 and 255 inclusive`);
|
|
1356
|
+
}
|
|
1357
|
+
return encodeInstr(StoreLocal(index));
|
|
1358
|
+
}
|
|
1359
|
+
function encodeLoadLocalInstr(index) {
|
|
1360
|
+
if (index < 0 || index > 0xff) {
|
|
1361
|
+
throw new Error(`LoadLocal index ${index} must be between 0 and 255 inclusive`);
|
|
1362
|
+
}
|
|
1363
|
+
return encodeInstr(LoadLocal(index));
|
|
1364
|
+
}
|
|
1365
|
+
function encodeI32(value) {
|
|
1366
|
+
return binToHex(i32Codec.encode(value));
|
|
1367
|
+
}
|
|
1368
|
+
function encodeU256Const(value) {
|
|
1369
|
+
if (value < 0) {
|
|
1370
|
+
throw new Error(`value ${value} must be non-negative`);
|
|
1371
|
+
}
|
|
1372
|
+
if (value < 6) {
|
|
1373
|
+
return (BigInt(0x0c) + value).toString(16).padStart(2, '0');
|
|
1374
|
+
}
|
|
1375
|
+
else {
|
|
1376
|
+
return encodeInstr(U256Const(value));
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
function encodeInstr(instr) {
|
|
1380
|
+
return binToHex(instrCodec.encode(instr));
|
|
1381
|
+
}
|
|
1382
|
+
function toFieldsSig(contractName, functionSig) {
|
|
1383
|
+
return {
|
|
1384
|
+
names: ['__contract__'].concat(functionSig.paramNames),
|
|
1385
|
+
types: [contractName].concat(functionSig.paramTypes),
|
|
1386
|
+
isMutable: [false].concat(functionSig.paramIsMutable)
|
|
1387
|
+
};
|
|
1388
|
+
}
|
|
1389
|
+
export async function multicallMethods(contract, instance, _callss, getContractByCodeHash) {
|
|
1390
|
+
const callss = Array.isArray(_callss) ? _callss : [_callss];
|
|
1391
|
+
const callEntries = callss.map((calls) => Object.entries(calls));
|
|
1392
|
+
const callsParams = callEntries.map((entries) => {
|
|
1393
|
+
return entries.map((entry) => {
|
|
1394
|
+
const [methodName, params] = entry;
|
|
1395
|
+
const methodIndex = contract.contract.getMethodIndex(methodName);
|
|
1396
|
+
const txId = params?.txId ?? randomTxId();
|
|
1397
|
+
const callArgs = getCallMethodArgs(params.args ?? {}, instance.groupIndex);
|
|
1398
|
+
return contract.contract.toApiCallContract({ ...params, txId: txId, args: callArgs }, instance.groupIndex, instance.address, methodIndex);
|
|
1399
|
+
});
|
|
1400
|
+
});
|
|
1401
|
+
const result = await getCurrentNodeProvider().contracts.postContractsMulticallContract({ calls: callsParams.flat() });
|
|
1402
|
+
let callResultIndex = 0;
|
|
1403
|
+
const results = callsParams.map((calls, index0) => {
|
|
1404
|
+
const callsResult = {};
|
|
1405
|
+
const entries = callEntries[`${index0}`];
|
|
1406
|
+
calls.forEach((call, index1) => {
|
|
1407
|
+
const methodIndex = call.methodIndex;
|
|
1408
|
+
const callResult = result.results[`${callResultIndex}`];
|
|
1409
|
+
const methodName = entries[`${index1}`][`0`];
|
|
1410
|
+
callsResult[`${methodName}`] = contract.contract.fromApiCallContractResult(callResult, call.txId, methodIndex, getContractByCodeHash);
|
|
1411
|
+
callResultIndex += 1;
|
|
1412
|
+
});
|
|
1413
|
+
return callsResult;
|
|
1414
|
+
});
|
|
1415
|
+
return Array.isArray(_callss) ? results : results[0];
|
|
1416
|
+
}
|
|
1417
|
+
export async function getContractEventsCurrentCount(contractAddress) {
|
|
1418
|
+
return getCurrentNodeProvider()
|
|
1419
|
+
.events.getEventsContractContractaddressCurrentCount(contractAddress)
|
|
1420
|
+
.catch((error) => {
|
|
1421
|
+
if (error instanceof Error && error.message.includes(`${contractAddress} not found`)) {
|
|
1422
|
+
return 0;
|
|
1423
|
+
}
|
|
1424
|
+
throw new TraceableError(`Failed to get the event count for the contract ${contractAddress}`, error);
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
export const getContractIdFromUnsignedTx = async (nodeProvider, unsignedTx) => {
|
|
1428
|
+
const result = await nodeProvider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx });
|
|
1429
|
+
const outputIndex = result.unsignedTx.fixedOutputs.length;
|
|
1430
|
+
const hex = result.unsignedTx.txId + outputIndex.toString(16).padStart(8, '0');
|
|
1431
|
+
const hashHex = binToHex(blake2b(hexToBinUnsafe(hex), { dkLen: 32 }));
|
|
1432
|
+
return hashHex.slice(0, 62) + result.fromGroup.toString(16).padStart(2, '0');
|
|
1433
|
+
};
|
|
1434
|
+
export const getTokenIdFromUnsignedTx = getContractIdFromUnsignedTx;
|
|
1435
|
+
export async function getContractCodeByCodeHash(nodeProvider, codeHash) {
|
|
1436
|
+
if (isHexString(codeHash) && codeHash.length === 64) {
|
|
1437
|
+
try {
|
|
1438
|
+
return await nodeProvider.contracts.getContractsCodehashCode(codeHash);
|
|
1439
|
+
}
|
|
1440
|
+
catch (error) {
|
|
1441
|
+
if (error instanceof Error && error.message.includes('not found')) {
|
|
1442
|
+
return undefined;
|
|
1443
|
+
}
|
|
1444
|
+
throw new TraceableError(`Failed to get contract by code hash ${codeHash}`, error);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
throw new Error(`Invalid code hash: ${codeHash}`);
|
|
1448
|
+
}
|
|
1449
|
+
//# sourceMappingURL=contract.js.map
|