# Contract Interaction ( Testnet)

## PumpFlowTokenFactory Documentation

### Fundamental Functions

#### `createMemeToken`

```solidity
function createMemeToken(
    string memory name,
    string memory symbol,
    uint256 fundingRaised,
    string memory uniqueId
) public payable returns (address)
```

**Description**

Creates a new meme token on the PumpFlow platform. A platform fee (3 FLOW) is required, and users can optionally provide initial funding to receive tokens immediately.

**Parameters**

* `name`: The name of the new meme token.
* `symbol`: The symbol of the meme token.
* `fundingRaised`: Amount of native blockchain currency (FLOW) allocated for initial funding.
* `uniqueId`: A unique identifier to prevent duplicate token creation.

**Requirements**

* User must send at least `memetokenCreationPlatformFee + fundingRaised` as `msg.value`.
* `uniqueId` must be unique.

**Process**

1. Verify sufficient `msg.value`.
2. Ensure `uniqueId` has not been used before.
3. Deduct platform fee and send it to the fee collector.
4. Deploy new meme token contract.
5. If `fundingRaised > 0`:
   * Deduct trading fee.
   * Calculate token amount using the bonding curve.
   * Transfer tokens to the creator.
6. Store token details and update platform records.
7. Emit events.

**Events Emitted**

* `MemeTokenCreated`: Contains details of the new token.
* `BoundingCurveCreated`: Details the bonding curve setup.
* `BondingCurveReached`: Emitted if initial funding exceeds the bonding curve threshold.

***

#### `buyTokens`

```solidity
function buyTokens(
    address memeTokenAddress,
    uint256 totalCost
) public payable nonReentrant
```

**Description**

Allows users to purchase meme tokens using a bonding curve pricing model.

**Parameters**

* `memeTokenAddress`: Address of the meme token.
* `totalCost`: Amount (in native currency) the user is willing to spend.

**Requirements**

* User must send `msg.value` ≥ `totalCost`.
* Token must not be blocked or have reached its bonding curve limit.
* Maximum of 805 million tokens can be sold before DEX integration.

**Process**

1. Verify `msg.value` is sufficient.
2. Ensure token is not blocked and hasn't exceeded bonding curve limits.
3. Deduct trading fee.
4. Calculate tokens purchasable using bonding curve.
5. Transfer tokens to the user and update metrics.
6. Emit events.

**Events Emitted**

* `TokensPurchased`: Contains details of the purchase.
* `BondingCurveReached`: Emitted if token reaches bonding curve limit.

***

#### `sellTokens`

```solidity
function sellTokens(
    address memeTokenAddress,
    uint256 numTokens
) public payable nonReentrant
```

**Description**

Allows users to sell their meme tokens back to the platform in exchange for native currency based on the bonding curve.

**Parameters**

* `memeTokenAddress`: Address of the meme token.
* `numTokens`: Number of tokens to sell.

**Requirements**

* Caller must own at least `numTokens` of the specified token.
* Bonding curve must not have been reached.
* Platform contract must have enough funds for refund.
* Caller must approve the contract to transfer tokens on their behalf.

**Process**

1. Ensure token is not blocked and hasn't reached bonding curve.
2. Verify caller's token balance and contract's funds.
3. Calculate refund amount using bonding curve formula.
4. Deduct trading fee from refund.
5. Transfer tokens from user to contract.
6. Send refund (minus fees) to user.
7. Update bonding curve metrics.
8. Emit events.

**Events Emitted**

* `TokensSold`: Contains details of the sale.

***

#### `getBoundingCurvePercentage`

```solidity
function getBoundingCurvePercentage(
    address memeTokenAddress
) public view returns (uint256)
```

**Description**

Retrieves the percentage progress of a meme token’s current supply relative to its bonding curve threshold.

**Parameters**

* `memeTokenAddress`: Address of the meme token.

**Returns**

* A percentage value (scaled by 100) indicating bonding curve progress.

***

#### `getRemainingTokensInLaunchpad`

```solidity
function getRemainingTokensInLaunchpad(
    address memeTokenAddress
) public view returns (uint256)
```

**Description**

Fetches the remaining token balance in the launchpad for a specific meme token.

**Parameters**

* `memeTokenAddress`: Address of the meme token.

**Returns**

* The number of tokens remaining in the launchpad.

***

#### `getAllMemeTokens`

```solidity
function getAllMemeTokens() public view returns (MemeToken[] memory)
```

**Description**

Returns a list of all meme tokens created through the platform, excluding blocked tokens.

**Returns**

* An array of `MemeToken` structs containing token details.

***

### Important Notes for Developers

#### Token Approval

Before calling `sellTokens`, ensure you call the `approve` function of the respective meme token contract to allow the factory to handle the tokens on your behalf.

***

### Contract Addresses (Testnet)

| Contract Name            | Contract Address                             |
| ------------------------ | -------------------------------------------- |
| **PumpFlowTokenFactory** | `0xBBfA869CF253aB76742AB9bc7902f783546BC830` |

***

### PumpFlowTokenFactory Contract ABI

```json
[
    {
        "inputs": [
            { "internalType": "string", "name": "name", "type": "string" },
            { "internalType": "string", "name": "symbol", "type": "string" },
            { "internalType": "uint256", "name": "fundingRaised", "type": "uint256" },
            { "internalType": "string", "name": "uniqueId", "type": "string" }
        ],
        "name": "createMemeToken",
        "outputs": [
            { "internalType": "address", "name": "", "type": "address" }
        ],
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "inputs": [
            { "internalType": "address", "name": "memeTokenAddress", "type": "address" },
            { "internalType": "uint256", "name": "totalCost", "type": "uint256" }
        ],
        "name": "buyTokens",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    }
]
```

***

### Meme Token Contract ABI

```json
[
    {
        "inputs": [
            { "internalType": "address", "name": "spender", "type": "address" },
            { "internalType": "uint256", "name": "value", "type": "uint256" }
        ],
        "name": "approve",
        "outputs": [
            { "internalType": "bool", "name": "", "type": "bool" }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }
]
```
