Paymaster API

const baseUrl = "https://paymaster.kanalabs.io"
const projectKey = process.env.PROJECT_KEY
const headers = {
      'Content-Type': 'application/json',
      'api-key': projectKey,
    }

To initialize Aptos account

Note: The address needs to be whitelisted before initializing it

  async initAccount() {
    const url = `${baseUrl}/initAccount`
    const params = { address: "0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b" }
    try {
      const response = await axios.get(url, { params, headers })
      return response.data
    } catch (error: any) {
      throw error?.data || error
    }
  }

To whitelist an address

  async addToWhitelist() {
    const url = `${baseUrl}/addToWhitelist`
    const params = { user_address: "0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b" }
    try {
      const response = await axios.get(url, { params, headers })
      return response.data
    } catch (error: any) {
      throw error?.data || error
    }
  }

To disable user

Note: To stop/pause users from using sponsored txns

  async disableUser() {
    const url = `${baseUrl}/modifyUserState`
    const params = { user_address: "0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b", status: false }
    try {
      const response = await axios.get(url, { params, headers })
      return response.data
    } catch (error: any) {
      throw error?.data || error
    }
  }

To enable user

Note: To resume sponsored txns for a user

 async enableUser() {
    const url = `${baseUrl}/modifyUserState`
    const params = { user_address: "0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b", status: true } 
    try {
      const response = await axios.get(url, { params, headers })
      return response.data
    } catch (error: any) {
      throw error?.data || error
    }
  }

Is whitelisted

Note: To check whether an address is already whitelisted or not

  async isWhitelisted() {
    const url = `${baseUrl}/isWhitelisted`
    const params = { user_address: "0x0b4b8ef78fb296f89006f1936f01427a3a7e0eadd11dd4998c6bf438a0c8ce6b"}
    try {
      const response = await axios.get(url, { params, headers })
      return response.data
    } catch (error: any) {
      throw error?.data || error
    }
  }

Build txn with feepayer : true and send it to /sponsorGas endpoint with api-key it will return { feePayerAddress, feePayerAuth }

async sponsoredTxn(payload: {
    data: InputGenerateTransactionPayloadData
    options?: InputGenerateTransactionOptions
  }) {
    try {
      const transaction = await this.aptosClient.transaction.build.simple({
        sender: this.wallet.accountAddress.toString(),
        data: payload.data,
        options: payload.options,
        withFeePayer: true,
      })

      const rawTransactionBytes = transaction.rawTransaction.bcsToBytes()

      const url = `${baseUrl}/sponsorGas`
      
      const response = await axios.post(url, { data: rawTransactionBytes }, { headers })
      
      const feePayerAddress = response.data.feePayerAddress
      
      const feepayerSignature = new Uint8Array(Object.values(response.data.feePayerAuth))
      const deserializerFeePayer = new Deserializer(feepayerSignature)
      const feepayerAuth = AccountAuthenticator.deserialize(deserializerFeePayer)
      const senderAuth = this.aptosClient.transaction.sign({
        signer: this.wallet, // User Account
        transaction,
      })
      const committedTxn = await this.aptosClient.transaction.submit.simple({
        transaction: {
          rawTransaction: transaction.rawTransaction,
          feePayerAddress: AccountAddress.fromString(feePayerAddress),
        },
        senderAuthenticator: senderAuth,
        feePayerAuthenticator: feepayerAuth,
      })
      const transactionReceipt = await aptos.waitForTransaction({
      transactionHash: committedTxn.hash,
      options: { checkSuccess: true },
      });
      console.log("gas used", transactionReceipt.gas_used);
    } catch (error: any) {
      throw error?.response?.data || error
    }
  }

Last updated