Controlling a NEAR account
This example is of a simple subscription service
that allows a user to subscribe to an arbitrary service and allows the contract to charge them 5 NEAR tokens every month; sort of like a standing order. For most chains an account has a single key, the power of NEARs account model combined with chain signatures is that you can add an MPC controlled key
to your account allowing a smart contract to control your account through code and limited actions (including ones that require a full access key to sign). You can also derive and use new implicit NEAR accounts via the MPC contract as you do with other chains but this use case is more interesting.
This concept also enables:
- Account recovery: allow a contract to add a new private key to your account after preset conditions are met.
- Trial accounts: Keypom uses this concept to create trial accounts that can only perform a limited number of actions and can be upgraded to a full account upon the completion of specified actions.
- DCA service: a contract that allows a DEX to buy a token for a user every fixed period with a pre-defined amount of $USDC.
- and more...
These were all previously possible - before chain signatures - since a NEAR account is also a smart contract, but this required the user to consume a lot of $NEAR in storage costs to upload the contract to the account and it lacked flexibility. This approach is much more scalable and new account services can be switched in and out easily.
Since a NEAR account is also a multichain account, any dervied foreign accounts associated with the NEAR account also inherit these account services.
Running the example​
This example has contracts written in Rust and scripts to interact with the contract in NodeJS.
Go ahead and clone the repository to get started:
# Clone the repository
git clone https://github.com/PiVortex/subscription-example.git
# Navigate to the scripts directory
cd subscription-example/scripts
# Install the dependencies
npm install
To interact with the contract you will need three different accounts. A subscriber, an admin, and an account to hold the contract. Run the following command to create the accounts and deploy the contract:
npm run setup
To subscribe to the service run the following command:
npm run subscribe
To charge the subscriber from the admin account run the command:
npm run charge
To unsubscribe from the service run the command:
npm run unsubscribe
In the next section we'll look at the contract code and walk through each part relevant to controlling a NEAR account with chain signatures.