Is there an allowance function on Solana that sets a limit of SPL tokens that can be withdrawn from your account by an approved address. If not what would you say is a good way to simulate this behaviour.
1 Answer
You can use @solana/spl-token
to approve a delegate for a token account.
Approving a delegate is the process of authorizing another account to transfer or burn tokens from a token account. When using a delegate, the authority over the token account remains with the original owner. The maximum amount of tokens a delegate may transfer or burn is specified at the time the owner of the token account approves the delegate. Note that there can only be one delegate account associated with a token account at any given time.
To approve a delegate using the spl-token library, you use the approve
function.
const transactionSignature = await approve(
connection,
payer,
account,
delegate,
owner,
amount
)
The approve function returns a TransactionSignature that can be viewed on Solana Explorer. The approve function requires the following arguments:
connection
the connection to the clusterpayer
the account of the payer for the transaction (keypair)account
the token account to delegate tokens from (public key)delegate
the account the owner is authorizing to transfer or burn tokens (public key)owner
the owner of the token account (public key)amount
the maximum number of tokens the delegate may transfer or burn
A previously approved delegate for a token account can be later revoked. Once a delegate is revoked, the delegate can no longer transfer tokens from the owner's token account. Any remaining amount left untransferred from the previously approved amount can no longer be transferred by the delegate.
To revoke a delegate using the spl-token library, you use the revoke
function.
const transactionSignature = await revoke(
connection,
payer,
account,
owner,
)