Skip to main content

Buy Fan Tokens

In this tutorial, you will learn step-by-step on how to buy any Fan Tokens on the Moxie protocol through the bonding curve contract.

While the tutorial shown here will be using viem in TypeScript, you should be able to use other programming language and their web3-equivalent libraries to execute the Fan Token buying process. Alternatives web3 libraries you can use are:

Starting 22nd November 2024, any EOA wallet or contracts on Base can buy Fan Tokens without needing to hold any Moxie Pass.

Pre-requisites

  1. Install dependencies
npm install viem
  1. Download the Moxie Token and Moxie Bonding Curve ABI.

  2. Make sure to have Moxie token in your wallet and sufficient Base ETH to cover the transaction fees.

Step 1: Setup Client

First, setup a viem client and provide the mnemonic of your account to create an account instance:

Keep Your Mnemonic Secure

Ensure to store the mnemonic securley and privately as an environment variable so that your wallet address will not be compromised.

client.ts
import { createWalletClient, createPublicClient, http } from "viem";
import { mnemonicToAccount } from "viem/accounts";
import { base } from "viem/chains";

const account = mnemonicToAccount("YOUR_WALLET_MNEMONIC");

const client = createWalletClient({
account,
chain: base,
transport: http(),
});

export const publicClient = createPublicClient({
chain: base,
transport: http(),
});

Step 2: Check Moxie Token Allowance

To check the Moxie Token Allowance, simply call the allowance function from Moxie token contract.

When calling the allowance function set the parameters as follows:

ParameterValue
ownerEither the user's EOA wallet or vesting contract address.
spenderThe Moxie bonding curve contract address

Step 3: Calculate Buy Price For Fan Tokens

To calculate the buy price, call the Moxie bonding curve smart contract calculateTokensForBuy with the following parameters:

ParameterValue
_subjectTokenAmounthow much FT user wanna buy (in wei)
_subjectThe subject address associated to the Fan tokens.

Step 4: Approve Moxie Tokens

If the Moxie token allowance is either 0 or less than what is needed to buy the inputted amount of Fan Tokens (from Step 2), then approval on the Moxie Token will need to be initiated to ensure that the buy transaction is successful.

Otherwise, if there is sufficient allowance value, skip to Step 5 to execute the buy Fan Tokens transaction.

EOA Wallet

From EOA wallet, simply call the approve function on the Moxie token with the following variables:

ParameterDescription
spenderthe Moxie bonding curve address
valuethe amount of Moxie to buy FT

Vesting Contract

From vesting contract there is the approveProtocol function that you can call to approve all Moxie token in your vesting contract to be accessible by other Moxie protocol contracts.

After the approval, all the Moxie Token in the vesting contract will be available for Fan Token purchase.

Step 5: Execute Contract Call

Lastly, call the buySharesV2 function on the Moxie bonding curve contract to execute the Fan Token purchase transaction:

index.ts
import client from "./client";
import { MoxieBondingCurveABI } from "./abi/ProtocolContracts#MoxieBondingCurve.json";

try {
client.writeContract({
account,
address: "0x373065e66B32a1C428aa14698dFa99BA7199B55E",
abi: MoxieBondingCurveABI,
functionName: "buySharesV2",
args: [
// subject address
"0x338fDD513Ed2eC7ee1249Ee286F967FC56492C78",
// amount of Moxie in wei to purchase the FT, calculated from Step 2
BigInt("263749847732490543"),
// (minimum) amount of FT to buy in wei
BigInt("10000000000000000"),
// referrer address
"0xadd746be46ff36f10c81d6e3ba282537f4c68077",
],
});
} catch (e) {
console.error(e);
}

Congrats! 🥳🎉 You've just successfully purchased a Fan Token!

Developer Support

If you have any questions or need help with other use cases, feel free to join the Moxie Official Warpcast channel and ask your questions there.

Our team is always ready to help you with any questions you may have.