Last active
June 27, 2022 20:19
-
-
Save daverickdunn/99580554d68fecc4d376dcc705829258 to your computer and use it in GitHub Desktop.
Stellar JS SDK create account and set trust-line
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
const StellarSdk = require('stellar-sdk'); | |
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork(); | |
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org'); | |
// an arbitary list of trusted assets | |
const ASSETS = { | |
'XPORT': new StellarSdk.Asset('XPORT', 'GCC4HAL7SOR7AZBF53SUTVAJT4OHYLBHOUJMM3IAF5YLFF7OVVAWLVFT'), | |
'USDT': new StellarSdk.Asset('USDT', 'GCQTGZQQ5G4PTM2GL7CDIFKUBIPEC52BROAQIAPW53XBRJVN6ZJVTG6V'), | |
'MOBI' : new StellarSdk.Asset('MOBI', 'GA6HCMBLTZS5VYYBCATRBRZ3BZJMAFUDKYYF6AH6MVCMGWMRDNSWJPIH'), | |
'XLM': StellarSdk.Asset.native() | |
} | |
// helper function to get desired asset object | |
const getAsset = (asset) => { | |
if (ASSETS[asset]) return ASSETS[asset] | |
return StellarSdk.Asset.native() | |
} | |
const options = { | |
starting_secret : 'S ...', | |
to_be_created_secret : 'S ...', | |
asset_code: 'XPORT' | |
} | |
const createAndTrust = (options) => { | |
const starting_keypair = StellarSdk.Keypair.fromSecret(options.starting_secret); | |
const to_be_created_keypair = StellarSdk.Keypair.fromSecret(options.to_be_created_secret) | |
// this transaction may or may not (see if condition below) have multiple signers | |
// it will certainly have the 'starting_keypair' as signer, so initalise an array with 'starting_keypair' included | |
const signers = [starting_keypair] | |
return server.loadAccount(starting_keypair.publicKey()) | |
.then( | |
account => { | |
const transaction = new StellarSdk.TransactionBuilder(account); | |
transaction.addOperation( | |
StellarSdk.Operation.createAccount({ | |
destination: 'G ...', | |
startingBalance: '2' // xlm | |
}) | |
); | |
if (options.asset_code != 'XLM') { // no need to set trust for XLM ... | |
transaction.addOperation( | |
StellarSdk.Operation.changeTrust({ | |
asset: getAsset(options.asset_code), // note: getAsset returns an Asset object with issuing Code+PK | |
source: to_be_created_keypair.publicKey() // note: the new account is the 'source' of this operation, it will have to be included in the transactions signers | |
}) | |
) | |
signers.push(to_be_created_keypair) // add second signer to list of signers | |
} | |
const tx = transaction.build() | |
tx.sign(...signers) // expand signers array to pass each signer as an arg | |
return server.submitTransaction(tx) | |
.then( | |
res => { console.log('Success! Account Created.') }, | |
err => { throw err } | |
) | |
} | |
); | |
} | |
createAndTrust(options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment