Sponsored Transactions
Sponsored transaction: sponsor pays gas
Set sponsored: true when building the swap. In this mode the swap does not use txb.gas as input or output, because the gas coin belongs to the sponsor.
import { Transaction } from "@mysten/sui/transactions"
import { SUI_TYPE_ARG } from "@mysten/sui/utils"
const sender = userSigner.toSuiAddress()
const sponsor = sponsorSigner.toSuiAddress()
const txb = new Transaction()
await client.fastRouterSwap({
router: routerRes,
txb,
slippage: 0.01,
sponsored: true,
})
// Build command-only bytes. Pass sender so CoinWithBalance can resolve
// the user's input coins before the sponsor adds gas data.
const txKindBytes = await client.buildTransactionKind(txb, sender)
const { objects: gasCoins } = await client.client.listCoins({
owner: sponsor,
coinType: SUI_TYPE_ARG,
limit: 1,
})
const sponsorCoins = gasCoins.map((coin) => ({
objectId: coin.objectId,
version: coin.version,
digest: coin.digest,
}))
const sponsoredTx = client.buildSponsoredTransaction({
txKindBytes,
sender,
sponsor,
sponsorCoins,
gasBudget: "100000000",
})
const result = await client.signAndExecuteSponsoredTransaction(
sponsoredTx,
userSigner,
sponsorSigner
)For gas-station integrations, the sponsored transaction flow can be split between the user and the sponsor:
1. The user builds the command-only transaction bytes by calling buildTransactionKind.
2. The sponsor converts those transaction-kind bytes into a full sponsored transaction by attaching gas data, including gasOwner and gasPayment.
3. Both the user and the sponsor sign the same full transaction bytes.
4. The fully signed transaction is then submitted to the network.
Last updated