Skip to content

ergo transaction builder optimized for contract development

License

Notifications You must be signed in to change notification settings

nirvanush/ergoscript

Repository files navigation


Ergoscriptjs

npm package Build Status Issues Commitizen Friendly Semantic Release

Install

npm install ergoscript

Usage

Basic transaction to send token

const tx = new Transaction([
  {
    funds: {
      ERG: 100000,
      tokens: [{ tokenId: 'token id', amount: '1' }],
    },
    toAddress: 'address',
    additionalRegisters: {},
  },
]);

const unsignedTx = (await tx.build()).toJSON();

// using ergo wallet
const signedTx = await ergo.sign_tx(unsignedTx);
await ergo.submit_tx(signedTx);

Multiple recipients / airdrop

const tokenId = '<tokenId>'
const recipients = [
  { address: '<address_1>', amount: 1 },
  { address: '<address_2>', amount: 10 },
  { address: '<address_3>', amount: 100 },
  { address: '<address_4>', amount: 1000 }
]

const tx = new Transaction(recipients.map(rec => {
  return {
    funds: {
      ERG: 100000,
      tokens: [{ tokenId, amount: rec.amount }],
    },
    toAddress: rec.address,
    additionalRegisters: {},
  }
}));

const unsignedTx = (await tx.build()).toJSON();

// using ergo wallet
const signedTx = await ergo.sign_tx(unsignedTx);
await ergo.submit_tx(signedTx);

Immutability

// Methods return a new instance instead of modifying the old instance
const INPUT_0 = new eUTXOBox(tokenToRent as ExplorerBox);

// modify ergoTree value
const OUTPUT_0 = INPUT_0.sendTo(changeAddress);
OUTPUT_0.ergoTree !== INPUT_0.ergoTree;

// setRegisters - add registers and return new instance
const INPUT_0 = new eUTXOBox(tokenToRent as ExplorerBox);
const OUTPUT_0 = INPUT_0.setRegisters({
  R5: { value: price, type: Long },
  R6: { value: period, type: Long },
});

const tx = new Transaction([
  [INPUT_0, OUTPUT_0], // setting those boxes as first input and output of the transaction - handy for smart contracts
  {
    funds: {
      ERG: 100000,
      tokens: [{ tokenId: 'token id', amount: '1' }],
    },
    toAddress: 'address',
    additionalRegisters: {},
  },
]);

// reset registers - remove all registers and return new instance
const OUTPUT_0 = INPUT_0.resetRegisters();

Test script with Mocha test

import buildScriptScope from 'ergoscript/lib/ergoscriptMock';

const script = `sigmaProp(true)`;

const tx = new Transaction([
  {
    funds: {
      ERG: 100000,
      tokens: [{ tokenId: 'token id', amount: '1' }],
    },
    toAddress: 'address',
    additionalRegisters: {},
  },
]);

describe('Rentring transaction', () => {
  it('reduces to true', async () => {
    const txBuilt = await tx.build();

    const simulator = await buildScriptScope(txBuilt);
    const response = simulator.execute(script);

    expect(response).to.be.true;
  });
});

About

ergo transaction builder optimized for contract development

Resources

License

Stars

Watchers

Forks

Packages

No packages published