Skip to content

Commit 8c2985a

Browse files
feat(docs): add smart account guides for Viem (#1159)
2 parents 831cd28 + 1d7c4ca commit 8c2985a

35 files changed

+1153
-30
lines changed

docs/smart-accounts/1-overview.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The payment receipt is a `bytes32` value.
4444
The first byte is reserved for the instruction code.
4545
The second byte is a wallet identifier;
4646
this is a number assigned to wallet providers by the Flare Foundation, and should otherwise be `0`.
47-
The remaining 31 bytes are the parameters for the chosen instruction.
47+
The remaining 30 bytes are the parameters for the chosen instruction.
4848

4949
In practice, the payment receipt should be prepared by a backend, through a web form.
5050

docs/smart-accounts/3-custom-instruction.mdx

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
sidebar_position: 1
33
slug: custom-instruction
4-
title: Custom instruction
5-
authors: [nikerzetic, filipkoprivec]
4+
title: Custom Instruction
5+
authors: [nikerzetic]
66
description: Performing custom function calls in the Flare Smart Accounts.
77
tags: [intermediate, ethereum, flare-smart-accounts]
88
keywords:
@@ -14,7 +14,6 @@ keywords:
1414
flare-network,
1515
account-abstraction,
1616
]
17-
unlisted: true
1817
---
1918

2019
import ThemedImage from "@theme/ThemedImage";
@@ -24,8 +23,9 @@ Flare Smart Accounts allow users to execute custom function calls on the Flare c
2423
The process expands on the workflow for other actions by including an additional step at the beginning.
2524

2625
In order for the `MasterAccountController` contract to be able to give a custom instruction to a personal account, the custom action must first be registered with the said contract.
27-
The custom instruction is stored in a mapper, with its 31-byte hash as a key.
28-
That hash is then sent as the payment reference, along with the byte representation of the number 99 in the first byte.
26+
The custom instruction is stored in a mapper, with its 30-byte hash as a key.
27+
That hash is then sent as the payment reference, along with the byte representation of the hexadecimal number `ff` (decimal `255`) in the first byte and the `walletId` in the second byte.
28+
The `walletId` is a Flare-designated value, used by the operator for wallet identification.
2929

3030
<div
3131
style={{
@@ -39,8 +39,12 @@ That hash is then sent as the payment reference, along with the byte representat
3939
alt="Breakdown of bytes in payment reference for the custom action"
4040
style={{ width: "100%" }}
4141
sources={{
42-
light: useBaseUrl("/img/docs/smart-accounts/custom_light.svg"),
43-
dark: useBaseUrl("/img/docs/smart-accounts/custom_dark.svg"),
42+
light: useBaseUrl(
43+
"/img/docs/smart-accounts/bytes-custom-instruction-light.png",
44+
),
45+
dark: useBaseUrl(
46+
"/img/docs/smart-accounts/bytes-custom-instruction-dark.png",
47+
),
4448
}}
4549
/>
4650
</div>
@@ -57,17 +61,16 @@ We expand the workflow described in the [Flare Smart Accounts overview](/smart-a
5761

5862
## Custom Instructions
5963

60-
Custom instructions are an array of the `IMasterAccountController.CustomInstruction` Solidity struct.
64+
Custom instructions are an array of the `CustomInstructions.CustomCall` Solidity struct.
6165
The struct contains three fields:
6266

63-
- **targetContract**: the address of the smart contract that will execute the custom function
64-
- **value**: the amount of FLR paid to the contract
65-
- **data**: transaction calldata, which includes a function selector and values of the function's arguments
67+
- `targetContract`: the address of the smart contract that will execute the custom function
68+
- `value`: the amount of FLR paid to the contract
69+
- `data`: transaction calldata, which includes a function selector and values of the function's arguments
6670

6771
Each of the custom instructions in the array will be performed in order.
6872
A call to the `targetContract` address is made, with the specified `value` and the calldata `data`.
6973

70-
{/* TODO:(Nik) explain how calldata is produced */}
7174
In Solidity, we can obtain the calldata by doing the following:
7275

7376
```Solidity
@@ -87,26 +90,45 @@ It is recommended to use established libraries, or an [online tool](https://abi.
8790

8891
## Call hash
8992

90-
To produce the custom instructions calldata, we first ABI encode the array of the `IMasterAccountController.CustomInstruction` struct.
91-
We then take the `keccak256` hash of that value, and drop the last byte.
93+
To produce the custom instructions calldata, we first ABI encode the array of the `CustomInstructions.CustomCall` struct.
94+
We then take the `keccak256` hash of that value, and drop the first two bytes (`(1 << 240) - 1` shifts the number binary number `1` left `30*8` times, and replaces it with `0` and all the `0`s that follow it with `1`; essentially, we create a mask of length `30*8` of only `1`s).
9295
That is the call hash that is provided as the payment reference for the custom action, and the ID under which the custom instructions are stored in the `MasterAccountController` contract.
9396

9497
```Solidity
95-
uint256(keccak256(abi.encode(_customInstruction))) >> 8;
98+
return bytes32(uint256(keccak256(abi.encode(_customInstruction))) & ((1 << 240) - 1));
9699
```
97100

98101
The call hash can also be obtained through the `encodeCustomInstruction` helper function of the `MasterAccountController` contract.
99102

100103
```Solidity
101104
function encodeCustomInstruction(
102-
CustomInstruction[] memory _customInstruction
103-
) public pure returns (uint256) {
104-
return uint256(keccak256(abi.encode(_customInstruction))) >> 8;
105+
CustomInstructions.CustomCall[] calldata _customInstruction
106+
) public pure returns (bytes32) {
107+
return CustomInstructions.encodeCustomInstruction(_customInstruction);
105108
}
106109
```
107110

111+
Behind the scenes, the `MasterAccountController` contract calls the `encodeCustomInstruction` function of the `CustomInstructions` library.
112+
113+
```Solidity
114+
function encodeCustomInstruction(
115+
CustomCall[] calldata _customInstruction
116+
)
117+
internal pure
118+
returns (bytes32)
119+
{
120+
return bytes32(uint256(keccak256(abi.encode(_customInstruction))) & ((1 << 240) - 1));
121+
}
122+
123+
```
124+
108125
## 0. Register custom instructions
109126

110127
We register a custom instruction by calling the `registerCustomInstruction` function on the `MasterAccountController` contract.
111-
The `IMasterAccountController.CustomInstruction` array is provided as an argument.
128+
The `CustomInstructions.CustomCall` array is provided as an argument.
112129
It is encoded as described above and stored in a `CustomInstructions` mapping.
130+
131+
To obtain the instruction that can be sent as the memo of an XRPL Payment transaction, we take the call hash produced by the `encodeCustomInstruction` function and modify it the following way.
132+
First, we remove the initial two bytes from this hash.
133+
Next, we prepend the hexadecimal value `ff` followed by the `walletId`.
134+
This is the encoded custom instruction.

docs/smart-accounts/guides/01-fsa-cli.mdx renamed to docs/smart-accounts/guides/cli/01-introduction.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
sidebar_position: 1
3-
slug: fsa-cli
4-
title: Flare Smart Accounts CLI
3+
slug: introduction
4+
title: Introduction
55
authors: [nikerzetic]
66
description: Introduction to the Flare Smart Accounts CLI.
77
tags: [intermediate, ethereum, flare-smart-accounts]
@@ -90,7 +90,7 @@ If the instruction includes a minting step (`fxrp-cr`, `firelight-cr-deposit` an
9090
<encode_command> | ./smart_accounts.py bridge instruction - | ./smart_accounts.py bridge mint-tx --wait -
9191
```
9292

93-
This is described in more detail in the [Bridge section](/smart-accounts/guides/fsa-cli#bridge) of this guide.
93+
This is described in more detail in the [Bridge section](/smart-accounts/guides/cli/introduction#bridge) of this guide.
9494

9595
## Encode
9696

docs/smart-accounts/guides/03-fassets-cycle.mdx renamed to docs/smart-accounts/guides/cli/02-fassets-cycle.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
sidebar_position: 1
33
slug: fassets-cycle
44
title: FAssets Cycle
5-
authors: [nikerzetic, filipkoprivec]
5+
authors: [nikerzetic]
66
description: An overview of the FAssets cycle with the use of Flare Smart Accounts.
77
tags: [intermediate, ethereum, flare-smart-accounts]
88
keywords:
@@ -29,7 +29,7 @@ The steps we will take will be as follows:
2929
3. **claim:** finish the withdrawal process of FXRP from the Upshift-type vault
3030
4. **redeem:** convert FXRP back to XRP
3131

32-
We will do all of that through the [Flare Smart Accounts CLI](/smart-accounts/guides/fsa-cli).
32+
We will do all of that through the [Flare Smart Accounts CLI](/smart-accounts/guides/cli/introduction).
3333
The CLI allows us to make XRPL transactions through terminal commands.
3434

3535
:::note

docs/smart-accounts/guides/05-mint-and-transfer.mdx renamed to docs/smart-accounts/guides/cli/04-mint-and-transfer.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
sidebar_position: 1
33
slug: mint-and-transfer
4-
title: Mint to any address
4+
title: Mint to any Address
55
authors: [nikerzetic]
66
description: Mint FXRP and transfer it to a Flare address using the Flare Smart Accounts CLI.
77
tags: [quickstart, ethereum, flare-smart-accounts]
@@ -17,7 +17,7 @@ keywords:
1717
unlisted: false
1818
---
1919

20-
In this guide we will take a look at how we can use the [Flare smart accounts CLI](/smart-accounts/guides/fsa-cli) to perform a mint action, followed by the transfer of freshly minted FXRP.
20+
In this guide we will take a look at how we can use the [Flare smart accounts CLI](/smart-accounts/guides/cli/introduction) to perform a mint action, followed by the transfer of freshly minted FXRP.
2121
What this allows us to do is to, effectively, **mint FXRP onto someone else's Flare address**.
2222
This guide is aimed primarily at Web3 developers who desire to engage with the FAssets system but want to sidestep the base minting process.
2323

docs/smart-accounts/guides/02-custom-instructions.mdx renamed to docs/smart-accounts/guides/other/01-custom-instruction-mocking.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
sidebar_position: 1
33
slug: custom-instruction
4-
title: Custom instructions
5-
authors: [nikerzetic, filipkoprivec]
4+
title: Custom Instructions
5+
authors: [nikerzetic]
66
description: Developing a custom instruction for the Flare Smart Accounts.
77
tags: [intermediate, ethereum, flare-smart-accounts]
88
keywords:

0 commit comments

Comments
 (0)