-
Notifications
You must be signed in to change notification settings - Fork 33
stellar smart contract #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ugo-X
wants to merge
8
commits into
stellar:main
Choose a base branch
from
Ugo-X:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68c0f56
stellar smart contract
Ugo-X 1fe7f25
Merge branch 'stellar:main' into main
Ugo-X e727176
all comments fixed
Ugo-X 00fc103
Merge branch 'stellar:main' into main
Ugo-X 5480e05
updated PR
Ugo-X 04cbe84
Merge branch 'main' of github.com:Ugo-X/basic-payment-app
Ugo-X ac82cc1
Updated PR
Ugo-X 5e197b3
Merge branch 'stellar:main' into main
Ugo-X File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { persisted } from 'svelte-local-storage-store'; | ||
| import { StrKey } from '@stellar/stellar-sdk'; | ||
| import { get } from 'svelte/store'; | ||
|
|
||
| /** | ||
| * @typedef {Object} SavedContract | ||
| * @property {string} contractId - The Stellar contract ID | ||
| * @property {string} name - Human-readable name for this contract | ||
| * @property {string} [description] - Optional description of the contract | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {Object} ContractStore | ||
| * @property {SavedContract[]} savedContracts - An array of saved contracts | ||
| * @property {SavedContract|null} currentContract - The currently active contract or null | ||
| */ | ||
|
|
||
| function createContractStore() { | ||
| /** | ||
| * @type {import('svelte/store').Writable<ContractStore>} | ||
| */ | ||
| const { subscribe, set, update } = persisted('bpa:contractStore', { | ||
| savedContracts: [], | ||
| currentContract: null | ||
| }); | ||
|
|
||
| return { | ||
| subscribe, | ||
|
|
||
| /** | ||
| * Saves a new contract to the store | ||
| * @param {SavedContract} contract - Contract details to save | ||
| * @throws Will throw an error if the contract ID is invalid | ||
| */ | ||
| saveContract: (contract) => | ||
| update(store => { | ||
| if (!StrKey.isValidContract(contract.contractId)) { | ||
| throw new Error('Invalid contract ID'); | ||
| } | ||
|
|
||
| const newContract = { ...contract }; | ||
| const updatedStore = { | ||
| ...store, | ||
| savedContracts: [...store.savedContracts, newContract] | ||
| }; | ||
|
|
||
| // Log a success message to the console | ||
| console.log('Contract saved successfully:', newContract); | ||
|
|
||
| return updatedStore; | ||
| }), | ||
|
|
||
| /** | ||
| * Removes a contract from the store | ||
| * @param {string} id - Unique identifier of the contract to remove | ||
| */ | ||
| removeContract: (id) => | ||
| update(store => ({ | ||
| ...store, | ||
| savedContracts: store.savedContracts.filter(c => c.contractId !== id), | ||
| currentContract: store.currentContract?.contractId === id ? null : store.currentContract | ||
| })), | ||
|
|
||
| /** | ||
| * Sets the current active contract using its ID | ||
| * @param {string|null} contractId - Contract ID to set as current, or null to clear | ||
| * @throws Will throw an error if the contract ID is not found in saved contracts | ||
| */ | ||
| setCurrentContract: (contractId) => | ||
| update(store => { | ||
| if (contractId === null) { | ||
| return { ...store, currentContract: null }; | ||
| } | ||
|
|
||
| const contract = store.savedContracts.find(c => c.contractId === contractId); | ||
| if (!contract) { | ||
| throw new Error('Contract not found'); | ||
| } | ||
|
|
||
| return { ...store, currentContract: contract }; | ||
| }), | ||
|
|
||
| /** | ||
| * Looks up a contract by its Stellar contract ID | ||
| * @param {string} contractId - Stellar contract ID to look up | ||
| * @returns {SavedContract|undefined} The found contract or undefined | ||
| */ | ||
| lookup: (contractId) => { | ||
| const store = get(contractStore); | ||
| return store.savedContracts.find(contract => contract.contractId === contractId); | ||
| }, | ||
|
|
||
| /** | ||
| * Clears all saved contracts from the store | ||
| */ | ||
| empty: () => set({ savedContracts: [], currentContract: null }) | ||
| }; | ||
| } | ||
|
|
||
| export const contractStore = createContractStore(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { StrKey, Contract } from '@stellar/stellar-sdk'; | ||
| import { Server } from '@stellar/stellar-sdk/rpc'; | ||
|
|
||
| /** | ||
| * Determines if a contract ID represents a Stellar Asset Contract (SAC) | ||
| * @param {string} contractId - The contract ID to check | ||
| * @returns {boolean} True if the contract ID is for a SAC | ||
| */ | ||
| function isStellarAssetContract(contractId) { | ||
| return contractId.startsWith('CA'); | ||
| } | ||
|
|
||
| /** | ||
| * Generates a contract client for interacting with a Stellar smart contract | ||
| * @param {string} contractId - The contract ID to validate and connect to | ||
| * @returns {Promise<Contract>} A Contract instance | ||
| */ | ||
| export async function generateContractClient(contractId) { | ||
| const server = new Server('https://soroban-testnet.stellar.org'); | ||
|
|
||
| // Validate contract ID | ||
| if (!StrKey.isValidContract(contractId)) { | ||
| throw new Error('Invalid contract ID format'); | ||
| } | ||
|
|
||
| try { | ||
| if (isStellarAssetContract(contractId)) { | ||
| // For SAC contracts, create contract instance without fetching WASM | ||
| const contract = new Contract(contractId); | ||
| console.log('Contract:', contract); | ||
| return contract; | ||
| } | ||
| else { | ||
| // For WASM contracts, fetch the WASM first | ||
| console.log('Fetching WASM for contract ID:', contractId); | ||
| const contractResponse = await server.getContractWasmByContractId(contractId); | ||
|
|
||
| console.log('Contract WASM length:', contractResponse.length); | ||
| const contract = new Contract(contractId); | ||
| console.log(contract); | ||
|
|
||
| // You might want to add the contract's interface here | ||
| // This could involve parsing the WASM to get available methods | ||
|
|
||
| return contract; | ||
| } | ||
| } catch (serverError) { | ||
| console.error('Error handling contract:', serverError); | ||
|
|
||
| if (serverError.response && serverError.response.status === 404) { | ||
| throw new Error(`Contract not found: ${contractId}`); | ||
| } | ||
|
|
||
| throw new Error(`Failed to handle contract: ${serverError.message}`); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <script lang="ts"> | ||
| import { contractStore } from '$lib/stores/contractStore'; | ||
| import { generateContractClient } from '$lib/utils/contractUtils'; | ||
|
|
||
| let inputContractId = ''; // Temporary variable for input | ||
| let contractName = ''; | ||
| let error = ''; | ||
| let loading = false; | ||
|
|
||
| async function handleLoadContract() { | ||
| loading = true; | ||
| error = ''; | ||
|
|
||
| try { | ||
| const contract = await generateContractClient(inputContractId); | ||
|
|
||
| // Optionally save the contract | ||
| if (contractName) { | ||
| contractStore.saveContract({ | ||
| contractId: contract.contractId(), | ||
| name: contractName | ||
| }); | ||
| } | ||
| contractStore.setCurrentContract(contract.contractId()); | ||
|
|
||
| } catch (err: any) { | ||
| error = err.message; | ||
| } finally { | ||
| loading = false; | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <div class="container mx-auto p-4"> | ||
| <h1 class="text-2xl font-bold mb-4">Stellar Smart Contract Interaction</h1> | ||
|
|
||
| <div class="mb-4"> | ||
| <label class="block text-sm font-medium mb-1" for="contractId"> | ||
| Contract ID | ||
| </label> | ||
| <input | ||
| id="contractId" | ||
| type="text" | ||
| bind:value={inputContractId} | ||
| placeholder="Enter contract ID (C...)" | ||
| class="w-full p-2 border rounded" | ||
| /> | ||
| </div> | ||
|
|
||
| <div class="mb-4"> | ||
| <label class="block text-sm font-medium mb-1" for="contractName"> | ||
| Contract Name (optional) | ||
| </label> | ||
| <input | ||
| id="contractName" | ||
| type="text" | ||
| bind:value={contractName} | ||
| placeholder="Enter a name for this contract" | ||
| class="w-full p-2 border rounded" | ||
| /> | ||
| </div> | ||
|
|
||
| <button | ||
| on:click={handleLoadContract} | ||
| disabled={loading} | ||
| class="bg-blue-500 text-white px-4 py-2 rounded" | ||
| > | ||
| {loading ? 'Loading...' : 'Load Contract'} | ||
| </button> | ||
|
|
||
| {#if error} | ||
| <div class="text-red-500 mt-2">{error}</div> | ||
| {/if} | ||
|
|
||
| <h2 class="text-xl font-bold mt-8 mb-4">Saved Contracts</h2> | ||
| {#each $contractStore.savedContracts as savedContract} | ||
| <div class="border p-4 rounded mb-2 flex justify-between items-center"> | ||
| <div> | ||
| <span class="font-medium">{savedContract.name || 'Unnamed Contract'}</span> | ||
| <span class="text-sm text-gray-500 block">{savedContract.contractId}</span> <!-- Correctly display saved contract ID --> | ||
| </div> | ||
| <button | ||
| on:click={() => inputContractId = savedContract.contractId} | ||
| class="bg-gray-200 px-3 py-1 rounded" | ||
| > | ||
| Load | ||
| </button> | ||
| </div> | ||
| {/each} | ||
| </div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.