-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.fs
105 lines (87 loc) · 4.01 KB
/
Helpers.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace Web3.fs
[<AutoOpen>]
module Helpers =
open Logger
///
/// Convenience function that returns a ContractConstants that contains the
/// address used for the session, along with other values ready for
/// manipulation via the `with` statement for modifying records. If the RPC
/// is a wallet, these defaults should work perfectly well. If the RPC is an
/// actual Ethereum node, the gas values and transaction type should be
/// changed as required.
///
let public createDefaultConstants (address: string) =
{
walletAddress = address |> EthAddress
transactionType = None
maxFeePerGas = None
maxPriorityFeePerGas = None
arguments = None
blockHeight = Some LATEST
defaultValue = Some "0"
}
///
/// Convenience function that creates all of the necessary parts of a
/// functioning web3 environment. The record contains the rpc connection, a
/// transaction monitor, contract constants, a Keccak digester, and the
/// initialized logger instance.
///
let public createWeb3Environment url version address =
let rpc = createWeb3Connection url version
{connection = rpc
monitor = createReceiptMonitor rpc
constants = createDefaultConstants address
log = startLogger() |> log }
///
/// Sometimes crafting or exposing bytestrings can be used programmatically,
/// such as to create calldata for use in certain functions.
///
let public returnEVMBytestring evmDatatypes =
match createInputByteString evmDatatypes with
| Ok resultValue -> resultValue
| Error e -> $"{e}"
///
/// Because of the potential for errors during contract loading,
/// `loadDeployedContract` returns a Result type. The Result is bound into
/// a singleton list in the success case. This function returns the
/// DeployedContract on its own, ready for use. However, its use of
/// `List.head` means that it will crash the library if you use it when the
/// contract won't parse properly or have some other Web3Error.
///
let public optimisticallyBindDeployedContract a =
a |> bindDeployedContract |> List.head
///
/// Combines the preparation and deployment of a contract. Automatically
/// calls Log on environment logger. Mostly for convenience.
///
let public prepareAndDeployContract bytecode abi chainId (constructorArguments: EVMDatatype list) value env =
prepareUndeployedContract bytecode abi chainId constructorArguments
|> Result.bind(fun contract -> deployContract contract value env )
|> env.log Log
|> fun _ -> ()
///
/// Combines the preparation, deployment, and loading steps of contract
/// interaction. Mostly for convenience.
///
let public prepareDeployAndLoadContract bytecode abi chainId (constructorArguments: EVMDatatype list) value env =
prepareUndeployedContract bytecode abi chainId constructorArguments
|> Result.bind(fun contract -> deployContract contract value env)
|> Result.bind(fun res ->
match res with
| TransactionReceiptResult transactionReceipt ->
loadDeployedContract abi chainId transactionReceipt.contractAddress.Value
| _ -> "Result of `deployEthContract` wasn't of the expected type" |> GenericPipelineError |> Error )
///
/// Public accessor for a bind function to get the simple string out of
/// contract function wrapper type. Use with
/// <contract>.functions.canonicalInputs
///
let public unwrapFunctionInputs evmInputs =
bindEVMFunctionInputs evmInputs
///
/// Public accessor for a bind function to get the simple string out of a
/// contract function wrapper type. Use with
/// <contract>.functions.canonicalOutputs
///
let public unwrapFunctionOutputs evmOutputs =
bindEVMFunctionOutputs evmOutputs