opnet 1.0.49 → 1.0.50
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/.babelrc +8 -7
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
- package/.github/workflows/ci.yml +48 -0
- package/.prettierrc.json +11 -11
- package/CONTRIBUTING.md +71 -71
- package/LICENSE.md +16 -16
- package/README.md +96 -93
- package/STAKING.md +60 -70
- package/browser/_version.d.ts +1 -1
- package/browser/bitcoin/BitcoinAddress.d.ts +0 -1
- package/browser/contracts/CallResult.d.ts +0 -1
- package/browser/contracts/Contract.d.ts +0 -1
- package/browser/contracts/ContractData.d.ts +0 -1
- package/browser/contracts/interfaces/IContract.d.ts +0 -1
- package/browser/contracts/interfaces/IRawContract.d.ts +0 -1
- package/browser/crypto/crypto.d.ts +0 -1
- package/browser/index.js +2 -2
- package/browser/providers/AbstractRpcProvider.d.ts +47 -48
- package/browser/providers/JSONRpcProvider.d.ts +10 -10
- package/browser/storage/StoredValue.d.ts +0 -1
- package/browser/storage/interfaces/IStorageValue.d.ts +0 -1
- package/browser/transactions/decoders/DeploymentTransaction.d.ts +0 -1
- package/browser/transactions/decoders/InteractionTransaction.d.ts +0 -1
- package/browser/transactions/interfaces/ITransaction.d.ts +0 -1
- package/browser/transactions/interfaces/ITransactionReceipt.d.ts +0 -1
- package/browser/transactions/interfaces/transactions/IDeploymentTransaction.d.ts +0 -1
- package/browser/transactions/interfaces/transactions/IInteractionTransaction.d.ts +0 -1
- package/browser/transactions/interfaces/transactions/IUnwrapTransaction.d.ts +0 -1
- package/browser/transactions/metadata/TransactionOutput.d.ts +0 -1
- package/browser/transactions/metadata/TransactionReceipt.d.ts +0 -1
- package/build/abi/BitcoinInterface.js +0 -1
- package/build/bitcoin/BitcoinAddress.d.ts +0 -1
- package/build/bitcoin/BitcoinAddress.js +30 -19
- package/build/bitcoin/UTXOs.js +0 -4
- package/build/block/Block.js +1 -19
- package/build/contracts/CallResult.d.ts +0 -1
- package/build/contracts/CallResult.js +2 -7
- package/build/contracts/Contract.d.ts +0 -1
- package/build/contracts/Contract.js +1 -6
- package/build/contracts/ContractData.d.ts +0 -1
- package/build/contracts/ContractData.js +0 -11
- package/build/contracts/OPNetEvent.js +2 -5
- package/build/contracts/interfaces/IContract.d.ts +0 -1
- package/build/contracts/interfaces/IRawContract.d.ts +0 -1
- package/build/crypto/crypto.d.ts +0 -1
- package/build/providers/AbstractRpcProvider.d.ts +0 -1
- package/build/providers/AbstractRpcProvider.js +3 -4
- package/build/providers/JSONRpcProvider.js +1 -3
- package/build/providers/WebsocketRpcProvider.js +0 -1
- package/build/storage/StoredValue.d.ts +0 -1
- package/build/storage/StoredValue.js +0 -4
- package/build/storage/interfaces/IStorageValue.d.ts +0 -1
- package/build/transactions/Transaction.js +0 -8
- package/build/transactions/decoders/DeploymentTransaction.d.ts +0 -1
- package/build/transactions/decoders/DeploymentTransaction.js +0 -10
- package/build/transactions/decoders/InteractionTransaction.d.ts +0 -1
- package/build/transactions/decoders/InteractionTransaction.js +0 -7
- package/build/transactions/decoders/UnwrapTransaction.js +0 -5
- package/build/transactions/decoders/WrapTransaction.js +0 -7
- package/build/transactions/interfaces/ITransaction.d.ts +0 -1
- package/build/transactions/interfaces/ITransactionReceipt.d.ts +0 -1
- package/build/transactions/interfaces/transactions/IDeploymentTransaction.d.ts +0 -1
- package/build/transactions/interfaces/transactions/IInteractionTransaction.d.ts +0 -1
- package/build/transactions/interfaces/transactions/IUnwrapTransaction.d.ts +0 -1
- package/build/transactions/metadata/TransactionInput.js +1 -5
- package/build/transactions/metadata/TransactionOutput.d.ts +0 -1
- package/build/transactions/metadata/TransactionOutput.js +0 -4
- package/build/transactions/metadata/TransactionReceipt.d.ts +0 -1
- package/build/transactions/metadata/TransactionReceipt.js +0 -4
- package/cjs/package.json +3 -3
- package/docs/assets/highlight.css +92 -92
- package/docs/classes/AbstractRpcProvider.html +334 -334
- package/docs/classes/BaseContract.html +198 -198
- package/docs/classes/BitcoinAddress.html +192 -192
- package/docs/classes/BitcoinInterface.html +182 -182
- package/docs/classes/BitcoinUtils.html +182 -182
- package/docs/classes/Block.html +196 -196
- package/docs/classes/CallResult.html +185 -185
- package/docs/classes/ContractData.html +187 -187
- package/docs/classes/DeploymentTransaction.html +220 -220
- package/docs/classes/GenericTransaction.html +200 -200
- package/docs/classes/IBaseContract.html +203 -203
- package/docs/classes/InteractionTransaction.html +213 -213
- package/docs/classes/JSONRpcProvider.html +332 -332
- package/docs/classes/OPNetEvent.html +181 -181
- package/docs/classes/StoredValue.html +181 -181
- package/docs/classes/TransactionBase.html +200 -200
- package/docs/classes/TransactionInput.html +180 -180
- package/docs/classes/TransactionOutput.html +180 -180
- package/docs/classes/TransactionParser.html +177 -177
- package/docs/classes/TransactionReceipt.html +187 -187
- package/docs/classes/UTXO.html +180 -180
- package/docs/classes/UnwrapTransaction.html +224 -224
- package/docs/classes/WebSocketRpcProvider.html +329 -329
- package/docs/classes/WrapTransaction.html +228 -228
- package/docs/enums/ABIDataTypes.html +193 -193
- package/docs/enums/BitcoinAbiTypes.html +177 -177
- package/docs/enums/GenerateTarget.html +174 -174
- package/docs/enums/JSONRPCErrorCode.html +183 -183
- package/docs/enums/JSONRPCErrorHttpCodes.html +183 -183
- package/docs/enums/JSONRpcMethods.html +196 -196
- package/docs/enums/OPNetTransactionTypes.html +178 -178
- package/docs/functions/getContract.html +182 -182
- package/docs/hierarchy.html +173 -173
- package/docs/index.html +218 -218
- package/docs/interfaces/BaseContractProperties.html +180 -180
- package/docs/interfaces/BitcoinAbiValue.html +179 -179
- package/docs/interfaces/BitcoinInterfaceAbiBase.html +177 -177
- package/docs/interfaces/BroadcastedTransaction.html +183 -183
- package/docs/interfaces/ContractEvents.html +173 -173
- package/docs/interfaces/EventBaseData.html +178 -178
- package/docs/interfaces/FunctionBaseData.html +181 -181
- package/docs/interfaces/IAccessList.html +175 -175
- package/docs/interfaces/IAccessListItem.html +175 -175
- package/docs/interfaces/IBlock.html +195 -195
- package/docs/interfaces/IBlockCommon.html +194 -194
- package/docs/interfaces/IBlockWitness.html +175 -175
- package/docs/interfaces/IBlockWitnessAPI.html +177 -177
- package/docs/interfaces/ICallRequestError.html +176 -176
- package/docs/interfaces/ICallResultData.html +179 -179
- package/docs/interfaces/ICommonTransaction.html +205 -205
- package/docs/interfaces/IContract.html +180 -180
- package/docs/interfaces/IDecodedEvent.html +177 -177
- package/docs/interfaces/IDeploymentTransaction.html +219 -219
- package/docs/interfaces/IGenericTransaction.html +199 -199
- package/docs/interfaces/IInteractionTransaction.html +213 -213
- package/docs/interfaces/IMotoContract.html +196 -196
- package/docs/interfaces/IMotoswapFactoryContract.html +193 -193
- package/docs/interfaces/IMotoswapPoolContract.html +208 -208
- package/docs/interfaces/IMotoswapRouterContract.html +227 -227
- package/docs/interfaces/IOP_20Contract.html +196 -196
- package/docs/interfaces/IOP_NETContract.html +183 -183
- package/docs/interfaces/IRawContract.html +187 -187
- package/docs/interfaces/IRawContractEvents.html +173 -173
- package/docs/interfaces/IStackingContract.html +183 -183
- package/docs/interfaces/IStorageValue.html +179 -179
- package/docs/interfaces/ITransactionBase.html +199 -199
- package/docs/interfaces/ITransactionInput.html +179 -179
- package/docs/interfaces/ITransactionOutput.html +177 -177
- package/docs/interfaces/ITransactionReceipt.html +183 -183
- package/docs/interfaces/IUTXO.html +179 -179
- package/docs/interfaces/IUnwrapTransaction.html +223 -223
- package/docs/interfaces/IWBTCContract.html +209 -209
- package/docs/interfaces/IWrapTransaction.html +227 -227
- package/docs/interfaces/JSONRpc2Request.html +177 -177
- package/docs/interfaces/JSONRpc2ResponseError.html +177 -177
- package/docs/interfaces/JSONRpc2ResponseResult.html +177 -177
- package/docs/interfaces/JSONRpc2ResultData.html +173 -173
- package/docs/interfaces/JSONRpcErrorData.html +173 -173
- package/docs/interfaces/JSONRpcParams.html +173 -173
- package/docs/interfaces/JSONRpcResultError.html +176 -176
- package/docs/interfaces/MergedOP_20AndStackingContract.html +201 -201
- package/docs/interfaces/NetEventDocument.html +182 -182
- package/docs/interfaces/ParsedPartialWBTCUTXODocument.html +178 -178
- package/docs/interfaces/PartialWBTCUTXODocument.html +178 -178
- package/docs/interfaces/ReorgInformation.html +176 -176
- package/docs/interfaces/UsedUTXO.html +175 -175
- package/docs/modules.html +296 -296
- package/docs/types/BaseContractProperty.html +175 -175
- package/docs/types/BigNumberish.html +173 -173
- package/docs/types/BitcoinAddressLike.html +173 -173
- package/docs/types/BitcoinInterfaceAbi.html +173 -173
- package/docs/types/BlockHeaderChecksumProof.html +173 -173
- package/docs/types/BlockTag.html +173 -173
- package/docs/types/BlockWitnesses.html +173 -173
- package/docs/types/ContractDecodedObjectResult.html +173 -173
- package/docs/types/DecodedCallResult.html +173 -173
- package/docs/types/DecodedOutput.html +173 -173
- package/docs/types/GenerationParameters.html +173 -173
- package/docs/types/ICallResult.html +174 -174
- package/docs/types/ITransaction.html +174 -174
- package/docs/types/InteractionType.html +173 -173
- package/docs/types/JSONRpc2RequestParams.html +173 -173
- package/docs/types/JSONRpc2Result.html +173 -173
- package/docs/types/JSONRpcId.html +173 -173
- package/docs/types/JsonRpcCallResult.html +173 -173
- package/docs/types/JsonRpcError.html +173 -173
- package/docs/types/JsonRpcPayload.html +173 -173
- package/docs/types/JsonRpcResult.html +173 -173
- package/docs/types/Numeric.html +173 -173
- package/docs/types/PointerLike.html +173 -173
- package/docs/types/RawContractEvents.html +173 -173
- package/docs/types/UTXOs.html +175 -175
- package/docs/variables/MOTOSWAP_ROUTER_ABI.html +173 -173
- package/docs/variables/MOTO_TOKEN_ABI.html +173 -173
- package/docs/variables/MotoSwapFactoryAbi.html +173 -173
- package/docs/variables/MotoSwapFactoryEvents.html +173 -173
- package/docs/variables/MotoSwapPoolEvents.html +173 -173
- package/docs/variables/MotoswapPoolAbi.html +173 -173
- package/docs/variables/OP20Events.html +173 -173
- package/docs/variables/OP_20_ABI.html +173 -173
- package/docs/variables/OP_NET_ABI.html +173 -173
- package/docs/variables/STAKING_ABI.html +173 -173
- package/docs/variables/StackingEvents.html +173 -173
- package/docs/variables/WBTCEvents.html +173 -173
- package/docs/variables/WBTC_ABI.html +173 -173
- package/docs/variables/version.html +173 -173
- package/gulpfile.js +49 -120
- package/package.json +111 -109
- package/src/_version.ts +1 -1
- package/src/abi/shared/json/MOTOSWAP_POOL_ABI.ts +299 -299
- package/src/contracts/Contract.ts +600 -600
- package/src/crypto/crypto.ts +1 -1
- package/src/providers/AbstractRpcProvider.ts +602 -602
- package/src/providers/JSONRpcProvider.ts +81 -81
- package/src/serialize/BigInt.ts +15 -15
- package/tsconfig.base.json +43 -43
- package/tsconfig.json +15 -8
- package/tsconfig.webpack.json +19 -19
- package/.github/workflows/node.js.yml +0 -21
- package/.idea/bsc.iml +0 -14
- package/.idea/bsi-binary.iml +0 -14
- package/.idea/codeStyles/Project.xml +0 -101
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -7
- package/.idea/markdown.xml +0 -7
- package/.idea/modules.xml +0 -8
- package/.idea/motoswap.iml +0 -12
- package/.idea/opnet.iml +0 -17
- package/.idea/prettier.xml +0 -7
- package/.idea/vcs.xml +0 -6
- package/browser/crypto/crypto-browser.d.ts +0 -40
- package/browser/generator/WrappedGenerationParameters.d.ts +0 -9
- package/browser/scripts/ITestContract.d.ts +0 -22
- package/browser/scripts/block.d.ts +0 -1
- package/browser/scripts/test.d.ts +0 -1
- package/browser/scripts/testContract.d.ts +0 -1
- package/browser/scripts/testMethods.d.ts +0 -1
- package/browser/scripts/testMethodsReg.d.ts +0 -1
- package/browser/scripts/testWBTCContract.d.ts +0 -1
- package/browser/transactions/DeploymentTransaction.d.ts +0 -16
- package/browser/transactions/GenericTransaction.d.ts +0 -6
- package/browser/transactions/InteractionTransaction.d.ts +0 -15
- package/browser/transactions/TransactionInput.d.ts +0 -16
- package/browser/transactions/TransactionOutput.d.ts +0 -19
- package/browser/transactions/TransactionReceipt.d.ts +0 -11
- package/build/abi/BitcoinInterfaceAbi.d.ts +0 -3
- package/build/abi/BitcoinInterfaceAbi.js +0 -1
- package/build/abi/enums/ParamTypes.d.ts +0 -0
- package/build/abi/enums/ParamTypes.js +0 -1
- package/build/abi/interface/BitcoinInterface.d.ts +0 -6
- package/build/abi/interface/BitcoinInterface.js +0 -12
- package/build/abi/interface/BitcoinInterfaceAbi.d.ts +0 -3
- package/build/abi/interface/BitcoinInterfaceAbi.js +0 -1
- package/build/abi/interfaces/BaseContractProperty.d.ts +0 -2
- package/build/abi/interfaces/BaseContractProperty.js +0 -1
- package/build/abi/interfaces/BitcoinAbiInput.d.ts +0 -5
- package/build/abi/interfaces/BitcoinAbiInput.js +0 -1
- package/build/abi/shared/MOTOSWAP_FACTORY_ABI.d.ts +0 -2
- package/build/abi/shared/MOTOSWAP_FACTORY_ABI.js +0 -68
- package/build/abi/shared/OP_20_ABI.d.ts +0 -2
- package/build/abi/shared/OP_20_ABI.js +0 -235
- package/build/abi/shared/OP_NET_ABI.d.ts +0 -2
- package/build/abi/shared/OP_NET_ABI.js +0 -31
- package/build/abi/shared/STAKING_ABI.d.ts +0 -2
- package/build/abi/shared/STAKING_ABI.js +0 -96
- package/build/abi/shared/WBTC_ABI.d.ts +0 -2
- package/build/abi/shared/WBTC_ABI.js +0 -40
- package/build/bitcoin/UXTOs.d.ts +0 -7
- package/build/bitcoin/UXTOs.js +0 -1
- package/build/block/index.d.ts +0 -2
- package/build/block/index.js +0 -1
- package/build/contracts/DecodedEvent.d.ts +0 -15
- package/build/contracts/DecodedEvent.js +0 -18
- package/build/contracts/IRawContract.d.ts +0 -13
- package/build/contracts/IRawContract.js +0 -1
- package/build/crypto/crypto-browser.d.ts +0 -0
- package/build/crypto/crypto-browser.js +0 -47
- package/build/generator/WrappedGenerationParameters.d.ts +0 -9
- package/build/generator/WrappedGenerationParameters.js +0 -14
- package/build/interface/BitcoinInterface.d.ts +0 -6
- package/build/interface/BitcoinInterface.js +0 -12
- package/build/interface/BitcoinInterfaceAbi.d.ts +0 -3
- package/build/interface/BitcoinInterfaceAbi.js +0 -1
- package/build/interface/Interface.d.ts +0 -2
- package/build/interface/Interface.js +0 -2
- package/build/interface.d.ts +0 -0
- package/build/interface.js +0 -1
- package/build/interfaces/Block.d.ts +0 -0
- package/build/interfaces/Block.js +0 -1
- package/build/interfaces/IBlock.d.ts +0 -0
- package/build/interfaces/IBlock.js +0 -1
- package/build/interfaces/blocks/IBlock.d.ts +0 -28
- package/build/interfaces/blocks/IBlock.js +0 -1
- package/build/interfaces/transactions/ITransaction.d.ts +0 -39
- package/build/interfaces/transactions/ITransaction.js +0 -1
- package/build/providers/index.d.ts +0 -4
- package/build/providers/index.js +0 -4
- package/build/providers/interfaces/GenerateResult.d.ts +0 -0
- package/build/providers/interfaces/GenerateResult.js +0 -1
- package/build/scripts/ITestContract.d.ts +0 -18
- package/build/scripts/ITestContract.js +0 -1
- package/build/scripts/block.d.ts +0 -1
- package/build/scripts/block.js +0 -13
- package/build/scripts/test.d.ts +0 -1
- package/build/scripts/test.js +0 -24
- package/build/scripts/testContract.d.ts +0 -1
- package/build/scripts/testContract.js +0 -11
- package/build/scripts/testMethods.d.ts +0 -1
- package/build/scripts/testMethods.js +0 -11
- package/build/scripts/testMethodsReg.d.ts +0 -1
- package/build/scripts/testMethodsReg.js +0 -8
- package/build/scripts/testWBTCContract.d.ts +0 -1
- package/build/scripts/testWBTCContract.js +0 -11
- package/build/storage/interfaces/IStorage.d.ts +0 -7
- package/build/storage/interfaces/IStorage.js +0 -1
- package/build/transactions/DeploymentTransaction.d.ts +0 -16
- package/build/transactions/DeploymentTransaction.js +0 -24
- package/build/transactions/GenericTransaction.d.ts +0 -6
- package/build/transactions/GenericTransaction.js +0 -6
- package/build/transactions/InteractionTransaction.d.ts +0 -15
- package/build/transactions/InteractionTransaction.js +0 -21
- package/build/transactions/TransactionInput.d.ts +0 -16
- package/build/transactions/TransactionInput.js +0 -14
- package/build/transactions/TransactionOutput.d.ts +0 -19
- package/build/transactions/TransactionOutput.js +0 -16
- package/build/transactions/TransactionReceipt.d.ts +0 -11
- package/build/transactions/TransactionReceipt.js +0 -53
- package/build/transactions/decoders/WrapInteractionTransaction.d.ts +0 -15
- package/build/transactions/decoders/WrapInteractionTransaction.js +0 -21
- package/build/transactions/index.d.ts +0 -6
- package/build/transactions/index.js +0 -6
- package/build/transactions/interfaces/IWrapTransaction.d.ts +0 -3
- package/build/transactions/interfaces/IWrapTransaction.js +0 -1
- package/build/transactions/types/DeploymentTransaction.d.ts +0 -16
- package/build/transactions/types/DeploymentTransaction.js +0 -24
- package/build/transactions/types/GenericTransaction.d.ts +0 -6
- package/build/transactions/types/GenericTransaction.js +0 -6
- package/build/transactions/types/InteractionTransaction.d.ts +0 -15
- package/build/transactions/types/InteractionTransaction.js +0 -21
- package/cjs/_version.d.ts +0 -1
- package/cjs/_version.js +0 -4
- package/cjs/abi/BaseContractProperty.d.ts +0 -3
- package/cjs/abi/BaseContractProperty.js +0 -2
- package/cjs/abi/BitcoinAbiTypes.d.ts +0 -4
- package/cjs/abi/BitcoinAbiTypes.js +0 -8
- package/cjs/abi/BitcoinInterface.d.ts +0 -10
- package/cjs/abi/BitcoinInterface.js +0 -60
- package/cjs/abi/interfaces/BaseContractProperties.d.ts +0 -5
- package/cjs/abi/interfaces/BaseContractProperties.js +0 -2
- package/cjs/abi/interfaces/BitcoinAbiValue.d.ts +0 -5
- package/cjs/abi/interfaces/BitcoinAbiValue.js +0 -2
- package/cjs/abi/interfaces/BitcoinInterfaceAbi.d.ts +0 -16
- package/cjs/abi/interfaces/BitcoinInterfaceAbi.js +0 -2
- package/cjs/abi/shared/interfaces/IMotoContract.d.ts +0 -6
- package/cjs/abi/shared/interfaces/IMotoContract.js +0 -2
- package/cjs/abi/shared/interfaces/IMotoswapFactoryContract.d.ts +0 -7
- package/cjs/abi/shared/interfaces/IMotoswapFactoryContract.js +0 -2
- package/cjs/abi/shared/interfaces/IMotoswapPoolContract.d.ts +0 -13
- package/cjs/abi/shared/interfaces/IMotoswapPoolContract.js +0 -2
- package/cjs/abi/shared/interfaces/IMotoswapRouterContract.d.ts +0 -15
- package/cjs/abi/shared/interfaces/IMotoswapRouterContract.js +0 -2
- package/cjs/abi/shared/interfaces/IOP_20Contract.d.ts +0 -16
- package/cjs/abi/shared/interfaces/IOP_20Contract.js +0 -2
- package/cjs/abi/shared/interfaces/IOP_NETContract.d.ts +0 -7
- package/cjs/abi/shared/interfaces/IOP_NETContract.js +0 -2
- package/cjs/abi/shared/interfaces/IStackingContract.d.ts +0 -11
- package/cjs/abi/shared/interfaces/IStackingContract.js +0 -2
- package/cjs/abi/shared/interfaces/IWBTCContract.d.ts +0 -8
- package/cjs/abi/shared/interfaces/IWBTCContract.js +0 -2
- package/cjs/abi/shared/json/MOTOSWAP_FACTORY_ABI.d.ts +0 -3
- package/cjs/abi/shared/json/MOTOSWAP_FACTORY_ABI.js +0 -74
- package/cjs/abi/shared/json/MOTOSWAP_POOL_ABI.d.ts +0 -3
- package/cjs/abi/shared/json/MOTOSWAP_POOL_ABI.js +0 -280
- package/cjs/abi/shared/json/MOTOSWAP_ROUTER_ABI.d.ts +0 -2
- package/cjs/abi/shared/json/MOTOSWAP_ROUTER_ABI.js +0 -277
- package/cjs/abi/shared/json/MOTO_TOKEN_ABI.d.ts +0 -2
- package/cjs/abi/shared/json/MOTO_TOKEN_ABI.js +0 -25
- package/cjs/abi/shared/json/OP_20_ABI.d.ts +0 -3
- package/cjs/abi/shared/json/OP_20_ABI.js +0 -270
- package/cjs/abi/shared/json/OP_NET_ABI.d.ts +0 -2
- package/cjs/abi/shared/json/OP_NET_ABI.js +0 -34
- package/cjs/abi/shared/json/STAKING_ABI.d.ts +0 -3
- package/cjs/abi/shared/json/STAKING_ABI.js +0 -132
- package/cjs/abi/shared/json/WBTC_ABI.d.ts +0 -3
- package/cjs/abi/shared/json/WBTC_ABI.js +0 -60
- package/cjs/bitcoin/BitcoinAddress.d.ts +0 -20
- package/cjs/bitcoin/BitcoinAddress.js +0 -116
- package/cjs/bitcoin/UTXOs.d.ts +0 -10
- package/cjs/bitcoin/UTXOs.js +0 -16
- package/cjs/bitcoin/interfaces/IUTXO.d.ts +0 -7
- package/cjs/bitcoin/interfaces/IUTXO.js +0 -2
- package/cjs/block/Block.d.ts +0 -26
- package/cjs/block/Block.js +0 -47
- package/cjs/block/interfaces/BlockWitness.d.ts +0 -11
- package/cjs/block/interfaces/BlockWitness.js +0 -2
- package/cjs/block/interfaces/IBlock.d.ts +0 -28
- package/cjs/block/interfaces/IBlock.js +0 -2
- package/cjs/common/CommonTypes.d.ts +0 -8
- package/cjs/common/CommonTypes.js +0 -2
- package/cjs/contracts/CallResult.d.ts +0 -19
- package/cjs/contracts/CallResult.js +0 -35
- package/cjs/contracts/Contract.d.ts +0 -44
- package/cjs/contracts/Contract.js +0 -398
- package/cjs/contracts/ContractData.d.ts +0 -17
- package/cjs/contracts/ContractData.js +0 -38
- package/cjs/contracts/OPNetEvent.d.ts +0 -15
- package/cjs/contracts/OPNetEvent.js +0 -22
- package/cjs/contracts/interfaces/IAccessList.d.ts +0 -6
- package/cjs/contracts/interfaces/IAccessList.js +0 -2
- package/cjs/contracts/interfaces/ICallResult.d.ts +0 -12
- package/cjs/contracts/interfaces/ICallResult.js +0 -2
- package/cjs/contracts/interfaces/IContract.d.ts +0 -12
- package/cjs/contracts/interfaces/IContract.js +0 -2
- package/cjs/contracts/interfaces/IRawContract.d.ts +0 -15
- package/cjs/contracts/interfaces/IRawContract.js +0 -2
- package/cjs/crypto/crypto.d.ts +0 -2
- package/cjs/crypto/crypto.js +0 -8
- package/cjs/index.d.ts +0 -3
- package/cjs/index.js +0 -32
- package/cjs/interfaces/opnet/OPNetTransactionTypes.d.ts +0 -8
- package/cjs/interfaces/opnet/OPNetTransactionTypes.js +0 -11
- package/cjs/opnet.d.ts +0 -70
- package/cjs/opnet.js +0 -89
- package/cjs/providers/AbstractRpcProvider.d.ts +0 -48
- package/cjs/providers/AbstractRpcProvider.js +0 -277
- package/cjs/providers/JSONRpcProvider.d.ts +0 -10
- package/cjs/providers/JSONRpcProvider.js +0 -59
- package/cjs/providers/WebsocketRpcProvider.d.ts +0 -9
- package/cjs/providers/WebsocketRpcProvider.js +0 -27
- package/cjs/providers/interfaces/Generate.d.ts +0 -5
- package/cjs/providers/interfaces/Generate.js +0 -7
- package/cjs/providers/interfaces/JSONRpc.d.ts +0 -11
- package/cjs/providers/interfaces/JSONRpc.js +0 -2
- package/cjs/providers/interfaces/JSONRpcMethods.d.ts +0 -17
- package/cjs/providers/interfaces/JSONRpcMethods.js +0 -21
- package/cjs/providers/interfaces/JSONRpcParams.d.ts +0 -3
- package/cjs/providers/interfaces/JSONRpcParams.js +0 -2
- package/cjs/providers/interfaces/JSONRpcResult.d.ts +0 -51
- package/cjs/providers/interfaces/JSONRpcResult.js +0 -29
- package/cjs/providers/interfaces/ReorgInformation.d.ts +0 -5
- package/cjs/providers/interfaces/ReorgInformation.js +0 -2
- package/cjs/serialize/BigInt.d.ts +0 -6
- package/cjs/serialize/BigInt.js +0 -5
- package/cjs/storage/StoredValue.d.ts +0 -10
- package/cjs/storage/StoredValue.js +0 -28
- package/cjs/storage/interfaces/IStorageValue.d.ts +0 -8
- package/cjs/storage/interfaces/IStorageValue.js +0 -2
- package/cjs/transactions/Transaction.d.ts +0 -17
- package/cjs/transactions/Transaction.js +0 -33
- package/cjs/transactions/TransactionParser.d.ts +0 -7
- package/cjs/transactions/TransactionParser.js +0 -43
- package/cjs/transactions/decoders/DeploymentTransaction.d.ts +0 -17
- package/cjs/transactions/decoders/DeploymentTransaction.js +0 -30
- package/cjs/transactions/decoders/GenericTransaction.d.ts +0 -6
- package/cjs/transactions/decoders/GenericTransaction.js +0 -10
- package/cjs/transactions/decoders/InteractionTransaction.d.ts +0 -15
- package/cjs/transactions/decoders/InteractionTransaction.js +0 -25
- package/cjs/transactions/decoders/UnwrapTransaction.d.ts +0 -11
- package/cjs/transactions/decoders/UnwrapTransaction.js +0 -33
- package/cjs/transactions/decoders/WrapTransaction.d.ts +0 -15
- package/cjs/transactions/decoders/WrapTransaction.js +0 -34
- package/cjs/transactions/interfaces/BroadcastedTransaction.d.ts +0 -7
- package/cjs/transactions/interfaces/BroadcastedTransaction.js +0 -2
- package/cjs/transactions/interfaces/ITransaction.d.ts +0 -23
- package/cjs/transactions/interfaces/ITransaction.js +0 -2
- package/cjs/transactions/interfaces/ITransactionReceipt.d.ts +0 -21
- package/cjs/transactions/interfaces/ITransactionReceipt.js +0 -2
- package/cjs/transactions/interfaces/transactions/ICommonTransaction.d.ts +0 -7
- package/cjs/transactions/interfaces/transactions/ICommonTransaction.js +0 -2
- package/cjs/transactions/interfaces/transactions/IDeploymentTransaction.d.ts +0 -13
- package/cjs/transactions/interfaces/transactions/IDeploymentTransaction.js +0 -2
- package/cjs/transactions/interfaces/transactions/IInteractionTransaction.d.ts +0 -14
- package/cjs/transactions/interfaces/transactions/IInteractionTransaction.js +0 -2
- package/cjs/transactions/interfaces/transactions/IUnwrapTransaction.d.ts +0 -28
- package/cjs/transactions/interfaces/transactions/IUnwrapTransaction.js +0 -2
- package/cjs/transactions/interfaces/transactions/IWrapTransaction.d.ts +0 -10
- package/cjs/transactions/interfaces/transactions/IWrapTransaction.js +0 -2
- package/cjs/transactions/metadata/TransactionInput.d.ts +0 -16
- package/cjs/transactions/metadata/TransactionInput.js +0 -18
- package/cjs/transactions/metadata/TransactionOutput.d.ts +0 -19
- package/cjs/transactions/metadata/TransactionOutput.js +0 -20
- package/cjs/transactions/metadata/TransactionReceipt.d.ts +0 -11
- package/cjs/transactions/metadata/TransactionReceipt.js +0 -57
- package/cjs/utils/BitcoinUtils.d.ts +0 -4
- package/cjs/utils/BitcoinUtils.js +0 -14
- package/tsconfig.cjs.json +0 -9
|
@@ -1,600 +1,600 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ABICoder,
|
|
3
|
-
ABIDataTypes,
|
|
4
|
-
Address,
|
|
5
|
-
BinaryReader,
|
|
6
|
-
BinaryWriter,
|
|
7
|
-
NetEvent,
|
|
8
|
-
} from '@btc-vision/bsi-binary';
|
|
9
|
-
import { BaseContractProperty } from '../abi/BaseContractProperty.js';
|
|
10
|
-
import { BitcoinAbiTypes } from '../abi/BitcoinAbiTypes.js';
|
|
11
|
-
import { BitcoinInterface } from '../abi/BitcoinInterface.js';
|
|
12
|
-
import { BaseContractProperties } from '../abi/interfaces/BaseContractProperties.js';
|
|
13
|
-
import { BitcoinAbiValue } from '../abi/interfaces/BitcoinAbiValue.js';
|
|
14
|
-
import {
|
|
15
|
-
BitcoinInterfaceAbi,
|
|
16
|
-
EventBaseData,
|
|
17
|
-
FunctionBaseData,
|
|
18
|
-
} from '../abi/interfaces/BitcoinInterfaceAbi.js';
|
|
19
|
-
import { BitcoinAddressLike, DecodedCallResult } from '../common/CommonTypes.js';
|
|
20
|
-
import { AbstractRpcProvider } from '../providers/AbstractRpcProvider.js';
|
|
21
|
-
import { ContractEvents } from '../transactions/interfaces/ITransactionReceipt.js';
|
|
22
|
-
import { IContract } from './interfaces/IContract.js';
|
|
23
|
-
import { OPNetEvent } from './OPNetEvent.js';
|
|
24
|
-
|
|
25
|
-
const internal = Symbol.for('_btc_internal');
|
|
26
|
-
const bitcoinAbiCoder = new ABICoder();
|
|
27
|
-
|
|
28
|
-
export type ContractDecodedObjectResult = { [key: string]: DecodedCallResult };
|
|
29
|
-
export type DecodedOutput = { values: Array<DecodedCallResult>; obj: ContractDecodedObjectResult };
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Represents the base contract class.
|
|
33
|
-
* @category Contracts
|
|
34
|
-
* @abstract
|
|
35
|
-
*/
|
|
36
|
-
export abstract class IBaseContract<T extends BaseContractProperties> implements IContract {
|
|
37
|
-
/**
|
|
38
|
-
* The address of the contract.
|
|
39
|
-
*/
|
|
40
|
-
public readonly address: BitcoinAddressLike;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The interface of the contract.
|
|
44
|
-
*/
|
|
45
|
-
public readonly interface!: BitcoinInterface;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* A generic provider for the contract.
|
|
49
|
-
*/
|
|
50
|
-
public readonly provider: AbstractRpcProvider;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Who is sending the transaction.
|
|
54
|
-
*/
|
|
55
|
-
public from?: Address;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The internal functions of the contract.
|
|
59
|
-
* @protected
|
|
60
|
-
*/
|
|
61
|
-
readonly [internal]: keyof T | undefined;
|
|
62
|
-
|
|
63
|
-
private events: Map<string, EventBaseData> = new Map();
|
|
64
|
-
|
|
65
|
-
protected constructor(
|
|
66
|
-
address: BitcoinAddressLike,
|
|
67
|
-
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
68
|
-
provider: AbstractRpcProvider,
|
|
69
|
-
from?: Address,
|
|
70
|
-
) {
|
|
71
|
-
this.address = address;
|
|
72
|
-
this.provider = provider;
|
|
73
|
-
this.interface = BitcoinInterface.from(abi);
|
|
74
|
-
this.from = from;
|
|
75
|
-
|
|
76
|
-
Object.defineProperty(this, internal, { value: {} });
|
|
77
|
-
|
|
78
|
-
this.defineInternalFunctions();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Sets the sender of the transaction.
|
|
83
|
-
* @param {Address} sender The sender of the transaction.
|
|
84
|
-
*/
|
|
85
|
-
public setSender(sender: Address): void {
|
|
86
|
-
this.from = sender;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public decodeEvents(events: NetEvent[] | ContractEvents): OPNetEvent[] {
|
|
90
|
-
const decodedEvents: OPNetEvent[] = [];
|
|
91
|
-
|
|
92
|
-
if (!Array.isArray(events)) {
|
|
93
|
-
events = events[this.address.toString()];
|
|
94
|
-
|
|
95
|
-
if (!events) {
|
|
96
|
-
return [];
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
for (let event of events) {
|
|
101
|
-
decodedEvents.push(this.decodeEvent(event));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return decodedEvents;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public decodeEvent(event: NetEvent): OPNetEvent {
|
|
108
|
-
const eventData = this.events.get(event.eventType);
|
|
109
|
-
if (!eventData || eventData.values.length === 0) {
|
|
110
|
-
return new OPNetEvent(event.eventType, event.eventDataSelector, event.eventData);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const binaryReader: BinaryReader = new BinaryReader(event.eventData);
|
|
114
|
-
const out: DecodedOutput = this.decodeOutput(eventData.values, binaryReader);
|
|
115
|
-
const decodedEvent = new OPNetEvent(
|
|
116
|
-
event.eventType,
|
|
117
|
-
event.eventDataSelector,
|
|
118
|
-
event.eventData,
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
decodedEvent.setDecoded(out);
|
|
122
|
-
|
|
123
|
-
return decodedEvent;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Encodes the calldata for a function.
|
|
128
|
-
* @param {string} functionName The name of the function.
|
|
129
|
-
* @param {unknown[]} args The arguments for the function.
|
|
130
|
-
* @returns {Buffer} The encoded calldata.
|
|
131
|
-
*/
|
|
132
|
-
public encodeCalldata(functionName: string, args: unknown[]): Buffer {
|
|
133
|
-
for (const element of this.interface.abi) {
|
|
134
|
-
if (element.name === functionName) {
|
|
135
|
-
const data = this.encodeFunctionData(element as FunctionBaseData, args);
|
|
136
|
-
return Buffer.from(data.getBuffer());
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
throw new Error(`Function not found: ${functionName}`);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
protected getFunction(
|
|
144
|
-
name: symbol,
|
|
145
|
-
): BaseContractProperty | undefined | string | number | symbol {
|
|
146
|
-
const key = name as keyof Omit<
|
|
147
|
-
IBaseContract<T>,
|
|
148
|
-
'address' | 'provider' | 'interface' | 'decodeEvents' | 'decodeEvent'
|
|
149
|
-
>;
|
|
150
|
-
|
|
151
|
-
// @ts-ignore
|
|
152
|
-
return this[key];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Defines the internal functions of the contract. These functions are generated for the ABI provided.
|
|
157
|
-
* @private
|
|
158
|
-
*/
|
|
159
|
-
private defineInternalFunctions(): void {
|
|
160
|
-
for (const element of this.interface.abi) {
|
|
161
|
-
switch (element.type) {
|
|
162
|
-
case BitcoinAbiTypes.Function: {
|
|
163
|
-
// @ts-ignore
|
|
164
|
-
// We will allow overwrites.
|
|
165
|
-
//if (this[element.name]) {
|
|
166
|
-
// throw new Error(`Duplicate function found in the ABI: ${element.name}.`);
|
|
167
|
-
//}
|
|
168
|
-
|
|
169
|
-
if (this[element.name]) {
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
Object.defineProperty(this, element.name, {
|
|
174
|
-
value: this.callFunction(element as FunctionBaseData).bind(this),
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
case BitcoinAbiTypes.Event: {
|
|
180
|
-
if (this.events.has(element.name)) {
|
|
181
|
-
throw new Error(`Duplicate event found in the ABI: ${element.name}.`);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
this.events.set(element.name, element);
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
default:
|
|
188
|
-
throw new Error(`Unsupported type.`);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
private encodeFunctionData(element: FunctionBaseData, args: unknown[]): BinaryWriter {
|
|
194
|
-
const writer = new BinaryWriter();
|
|
195
|
-
const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(element.name));
|
|
196
|
-
writer.writeSelector(selector);
|
|
197
|
-
|
|
198
|
-
if (args.length !== (element.inputs?.length ?? 0)) {
|
|
199
|
-
throw new Error('Invalid number of arguments provided');
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (!element.inputs || (element.inputs && element.inputs.length === 0)) {
|
|
203
|
-
return writer;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
for (let i = 0; i < element.inputs.length; i++) {
|
|
207
|
-
this.encodeInput(writer, element.inputs[i], args[i]);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return writer;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private encodeInput(writer: BinaryWriter, abi: BitcoinAbiValue, value: unknown): void {
|
|
214
|
-
const type = abi.type;
|
|
215
|
-
const name = abi.name;
|
|
216
|
-
|
|
217
|
-
switch (type) {
|
|
218
|
-
case ABIDataTypes.UINT256: {
|
|
219
|
-
if (typeof value !== 'bigint') {
|
|
220
|
-
throw new Error(`Expected value to be of type bigint (${name})`);
|
|
221
|
-
}
|
|
222
|
-
writer.writeU256(value as bigint);
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
case ABIDataTypes.BOOL: {
|
|
226
|
-
if (typeof value !== 'boolean') {
|
|
227
|
-
throw new Error(`Expected value to be of type boolean (${name})`);
|
|
228
|
-
}
|
|
229
|
-
writer.writeBoolean(value as boolean);
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
case ABIDataTypes.STRING: {
|
|
233
|
-
if (typeof value !== 'string') {
|
|
234
|
-
throw new Error(`Expected value to be of type string (${name})`);
|
|
235
|
-
}
|
|
236
|
-
writer.writeStringWithLength(value as string);
|
|
237
|
-
break;
|
|
238
|
-
}
|
|
239
|
-
case ABIDataTypes.ADDRESS: {
|
|
240
|
-
const address = value as BitcoinAddressLike;
|
|
241
|
-
writer.writeAddress(address.toString());
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
244
|
-
case ABIDataTypes.TUPLE: {
|
|
245
|
-
if (!(value instanceof Array)) {
|
|
246
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
writer.writeTuple(value as bigint[]);
|
|
250
|
-
break;
|
|
251
|
-
}
|
|
252
|
-
case ABIDataTypes.UINT8: {
|
|
253
|
-
if (typeof value !== 'number') {
|
|
254
|
-
throw new Error(`Expected value to be of type number (${name})`);
|
|
255
|
-
}
|
|
256
|
-
writer.writeU8(value as number);
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case ABIDataTypes.UINT16: {
|
|
260
|
-
if (typeof value !== 'number') {
|
|
261
|
-
throw new Error(`Expected value to be of type number (${name})`);
|
|
262
|
-
}
|
|
263
|
-
writer.writeU16(value as number);
|
|
264
|
-
break;
|
|
265
|
-
}
|
|
266
|
-
case ABIDataTypes.UINT32: {
|
|
267
|
-
if (typeof value !== 'number') {
|
|
268
|
-
throw new Error(`Expected value to be of type number (${name})`);
|
|
269
|
-
}
|
|
270
|
-
writer.writeU32(value as number);
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
case ABIDataTypes.BYTES32: {
|
|
274
|
-
if (!(value instanceof Uint8Array)) {
|
|
275
|
-
throw new Error(`Expected value to be of type Uint8Array (${name})`);
|
|
276
|
-
}
|
|
277
|
-
writer.writeBytes(value as Uint8Array);
|
|
278
|
-
break;
|
|
279
|
-
}
|
|
280
|
-
case ABIDataTypes.ADDRESS_UINT256_TUPLE: {
|
|
281
|
-
if (!(value instanceof Map)) {
|
|
282
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
writer.writeAddressValueTupleMap(value as Map<Address, bigint>);
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
case ABIDataTypes.BYTES: {
|
|
289
|
-
if (!(value instanceof Uint8Array)) {
|
|
290
|
-
throw new Error(`Expected value to be of type Uint8Array (${name})`);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
writer.writeBytesWithLength(value as Uint8Array);
|
|
294
|
-
break;
|
|
295
|
-
}
|
|
296
|
-
case ABIDataTypes.UINT64: {
|
|
297
|
-
if (typeof value !== 'bigint') {
|
|
298
|
-
throw new Error(`Expected value to be of type bigint (${name})`);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
writer.writeU64(value as bigint);
|
|
302
|
-
break;
|
|
303
|
-
}
|
|
304
|
-
case ABIDataTypes.ARRAY_OF_ADDRESSES: {
|
|
305
|
-
if (!(value instanceof Array)) {
|
|
306
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
writer.writeAddressArray(value as Address[]);
|
|
310
|
-
break;
|
|
311
|
-
}
|
|
312
|
-
case ABIDataTypes.ARRAY_OF_UINT256: {
|
|
313
|
-
if (!(value instanceof Array)) {
|
|
314
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
writer.writeU256Array(value as bigint[]);
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
case ABIDataTypes.ARRAY_OF_UINT32: {
|
|
321
|
-
if (!(value instanceof Array)) {
|
|
322
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
writer.writeU32Array(value as number[]);
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
case ABIDataTypes.ARRAY_OF_STRING: {
|
|
330
|
-
if (!(value instanceof Array)) {
|
|
331
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
writer.writeStringArray(value as string[]);
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
case ABIDataTypes.ARRAY_OF_BYTES: {
|
|
339
|
-
if (!(value instanceof Array)) {
|
|
340
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
writer.writeBytesArray(value as Uint8Array[]);
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
case ABIDataTypes.ARRAY_OF_UINT64: {
|
|
348
|
-
if (!(value instanceof Array)) {
|
|
349
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
writer.writeU64Array(value as bigint[]);
|
|
353
|
-
break;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
case ABIDataTypes.ARRAY_OF_UINT8: {
|
|
357
|
-
if (!(value instanceof Array)) {
|
|
358
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
writer.writeU8Array(value as number[]);
|
|
362
|
-
break;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
case ABIDataTypes.ARRAY_OF_UINT16: {
|
|
366
|
-
if (!(value instanceof Array)) {
|
|
367
|
-
throw new Error(`Expected value to be of type Array (${name})`);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
writer.writeU16Array(value as number[]);
|
|
371
|
-
break;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
default: {
|
|
375
|
-
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
private decodeOutput(abi: BitcoinAbiValue[], reader: BinaryReader): DecodedOutput {
|
|
381
|
-
const result: Array<DecodedCallResult> = [];
|
|
382
|
-
const obj: ContractDecodedObjectResult = {};
|
|
383
|
-
|
|
384
|
-
for (let i = 0; i < abi.length; i++) {
|
|
385
|
-
const type = abi[i].type;
|
|
386
|
-
const name = abi[i].name;
|
|
387
|
-
|
|
388
|
-
let decodedResult: DecodedCallResult;
|
|
389
|
-
switch (type) {
|
|
390
|
-
case ABIDataTypes.UINT256:
|
|
391
|
-
decodedResult = reader.readU256();
|
|
392
|
-
break;
|
|
393
|
-
case ABIDataTypes.BOOL:
|
|
394
|
-
decodedResult = reader.readBoolean();
|
|
395
|
-
break;
|
|
396
|
-
case ABIDataTypes.STRING:
|
|
397
|
-
decodedResult = reader.readStringWithLength();
|
|
398
|
-
break;
|
|
399
|
-
case ABIDataTypes.ADDRESS:
|
|
400
|
-
decodedResult = reader.readAddress();
|
|
401
|
-
break;
|
|
402
|
-
case ABIDataTypes.TUPLE:
|
|
403
|
-
decodedResult = reader.readTuple();
|
|
404
|
-
break;
|
|
405
|
-
case ABIDataTypes.UINT8:
|
|
406
|
-
decodedResult = reader.readU8();
|
|
407
|
-
break;
|
|
408
|
-
case ABIDataTypes.UINT16:
|
|
409
|
-
decodedResult = reader.readU16();
|
|
410
|
-
break;
|
|
411
|
-
case ABIDataTypes.UINT32:
|
|
412
|
-
decodedResult = reader.readU32();
|
|
413
|
-
break;
|
|
414
|
-
case ABIDataTypes.BYTES32:
|
|
415
|
-
decodedResult = reader.readBytes(32);
|
|
416
|
-
break;
|
|
417
|
-
case ABIDataTypes.ADDRESS_UINT256_TUPLE:
|
|
418
|
-
decodedResult = reader.readAddressValueTuple();
|
|
419
|
-
break;
|
|
420
|
-
case ABIDataTypes.BYTES: {
|
|
421
|
-
decodedResult = reader.readBytesWithLength();
|
|
422
|
-
break;
|
|
423
|
-
}
|
|
424
|
-
case ABIDataTypes.UINT64: {
|
|
425
|
-
decodedResult = reader.readU64();
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
case ABIDataTypes.ARRAY_OF_ADDRESSES: {
|
|
429
|
-
decodedResult = reader.readAddressArray();
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
case ABIDataTypes.ARRAY_OF_UINT256: {
|
|
433
|
-
decodedResult = reader.readU256Array();
|
|
434
|
-
break;
|
|
435
|
-
}
|
|
436
|
-
case ABIDataTypes.ARRAY_OF_UINT32: {
|
|
437
|
-
decodedResult = reader.readU32Array();
|
|
438
|
-
break;
|
|
439
|
-
}
|
|
440
|
-
case ABIDataTypes.ARRAY_OF_STRING: {
|
|
441
|
-
decodedResult = reader.readStringArray();
|
|
442
|
-
break;
|
|
443
|
-
}
|
|
444
|
-
case ABIDataTypes.ARRAY_OF_BYTES: {
|
|
445
|
-
decodedResult = reader.readBytesArray();
|
|
446
|
-
break;
|
|
447
|
-
}
|
|
448
|
-
case ABIDataTypes.ARRAY_OF_UINT64: {
|
|
449
|
-
decodedResult = reader.readU64Array();
|
|
450
|
-
break;
|
|
451
|
-
}
|
|
452
|
-
case ABIDataTypes.ARRAY_OF_UINT8: {
|
|
453
|
-
decodedResult = reader.readU8Array();
|
|
454
|
-
break;
|
|
455
|
-
}
|
|
456
|
-
case ABIDataTypes.ARRAY_OF_UINT16: {
|
|
457
|
-
decodedResult = reader.readU16Array();
|
|
458
|
-
break;
|
|
459
|
-
}
|
|
460
|
-
default:
|
|
461
|
-
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
result.push(decodedResult);
|
|
465
|
-
obj[name] = decodedResult;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
return {
|
|
469
|
-
values: result,
|
|
470
|
-
obj: obj,
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
private callFunction(
|
|
475
|
-
element: FunctionBaseData,
|
|
476
|
-
): (...args: unknown[]) => Promise<BaseContractProperty> {
|
|
477
|
-
return async (...args: unknown[]): Promise<BaseContractProperty> => {
|
|
478
|
-
const data = this.encodeFunctionData(element, args);
|
|
479
|
-
const buffer = Buffer.from(data.getBuffer());
|
|
480
|
-
const response = await this.provider.call(this.address, buffer, this.from);
|
|
481
|
-
|
|
482
|
-
if ('error' in response) {
|
|
483
|
-
return response;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
const decoded: DecodedOutput = element.outputs
|
|
487
|
-
? this.decodeOutput(element.outputs, response.result)
|
|
488
|
-
: { values: [], obj: {} };
|
|
489
|
-
|
|
490
|
-
response.setDecoded(decoded);
|
|
491
|
-
response.setCalldata(buffer);
|
|
492
|
-
|
|
493
|
-
return response;
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Represents the base contract class.
|
|
500
|
-
* @category Contracts
|
|
501
|
-
*/
|
|
502
|
-
export class BaseContract<T extends BaseContractProperties> extends IBaseContract<T> {
|
|
503
|
-
constructor(
|
|
504
|
-
address: BitcoinAddressLike,
|
|
505
|
-
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
506
|
-
provider: AbstractRpcProvider,
|
|
507
|
-
sender?: Address,
|
|
508
|
-
) {
|
|
509
|
-
super(address, abi, provider, sender);
|
|
510
|
-
|
|
511
|
-
return this.proxify();
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Proxifies the contract to allow for type checking.
|
|
516
|
-
* @private
|
|
517
|
-
*/
|
|
518
|
-
private proxify(): T & BaseContract<T> {
|
|
519
|
-
return new Proxy(this, {
|
|
520
|
-
get: (target: BaseContract<T>, prop: typeof internal, receiver) => {
|
|
521
|
-
if (typeof prop === 'symbol' || prop in target) {
|
|
522
|
-
return Reflect.get(target, prop, receiver);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
// Undefined properties should return undefined
|
|
526
|
-
try {
|
|
527
|
-
return this.getFunction(prop);
|
|
528
|
-
} catch (error: unknown) {
|
|
529
|
-
if (!(error instanceof Error)) {
|
|
530
|
-
throw new Error(
|
|
531
|
-
`Something went wrong when trying to get the function: ${error}`,
|
|
532
|
-
);
|
|
533
|
-
} else {
|
|
534
|
-
throw error;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
},
|
|
538
|
-
has: (target, prop) => {
|
|
539
|
-
if (typeof prop === 'symbol' || prop in target) {
|
|
540
|
-
return Reflect.has(target, prop);
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
return target.interface.hasFunction(prop);
|
|
544
|
-
},
|
|
545
|
-
}) as BaseContract<T> & T;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
* Creates a new contract class.
|
|
551
|
-
*/
|
|
552
|
-
function contractBase<T extends BaseContractProperties>(): new (
|
|
553
|
-
address: BitcoinAddressLike,
|
|
554
|
-
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
555
|
-
provider: AbstractRpcProvider,
|
|
556
|
-
sender?: Address,
|
|
557
|
-
) => BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
558
|
-
return BaseContract as new (
|
|
559
|
-
address: BitcoinAddressLike,
|
|
560
|
-
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
561
|
-
provider: AbstractRpcProvider,
|
|
562
|
-
sender?: Address,
|
|
563
|
-
) => BaseContract<T> & Omit<T, keyof BaseContract<T>>;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
/**
|
|
567
|
-
* Creates a new contract instance.
|
|
568
|
-
* @param {BitcoinAddressLike} address The address of the contract.
|
|
569
|
-
* @param {BitcoinInterface | BitcoinInterfaceAbi} abi The ABI of the contract.
|
|
570
|
-
* @param {AbstractRpcProvider} provider The provider for the contract.
|
|
571
|
-
* @param {Address} [sender] Who is sending the transaction.
|
|
572
|
-
* @returns {BaseContract<T> & Omit<T, keyof BaseContract<T>>} The contract instance.
|
|
573
|
-
* @template T The properties of the contract.
|
|
574
|
-
* @category Contracts
|
|
575
|
-
*
|
|
576
|
-
* @example
|
|
577
|
-
* const provider: JSONRpcProvider = new JSONRpcProvider('https://regtest.opnet.org');
|
|
578
|
-
* const contract: IOP_20Contract = getContract<IOP_20Contract>(
|
|
579
|
-
* 'bcrt1qxeyh0pacdtkqmlna9n254fztp3ptadkkfu6efl',
|
|
580
|
-
* OP_20_ABI,
|
|
581
|
-
* provider,
|
|
582
|
-
* );
|
|
583
|
-
*
|
|
584
|
-
* const balanceExample = await contract.balanceOf(
|
|
585
|
-
* 'bcrt1pyrs3eqwnrmd4ql3nwvx66yzp0wc24xd2t9pf8699ln340pjs7f3sar3tum',
|
|
586
|
-
* );
|
|
587
|
-
*
|
|
588
|
-
* if ('error' in balanceExample) throw new Error('Error in fetching balance');
|
|
589
|
-
* console.log('Balance:', balanceExample.decoded);
|
|
590
|
-
*/
|
|
591
|
-
export function getContract<T extends BaseContractProperties>(
|
|
592
|
-
address: BitcoinAddressLike,
|
|
593
|
-
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
594
|
-
provider: AbstractRpcProvider,
|
|
595
|
-
sender?: Address,
|
|
596
|
-
): BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
597
|
-
const base = contractBase<T>();
|
|
598
|
-
|
|
599
|
-
return new base(address, abi, provider, sender);
|
|
600
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ABICoder,
|
|
3
|
+
ABIDataTypes,
|
|
4
|
+
Address,
|
|
5
|
+
BinaryReader,
|
|
6
|
+
BinaryWriter,
|
|
7
|
+
NetEvent,
|
|
8
|
+
} from '@btc-vision/bsi-binary';
|
|
9
|
+
import { BaseContractProperty } from '../abi/BaseContractProperty.js';
|
|
10
|
+
import { BitcoinAbiTypes } from '../abi/BitcoinAbiTypes.js';
|
|
11
|
+
import { BitcoinInterface } from '../abi/BitcoinInterface.js';
|
|
12
|
+
import { BaseContractProperties } from '../abi/interfaces/BaseContractProperties.js';
|
|
13
|
+
import { BitcoinAbiValue } from '../abi/interfaces/BitcoinAbiValue.js';
|
|
14
|
+
import {
|
|
15
|
+
BitcoinInterfaceAbi,
|
|
16
|
+
EventBaseData,
|
|
17
|
+
FunctionBaseData,
|
|
18
|
+
} from '../abi/interfaces/BitcoinInterfaceAbi.js';
|
|
19
|
+
import { BitcoinAddressLike, DecodedCallResult } from '../common/CommonTypes.js';
|
|
20
|
+
import { AbstractRpcProvider } from '../providers/AbstractRpcProvider.js';
|
|
21
|
+
import { ContractEvents } from '../transactions/interfaces/ITransactionReceipt.js';
|
|
22
|
+
import { IContract } from './interfaces/IContract.js';
|
|
23
|
+
import { OPNetEvent } from './OPNetEvent.js';
|
|
24
|
+
|
|
25
|
+
const internal = Symbol.for('_btc_internal');
|
|
26
|
+
const bitcoinAbiCoder = new ABICoder();
|
|
27
|
+
|
|
28
|
+
export type ContractDecodedObjectResult = { [key: string]: DecodedCallResult };
|
|
29
|
+
export type DecodedOutput = { values: Array<DecodedCallResult>; obj: ContractDecodedObjectResult };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Represents the base contract class.
|
|
33
|
+
* @category Contracts
|
|
34
|
+
* @abstract
|
|
35
|
+
*/
|
|
36
|
+
export abstract class IBaseContract<T extends BaseContractProperties> implements IContract {
|
|
37
|
+
/**
|
|
38
|
+
* The address of the contract.
|
|
39
|
+
*/
|
|
40
|
+
public readonly address: BitcoinAddressLike;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The interface of the contract.
|
|
44
|
+
*/
|
|
45
|
+
public readonly interface!: BitcoinInterface;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A generic provider for the contract.
|
|
49
|
+
*/
|
|
50
|
+
public readonly provider: AbstractRpcProvider;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Who is sending the transaction.
|
|
54
|
+
*/
|
|
55
|
+
public from?: Address;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The internal functions of the contract.
|
|
59
|
+
* @protected
|
|
60
|
+
*/
|
|
61
|
+
readonly [internal]: keyof T | undefined;
|
|
62
|
+
|
|
63
|
+
private events: Map<string, EventBaseData> = new Map();
|
|
64
|
+
|
|
65
|
+
protected constructor(
|
|
66
|
+
address: BitcoinAddressLike,
|
|
67
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
68
|
+
provider: AbstractRpcProvider,
|
|
69
|
+
from?: Address,
|
|
70
|
+
) {
|
|
71
|
+
this.address = address;
|
|
72
|
+
this.provider = provider;
|
|
73
|
+
this.interface = BitcoinInterface.from(abi);
|
|
74
|
+
this.from = from;
|
|
75
|
+
|
|
76
|
+
Object.defineProperty(this, internal, { value: {} });
|
|
77
|
+
|
|
78
|
+
this.defineInternalFunctions();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Sets the sender of the transaction.
|
|
83
|
+
* @param {Address} sender The sender of the transaction.
|
|
84
|
+
*/
|
|
85
|
+
public setSender(sender: Address): void {
|
|
86
|
+
this.from = sender;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public decodeEvents(events: NetEvent[] | ContractEvents): OPNetEvent[] {
|
|
90
|
+
const decodedEvents: OPNetEvent[] = [];
|
|
91
|
+
|
|
92
|
+
if (!Array.isArray(events)) {
|
|
93
|
+
events = events[this.address.toString()];
|
|
94
|
+
|
|
95
|
+
if (!events) {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (let event of events) {
|
|
101
|
+
decodedEvents.push(this.decodeEvent(event));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return decodedEvents;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public decodeEvent(event: NetEvent): OPNetEvent {
|
|
108
|
+
const eventData = this.events.get(event.eventType);
|
|
109
|
+
if (!eventData || eventData.values.length === 0) {
|
|
110
|
+
return new OPNetEvent(event.eventType, event.eventDataSelector, event.eventData);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const binaryReader: BinaryReader = new BinaryReader(event.eventData);
|
|
114
|
+
const out: DecodedOutput = this.decodeOutput(eventData.values, binaryReader);
|
|
115
|
+
const decodedEvent = new OPNetEvent(
|
|
116
|
+
event.eventType,
|
|
117
|
+
event.eventDataSelector,
|
|
118
|
+
event.eventData,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
decodedEvent.setDecoded(out);
|
|
122
|
+
|
|
123
|
+
return decodedEvent;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Encodes the calldata for a function.
|
|
128
|
+
* @param {string} functionName The name of the function.
|
|
129
|
+
* @param {unknown[]} args The arguments for the function.
|
|
130
|
+
* @returns {Buffer} The encoded calldata.
|
|
131
|
+
*/
|
|
132
|
+
public encodeCalldata(functionName: string, args: unknown[]): Buffer {
|
|
133
|
+
for (const element of this.interface.abi) {
|
|
134
|
+
if (element.name === functionName) {
|
|
135
|
+
const data = this.encodeFunctionData(element as FunctionBaseData, args);
|
|
136
|
+
return Buffer.from(data.getBuffer());
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
throw new Error(`Function not found: ${functionName}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
protected getFunction(
|
|
144
|
+
name: symbol,
|
|
145
|
+
): BaseContractProperty | undefined | string | number | symbol {
|
|
146
|
+
const key = name as keyof Omit<
|
|
147
|
+
IBaseContract<T>,
|
|
148
|
+
'address' | 'provider' | 'interface' | 'decodeEvents' | 'decodeEvent'
|
|
149
|
+
>;
|
|
150
|
+
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
return this[key];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Defines the internal functions of the contract. These functions are generated for the ABI provided.
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
private defineInternalFunctions(): void {
|
|
160
|
+
for (const element of this.interface.abi) {
|
|
161
|
+
switch (element.type) {
|
|
162
|
+
case BitcoinAbiTypes.Function: {
|
|
163
|
+
// @ts-ignore
|
|
164
|
+
// We will allow overwrites.
|
|
165
|
+
//if (this[element.name]) {
|
|
166
|
+
// throw new Error(`Duplicate function found in the ABI: ${element.name}.`);
|
|
167
|
+
//}
|
|
168
|
+
|
|
169
|
+
if (this[element.name]) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
Object.defineProperty(this, element.name, {
|
|
174
|
+
value: this.callFunction(element as FunctionBaseData).bind(this),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
case BitcoinAbiTypes.Event: {
|
|
180
|
+
if (this.events.has(element.name)) {
|
|
181
|
+
throw new Error(`Duplicate event found in the ABI: ${element.name}.`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
this.events.set(element.name, element);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
default:
|
|
188
|
+
throw new Error(`Unsupported type.`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private encodeFunctionData(element: FunctionBaseData, args: unknown[]): BinaryWriter {
|
|
194
|
+
const writer = new BinaryWriter();
|
|
195
|
+
const selector = Number('0x' + bitcoinAbiCoder.encodeSelector(element.name));
|
|
196
|
+
writer.writeSelector(selector);
|
|
197
|
+
|
|
198
|
+
if (args.length !== (element.inputs?.length ?? 0)) {
|
|
199
|
+
throw new Error('Invalid number of arguments provided');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!element.inputs || (element.inputs && element.inputs.length === 0)) {
|
|
203
|
+
return writer;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < element.inputs.length; i++) {
|
|
207
|
+
this.encodeInput(writer, element.inputs[i], args[i]);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return writer;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private encodeInput(writer: BinaryWriter, abi: BitcoinAbiValue, value: unknown): void {
|
|
214
|
+
const type = abi.type;
|
|
215
|
+
const name = abi.name;
|
|
216
|
+
|
|
217
|
+
switch (type) {
|
|
218
|
+
case ABIDataTypes.UINT256: {
|
|
219
|
+
if (typeof value !== 'bigint') {
|
|
220
|
+
throw new Error(`Expected value to be of type bigint (${name})`);
|
|
221
|
+
}
|
|
222
|
+
writer.writeU256(value as bigint);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case ABIDataTypes.BOOL: {
|
|
226
|
+
if (typeof value !== 'boolean') {
|
|
227
|
+
throw new Error(`Expected value to be of type boolean (${name})`);
|
|
228
|
+
}
|
|
229
|
+
writer.writeBoolean(value as boolean);
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
case ABIDataTypes.STRING: {
|
|
233
|
+
if (typeof value !== 'string') {
|
|
234
|
+
throw new Error(`Expected value to be of type string (${name})`);
|
|
235
|
+
}
|
|
236
|
+
writer.writeStringWithLength(value as string);
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case ABIDataTypes.ADDRESS: {
|
|
240
|
+
const address = value as BitcoinAddressLike;
|
|
241
|
+
writer.writeAddress(address.toString());
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
case ABIDataTypes.TUPLE: {
|
|
245
|
+
if (!(value instanceof Array)) {
|
|
246
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
writer.writeTuple(value as bigint[]);
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
case ABIDataTypes.UINT8: {
|
|
253
|
+
if (typeof value !== 'number') {
|
|
254
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
255
|
+
}
|
|
256
|
+
writer.writeU8(value as number);
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
case ABIDataTypes.UINT16: {
|
|
260
|
+
if (typeof value !== 'number') {
|
|
261
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
262
|
+
}
|
|
263
|
+
writer.writeU16(value as number);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case ABIDataTypes.UINT32: {
|
|
267
|
+
if (typeof value !== 'number') {
|
|
268
|
+
throw new Error(`Expected value to be of type number (${name})`);
|
|
269
|
+
}
|
|
270
|
+
writer.writeU32(value as number);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case ABIDataTypes.BYTES32: {
|
|
274
|
+
if (!(value instanceof Uint8Array)) {
|
|
275
|
+
throw new Error(`Expected value to be of type Uint8Array (${name})`);
|
|
276
|
+
}
|
|
277
|
+
writer.writeBytes(value as Uint8Array);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case ABIDataTypes.ADDRESS_UINT256_TUPLE: {
|
|
281
|
+
if (!(value instanceof Map)) {
|
|
282
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
writer.writeAddressValueTupleMap(value as Map<Address, bigint>);
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
case ABIDataTypes.BYTES: {
|
|
289
|
+
if (!(value instanceof Uint8Array)) {
|
|
290
|
+
throw new Error(`Expected value to be of type Uint8Array (${name})`);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
writer.writeBytesWithLength(value as Uint8Array);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case ABIDataTypes.UINT64: {
|
|
297
|
+
if (typeof value !== 'bigint') {
|
|
298
|
+
throw new Error(`Expected value to be of type bigint (${name})`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
writer.writeU64(value as bigint);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case ABIDataTypes.ARRAY_OF_ADDRESSES: {
|
|
305
|
+
if (!(value instanceof Array)) {
|
|
306
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
writer.writeAddressArray(value as Address[]);
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
case ABIDataTypes.ARRAY_OF_UINT256: {
|
|
313
|
+
if (!(value instanceof Array)) {
|
|
314
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
writer.writeU256Array(value as bigint[]);
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case ABIDataTypes.ARRAY_OF_UINT32: {
|
|
321
|
+
if (!(value instanceof Array)) {
|
|
322
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
writer.writeU32Array(value as number[]);
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
case ABIDataTypes.ARRAY_OF_STRING: {
|
|
330
|
+
if (!(value instanceof Array)) {
|
|
331
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
writer.writeStringArray(value as string[]);
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
case ABIDataTypes.ARRAY_OF_BYTES: {
|
|
339
|
+
if (!(value instanceof Array)) {
|
|
340
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
writer.writeBytesArray(value as Uint8Array[]);
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
case ABIDataTypes.ARRAY_OF_UINT64: {
|
|
348
|
+
if (!(value instanceof Array)) {
|
|
349
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
writer.writeU64Array(value as bigint[]);
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
case ABIDataTypes.ARRAY_OF_UINT8: {
|
|
357
|
+
if (!(value instanceof Array)) {
|
|
358
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
writer.writeU8Array(value as number[]);
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
case ABIDataTypes.ARRAY_OF_UINT16: {
|
|
366
|
+
if (!(value instanceof Array)) {
|
|
367
|
+
throw new Error(`Expected value to be of type Array (${name})`);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
writer.writeU16Array(value as number[]);
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
default: {
|
|
375
|
+
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private decodeOutput(abi: BitcoinAbiValue[], reader: BinaryReader): DecodedOutput {
|
|
381
|
+
const result: Array<DecodedCallResult> = [];
|
|
382
|
+
const obj: ContractDecodedObjectResult = {};
|
|
383
|
+
|
|
384
|
+
for (let i = 0; i < abi.length; i++) {
|
|
385
|
+
const type = abi[i].type;
|
|
386
|
+
const name = abi[i].name;
|
|
387
|
+
|
|
388
|
+
let decodedResult: DecodedCallResult;
|
|
389
|
+
switch (type) {
|
|
390
|
+
case ABIDataTypes.UINT256:
|
|
391
|
+
decodedResult = reader.readU256();
|
|
392
|
+
break;
|
|
393
|
+
case ABIDataTypes.BOOL:
|
|
394
|
+
decodedResult = reader.readBoolean();
|
|
395
|
+
break;
|
|
396
|
+
case ABIDataTypes.STRING:
|
|
397
|
+
decodedResult = reader.readStringWithLength();
|
|
398
|
+
break;
|
|
399
|
+
case ABIDataTypes.ADDRESS:
|
|
400
|
+
decodedResult = reader.readAddress();
|
|
401
|
+
break;
|
|
402
|
+
case ABIDataTypes.TUPLE:
|
|
403
|
+
decodedResult = reader.readTuple();
|
|
404
|
+
break;
|
|
405
|
+
case ABIDataTypes.UINT8:
|
|
406
|
+
decodedResult = reader.readU8();
|
|
407
|
+
break;
|
|
408
|
+
case ABIDataTypes.UINT16:
|
|
409
|
+
decodedResult = reader.readU16();
|
|
410
|
+
break;
|
|
411
|
+
case ABIDataTypes.UINT32:
|
|
412
|
+
decodedResult = reader.readU32();
|
|
413
|
+
break;
|
|
414
|
+
case ABIDataTypes.BYTES32:
|
|
415
|
+
decodedResult = reader.readBytes(32);
|
|
416
|
+
break;
|
|
417
|
+
case ABIDataTypes.ADDRESS_UINT256_TUPLE:
|
|
418
|
+
decodedResult = reader.readAddressValueTuple();
|
|
419
|
+
break;
|
|
420
|
+
case ABIDataTypes.BYTES: {
|
|
421
|
+
decodedResult = reader.readBytesWithLength();
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
case ABIDataTypes.UINT64: {
|
|
425
|
+
decodedResult = reader.readU64();
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case ABIDataTypes.ARRAY_OF_ADDRESSES: {
|
|
429
|
+
decodedResult = reader.readAddressArray();
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
case ABIDataTypes.ARRAY_OF_UINT256: {
|
|
433
|
+
decodedResult = reader.readU256Array();
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
case ABIDataTypes.ARRAY_OF_UINT32: {
|
|
437
|
+
decodedResult = reader.readU32Array();
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
case ABIDataTypes.ARRAY_OF_STRING: {
|
|
441
|
+
decodedResult = reader.readStringArray();
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
case ABIDataTypes.ARRAY_OF_BYTES: {
|
|
445
|
+
decodedResult = reader.readBytesArray();
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
case ABIDataTypes.ARRAY_OF_UINT64: {
|
|
449
|
+
decodedResult = reader.readU64Array();
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
case ABIDataTypes.ARRAY_OF_UINT8: {
|
|
453
|
+
decodedResult = reader.readU8Array();
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
case ABIDataTypes.ARRAY_OF_UINT16: {
|
|
457
|
+
decodedResult = reader.readU16Array();
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
default:
|
|
461
|
+
throw new Error(`Unsupported type: ${type} (${name})`);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
result.push(decodedResult);
|
|
465
|
+
obj[name] = decodedResult;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
values: result,
|
|
470
|
+
obj: obj,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private callFunction(
|
|
475
|
+
element: FunctionBaseData,
|
|
476
|
+
): (...args: unknown[]) => Promise<BaseContractProperty> {
|
|
477
|
+
return async (...args: unknown[]): Promise<BaseContractProperty> => {
|
|
478
|
+
const data = this.encodeFunctionData(element, args);
|
|
479
|
+
const buffer = Buffer.from(data.getBuffer());
|
|
480
|
+
const response = await this.provider.call(this.address, buffer, this.from);
|
|
481
|
+
|
|
482
|
+
if ('error' in response) {
|
|
483
|
+
return response;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const decoded: DecodedOutput = element.outputs
|
|
487
|
+
? this.decodeOutput(element.outputs, response.result)
|
|
488
|
+
: { values: [], obj: {} };
|
|
489
|
+
|
|
490
|
+
response.setDecoded(decoded);
|
|
491
|
+
response.setCalldata(buffer);
|
|
492
|
+
|
|
493
|
+
return response;
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Represents the base contract class.
|
|
500
|
+
* @category Contracts
|
|
501
|
+
*/
|
|
502
|
+
export class BaseContract<T extends BaseContractProperties> extends IBaseContract<T> {
|
|
503
|
+
constructor(
|
|
504
|
+
address: BitcoinAddressLike,
|
|
505
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
506
|
+
provider: AbstractRpcProvider,
|
|
507
|
+
sender?: Address,
|
|
508
|
+
) {
|
|
509
|
+
super(address, abi, provider, sender);
|
|
510
|
+
|
|
511
|
+
return this.proxify();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Proxifies the contract to allow for type checking.
|
|
516
|
+
* @private
|
|
517
|
+
*/
|
|
518
|
+
private proxify(): T & BaseContract<T> {
|
|
519
|
+
return new Proxy(this, {
|
|
520
|
+
get: (target: BaseContract<T>, prop: typeof internal, receiver) => {
|
|
521
|
+
if (typeof prop === 'symbol' || prop in target) {
|
|
522
|
+
return Reflect.get(target, prop, receiver);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Undefined properties should return undefined
|
|
526
|
+
try {
|
|
527
|
+
return this.getFunction(prop);
|
|
528
|
+
} catch (error: unknown) {
|
|
529
|
+
if (!(error instanceof Error)) {
|
|
530
|
+
throw new Error(
|
|
531
|
+
`Something went wrong when trying to get the function: ${error}`,
|
|
532
|
+
);
|
|
533
|
+
} else {
|
|
534
|
+
throw error;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
has: (target, prop) => {
|
|
539
|
+
if (typeof prop === 'symbol' || prop in target) {
|
|
540
|
+
return Reflect.has(target, prop);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return target.interface.hasFunction(prop);
|
|
544
|
+
},
|
|
545
|
+
}) as BaseContract<T> & T;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Creates a new contract class.
|
|
551
|
+
*/
|
|
552
|
+
function contractBase<T extends BaseContractProperties>(): new (
|
|
553
|
+
address: BitcoinAddressLike,
|
|
554
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
555
|
+
provider: AbstractRpcProvider,
|
|
556
|
+
sender?: Address,
|
|
557
|
+
) => BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
558
|
+
return BaseContract as new (
|
|
559
|
+
address: BitcoinAddressLike,
|
|
560
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
561
|
+
provider: AbstractRpcProvider,
|
|
562
|
+
sender?: Address,
|
|
563
|
+
) => BaseContract<T> & Omit<T, keyof BaseContract<T>>;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Creates a new contract instance.
|
|
568
|
+
* @param {BitcoinAddressLike} address The address of the contract.
|
|
569
|
+
* @param {BitcoinInterface | BitcoinInterfaceAbi} abi The ABI of the contract.
|
|
570
|
+
* @param {AbstractRpcProvider} provider The provider for the contract.
|
|
571
|
+
* @param {Address} [sender] Who is sending the transaction.
|
|
572
|
+
* @returns {BaseContract<T> & Omit<T, keyof BaseContract<T>>} The contract instance.
|
|
573
|
+
* @template T The properties of the contract.
|
|
574
|
+
* @category Contracts
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* const provider: JSONRpcProvider = new JSONRpcProvider('https://regtest.opnet.org');
|
|
578
|
+
* const contract: IOP_20Contract = getContract<IOP_20Contract>(
|
|
579
|
+
* 'bcrt1qxeyh0pacdtkqmlna9n254fztp3ptadkkfu6efl',
|
|
580
|
+
* OP_20_ABI,
|
|
581
|
+
* provider,
|
|
582
|
+
* );
|
|
583
|
+
*
|
|
584
|
+
* const balanceExample = await contract.balanceOf(
|
|
585
|
+
* 'bcrt1pyrs3eqwnrmd4ql3nwvx66yzp0wc24xd2t9pf8699ln340pjs7f3sar3tum',
|
|
586
|
+
* );
|
|
587
|
+
*
|
|
588
|
+
* if ('error' in balanceExample) throw new Error('Error in fetching balance');
|
|
589
|
+
* console.log('Balance:', balanceExample.decoded);
|
|
590
|
+
*/
|
|
591
|
+
export function getContract<T extends BaseContractProperties>(
|
|
592
|
+
address: BitcoinAddressLike,
|
|
593
|
+
abi: BitcoinInterface | BitcoinInterfaceAbi,
|
|
594
|
+
provider: AbstractRpcProvider,
|
|
595
|
+
sender?: Address,
|
|
596
|
+
): BaseContract<T> & Omit<T, keyof BaseContract<T>> {
|
|
597
|
+
const base = contractBase<T>();
|
|
598
|
+
|
|
599
|
+
return new base(address, abi, provider, sender);
|
|
600
|
+
}
|