Transaction Examples  [XDB DigitalBits Base]

Creating an account

In the example below a new account is created by the source account with secret SA3W53XXG64ITFFIYQSBIJDG26LMXYRIMEVMNQMFAQJOYCZACCYBA34L. The source account is giving the new account 25 XDB as its initial balance.

const server = new DigitalBitsSdk.Server('https://frontier.testnet.digitalbits.io')
const source = DigitalBitsSdk.Keypair.fromSecret('SA3W53XXG64ITFFIYQSBIJDG26LMXYRIMEVMNQMFAQJOYCZACCYBA34L')
const destination = DigitalBitsSdk.Keypair.random()

server.accounts()
  .accountId(source.publicKey())
  .call()
  .then(({ sequence }) => {
    const account = new DigitalBitsSdk.Account(source.publicKey(), sequence)
    const transaction = new DigitalBitsSdk.TransactionBuilder(account, {
      fee: DigitalBitsSdk.BASE_FEE,
      networkPassphrase: DigitalBitsSdk.Networks.TESTNET
    })
      .addOperation(DigitalBitsSdk.Operation.createAccount({
        destination: destination.publicKey(),
        startingBalance: '25'
      }))
      .setTimeout(30)
      .build()
    transaction.sign(DigitalBitsSdk.Keypair.fromSecret(source.secret()))
    return server.submitTransaction(transaction)
  })
  .then(results => {
    console.log('Transaction', results._links.transaction.href)
    console.log('New Keypair', destination.publicKey(), destination.secret())
  })

Assets

Object of the Asset class represents an asset in the DigitalBits network. Right now there are 3 possible types of assets in the DigitalBits network:

  • native XDB asset (ASSET_TYPE_NATIVE),
  • issued assets with asset code of maximum 4 characters (ASSET_TYPE_CREDIT_ALPHANUM4),
  • issued assets with asset code of maximum 12 characters (ASSET_TYPE_CREDIT_ALPHANUM12).

To create a new native asset representation use static native() method:

var nativeAsset = DigitalBitsSdk.Asset.native();
var isNative = nativeAsset.isNative(); // true

To represent an issued asset you need to create a new object of type Asset with an asset code and issuer:

// Creates TEST asset issued by GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB
var testAsset = new DigitalBitsSdk.Asset('TEST', 'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB');
var isNative = testAsset.isNative(); // false
// Creates Google stock asset issued by GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB
var googleStockAsset = new DigitalBitsSdk.Asset('US38259P7069', 'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB');

Path payment

In the example below we’re sending 1000 XDB (at max) from GABJLI6IVBKJ7HIC5NN7HHDCIEW3CMWQ2DWYHREQQUFWSWZ2CDAMZZX4 to GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB. Destination Asset will be GBP issued by GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW. Assets will be exchanged using the following path:

  • USD issued by GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB,
  • EUR issued by GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL.

The path payment will cause the destination address to get 5.5 GBP. It will cost the sender no more than 1000 XDB. In this example there will be 3 exchanges, XDB -> USD, USD-> EUR, EUR->GBP.

var keypair = DigitalBitsSdk.Keypair.fromSecret(secretString);

var source = new DigitalBitsSdk.Account(keypair.publicKey(), "46316927324160");
var transaction = new DigitalBitsSdk.TransactionBuilder(source, {
    fee: DigitalBitsSdk.BASE_FEE,
    networkPassphrase: DigitalBitsSdk.Networks.TESTNET
  })
  .addOperation(DigitalBitsSdk.Operation.pathPayment({
      sendAsset: DigitalBitsSdk.Asset.native(),
      sendMax: "1000",
      destination: 'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB',
      destAsset: new DigitalBitsSdk.Asset('GBP', 'GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW'),
      destAmount: "5.50",
      path: [
        new DigitalBitsSdk.Asset('USD', 'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB'),
        new DigitalBitsSdk.Asset('EUR', 'GDTNXRLOJD2YEBPKK7KCMR7J33AAG5VZXHAJTHIG736D6LVEFLLLKPDL')
      ]
  }))
  .setTimeout(30)
  .build();

transaction.sign(keypair);

Multi-signature account

Multi-signature accounts can be used to require that transactions require multiple public keys to sign before they are considered valid. This is done by first configuring your account’s “threshold” levels. Each operation has a threshold level of either low, medium, or high. You give each threshold level a number between 1-255 in your account. Then, for each key in your account, you assign it a weight (1-255, setting a 0 weight deletes the key). Any transaction must be signed with enough keys to meet the threshold.

For example, let’s say you set your threshold levels; low = 1, medium = 2, high = 3. You want to send a payment operation, which is a medium threshold operation. Your master key has weight 1. Additionally, you have a secondary key associated with your account which has a weight of 1. Now, the transaction you submit for this payment must include both signatures of your master key and secondary key since their combined weight is 2 which is enough to authorize the payment operation.

In this example, we will:

  • Add a second signer to the account
  • Set our account’s masterkey weight and threshold levels
  • Create a multi signature transaction that sends a payment

In each example, we’ll use the root account.

Set up multisig account

var rootKeypair = DigitalBitsSdk.Keypair.fromSecret("SBQWY3DNPFWGSZTFNV4WQZLBOJ2GQYLTMJSWK3TTMVQXEY3INFXGO52X")
var account = new DigitalBitsSdk.Account(rootkeypair.publicKey(), "46316927324160");

var secondaryAddress = "GC6HHHS7SH7KNUAOBKVGT2QZIQLRB5UA7QAGLA3IROWPH4TN65UKNJPK";

var transaction = new DigitalBitsSdk.TransactionBuilder(account, {
    fee: DigitalBitsSdk.BASE_FEE,
    networkPassphrase: DigitalBitsSdk.Networks.TESTNET
  })
  .addOperation(DigitalBitsSdk.Operation.setOptions({
    signer: {
      ed25519PublicKey: secondaryAddress,
      weight: 1
    }
  }))
  .addOperation(DigitalBitsSdk.Operation.setOptions({
    masterWeight: 1, // set master key weight
    lowThreshold: 1,
    medThreshold: 2, // a payment is medium threshold
    highThreshold: 2 // make sure to have enough weight to add up to the high threshold!
  }))
  .setTimeout(30)
  .build();

transaction.sign(rootKeypair); // only need to sign with the root signer as the 2nd signer won't be added to the account till after this transaction completes

// now create a payment with the account that has two signers

var transaction = new DigitalBitsSdk.TransactionBuilder(account, {
      fee: DigitalBitsSdk.BASE_FEE,
      networkPassphrase: DigitalBitsSdk.Networks.TESTNET
    })
    .addOperation(DigitalBitsSdk.Operation.payment({
        destination: "GBTVUCDT5CNSXIHJTDHYSZG3YJFXBAJ6FM4CKS5GKSAWJOLZW6XX7NVC",
        asset: DigitalBitsSdk.Asset.native(),
        amount: "2000" // 2000 XDB
    }))
    .setTimeout(30)
    .build();

var secondKeypair = DigitalBitsSdk.Keypair.fromSecret("SAMZUAAPLRUH62HH3XE7NVD6ZSMTWPWGM6DS4X47HLVRHEBKP4U2H5E7");

// now we need to sign the transaction with both the root and the secondaryAddress
transaction.sign(rootKeypair);
transaction.sign(secondKeypair);

Edit this doc in GitHub

THE FUTURE OF LOYALTY STARTS HERE

Stay up to date on the latest happenings at DigitalBits. Get the latest news.