or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

eip-712.mderror-handling.mdevent-processing.mdfunction-operations.mdindex.mdparameter-processing.md

function-operations.mddocs/

0

# Function Operations

1

2

Core functionality for encoding function calls and decoding function return values. Essential for smart contract interactions and transaction building.

3

4

## Capabilities

5

6

### Encode Function Signature

7

8

Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 hash of the function signature.

9

10

```typescript { .api }

11

/**

12

* Encodes the function name to its ABI representation

13

* @param functionName - The function name to encode or the JSON interface object of the function

14

* @returns The ABI signature of the function (4 bytes)

15

*/

16

function encodeFunctionSignature(functionName: string | AbiFunctionFragment): string;

17

```

18

19

**Usage Examples:**

20

21

```typescript

22

import { encodeFunctionSignature } from "web3-eth-abi";

23

24

// Using function name string

25

const signature = encodeFunctionSignature('myMethod(uint256,string)');

26

console.log(signature); // 0x24ee0097

27

28

// Using JSON interface object

29

const signature2 = encodeFunctionSignature({

30

name: "myMethod",

31

type: "function",

32

inputs: [

33

{ type: "uint256", name: "myNumber" },

34

{ type: "string", name: "myString" }

35

]

36

});

37

console.log(signature2); // 0x24ee0097

38

```

39

40

### Encode Function Call

41

42

Encodes a function call using its JSON interface object and given parameters. Returns the complete encoded function call including signature and parameters.

43

44

```typescript { .api }

45

/**

46

* Encodes a function call using its JSON interface object and given parameters

47

* @param jsonInterface - The JSON interface object of the function

48

* @param params - The parameters to encode

49

* @returns The ABI encoded function call (signature + parameters)

50

*/

51

function encodeFunctionCall(

52

jsonInterface: AbiFunctionFragment,

53

params: unknown[]

54

): string;

55

```

56

57

**Usage Examples:**

58

59

```typescript

60

import { encodeFunctionCall } from "web3-eth-abi";

61

62

// Encode a function call with parameters

63

const encodedCall = encodeFunctionCall(

64

{

65

name: "myMethod",

66

type: "function",

67

inputs: [

68

{ type: "uint256", name: "myNumber" },

69

{ type: "string", name: "myString" }

70

]

71

},

72

["2345675643", "Hello!%"]

73

);

74

console.log(encodedCall);

75

// 0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000

76

77

// Encode balanceOf function call

78

const balanceCall = encodeFunctionCall(

79

{

80

inputs: [{ name: "account", type: "address" }],

81

name: "balanceOf",

82

outputs: [{ name: "", type: "uint256" }],

83

stateMutability: "view",

84

type: "function"

85

},

86

["0x1234567890123456789012345678901234567890"]

87

);

88

console.log(balanceCall);

89

// 0x70a082310000000000000000000000001234567890123456789012345678901234567890

90

```

91

92

### Decode Function Call

93

94

Decodes a function call data using its JSON interface object. Extracts the parameters from encoded function call data.

95

96

```typescript { .api }

97

/**

98

* Decodes a function call data using its JSON interface object

99

* @param functionsAbi - The JSON interface object of the function

100

* @param data - The data to decode

101

* @param methodSignatureProvided - If false, do not remove the first 4 bytes

102

* @returns The decoded parameters with method signature

103

*/

104

function decodeFunctionCall(

105

functionsAbi: AbiFunctionFragment | AbiConstructorFragment,

106

data: HexString,

107

methodSignatureProvided?: boolean

108

): DecodedParams & { __method__: string };

109

```

110

111

**Usage Examples:**

112

113

```typescript

114

import { decodeFunctionCall } from "web3-eth-abi";

115

116

const data = '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000';

117

118

const decodedParams = decodeFunctionCall(

119

{

120

inputs: [

121

{ internalType: 'string', name: '_greeting', type: 'string' },

122

{ internalType: 'string', name: '_second_greeting', type: 'string' }

123

],

124

name: 'setGreeting',

125

outputs: [

126

{ internalType: 'bool', name: '', type: 'bool' },

127

{ internalType: 'string', name: '', type: 'string' }

128

],

129

stateMutability: 'nonpayable',

130

type: 'function'

131

},

132

data

133

);

134

135

console.log(decodedParams);

136

// {

137

// '0': 'Hello',

138

// '1': 'Another Greeting',

139

// __length__: 2,

140

// __method__: 'setGreeting(string,string)',

141

// _greeting: 'Hello',

142

// _second_greeting: 'Another Greeting'

143

// }

144

```

145

146

### Decode Function Return

147

148

Decodes function return values using the function's JSON interface object. Handles both single and multiple return values.

149

150

```typescript { .api }

151

/**

152

* Decodes function return values using its JSON interface object

153

* @param functionsAbi - The JSON interface object of the function

154

* @param returnValues - The function return data to decode

155

* @returns The decoded return values (single value or object for multiple)

156

*/

157

function decodeFunctionReturn(

158

functionsAbi: AbiFunctionFragment,

159

returnValues?: HexString

160

): unknown;

161

```

162

163

**Usage Examples:**

164

165

```typescript

166

import { decodeFunctionReturn } from "web3-eth-abi";

167

168

// Decode multi-value return

169

const data1 = '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';

170

171

const decodedMulti = decodeFunctionReturn(

172

{

173

inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }],

174

name: 'setGreeting',

175

outputs: [

176

{ internalType: 'string', name: '', type: 'string' },

177

{ internalType: 'bool', name: '', type: 'bool' }

178

],

179

stateMutability: 'nonpayable',

180

type: 'function'

181

},

182

data1

183

);

184

console.log(decodedMulti); // { '0': 'Hello', '1': true, __length__: 2 }

185

186

// Decode single-value return

187

const data2 = '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';

188

189

const decodedSingle = decodeFunctionReturn(

190

{

191

inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }],

192

name: 'setGreeting',

193

outputs: [{ internalType: 'string', name: '', type: 'string' }],

194

stateMutability: 'nonpayable',

195

type: 'function'

196

},

197

data2

198

);

199

console.log(decodedSingle); // 'Hello'

200

```

201

202

## Types

203

204

```typescript { .api }

205

interface AbiFunctionFragment {

206

type: "function";

207

name?: string;

208

inputs?: AbiInput[];

209

outputs?: AbiOutput[];

210

stateMutability?: "pure" | "view" | "nonpayable" | "payable";

211

}

212

213

interface AbiConstructorFragment {

214

type: "constructor";

215

inputs?: AbiInput[];

216

stateMutability?: "nonpayable" | "payable";

217

}

218

219

interface AbiInput {

220

name?: string;

221

type: string;

222

components?: AbiInput[];

223

}

224

225

interface AbiOutput {

226

name?: string;

227

type: string;

228

components?: AbiOutput[];

229

}

230

231

interface DecodedParams {

232

[key: string]: unknown;

233

__length__: number;

234

}

235

236

type HexString = string;

237

```