Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Recurring Buys

Prefund a shared USDC balance, then create a plan that buys one allowlisted target on a schedule. Both steps run on ValueChain. Contract addresses are on Wealth Contracts.

AutomaticInvestmentDeposit holds the USDC and mints 18-decimal shares to the owner. AutomaticInvestment stores the plan. Creating a plan records the schedule. An operator spends amount from the shared balance when the plan is due. The same balance funds every plan of that wallet.

Each owner has one active or paused plan per target. Cancel that plan before creating another for the same target. The cancelled plan index stays assigned.

Read the resulting plans with Recurring Buy Plans API. availableAssets there is this shared balance.

Schedule

amount is raw USDC units, the same unit as asset() on the deposit contract. planType and dayOfPeriod are integers.

planType

Value

dayOfPeriod

When it is due

DAILY

0

0

Every local day.

WEEKLY

1

1 Monday through 7 Sunday

That local weekday.

BI_WEEKLY

2

1 Monday through 7 Sunday

That weekday in the creation week, then every second local week.

MONTHLY

3

1–31

That day of the local month. A shorter month has no execution.

The operator can execute from local midnight until 09:30 local time. A China vault (vaultType 0) uses UTC+8. A US book target (vaultType 1) uses UTC-4, with no daylight-saving change. The user does not send the execution transaction.

If the shared balance is below amount when the operator runs, that execution reverts and the plan stays active.

1. Select a target

getTargetConfig(vault) returns minInvestAmount. A zero minimum means the address is not allowlisted. amount must be at least that minimum. List allowlisted targets with getNumVaults() and getVaultByRange(start, end).

For a China vault, vault is the fund vault from Fund Information. The USDC must already be on ValueChain. If it is still on another network, move it with Mirror Protocol first.

2. Prefund

Call deposit from the owner's wallet. The wallet pays SOSO gas. Approve the deposit contract to spend the USDC first.

depositOnBehalf is the relayed form. The owner signs an ERC-2612 permit that names the deposit contract as spender. The permit signature is 65 bytes, and the contract submits it on every depositOnBehalf call. Read the token name() and nonces(owner) for that permit. The permit domain version used by vUSDC is "1".

Install viem and tsx. Set PRIVATE_KEY and PREFUNDED_USDC (raw USDC units). Save as recurring-buy.mts and run npx tsx recurring-buy.mts.

balanceOfAssets is the USDC value of the position. balanceOfShares is the 18-decimal share balance. decimals() on the deposit contract returns 18. A deposit that converts to zero shares reverts.

Shares are an internal ledger. They are not a transferable ERC-20. Deposit rounds shares down. A later debit rounds shares up so the recipient can redeem at least the USDC amount.

Withdraw unused USDC

redeem(shares) burns the owner's shares and sends USDC to that owner. redeemOnBehalf(owner, shares, deadline, signature) is the relayed form. Its EIP-712 domain name is AutomaticInvestmentDeposit, version "1", and verifyingContract is the deposit contract. The nonce is nonces(owner) on the deposit contract, which is separate from the plan-contract nonce.

USDC is sent to owner. The relayer does not receive it.

3. Create a plan

Call createInvestmentPlan from the owner's wallet.

Append this to the client above. Set VAULT_ADDRESS, PLAN_AMOUNT (raw USDC units per execution), PLAN_TYPE, and DAY_OF_PERIOD.

getUserPlanIndex reverts when that owner has no active or paused plan for the vault. After a successful create it returns the plan index. The same index is the third topic of InvestmentPlanCreated.

Signed create

Use this path instead of the direct createInvestmentPlan call when another wallet pays gas. Keep the client, vault, amount, planType, and dayOfPeriod from the sections above. The owner signs EIP-712 CreateInvestmentPlan. The domain name is AutomaticInvestment, version "1", and verifyingContract is the plan contract. Read nonces(owner) on the plan contract and sign that value. The transaction consumes it.

Set USER_PK for the plan owner and RELAYER_PK for the gas payer. To have SoDEX broadcast the call, use the Transaction Relayer. The relayer is msg.sender, so the owner must be inside the signed CreateParams.

A signed prefund uses the same relayer pattern against the deposit contract:

The permit spender is the deposit contract, and value is the USDC amount. Sign it with the USDC nonce, then pass the 65-byte signature as permitSignature.

4. Pause, resume, and cancel

The owner calls these with the vault address. The OnBehalf forms take owner, vault, deadline, and an EIP-712 signature. They use the same AutomaticInvestment domain and the plan-contract nonce.

Action
Direct call
Typed data

Pause

pauseInvestmentPlan(vault)

PauseInvestmentPlan(address owner,address vault,uint256 nonce,uint256 deadline)

Resume

resumeInvestmentPlan(vault)

ResumeInvestmentPlan(address owner,address vault,uint256 nonce,uint256 deadline)

Cancel

cancelInvestmentPlan(vault)

CancelInvestmentPlan(address owner,address vault,uint256 nonce,uint256 deadline)

Pause requires an active plan. Resume requires a paused plan. Cancel removes the vault from the owner's plan map and leaves the prefund in the deposit contract. Withdraw that USDC with redeem. The plan index remains queryable through the API with status=history.

Last updated