# Transaction

* To create the transaction with wallet

### ETH Transaction

#### 1. Build ETH Transaction

To create an Ethereum transaction, use the `ETHTransactionRequest` with the following attributes:

* create a new Build `ETHTransactionRequest` should include the following attributes:
  * `chainID` : Chain ID (e.g., Ethereum is 1).
  * `toAddress` : Recipient's wallet address.
  * `amount` : Amount to transfer.
  * `gasPrice` : Gas price per unit.
  * `gasLimit` : Maximum gas allowed for the transaction.
  * `nonce` : Transaction count for the sender's account.
  * `data` : Optional additional data for the transaction.
  * `paillierGroupID` : User's Paillier group ID.
  * `keyGenGroupID` : the keygen Group ID of master wallet
  * `keyIdentity` : the keyIdentity of wallet
  * `ecType`: Cryptographic algorithm type (e.g., `eddsa`, `ecdsa`)

**Example**:

```swift
    let request = PayloadBuildETHTransactionRequest()
    request.chainID = chainId
    request.toAddress = toAddress
    request.amount = amount
    request.gasPrice = gasPrice
    request.gasLimit = gasLimit
    request.nonce = nonce
    request.data = data
    request.ecType = wallet.ecType
    request.paillierGroupID = paillierGroupId
    request.keyGenGroupID = keygenGroupId
    request.keyIdentity = wallet.keyIdentity

    let rawTransaction = try client.buildETHTransaction(request)
```

#### 2. Send ETH Transaction with Web3

**Example**:

```swift
    import web3swift
    func sendRawTransaction(rawTransactionData: Data) {
    let infuraUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
    let web3 = try! Web3.new(URL(string: infuraUrl)!)

    // Create a raw transaction from data
    let rawTxBytes = rawTransactionData

    // Send the raw transaction
    web3.eth.sendRawTransaction(rawTx: rawTxBytes) { result in
        switch result {
        case .success(let transactionHash):
            print("Transaction successfully sent. Hash: \(transactionHash)")
        case .failure(let error):
            print("Failed to send transaction: \(error.localizedDescription)")
        }
    }
}
```

### SOLANA Transaction

#### 1. Build Solana Transaction

* create a new Build `BuildSolTransactionRequest` should include the following attributes:
  * chainID: Chain ID (e.g., Solana mainnet is 501).
  * fromAddress: Sender's wallet address.
  * toAddress: Recipient's wallet address.
  * Amount: Amount to transfer.
  * paillierGroupID: User's Paillier group ID.
  * keyGenGroupID: Master wallet's key generation group ID.
  * KkeyIdentity: the keyIdentity of wallet
  * eCType: Cryptographic algorithm type (e.g., eddsa, ecdsa).

**Example**:

```swift
   let request = PayloadBuildSolTransactionRequest()
    request.chainID = chainId
    request.fromAddress = fromAddress
    request.toAddress = toAddress
    request.amount = amount
    request.ecType = wallet.ecType
    request.paillierGroupID = paillierGroupId
    request.keyGenGroupID = keygenGroupId
    request.keyIdentity = wallet.keyIdentity
    
    let rawTransaction = try client.buildSolTransaction(request)

```

#### 2. Send SOLANA Transaction

* Use a Solana library to send the raw transaction. Encode the raw transaction to Base58 before sending. **Example**:

```swift
import Solana

func sendRawTransaction(rawTxBytes: Data) {
    let endpoint = APIEndPoint.mainnetBeta
    let solana = Solana(router: NetworkingRouter(endpoint: endpoint), accountStorage: InMemoryAccountStorage())

    // Encode raw transaction bytes to Base58
    let rawTxBase58 = rawTxBytes.base58EncodedString()

    // Send raw transaction
    solana.api.sendTransaction(
        serializedTransaction: rawTxBase58,
        configs: RequestConfiguration(encoding: "base64", skipPreflight: false, commitment: "finalized")
    ) { result in
        switch result {
        case .success(let transactionHash):
            print("Transaction successful! Hash: \(transactionHash)")
        case .failure(let error):
            print("Transaction failed: \(error.localizedDescription)")
        }
    }
}

import Base58

extension Data {
    func base58EncodedString() -> String {
        return Base58.encode(self)
    }
}

```

### Tron Transaction

#### 1. Build Tron Transaction

* create a new Build `BuildTronTransactionRequest` should include the following attributes:
  * chainID: Chain ID (e.g., Tron mainnet is 728, testnet is 939).
  * fromAddress: Sender's wallet address.
  * toAddress: Recipient's wallet address.
  * Amount: Amount to transfer.
  * paillierGroupID: User's Paillier group ID.
  * keyGenGroupID: Master wallet's key generation group ID.
  * KkeyIdentity: the keyIdentity of wallet
  * eCType: Cryptographic algorithm type (e.g., eddsa, ecdsa).

**Example**:

```swift
   let request = PayloadBuildTronTransactionRequest()
    request.chainID = chainId
    request.fromAddress = fromAddress
    request.toAddress = toAddress
    request.amount = amount
    request.ecType = wallet.ecType
    request.paillierGroupID = paillierGroupId
    request.keyGenGroupID = keygenGroupId
    request.keyIdentity = wallet.keyIdentity
    
    let rawTransaction = try client.buildTronTransaction(request)

```

#### 2. Send Tron Transaction

* Use a Tron library to send the raw transaction. Encode the raw transaction to Base58 before sending. **Example**:

```swift
import Tron

func sendRawTransaction(rawTxBytes: Data) {
    let endpoint = APIEndPoint.mainnetBeta
    let rawTxBytes = Tron(router: NetworkingRouter(endpoint: endpoint), accountStorage: InMemoryAccountStorage())

    // Send raw transaction
    tron.api.sendTransaction(
        serializedTransaction: rawTxBytes,
        configs: RequestConfiguration(encoding: "base64", skipPreflight: false, commitment: "finalized")
    ) { result in
        switch result {
        case .success(let transactionHash):
            print("Transaction successful! Hash: \(transactionHash)")
        case .failure(let error):
            print("Transaction failed: \(error.localizedDescription)")
        }
    }
}

```

### Ton CoinTransaction

#### 1. Build Ton Coin Transaction

* create a new Build `PayloadBuildTonTransactionRequest` should include the following attributes:
  * chainID: Chain ID (e.g., Ton mainnet is ton, testnet is ton-testnet).
  * fromAddress: Sender's wallet address.
  * toAddress: Recipient's wallet address.
  * amount: Amount to transfer.
  * paillierGroupID: User's Paillier group ID.
  * keyGenGroupID: Master wallet's key generation group ID.
  * keyIdentity: the keyIdentity of wallet
  * ellipticCurveType: Cryptographic algorithm type (e.g., eddsa, ecdsa).
  * derivationPath: derivation Path of Ton chain "m/44'/607'/0'"
  * subwallet: The type of ton wallet v3r2 (698983191)
  * comment: memo of transaction
  * bounce: the transaction to bounce (true, false)

**Example**:

```swift
   let request = PayloadBuildTonTransactionRequest()
        request.keyGenGroupID = "keygenGroupId"
        request.paillierGroupID = "paillierGroupId"
        request.keyIdentity = "keyIdentity"
        request.ellipticCurveType = "eddsa"
        request.derivationPath = "derivationPath"
        request.chainID = "ton-testnet"
        request.toAddress = "toAddress"
        request.fromAddress = "fromAddress"
        request.amount = "amount"
        request.subwallet = "subWallet"
        request.comment = "memo"
        request.bounce = true
    
    let rawTxBytes = try client.buildTonTransaction(request)

```

#### 2. Send Ton Coin Transaction

* Use a Ton library to send the raw transaction. Encode the raw transaction to Base64 before sending. **Example**:

```swift
import Foundation

struct BOCRequest: Codable {
    let boc: String
}

func sendRawTransaction(rawTxBytes: Data) {
    let bocMessage = rawTxBytes.base64EncodedString()
    // 2. Create the URL
    guard let url = URL(string: "https://quiknode/sendBocReturnHash") else {
        print("Invalid URL")
        return
    }
    
    // 3. Construct the URLRequest
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    // Add headers
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    
    // 4. Create the request body
    let bocPayload = BOCRequest(
        boc: bocMessage
    )
    
    do {
        // Convert the Swift struct to JSON
        let jsonData = try JSONEncoder().encode(bocPayload)
        request.httpBody = jsonData
    } catch {
        print("Error encoding JSON: \(error.localizedDescription)")
        return
    }
    
    // 5. Send the request with URLSession
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        // Parse the response data (if any)
        if let data = data,
           let responseString = String(data: data, encoding: .utf8) {
            print("Response: \(responseString)")
        }
    }
    
    task.resume()

}

```

### Bitcoin Transaction

#### 1. Build Bitcoin Transaction

* create a new Build `PayloadBuildBtcTransactionRequest` should include the following attributes:
  * chainID: Chain ID (e.g., BTC mainnet is btc, testnet is btc-testnet).
  * fromAddress: Sender's wallet address.
  * receiverAddress: Recipient's wallet address.
  * amount: Amount to transfer.
  * paillierGroupID: User's Paillier group ID.
  * keyGenGroupID: Master wallet's key generation group ID.
  * keyIdentity: the keyIdentity of wallet
  * ellipticCurveType: Cryptographic algorithm type (e.g., eddsa, ecdsa).
  * IgnoringFee: The flag used to skip fee enforcement on testnet. This should always be set to false on mainnet.

**Example**:

```swift
   let request = PayloadBuildBtcTransactionRequest()
        request.chainId = "chainId"
        request.fromAddress = "fromAddress"
        request.receiverAddress = "toAddress"
        request.amount = "amount"
        request.ellipticCurveType = "ecType"
        request.paillierGroupID = "paillierGroupId"
        request.keyGenGroupID = "keygenGroupId"
        request.keyIdentity = "keyIdentity"
        request.derivationPath = "derivationPath"
        request.IgnoringFee = true
    
    let rawTxBytes = try client.buildBtcTransaction(request)

```

#### 2. Send Bitcoin Transaction

* Use a BTC library to send the raw transaction **Example**:

```swift
import Foundation

extension Data {
    /// Returns a lowercase hex string representation of the Data.
    func toHexString() -> String {
        return self.map { String(format: "%02x", $0) }.joined()
    }
}

func sendRawTransaction(rawTxBytes: Data) {
    
    // 1. Define the URL
    guard let url = URL(string: "https://blockstream.info/testnet/api/tx") else {
        print("Invalid URL")
        return
    }
    
    // 2. Prepare the URLRequest
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    // Blockstream’s API expects `text/plain`
    request.setValue("text/plain", forHTTPHeaderField: "Content-Type")
    
    // 3. The raw transaction in hex (same as in your curl --data)
    let rawTransactionHex = rawTxBytes.toHexString()
    
    // 4. Convert the hex string to Data and assign to request's body
    request.httpBody = rawTransactionHex.data(using: .utf8)
    
    // 5. Send the request
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        // Handle client-side error
        if let error = error {
            print("Error: \(error)")
            return
        }
        
        // Check server response
        guard let httpResponse = response as? HTTPURLResponse else {
            print("No response from server.")
            return
        }
        
        // Print status for debugging
        print("Status code: \(httpResponse.statusCode)")
        
        // Handle response data
        if let data = data, let responseString = String(data: data, encoding: .utf8) {
            // The API typically returns the transaction ID if successful,
            // or an error message otherwise.
            print("Response body: \(responseString)")
        } else {
            print("No data in response.")
        }
    }
    
    task.resume()

}

```

### Xrp Transaction

#### 1. Build XRP Transaction <a href="#id-1-build-xrp-transaction" id="id-1-build-xrp-transaction"></a>

* create a new Build `PayloadBuildXrpTransactionRequest` should include the following attributes:
  * chainId: Chain ID (e.g., XRP mainnet is xrp, testnet is xrp-testnet).
  * fromAddress: Sender's XRP wallet address.
  * toAddr: Recipient's XRP wallet address.
  * amount: Amount to transfer.
  * paillierGroupID: User's Paillier group ID.
  * keyGenGroupID: Master wallet's key generation group ID.
  * keyIdentity: The keyIdentity of the wallet.
  * ellipticCurveType: Cryptographic algorithm type (e.g., ecdsa, eddsa).
  * derivationPath: Derivation path for the key.

**Example**:

```swift
    let request = PayloadBuildXrpTransactionRequest()
    request.chainId = "chainId"
    request.fromAddress = "fromAddress"
    request.toAddr = "toAddress"
    request.amount = "amount"
    request.ellipticCurveType = "ecType"
    request.paillierGroupID = "paillierGroupId"
    request.keyGenGroupID = "keygenGroupId"
    request.keyIdentity = "keyIdentity"
    request.derivationPath = "derivationPath"

    let rawTxBytes = try client.buildXrpTransaction(request)
```

#### 2. Send XRP Transaction <a href="#id-2-send-xrp-transaction" id="id-2-send-xrp-transaction"></a>

* Use an XRP library or network provider (such as the [XRPL JSON-RPC API](https://xrpl.org/public-servers.html)) to send the raw transaction.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cramiumlabs.gitbook.io/docs/sdk-mpc-ios/usage-guide-for-an-mpc-wallet-app/transaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
