Receive

The receive operation is used to declare that your account expects to receive a certain token or asset from another account. It’s commonly used in swaps and other multi-party interactions where one party commits to sending something, and the other defines what they want in return.

This operator is added to a transaction using a builder and is usually paired with a corresponding send operation.

When to Use

  • Declaring part of a swap (e.g., “I send X, and expect to receive Y”)

  • Defining expected incoming tokens or assets in a contract

  • Building conditional transactions where value is exchanged

How it works

When performing a swap, the send() operation defines what your account is giving, and the receive() operation defines what you expect in return.

You create a transaction using both operations, then compute the block — but don’t publish it. The resulting unsigned block is meant to be sent to the counterparty (e.g. via QR, API, or message).

They can then verify, sign, and publish the transaction on-chain. Once published with signatures, the swap is finalized.

const signer = KeetaNet.lib.Account.fromSeed(DEMO_ACCOUNT_SEED, 0);
const client = KeetaNet.UserClient.fromNetwork('test', signer);

// Addresses
const recipient = KeetaNet.lib.Account.fromPublicKeyString('<recipient-address>');
const sendToken = KeetaNet.lib.Account.fromPublicKeyString('<abc-token-address>');
const receiveToken = KeetaNet.lib.Account.fromPublicKeyString('<xyz-token-address>');

// Amounts (assumes you’ve already fetched decimals and validated)
const sendAmount = Numeric.fromDecimalString("10.0", 2);     // 10 ABC
const receiveAmount = Numeric.fromDecimalString("5.0", 2);   // 5 XYZ

// Create transaction
const builder = client.initBuilder();

builder.send(recipient, sendAmount.valueOf(), sendToken);
builder.receive(recipient, receiveAmount.valueOf(), receiveToken, true);

// Compute the transaction block (not yet published)
const { blocks } = await client.computeBuilderBlocks(builder);

// This unsigned block can now be signed and published by the other party
const unsignedBytes = blocks[0].toBytes();

console.log("📦 Unsigned swap block ready for signature:", unsignedBytes);

Signature Flow:

  • You (Party A) compute the transaction with send() and receive().

  • You send the unpublished block to Party B (e.g., over API or QR code).

  • Party B verifies it, signs it, and publishes the transaction to KeetaNet.

  • Done — the atomic swap is complete!

Last updated