From 3a69830ad63b6b6b0035a4a954fa834372239f16 Mon Sep 17 00:00:00 2001 From: yogeshwar-bitgo Date: Thu, 23 Jan 2025 09:31:46 +0530 Subject: [PATCH] feat(sdk-coin-flr): add transaction builder Ticket: WIN-4179 --- modules/sdk-coin-flr/.eslintignore | 5 ++ modules/sdk-coin-flr/.gitignore | 3 ++ modules/sdk-coin-flr/.mocharc.yml | 8 +++ modules/sdk-coin-flr/.npmignore | 14 +++++ modules/sdk-coin-flr/.prettierignore | 2 + modules/sdk-coin-flr/.prettierrc.yml | 3 ++ modules/sdk-coin-flr/CHANGELOG.md | 8 +++ modules/sdk-coin-flr/README.md | 30 +++++++++++ modules/sdk-coin-flr/package.json | 52 +++++++++++++++++++ modules/sdk-coin-flr/src/flr.ts | 43 +++++++++++++++ modules/sdk-coin-flr/src/index.ts | 4 ++ modules/sdk-coin-flr/src/lib/index.ts | 6 +++ modules/sdk-coin-flr/src/lib/resources.ts | 28 ++++++++++ .../src/lib/transactionBuilder.ts | 30 +++++++++++ .../sdk-coin-flr/src/lib/transferBuilder.ts | 1 + modules/sdk-coin-flr/src/lib/utils.ts | 21 ++++++++ modules/sdk-coin-flr/src/register.ts | 8 +++ modules/sdk-coin-flr/src/tflr.ts | 18 +++++++ modules/sdk-coin-flr/test/unit/flr.ts | 42 +++++++++++++++ modules/sdk-coin-flr/test/unit/getBuilder.ts | 6 +++ .../test/unit/transactionBuilder/send.ts | 16 ++++++ modules/sdk-coin-flr/test/unit/utils.ts | 29 +++++++++++ modules/sdk-coin-flr/tsconfig.json | 29 +++++++++++ modules/sdk-core/src/bitgo/environments.ts | 2 + 24 files changed, 408 insertions(+) create mode 100644 modules/sdk-coin-flr/.eslintignore create mode 100644 modules/sdk-coin-flr/.gitignore create mode 100644 modules/sdk-coin-flr/.mocharc.yml create mode 100644 modules/sdk-coin-flr/.npmignore create mode 100644 modules/sdk-coin-flr/.prettierignore create mode 100644 modules/sdk-coin-flr/.prettierrc.yml create mode 100644 modules/sdk-coin-flr/CHANGELOG.md create mode 100644 modules/sdk-coin-flr/README.md create mode 100644 modules/sdk-coin-flr/package.json create mode 100644 modules/sdk-coin-flr/src/flr.ts create mode 100644 modules/sdk-coin-flr/src/index.ts create mode 100644 modules/sdk-coin-flr/src/lib/index.ts create mode 100644 modules/sdk-coin-flr/src/lib/resources.ts create mode 100644 modules/sdk-coin-flr/src/lib/transactionBuilder.ts create mode 100644 modules/sdk-coin-flr/src/lib/transferBuilder.ts create mode 100644 modules/sdk-coin-flr/src/lib/utils.ts create mode 100644 modules/sdk-coin-flr/src/register.ts create mode 100644 modules/sdk-coin-flr/src/tflr.ts create mode 100644 modules/sdk-coin-flr/test/unit/flr.ts create mode 100644 modules/sdk-coin-flr/test/unit/getBuilder.ts create mode 100644 modules/sdk-coin-flr/test/unit/transactionBuilder/send.ts create mode 100644 modules/sdk-coin-flr/test/unit/utils.ts create mode 100644 modules/sdk-coin-flr/tsconfig.json diff --git a/modules/sdk-coin-flr/.eslintignore b/modules/sdk-coin-flr/.eslintignore new file mode 100644 index 0000000000..190f83e0df --- /dev/null +++ b/modules/sdk-coin-flr/.eslintignore @@ -0,0 +1,5 @@ +node_modules +.idea +public +dist + diff --git a/modules/sdk-coin-flr/.gitignore b/modules/sdk-coin-flr/.gitignore new file mode 100644 index 0000000000..67ccce4c64 --- /dev/null +++ b/modules/sdk-coin-flr/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +.idea/ +dist/ diff --git a/modules/sdk-coin-flr/.mocharc.yml b/modules/sdk-coin-flr/.mocharc.yml new file mode 100644 index 0000000000..7b4b0d51f4 --- /dev/null +++ b/modules/sdk-coin-flr/.mocharc.yml @@ -0,0 +1,8 @@ +require: 'ts-node/register' +timeout: '120000' +reporter: 'min' +reporter-option: + - 'cdn=true' + - 'json=false' +exit: true +spec: ['test/unit/**/*.ts'] diff --git a/modules/sdk-coin-flr/.npmignore b/modules/sdk-coin-flr/.npmignore new file mode 100644 index 0000000000..d5fb3a098c --- /dev/null +++ b/modules/sdk-coin-flr/.npmignore @@ -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 diff --git a/modules/sdk-coin-flr/.prettierignore b/modules/sdk-coin-flr/.prettierignore new file mode 100644 index 0000000000..3a11d6af29 --- /dev/null +++ b/modules/sdk-coin-flr/.prettierignore @@ -0,0 +1,2 @@ +.nyc_output/ +dist/ diff --git a/modules/sdk-coin-flr/.prettierrc.yml b/modules/sdk-coin-flr/.prettierrc.yml new file mode 100644 index 0000000000..7c3d8dd32a --- /dev/null +++ b/modules/sdk-coin-flr/.prettierrc.yml @@ -0,0 +1,3 @@ +printWidth: 120 +singleQuote: true +trailingComma: 'es5' diff --git a/modules/sdk-coin-flr/CHANGELOG.md b/modules/sdk-coin-flr/CHANGELOG.md new file mode 100644 index 0000000000..770ce10fe1 --- /dev/null +++ b/modules/sdk-coin-flr/CHANGELOG.md @@ -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 diff --git a/modules/sdk-coin-flr/README.md b/modules/sdk-coin-flr/README.md new file mode 100644 index 0000000000..adc9b93b6d --- /dev/null +++ b/modules/sdk-coin-flr/README.md @@ -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. diff --git a/modules/sdk-coin-flr/package.json b/modules/sdk-coin-flr/package.json new file mode 100644 index 0000000000..c10685f777 --- /dev/null +++ b/modules/sdk-coin-flr/package.json @@ -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 ", + "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" + } +} diff --git a/modules/sdk-coin-flr/src/flr.ts b/modules/sdk-coin-flr/src/flr.ts new file mode 100644 index 0000000000..075deefe1f --- /dev/null +++ b/modules/sdk-coin-flr/src/flr.ts @@ -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 { + protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly) { + super(bitgo, staticsCoin); + } + + static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly): 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): Promise> { + const apiToken = common.Environments[this.bitgo.getEnv()].flrExplorerApiToken; + const explorerUrl = common.Environments[this.bitgo.getEnv()].flrExplorerBaseUrl; + return await recoveryBlockchainExplorerQuery(query, explorerUrl as string, apiToken); + } +} diff --git a/modules/sdk-coin-flr/src/index.ts b/modules/sdk-coin-flr/src/index.ts new file mode 100644 index 0000000000..6debfc7f9a --- /dev/null +++ b/modules/sdk-coin-flr/src/index.ts @@ -0,0 +1,4 @@ +export * from './lib'; +export * from './flr'; +export * from './tflr'; +export * from './register'; diff --git a/modules/sdk-coin-flr/src/lib/index.ts b/modules/sdk-coin-flr/src/lib/index.ts new file mode 100644 index 0000000000..20f9e761a1 --- /dev/null +++ b/modules/sdk-coin-flr/src/lib/index.ts @@ -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 }; diff --git a/modules/sdk-coin-flr/src/lib/resources.ts b/modules/sdk-coin-flr/src/lib/resources.ts new file mode 100644 index 0000000000..0777a2fa80 --- /dev/null +++ b/modules/sdk-coin-flr/src/lib/resources.ts @@ -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], + } +); diff --git a/modules/sdk-coin-flr/src/lib/transactionBuilder.ts b/modules/sdk-coin-flr/src/lib/transactionBuilder.ts new file mode 100644 index 0000000000..1232e4f26b --- /dev/null +++ b/modules/sdk-coin-flr/src/lib/transactionBuilder.ts @@ -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) { + 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.'); + } +} diff --git a/modules/sdk-coin-flr/src/lib/transferBuilder.ts b/modules/sdk-coin-flr/src/lib/transferBuilder.ts new file mode 100644 index 0000000000..7447c0cf3d --- /dev/null +++ b/modules/sdk-coin-flr/src/lib/transferBuilder.ts @@ -0,0 +1 @@ +export { TransferBuilder } from '@bitgo/abstract-eth'; diff --git a/modules/sdk-coin-flr/src/lib/utils.ts b/modules/sdk-coin-flr/src/lib/utils.ts new file mode 100644 index 0000000000..dc81fa08f9 --- /dev/null +++ b/modules/sdk-coin-flr/src/lib/utils.ts @@ -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 = new Map([ + [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; +} diff --git a/modules/sdk-coin-flr/src/register.ts b/modules/sdk-coin-flr/src/register.ts new file mode 100644 index 0000000000..d8aaccd508 --- /dev/null +++ b/modules/sdk-coin-flr/src/register.ts @@ -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); +}; diff --git a/modules/sdk-coin-flr/src/tflr.ts b/modules/sdk-coin-flr/src/tflr.ts new file mode 100644 index 0000000000..455ad21558 --- /dev/null +++ b/modules/sdk-coin-flr/src/tflr.ts @@ -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) { + super(bitgo, staticsCoin); + } + + static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly): BaseCoin { + return new Tflr(bitgo, staticsCoin); + } +} diff --git a/modules/sdk-coin-flr/test/unit/flr.ts b/modules/sdk-coin-flr/test/unit/flr.ts new file mode 100644 index 0000000000..0639150cff --- /dev/null +++ b/modules/sdk-coin-flr/test/unit/flr.ts @@ -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); + }); + }); +}); diff --git a/modules/sdk-coin-flr/test/unit/getBuilder.ts b/modules/sdk-coin-flr/test/unit/getBuilder.ts new file mode 100644 index 0000000000..261b490989 --- /dev/null +++ b/modules/sdk-coin-flr/test/unit/getBuilder.ts @@ -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)); +}; diff --git a/modules/sdk-coin-flr/test/unit/transactionBuilder/send.ts b/modules/sdk-coin-flr/test/unit/transactionBuilder/send.ts new file mode 100644 index 0000000000..0f8bbce681 --- /dev/null +++ b/modules/sdk-coin-flr/test/unit/transactionBuilder/send.ts @@ -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()); + }); + }); +}); diff --git a/modules/sdk-coin-flr/test/unit/utils.ts b/modules/sdk-coin-flr/test/unit/utils.ts new file mode 100644 index 0000000000..14906153de --- /dev/null +++ b/modules/sdk-coin-flr/test/unit/utils.ts @@ -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' + ); + }); +}); diff --git a/modules/sdk-coin-flr/tsconfig.json b/modules/sdk-coin-flr/tsconfig.json new file mode 100644 index 0000000000..52c51f35c6 --- /dev/null +++ b/modules/sdk-coin-flr/tsconfig.json @@ -0,0 +1,29 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./", + "strictPropertyInitialization": false, + "esModuleInterop": true, + "typeRoots": ["../../types", "./node_modules/@types", "../../node_modules/@types"] + }, + "include": ["src/**/*", "test/**/*"], + "exclude": ["node_modules"], + "references": [ + { + "path": "../abstract-eth" + }, + { + "path": "../sdk-api" + }, + { + "path": "../sdk-core" + }, + { + "path": "../statics" + }, + { + "path": "../sdk-test" + } + ] +} diff --git a/modules/sdk-core/src/bitgo/environments.ts b/modules/sdk-core/src/bitgo/environments.ts index 8263eadc8d..c52d7da7d4 100644 --- a/modules/sdk-core/src/bitgo/environments.ts +++ b/modules/sdk-core/src/bitgo/environments.ts @@ -61,6 +61,8 @@ interface EnvironmentTemplate { coredaoExplorerApiToken?: string; xdcExplorerBaseUrl?: string; xdcExplorerApiToken?: string; + flrExplorerBaseUrl?: string; + flrExplorerApiToken?: string; } export interface Environment extends EnvironmentTemplate {