Receiving payments from Contract Accounts (C to G)
If a sender is using a Contract Account, and sending an SAC token (such as XLM, USDC, etc.), the transaction will be received by the G account normally since it is supported on a protocol level.Receiving payments from Contract Accounts requiring a memo
Entities such as exchanges and asset issuers usually use omnibus accounts, using a shared receiving address and a unique identifier memo like:- Account:
GBLVHX33XGOBDOXK7ERDL34NVH6WW7VTT2OBAHPJ7G3D423HBG5NOMY7 - Memo ID:
123456789
Example using Horizon
Receiving from a G address The memo field will be present in the response of Horizon’s accounts/:account_id/transactions endpoint Example response from a G address:transfer function in the Stellar Asset Contract (SAC).
Example response from a Contract Account:
Example using RPC
Since the release of the Unified Asset Events (CAP-67) on Protocol 23, a number of transactions involving Stellar assets now emit standardized events that can be ingested by both the Horizon API and the Stellar RPC. If funds are sent through a ‘payment’ operation or by invoking the ‘transfer’ function of the asset contract, the same kind of event is emitted for both cases. To monitor these payments, the method ‘getEvents’ of the Stellar RPC is used to stream events emitted by the Stellar Asset Contract. A filter can be used to specify the target asset (SAC contract ID) and accounts. From a G address When the receiver is a normal G address, the value portion of the event is just the amount received.Sending payments from Contract Accounts to a recipient that requires a memo
If a sender is using a Contract Account, and sending an SAC token (such as XLM, USDC, etc.) to a recipient that requires a memo, (an exchange, for example) it should create a muxed address that includes the receiving account (GBLVHX…) and provided memo id in the muxed id field (123456789). In the user interface, the resulting memo address doesn’t need to be surfaced to the user, it should be used directly in the transaction. If the receiving party already provides an M address, it should be used directly, without requiring a memo field. Since this is the value provided by the user, it should be used in the transaction and displayed to the user. The same applies when the receiving party provides a W address. See Sending to a muxed contract address.Sending payments to Contract Accounts (G to C)
Sending a ‘transfer’ via Stellar RPC
This approach is simpler but requires the use of an RPC and involves a slightly different transaction workflow than a traditional native operation. For a detailed code example, see the section ‘Sending Payments through the SAC -> Using the Stellar RPC’ of this repository’s examples. Assembling the operation and transaction To perform a ‘transfer’ through an SAC, it is necessary to assemble a ‘InvokeHostFunctionOp’ operation with similar arguments as a ‘payment’ operation along with the contract identifier and target function name(‘transfer’). This can be seen in detail in the ‘assembleTransferOperation’ function of the demo. While the example demonstrates this process in greater detail, it is possible to use the js-stellar-sdk and its Contract client in a simpler manner. The transaction is then built by adding this operation and configuring the additional parameters as any other ‘payment’ transaction. Simulating the transaction After building the transaction, the RPC method for ‘simulateTransaction’ is used to simulate an execution of this transaction against the current state of the network, identify if it is expected to succeed and, also provide a number of additional parameters about this execution. This process is fully handled by the SDK in a very simple step by using the ‘prepareTransaction’ function of the RPC server client. It automatically simulates and provides an updated transaction object. See this process in detail in the demo. Signing When the same account is used as the ‘sender’ of the funds and also the ‘source’ of the transaction, the signing step will be exactly the same. Once the updated transaction object is returned from the previous step, you just sign it with the source/sender account and it will be ready. When the ‘source’ account of the transaction differs from the ‘sender’ account, an additional signed entry will be necessary to authorize the ‘transfer’ invocation explicitly. Refer to the Soroban Authorization Framework documentation for further details. Sending the Transaction for processing When a transaction is submitted for processing through the Stellar RPC, differently from the Horizon API, it will resolve immediately confirming its submission but not the processing. It is necessary to poll the transaction status for the next moment until you get a confirmation that it was successfully processed or a status update on a potential failure.Sending a ‘transfer’ via Horizon API
When sending a ‘transfer’ via the Horizon API, this approach introduces a few extra steps when compared to the use of an RPC. For a detailed code example, see the section ‘Sending Payments through the SAC -> Using the Horizon API’ of this repository’s examples. Assembling the operation and transaction This process is mostly the same as described in the previous section ‘Sending a ‘transfer’ via Stellar RPC’ with some slight modifications. Since the Horizon API does not provide an endpoint to simulate a transaction execution, this means that when the transaction is assembled we need to ensure it also contains:- The Soroban authorization entries that fulfill the smart contracts authorization requirements.
- The Soroban data object, detailing the footprint, resources, and resource fee expected for this contract invocation.
Sending to a muxed contract address
From Protocol 30 (CAP-84), a contract address can carry a 64-bit ID. The result is a muxed contract address, which starts withW. A recipient that holds the funds of many users in one contract can give each user a W address, the same way a pooled account gives each user an M address.
To support payments to a W address:
- Accept a
Waddress where you accept aCaddress, and validate it with your SDK. Older SDK releases reject it. JavaScript SDK v17.3.0, Go SDK v0.8.0, and Java SDK 6.0.0 are the first releases that parse it. JavaScript SDK v16.4.0 decodes the Protocol 30 XDR, but itsAddressstill rejects aWaddress. - Pass the
Waddress as thetoargument of the SACtransferfunction. The rest of the transaction is the same as for aCaddress.
C address. The transfer event has the C address in its to topic and the ID in to_muxed_id, the same as for a muxed account. See Sending to a muxed address for the rules.