Skip to content

Commit

Permalink
Merge pull request #3976 from BitGo/WIN-695-add-opeth-skeleton
Browse files Browse the repository at this point in the history
feat(sdk-coin-opeth): add opeth sdk skeleton
  • Loading branch information
gianchandania authored Oct 17, 2023
2 parents 935f4f5 + 42fbefa commit 2fe58df
Show file tree
Hide file tree
Showing 28 changed files with 353 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ COPY --from=builder /tmp/bitgo/modules/sdk-coin-hbar /var/modules/sdk-coin-hbar/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-injective /var/modules/sdk-coin-injective/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-islm /var/modules/sdk-coin-islm/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-near /var/modules/sdk-coin-near/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-opeth /var/modules/sdk-coin-opeth/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-osmo /var/modules/sdk-coin-osmo/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-polygon /var/modules/sdk-coin-polygon/
COPY --from=builder /tmp/bitgo/modules/sdk-coin-rbtc /var/modules/sdk-coin-rbtc/
Expand Down Expand Up @@ -138,6 +139,7 @@ cd /var/modules/sdk-coin-hbar && yarn link && \
cd /var/modules/sdk-coin-injective && yarn link && \
cd /var/modules/sdk-coin-islm && yarn link && \
cd /var/modules/sdk-coin-near && yarn link && \
cd /var/modules/sdk-coin-opeth && yarn link && \
cd /var/modules/sdk-coin-osmo && yarn link && \
cd /var/modules/sdk-coin-polygon && yarn link && \
cd /var/modules/sdk-coin-rbtc && yarn link && \
Expand Down Expand Up @@ -205,6 +207,7 @@ RUN cd /var/bitgo-express && \
yarn link @bitgo/sdk-coin-injective && \
yarn link @bitgo/sdk-coin-islm && \
yarn link @bitgo/sdk-coin-near && \
yarn link @bitgo/sdk-coin-opeth && \
yarn link @bitgo/sdk-coin-osmo && \
yarn link @bitgo/sdk-coin-polygon && \
yarn link @bitgo/sdk-coin-rbtc && \
Expand Down
1 change: 1 addition & 0 deletions modules/account-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@bitgo/sdk-coin-injective": "^1.4.11",
"@bitgo/sdk-coin-islm": "^1.0.0",
"@bitgo/sdk-coin-near": "^1.6.11",
"@bitgo/sdk-coin-opeth": "^1.0.0",
"@bitgo/sdk-coin-osmo": "^1.6.11",
"@bitgo/sdk-coin-polygon": "^1.8.0",
"@bitgo/sdk-coin-rbtc": "^1.5.11",
Expand Down
5 changes: 5 additions & 0 deletions modules/account-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export { Bsc };
import * as Polygon from '@bitgo/sdk-coin-polygon';
export { Polygon };

import * as Opeth from '@bitgo/sdk-coin-opeth';
export { Opeth };

import * as Sui from '@bitgo/sdk-coin-sui';
export { Sui };

Expand Down Expand Up @@ -180,6 +183,8 @@ const coinBuilderMap = {
tcore: Core.TransactionBuilderFactory,
arbeth: Arbeth.TransactionBuilder,
tarbeth: Arbeth.TransactionBuilder,
opeth: Opeth.TransactionBuilder,
topeth: Opeth.TransactionBuilder,
};

/**
Expand Down
3 changes: 3 additions & 0 deletions modules/account-lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
{
"path": "../sdk-coin-near"
},
{
"path": "../sdk-coin-opeth"
},
{
"path": "../sdk-coin-osmo"
},
Expand Down
1 change: 1 addition & 0 deletions modules/bitgo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@bitgo/sdk-coin-islm": "^1.0.0",
"@bitgo/sdk-coin-ltc": "^2.2.11",
"@bitgo/sdk-coin-near": "^1.6.11",
"@bitgo/sdk-coin-opeth": "^1.0.0",
"@bitgo/sdk-coin-osmo": "^1.6.11",
"@bitgo/sdk-coin-polygon": "^1.8.0",
"@bitgo/sdk-coin-rbtc": "^1.5.11",
Expand Down
4 changes: 4 additions & 0 deletions modules/bitgo/src/v2/coinFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
Ltc,
Ofc,
OfcToken,
Opeth,
Osmo,
Polygon,
PolygonToken,
Expand Down Expand Up @@ -95,6 +96,7 @@ import {
Tislm,
Tltc,
Tosmo,
Topeth,
Tpolygon,
Trbtc,
Trx,
Expand Down Expand Up @@ -155,6 +157,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('islm', Islm.createInstance);
globalCoinFactory.register('near', Near.createInstance);
globalCoinFactory.register('ofc', Ofc.createInstance);
globalCoinFactory.register('opeth', Opeth.createInstance);
globalCoinFactory.register('osmo', Osmo.createInstance);
globalCoinFactory.register('polygon', Polygon.createInstance);
globalCoinFactory.register('rbtc', Rbtc.createInstance);
Expand Down Expand Up @@ -197,6 +200,7 @@ function registerCoinConstructors(globalCoinFactory: CoinFactory): void {
globalCoinFactory.register('tislm', Tislm.createInstance);
globalCoinFactory.register('tltc', Tltc.createInstance);
globalCoinFactory.register('tnear', TNear.createInstance);
globalCoinFactory.register('topeth', Topeth.createInstance);
globalCoinFactory.register('tosmo', Tosmo.createInstance);
globalCoinFactory.register('tpolygon', Tpolygon.createInstance);
globalCoinFactory.register('trbtc', Trbtc.createInstance);
Expand Down
2 changes: 2 additions & 0 deletions modules/bitgo/src/v2/coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Hbar, Thbar } from '@bitgo/sdk-coin-hbar';
import { Injective, Tinjective } from '@bitgo/sdk-coin-injective';
import { Islm, Tislm } from '@bitgo/sdk-coin-islm';
import { Ltc, Tltc } from '@bitgo/sdk-coin-ltc';
import { Opeth, Topeth } from '@bitgo/sdk-coin-opeth';
import { Osmo, Tosmo } from '@bitgo/sdk-coin-osmo';
import { Polygon, PolygonToken, Tpolygon } from '@bitgo/sdk-coin-polygon';
import { Rbtc, Trbtc } from '@bitgo/sdk-coin-rbtc';
Expand Down Expand Up @@ -72,6 +73,7 @@ export { Etc, Tetc };
export { Hash, Thash };
export { Hbar, Thbar };
export { Ltc, Tltc };
export { Opeth, Topeth };
export { Osmo, Tosmo };
export { Polygon, PolygonToken, Tpolygon };
export { Rbtc, Trbtc };
Expand Down
3 changes: 3 additions & 0 deletions modules/bitgo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
{
"path": "../sdk-coin-near"
},
{
"path": "../sdk-coin-opeth"
},
{
"path": "../sdk-coin-osmo"
},
Expand Down
4 changes: 4 additions & 0 deletions modules/sdk-coin-opeth/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.idea
public
dist
3 changes: 3 additions & 0 deletions modules/sdk-coin-opeth/.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-opeth/.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-opeth/.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-opeth/.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-opeth/.prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
printWidth: 120
singleQuote: true
trailingComma: 'es5'
32 changes: 32 additions & 0 deletions modules/sdk-coin-opeth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# BitGo sdk-coin-opeth

Integration of this coin is still a Work In Progress.

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-opeth`.

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

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 { Opeth } from '@bitgo/sdk-coin-opeth';

const sdk = new BitGoAPI();

sdk.register('opeth', Opeth.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.
53 changes: 53 additions & 0 deletions modules/sdk-coin-opeth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@bitgo/sdk-coin-opeth",
"version": "1.0.0",
"description": "BitGo SDK coin library for Optimism",
"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": ">=16 <19"
},
"repository": {
"type": "git",
"url": "https://github.com/BitGo/BitGoJS.git",
"directory": "modules/sdk-coin-opeth"
},
"lint-staged": {
"*.{js,ts}": [
"yarn prettier --write",
"yarn eslint --fix"
]
},
"publishConfig": {
"access": "public"
},
"nyc": {
"extension": [
".ts"
]
},
"dependencies": {
"@bitgo/abstract-eth": "^1.4.11",
"@bitgo/sdk-coin-eth": "^4.8.0",
"@bitgo/sdk-core": "^8.24.0",
"@bitgo/statics": "^27.0.0",
"@ethereumjs/common": "^2.4.0"
},
"devDependencies": {
"@bitgo/sdk-api": "^1.22.0",
"@bitgo/sdk-test": "^1.2.41"
}
}
4 changes: 4 additions & 0 deletions modules/sdk-coin-opeth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib';
export * from './opeth';
export * from './topeth';
export * from './register';
6 changes: 6 additions & 0 deletions modules/sdk-coin-opeth/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/sdk-coin-eth';
export { Utils };
24 changes: 24 additions & 0 deletions modules/sdk-coin-opeth/src/lib/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import EthereumCommon from '@ethereumjs/common';
import { coins, EthereumNetwork } from '@bitgo/statics';

export const testnetCommon = EthereumCommon.custom(
{
name: 'optimistim sepolia',
networkId: (coins.get('topeth').network as EthereumNetwork).chainId,
chainId: (coins.get('topeth').network as EthereumNetwork).chainId,
},
{
baseChain: 'sepolia',
}
);

export const mainnetCommon = EthereumCommon.custom(
{
name: 'optimism',
networkId: (coins.get('opeth').network as EthereumNetwork).chainId,
chainId: (coins.get('opeth').network as EthereumNetwork).chainId,
},
{
baseChain: 'mainnet',
}
);
30 changes: 30 additions & 0 deletions modules/sdk-coin-opeth/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 { KeyPair, Transaction, TransactionBuilder as EthTransactionBuilder } from '@bitgo/sdk-coin-eth';
import { getCommon } from './utils';
import { TransferBuilder } from './transferBuilder';

export class TransactionBuilder extends EthTransactionBuilder {
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;
}

publicKey(key: string): void {
this._sourceKeyPair = new KeyPair({ pub: key });
}
}
8 changes: 8 additions & 0 deletions modules/sdk-coin-opeth/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TransferBuilder as EthTransferBuilder } from '@bitgo/sdk-coin-eth';

export class TransferBuilder extends EthTransferBuilder {
/** @inheritdoc */
protected getNativeOperationHashPrefix(): string {
return 'OPETH';
}
}
21 changes: 21 additions & 0 deletions modules/sdk-coin-opeth/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;
}
19 changes: 19 additions & 0 deletions modules/sdk-coin-opeth/src/opeth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseCoin, BitGoBase } from '@bitgo/sdk-core';
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
import { AbstractEthLikeMPCCoin } from '@bitgo/abstract-eth';
import { TransactionBuilder as EthTransactionBuilder } from '@bitgo/sdk-coin-eth';
import { TransactionBuilder } from './lib';

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

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

protected getTransactionBuilder(): EthTransactionBuilder {
return new TransactionBuilder(coins.get(this.getBaseChain()));
}
}
8 changes: 8 additions & 0 deletions modules/sdk-coin-opeth/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BitGoBase } from '@bitgo/sdk-core';
import { Opeth } from './opeth';
import { Topeth } from './topeth';

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

import { Opeth } from './opeth';

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

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

0 comments on commit 2fe58df

Please sign in to comment.