https://github.com/nirvanush/ergoscript
ergo transaction builder optimized for contract development
https://github.com/nirvanush/ergoscript
Last synced: 5 days ago
JSON representation
ergo transaction builder optimized for contract development
- Host: GitHub
- URL: https://github.com/nirvanush/ergoscript
- Owner: nirvanush
- License: mit
- Created: 2022-06-03T18:56:15.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-05T17:30:49.000Z (almost 3 years ago)
- Last Synced: 2025-04-07T16:08:49.559Z (29 days ago)
- Language: TypeScript
- Size: 904 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ergo - Ergoscript Language Tools (nirvanush) - Tools related to the ErgoScript language. *(Community)* (🛠️ Development Tooling <a id="development-tooling"></a> / 📜 Smart Contracts & ErgoScript <a id="smart-contracts--ergoscript"></a>)
README
---
# Ergoscriptjs
[![npm package][npm-img]][npm-url]
[![Build Status][build-img]][build-url]
[![Issues][issues-img]][issues-url]
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]## Install
```bash
npm install ergoscript
```## Usage
### Basic transaction to send token
```ts
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
```ts
const tokenId = ''
const recipients = [
{ address: '', amount: 1 },
{ address: '', amount: 10 },
{ address: '', amount: 100 },
{ address: '', 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
```ts
// 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
```ts
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;
});
});
```[build-img]: https://github.com/nirvanush/ergoscript/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/nirvanush/ergoscript/actions/workflows/release.yml
[npm-img]: https://img.shields.io/npm/v/ergoscript
[npm-url]: https://www.npmjs.com/package/ergoscript
[issues-img]: https://img.shields.io/github/issues/nirvanush/ergoscript
[issues-url]: https://github.com/nirvanush/ergoscript/issues
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
[commitizen-url]: http://commitizen.github.io/cz-cli/