A token pay plan protocol for ethereum like chain

token-pay-plan protocol

任何人或者产品可以使用这个协议来实现 token 支付计划,只要遵循以下规则即可。

plan,支付计划

plan 是此合约最核心的概念,它包含以下字段:

  • id: plan 的唯一标识,是一个自增的数字
  • spender: plan 的消费方,是一个地址
  • token: plan 的 token 类型,是一个地址
  • perAmount: plan 的每个周期消费的 token 数量
  • payAt: plan 的周期长度,是一个枚举,可以是 week, month, 季度, year
  • status: plan 的状态,是一个枚举,可以是 active, inactive
  • lastPayTime: 上次执行时间
  • approvedUsers: plan 的订阅者,是一个数组,数组中的每个元素是一个地址

任何人都可以作为项目方创建自己的 plan, plan 一旦创建不可更改,您可以为你的产品创建不同的 plan

例如 你可以为你的产品创建四个 plan,他们分别是:

  • Plan Ⅰ:每个周支付 1000 个 token
  • Plan Ⅱ:每个月支付 3000 个 token
  • Plan Ⅲ:每个季度支付 8000 个 token
  • Plan Ⅳ:每年支付 30000 个 token

订阅

任何人都可以订阅任意的 plan,订阅一个 plan 的前提条件是:

  1. 订阅者必须先授权此合约可以从他的账户中转出特定数量的 token。

这是本合约的核心,我们开发此合约的目的就是保护你的 token,你不需要为每个产品授权 token 的转出权限,只需要授权此合约即可,授权后此合约没有任何权限转出你的 token,而是通过你订阅的 plan 列表来控制 plan 的消费方转出您 token 的数量和频率,如此一来,你的 token 就不会被任何人转出,而是被你订阅的 plan 消费方按照你的计划来消费。

  1. 针对 plan 中定义的 token,订阅者给予本合约的授权金额,不低于 plan 中定义的 perAmount。

取消订阅

在任何时候,您都可以取消订阅某个 plan,取消订阅之后,此产品将没有权力从您的钱包转出 token。

同时,产品方在查询您的订阅状态的时候,会返回 false,所以产品方可以根据您的订阅状态来决定是否为您继续提供服务。

执行计划

plan 的执行者是 plan 的消费方(spender),消费方可以在每个 plan 的周期结束后,执行一次 plan。

执行 plan 会尝试向 plan 的所有订阅者划取费用,如果订阅者的余额不足(可能是授权不足或者余额不足),则会将此订阅者直接从订阅者列表中删除(永久),并跳过此订阅者的扣款动作。

其他问题

我们限制了每个 plan 的订阅者数量,这是为了防止执行 plan 的时候耗费过量的 gas 导致计划执行失败,如果您的产品需要大量的订阅者,可以创建多个 plan,每个 plan 的订阅者数量不超过 1000 个。

扩展

我们会尝试将此合约扩展成一个标准的 ERC721 合约,这样就可以将每个订阅者和 Plan 的授权映射外化成一个 NFT Token,这样就可以在任何支持 ERC721 的平台上显示你授权的 plan 了。

token-pay-plan protocol

Any person or product can use this protocol to implement a token payment plan, as long as the following rules are followed.

plan, payment plan

plan is the core concept of this contract, it contains the following fields:

  • id: The unique identifier of the plan, which is an auto-incrementing number
  • spender: the consumer of the plan, which is an address
  • token: the token type of plan, which is an address
  • perAmount: The number of tokens consumed in each cycle of the plan
  • payAt: the period length of plan, is an enumeration, can be week, month, quarter, year
  • status: the status of plan, is an enumeration, can be active, inactive
  • lastPayTime: last execution time
  • approvedUsers: subscribers of plan, is an array, each element in the array is an address

Anyone can create their own plan as a project party, once a plan is created it cannot be changed, you can create a different plan for your product

For example, you can create four plans for your product, they are:

  • Plan Ⅰ: Pay 1000 tokens per week
  • Plan II: Pay 3000 tokens per month
  • Plan Ⅲ: 8000 tokens paid every quarter
  • Plan IV: Pay 30,000 tokens per year

subscribe

Anyone can subscribe to any plan. The prerequisites for subscribing to a plan are:

  1. The subscriber must first authorize the contract to transfer a specific amount of tokens from his account.

This is the core of this contract. The purpose of developing this contract is to protect your token. You do not need to authorize the token transfer permission for each product. You only need to authorize this contract. After authorization, this contract does not have any permission to transfer out. Your token, but the list of plans you subscribe to control the number and frequency of the plan’s consumer to transfer your token, so that your token will not be transferred by anyone, but by the plan you subscribed to The consumer consumes according to your plan.

  1. For the token defined in the plan, the authorized amount given by the subscriber to this contract is not less than the perAmount defined in the plan.

unsubscribe

At any time, you can unsubscribe from a plan, after unsubscribing, the product will not have the right to transfer tokens from your wallet.

At the same time, when the product side queries your subscription status, it will return false, so the product side can decide whether to continue to provide you with services based on your subscription status.

Implementation plan

The executor of the plan is the plan’s consumer (spender), and the consumer can execute the plan once after each plan period ends.

Executing the plan will try to charge all the subscribers of the plan. If the subscriber’s balance is insufficient (possibly due to insufficient authorization or insufficient balance), the subscriber will be deleted directly from the subscriber list (permanently) and skipped Chargeback action for this subscriber.

other problems

We limit the number of subscribers per plan. This is to prevent plan execution failure due to excessive gas consumption during plan execution. If your product requires a large number of subscribers, you can create multiple plans with subscribers for each plan. The quantity does not exceed 1000.

extension

We will try to extend this contract into a standard ERC721 contract, so that the authorization mapping of each subscriber and plan can be externalized into an NFT token, so that the plan you authorized can be displayed on any platform that supports ERC721.

1 Like

第一版协议存在的限制

  1. 扣款是所有订阅者一次性扣款,不存在每个用户的扣款周期,因此也无法为每个用户记录订阅周期。
  2. 第一次订阅之后,不会立即扣款,而是等待下一个统一的扣款周期。

第二版协议可能会尝试解决,不过会增加交互的成本,两版协议可以同时运行,供服务提供方选择。