Skip to content
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

feat(sdk-coin-flr): add transaction builder #5412

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/sdk-coin-flr/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.idea
public
dist

3 changes: 3 additions & 0 deletions modules/sdk-coin-flr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.idea/
dist/
8 changes: 8 additions & 0 deletions modules/sdk-coin-flr/.mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require: 'ts-node/register'
timeout: '120000'
reporter: 'min'
reporter-option:
- 'cdn=true'
- 'json=false'
exit: true
spec: ['test/unit/**/*.ts']
14 changes: 14 additions & 0 deletions modules/sdk-coin-flr/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
!dist/
dist/test/
dist/tsconfig.tsbuildinfo
.idea/
.prettierrc.yml
tsconfig.json
src/
test/
scripts/
.nyc_output
CODEOWNERS
node_modules/
.prettierignore
.mocharc.js
2 changes: 2 additions & 0 deletions modules/sdk-coin-flr/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.nyc_output/
dist/
3 changes: 3 additions & 0 deletions modules/sdk-coin-flr/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printWidth: 120
singleQuote: true
trailingComma: 'es5'
8 changes: 8 additions & 0 deletions modules/sdk-coin-flr/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

**Note:** Version bump only for package @bitgo/sdk-coin-flr

### Features
30 changes: 30 additions & 0 deletions modules/sdk-coin-flr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# BitGo sdk-coin-flr

SDK coins provide a modular approach to a monolithic architecture. This and all BitGoJS SDK coins allow developers to use only the coins needed for a given project.

## Installation

All coins are loaded traditionally through the `bitgo` package. If you are using coins individually, you will be accessing the coin via the `@bitgo/sdk-api` package.

In your project install both `@bitgo/sdk-api` and `@bitgo/sdk-coin-flr`.

```shell
npm i @bitgo/sdk-api @bitgo/sdk-coin-flr
```

Next, you will be able to initialize an instance of "bitgo" through `@bitgo/sdk-api` instead of `bitgo`.

```javascript
import { BitGoAPI } from '@bitgo/sdk-api';
import { flr } from '@bitgo/sdk-coin-flr';

const sdk = new BitGoAPI();

sdk.register('flr', flr.createInstance);
```

## Development

Most of the coin implementations are derived from `@bitgo/sdk-core`, `@bitgo/statics`, and coin specific packages. These implementations are used to interact with the BitGo API and BitGo platform services.

You will notice that the basic version of common class extensions have been provided to you and must be resolved before the package build will succeed. Upon initiation of a given SDK coin, you will need to verify that your coin has been included in the root `tsconfig.packages.json` and that the linting, formatting, and testing succeeds when run both within the coin and from the root of BitGoJS.
52 changes: 52 additions & 0 deletions modules/sdk-coin-flr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@bitgo/sdk-coin-flr",
"version": "1.0.0",
"description": "BitGo SDK coin library for Flr",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"scripts": {
"build": "yarn tsc --build --incremental --verbose .",
"fmt": "prettier --write .",
"check-fmt": "prettier --check .",
"clean": "rm -r ./dist",
"lint": "eslint --quiet .",
"prepare": "npm run build",
"test": "npm run coverage",
"coverage": "nyc -- npm run unit-test",
"unit-test": "mocha"
},
"author": "BitGo SDK Team <[email protected]>",
"license": "MIT",
"engines": {
"node": ">=18 <21"
},
"repository": {
"type": "git",
"url": "https://github.com/BitGo/BitGoJS.git",
"directory": "modules/sdk-coin-flr"
},
"lint-staged": {
"*.{js,ts}": [
"yarn prettier --write",
"yarn eslint --fix"
]
},
"publishConfig": {
"access": "public"
},
"nyc": {
"extension": [
".ts"
]
},
"dependencies": {
"@bitgo/abstract-eth": "^22.4.10",
"@bitgo/sdk-core": "^28.20.0",
"@bitgo/statics": "^50.20.0",
"@ethereumjs/common": "^2.6.5"
},
"devDependencies": {
"@bitgo/sdk-api": "^1.58.2",
"@bitgo/sdk-test": "^8.0.65"
}
}
43 changes: 43 additions & 0 deletions modules/sdk-coin-flr/src/flr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/***
* @developerReferences
* - Developer Hub: https://dev.flare.network/
* - Doc: https://docs.flare.network/user/wallets/
*
* @coinFullName Flare
* @coinWebsite https://flare-explorer.flare.network
*/

import { BaseCoin, BitGoBase, common, MPCAlgorithm } from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
import { AbstractEthLikeNewCoins, recoveryBlockchainExplorerQuery } from '@bitgo/abstract-eth';
import { TransactionBuilder } from './lib';

export class Flr extends AbstractEthLikeNewCoins {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add jsdoc

full name

website of the coin

developer references

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo, staticsCoin);
}

static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
return new Flr(bitgo, staticsCoin);
}

protected getTransactionBuilder(): TransactionBuilder {
return new TransactionBuilder(coins.get(this.getBaseChain()));
}

/** @inheritDoc */
supportsTss(): boolean {
return true;
}

/** @inheritDoc */
getMPCAlgorithm(): MPCAlgorithm {
return 'ecdsa';
}

async recoveryBlockchainExplorerQuery(query: Record<string, string>): Promise<Record<string, unknown>> {
const apiToken = common.Environments[this.bitgo.getEnv()].flrExplorerApiToken;
const explorerUrl = common.Environments[this.bitgo.getEnv()].flrExplorerBaseUrl;
return await recoveryBlockchainExplorerQuery(query, explorerUrl as string, apiToken);
}
}
4 changes: 4 additions & 0 deletions modules/sdk-coin-flr/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib';
export * from './flr';
export * from './tflr';
export * from './register';
6 changes: 6 additions & 0 deletions modules/sdk-coin-flr/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as Utils from './utils';

export { TransactionBuilder } from './transactionBuilder';
export { TransferBuilder } from './transferBuilder';
export { Transaction, KeyPair } from '@bitgo/abstract-eth';
export { Utils };
28 changes: 28 additions & 0 deletions modules/sdk-coin-flr/src/lib/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import EthereumCommon from '@ethereumjs/common';
import { coins, EthereumNetwork } from '@bitgo/statics';

export const testnetCommon = EthereumCommon.custom(
{
name: 'flr testnet',
networkId: (coins.get('tflr').network as EthereumNetwork).chainId,
chainId: (coins.get('tflr').network as EthereumNetwork).chainId,
},
{
baseChain: 'sepolia',
hardfork: 'london',
eips: [1559],
}
);

export const mainnetCommon = EthereumCommon.custom(
{
name: 'flr mainnet',
networkId: (coins.get('flr').network as EthereumNetwork).chainId,
chainId: (coins.get('flr').network as EthereumNetwork).chainId,
},
{
baseChain: 'mainnet',
hardfork: 'london',
eips: [1559],
}
);
30 changes: 30 additions & 0 deletions modules/sdk-coin-flr/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import { TransactionBuilder as AbstractTransactionBuilder, Transaction } from '@bitgo/abstract-eth';
import { getCommon } from './utils';
import { TransferBuilder } from './transferBuilder';

export class TransactionBuilder extends AbstractTransactionBuilder {
protected _transfer: TransferBuilder;

constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
this._common = getCommon(this._coinConfig.network.type);
this.transaction = new Transaction(this._coinConfig, this._common);
}

/** @inheritdoc */
transfer(data?: string): TransferBuilder {
if (this._type !== TransactionType.Send) {
throw new BuildTransactionError('Transfers can only be set for send transactions');
}
if (!this._transfer) {
this._transfer = new TransferBuilder(data);
}
return this._transfer;
}

protected getContractData(addresses: string[]): string {
throw new Error('Method not implemented.');
}
}
1 change: 1 addition & 0 deletions modules/sdk-coin-flr/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TransferBuilder } from '@bitgo/abstract-eth';
21 changes: 21 additions & 0 deletions modules/sdk-coin-flr/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NetworkType } from '@bitgo/statics';
import EthereumCommon from '@ethereumjs/common';
import { InvalidTransactionError } from '@bitgo/sdk-core';
import { testnetCommon, mainnetCommon } from './resources';

const commons: Map<NetworkType, EthereumCommon> = new Map<NetworkType, EthereumCommon>([
[NetworkType.MAINNET, mainnetCommon],
[NetworkType.TESTNET, testnetCommon],
]);

/**
* @param {NetworkType} network either mainnet or testnet
* @returns {EthereumCommon} Ethereum common configuration object
*/
export function getCommon(network: NetworkType): EthereumCommon {
const common = commons.get(network);
if (!common) {
throw new InvalidTransactionError('Missing network common configuration');
}
return common;
}
8 changes: 8 additions & 0 deletions modules/sdk-coin-flr/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BitGoBase } from '@bitgo/sdk-core';
import { Flr } from './flr';
import { Tflr } from './tflr';

export const register = (sdk: BitGoBase): void => {
sdk.register('flr', Flr.createInstance);
sdk.register('tflr', Tflr.createInstance);
};
18 changes: 18 additions & 0 deletions modules/sdk-coin-flr/src/tflr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Testnet Flr
*
* @format
*/
import { BaseCoin, BitGoBase } from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
import { Flr } from './flr';

export class Tflr extends Flr {
protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo, staticsCoin);
}

static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
return new Tflr(bitgo, staticsCoin);
}
}
42 changes: 42 additions & 0 deletions modules/sdk-coin-flr/test/unit/flr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'should';

import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
import { BitGoAPI } from '@bitgo/sdk-api';

import { Flr, Tflr } from '../../src/index';

const bitgo: TestBitGoAPI = TestBitGo.decorate(BitGoAPI, { env: 'test' });

describe('flr', function () {
before(function () {
bitgo.safeRegister('flr', Flr.createInstance);
bitgo.safeRegister('tflr', Tflr.createInstance);
bitgo.initializeTestVars();
});

describe('Basic Coin Info', function () {
it('should return the right info for flr', function () {
const flr = bitgo.coin('flr');

flr.should.be.an.instanceof(Flr);
flr.getChain().should.equal('flr');
flr.getFamily().should.equal('flr');
flr.getFullName().should.equal('flare');
flr.getBaseFactor().should.equal(1e18);
flr.supportsTss().should.equal(true);
flr.allowsAccountConsolidations().should.equal(false);
});

it('should return the right info for tflr', function () {
const tflr = bitgo.coin('tflr');

tflr.should.be.an.instanceof(Tflr);
tflr.getChain().should.equal('tflr');
tflr.getFamily().should.equal('flr');
tflr.getFullName().should.equal('Testnet flare chain');
tflr.getBaseFactor().should.equal(1e18);
tflr.supportsTss().should.equal(true);
tflr.allowsAccountConsolidations().should.equal(false);
});
});
});
6 changes: 6 additions & 0 deletions modules/sdk-coin-flr/test/unit/getBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TransactionBuilder } from '../../src';
import { coins } from '@bitgo/statics';

export const getBuilder = (coin: string): TransactionBuilder => {
return new TransactionBuilder(coins.get(coin));
};
16 changes: 16 additions & 0 deletions modules/sdk-coin-flr/test/unit/transactionBuilder/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getBuilder } from '../getBuilder';
import should from 'should';

describe('Flr Transfer Builder', () => {
describe('Build from TxHex', function () {
it('Should successfully build from txHex', async function () {
const txBuilder = getBuilder('tflr');
const txHex =
'0xf86e038505d21dba00825208944943dd2a2494e3ea5937954cb836692a047695b5880de0b6b3a764000080820108a07fdc8942a7230a91022492180290b10ab9f50c14836966e7e0b6a25ce8c3fedea06d3ce650b2bd7117a5315b10c6054f5f1960fbeda4399d6a34b2f6ca6c68fd8e';
txBuilder.from(txHex);
const parsedTx = await txBuilder.build();

should.exist(parsedTx.toJson());
});
});
});
29 changes: 29 additions & 0 deletions modules/sdk-coin-flr/test/unit/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from 'assert';
import should from 'should';
import { NetworkType } from '@bitgo/statics';
import { getCommon } from '../../src/lib/utils';

describe('Network Common Configuration', () => {
it('getCommon for mainnet', () => {
const common = getCommon(NetworkType.MAINNET);
should.equal(common.chainName(), 'flr mainnet');
should.equal(common.hardfork(), 'london');
should.equal(common.chainIdBN().toString(), '14');
should.equal(common.networkIdBN().toString(), '14');
});

it('getCommon for testnet', () => {
const common = getCommon(NetworkType.TESTNET);
should.equal(common.chainName(), 'flr testnet');
should.equal(common.hardfork(), 'london');
should.equal(common.chainIdBN().toString(), '114');
should.equal(common.networkIdBN().toString(), '114');
});

it('getCommon for invalid network', () => {
assert.throws(
() => getCommon('invalidNetwork' as NetworkType),
(e: any) => e.message === 'Missing network common configuration'
);
});
});
Loading
Loading