|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +slug: custom-instruction |
| 4 | +title: Custom instructions |
| 5 | +authors: [nikerzetic, filipkoprivec] |
| 6 | +description: Developing a custom instruction for the Flare Smart Accounts. |
| 7 | +tags: [intermediate, ethereum, flare-smart-accounts] |
| 8 | +keywords: |
| 9 | + [ |
| 10 | + flare-fdc, |
| 11 | + ethereum, |
| 12 | + flare-smart-accounts, |
| 13 | + evm, |
| 14 | + flare-network, |
| 15 | + account-abstraction, |
| 16 | + ] |
| 17 | +unlisted: false |
| 18 | +--- |
| 19 | + |
| 20 | +{/* NOTE:(Nik) This article has been placed right after the introduction into Flare Smart Accounts instead of at the end, so that readers don't get bored of reading before they reach it. */} |
| 21 | + |
| 22 | +import ThemedImage from "@theme/ThemedImage"; |
| 23 | +import useBaseUrl from "@docusaurus/useBaseUrl"; |
| 24 | + |
| 25 | +The Flare Smart Accounts allow XRPL users to make custom function calls on Flare through instructions sent on XRPL. |
| 26 | +In this guide we will look at how the custom instructions can be developed using a mock version of the `MasterAccountController` contract. |
| 27 | + |
| 28 | +In a typical workflow the users sends instructions as memo data on XRPL. |
| 29 | +Those then have to be verified by the FDC on the Flare chain. |
| 30 | +That process requires waiting, which is less than ideal in a development environment. |
| 31 | + |
| 32 | +For that reason, a mock version of the `MasterAccountController` contract has been deployed. |
| 33 | +It implements two additional functions: |
| 34 | + |
| 35 | +- `createFundPersonalAccount` |
| 36 | +- `executeCustomInstructionDevelopment` |
| 37 | + |
| 38 | +The `createFundPersonalAccount` function creates a new `PersonalAccount` for the user. |
| 39 | +It accepts as its argument a string `_xrplAddress`, which represents and address on XRPL. |
| 40 | +The string is then concatenated with the `msg.sender` value; |
| 41 | +the `PersonalAccount` is created for this address. |
| 42 | + |
| 43 | +Thus, a developer can create multiple XRPL "addresses". |
| 44 | +This allows them to more easily test the interactions between different personal accounts. |
| 45 | +The "address" is combined with their Flare address, so that they can give meaningful names to their "addresses" without a danger of multiple developers' addresses crashing. |
| 46 | + |
| 47 | +The `createFundPersonalAccount` function is a payable function. |
| 48 | +And funds sent with the transaction are deposited to the newly created personal account. |
| 49 | +That way, the personal account can interact with payable functions from the get-go. |
| 50 | + |
| 51 | +The `executeCustomInstructionDevelopment` function sidesteps the XRP transaction and the FDC `Payment` verification process. |
| 52 | +It allows developers to send an array of custom instructions to the `MasterAccountController` contract directly. |
| 53 | + |
| 54 | +The two parameters this function takes are the `_xrplAddress` string, and an array of `IMasterAccountController.CustomInstruction` structs. |
| 55 | +It first concatenates the input string with the `msg.sender` value, the same way the `createFundPersonalAccount` function does. |
| 56 | +Then, it retrieves the `PersonalAccount` representing that address, and calls its `custom` function with the array of custom instructions it received as input. |
| 57 | + |
| 58 | +{/* TODO:(Nik) Explain how we can do these two things through the CLI. */} |
| 59 | + |
| 60 | +## A simple example |
| 61 | + |
| 62 | +We will now use the Flare Smart Accounts CLI to interact with a simple contract. |
| 63 | +The contract `Foo` has a single payable function `bar`. |
| 64 | +The `bar` function accepts a `uint256` value as input, and add the fee paid with the transaction to a mapping. |
| 65 | + |
| 66 | +```Solidity |
| 67 | +// SPDX-License-Identifier: MIT |
| 68 | +pragma solidity ^0.8.25; |
| 69 | +
|
| 70 | +contract Foo { |
| 71 | + mapping(uint256 => uint256) public map; |
| 72 | +
|
| 73 | + function bar(uint256 a) public payable { |
| 74 | + map[a] = map[a] + msg.value; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +We want to send `1 FLR` to the contract, and save it under number `42`. |
| 80 | +The address of the `Foo` contract is `0x296432C15504Ed465fAce11E54Ce4aac50cCd8A3`. |
| 81 | +Using an online ABI-encoding tool, we get that the following hash for the `bar` function, with `42` as input: `0x0423a132000000000000000000000000000000000000000000000000000000000000002a`. |
| 82 | + |
| 83 | +There are two ways we can go about developing the custom instructions. |
| 84 | +We will start with an approach, which is what the production code would take. |
| 85 | +Afterwards, we will use the the mock functions to speed up the development. |
| 86 | + |
| 87 | +### Normal approach |
| 88 | + |
| 89 | +Before we can execute the above instructions we need to top up the smart account that will perform the function call. |
| 90 | +We run the following command, which fails because our account lacks funds on Flare. |
| 91 | +It is necessary to send some instructions, because that is what creates an account for us in the first place. |
| 92 | + |
| 93 | +```sh |
| 94 | +./smart_accounts.py bridge custom -a "0x296432C15504Ed465fAce11E54Ce4aac50cCd8A3" -v 1 -d "0423a132000000000000000000000000000000000000000000000000000000000000002a" |
| 95 | +``` |
| 96 | + |
| 97 | +We then need to retrieve the smart account address. |
| 98 | + |
| 99 | +```sh |
| 100 | +./smart_accounts personal-account --from-env print |
| 101 | +``` |
| 102 | + |
| 103 | +With the address, we can go to the [Flare faucet](https://faucet.flare.network/coston2) and request C2FLR for the smart account address. |
| 104 | +We can also do this through the CLI. |
| 105 | + |
| 106 | +```sh |
| 107 | +./smart_accounts personal-account --from-env faucet |
| 108 | +``` |
| 109 | + |
| 110 | +Afterwards, we can run the following command again. |
| 111 | +This time, it works. |
| 112 | + |
| 113 | +```sh |
| 114 | +./smart_accounts.py bridge custom -a "0x296432C15504Ed465fAce11E54Ce4aac50cCd8A3" -v 1 -d "0423a132000000000000000000000000000000000000000000000000000000000000002a" |
| 115 | +``` |
| 116 | + |
| 117 | +### Mocking |
| 118 | + |
| 119 | +We can speed up the process, as well as simplify it, by using the Flare Smart Accounts CLI. |
| 120 | +First, we need to create a mock account, which we do with the command. |
| 121 | +This will only work if our Flare address has sufficient funds. |
| 122 | + |
| 123 | +```sh |
| 124 | +./smart_accounts mock-create-fund --seed "mockAccount" --value 1 |
| 125 | +``` |
| 126 | + |
| 127 | +Here we arbitrarily chose the name `mockAccount` as the account address. |
| 128 | +Behind the scenes, the string `mockAccount` will be concatenated with our Flare address. |
| 129 | + |
| 130 | +Then, we execute the custom instructions. |
| 131 | +We use the string `mockAccount` as the seed. |
| 132 | + |
| 133 | +```sh |
| 134 | +./smart_accounts.py debug mock-custom -s "mockAccount" -a "0x296432C15504Ed465fAce11E54Ce4aac50cCd8A3" -v 1 -d "0423a132000000000000000000000000000000000000000000000000000000000000002a" |
| 135 | +``` |
0 commit comments