Create Trustline
The "Create Trustline" component establishes a trustline to a custom asset on the Stellar network.
Features
- Collects Issuer secret key, receiver public and secret key, and asset code.
- Provides a straightforward form for users to establish trustlines to their custom assets.
Additional Resources
Trustlines serve as a deliberate agreement for an account to both possess and exchange a specific asset. In order for an account to possess a particular asset, it must establish a trustline with the issuing account through the change_trust operation. These trustlines keep track of the asset balance within an account and can also impose limitations on the maximum amount of the asset that the account can hold.
C#
Install the Pakana.Stellar.Components NuGet package via the NuGet Package Manager Console.
.Net8.0 or greater is required.
dotnet add package Pakana.Stellar.Components
StellarSDK Dependency will install automatically
<pakana.stellar.components.Pages.CreateTrustline/>
@page "/Trust"
@rendermode InteractiveAuto
@inject IJSRuntime JSRuntime
<h1>Create Trustline</h1>
<EditForm Model="@this" OnValidSubmit="HandleValidSubmit" FormName="CreateTrustlineForm">
<DataAnnotationsValidator />
<ValidationSummary />
<div>
<label for="assetCode">Asset Code:</label>
<InputText id="assetCode" @bind-Value="assetCode" required></InputText><br><br>
</div>
<div>
<label for="issuerAccount">Issuer Account:</label>
<InputText id="issuerAccount" @bind-Value="issuerAccount" required></InputText><br><br>
</div>
<div>
<label for="destinationAccount">Destination Account:</label>
<InputText id="destinationAccount" @bind-Value="destinationAccount" required></InputText><br><br>
</div>
<div>
<label for="secretKey">Destination Account Secret Key:</label>
<InputText id="secretKey" @bind-Value="secretKey" required></InputText><br><br>
</div>
<button type="submit">Create Trustline</button>
</EditForm>
@if (!string.IsNullOrEmpty(message))
{
<div id="message">@message</div>
}
@code {
private string assetCode = "USD";
private string issuerAccount = "GDUKMGUGDQWSQ3DRY5E7D3ZPHDSFDPZOJKKGOV5ABLMCIDG6ASE4VRUB";
private string destinationAccount = "GC4DJYMFQFI4L6QHV7H3YSDG5E2B3OW2IGZQQRFKNYSVBZXG3X64F2W4";
private string secretKey = "SCZ6JIZDJMNZ4OQJAJBH5JFBQ4UWQMI2V3SLEQG67YRJPKRYQLT7TIT5";
private string message;
private async Task HandleValidSubmit()
{
// This script is a placeholder. You should replace it with your actual initialization logic.
var createTrustlineScript = $@"
alert('Submitting form with: Asset Code={assetCode}, Issuer Account={issuerAccount}, Destination Account={destinationAccount}, Secret Key={secretKey}');
// Actual Stellar SDK interactions should go here.
// For testing, we use a simple alert to simulate the process.
";
await JSRuntime.InvokeVoidAsync("eval", createTrustlineScript);
// Simulated response after "submitting" the transaction
UpdateMessage($"Simulated: Trustline for asset '{assetCode}' supposed to be created successfully.");
}
[JSInvokable]
public void UpdateMessage(string newMessage)
{
message = newMessage;
}
}
Javascript
We've provided simple HTML and JavaScript that can be used in your project as partial-components or be customized and integrated into your existing codebase.
*Current Version as of 2024-24-02*
<script src="https://cdnjs.cloudflare.com/ajax/libs/stellar-sdk/11.2.2/stellar-sdk.js"></script>
<script>
document.getElementById('trustlineForm').addEventListener('submit', function (event) {
event.preventDefault();
const assetCode = document.getElementById('assetCode').value;
const issuerAccount = document.getElementById('issuerAccount').value;
const destinationAccount = document.getElementById('destinationAccount').value;
const secretKey = document.getElementById('secretKey').value;
const server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');
const sourceKeys = StellarSdk.Keypair.fromSecret(secretKey);
server.loadAccount(sourceKeys.publicKey())
.then(account => {
const transaction = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET
})
.addOperation(StellarSdk.Operation.changeTrust({
asset: new StellarSdk.Asset(assetCode, issuerAccount),
source: destinationAccount // Set the destination account for the trustline
}))
.setTimeout(30)
.build();
transaction.sign(sourceKeys);
return server.submitTransaction(transaction);
})
.then(result => {
console.log('Trustline created successfully:', result);
document.getElementById('message').textContent = `Trustline for asset "${assetCode}" created successfully.`;
})
.catch(error => {
console.error('Error creating trustline:', error);
document.getElementById('message').textContent = 'Failed to create trustline. Please try again.';
});
});
</script>
<body>
<h1>Create Trustline</h1>
<form id="trustlineForm">
<label for="assetCode">Asset Code:</label>
<input type="text" id="assetCode" required><br><br>
<label for="issuerAccount">Issuer Account:</label>
<input type="text" id="issuerAccount" required><br><br>
<label for="destinationAccount">Destination Account:</label>
<input type="text" id="destinationAccount" required><br><br>
<label for="secretKey">Destination Account Secret Key:</label>
<input type="text" id="secretKey" required><br><br>
<button type="submit">Create Trustline</button>
</form>
<div id="message"></div>
</body>