Skip to content

Commit 7a80061

Browse files
Merge pull request #6299 from BitGo/COIN-4511
fix: fixed the common handling for ethlike coin
2 parents fdf1f61 + 098726b commit 7a80061

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

modules/sdk-coin-ethlike/src/ethlikeCoin.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { BaseCoin, BitGoBase, common } from '@bitgo/sdk-core';
44
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
55
import { EthLikeTransactionBuilder } from './lib';
66

7+
interface CommonConfig {
8+
chain?: number;
9+
chainId?: number;
10+
hardfork?: string;
11+
}
12+
713
export class EthLikeCoin extends AbstractEthLikeNewCoins {
814
protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
915
super(bitgo, staticsCoin);
@@ -13,8 +19,31 @@ export class EthLikeCoin extends AbstractEthLikeNewCoins {
1319
return new EthLikeCoin(bitgo, staticsCoin);
1420
}
1521

16-
protected getTransactionBuilder(common?: EthereumCommon): EthLikeTransactionBuilder {
17-
return new EthLikeTransactionBuilder(coins.get(this.getBaseChain()), common);
22+
protected getTransactionBuilder(common?: EthereumCommon | CommonConfig): EthLikeTransactionBuilder {
23+
let ethereumCommon: EthereumCommon | undefined;
24+
25+
// If common is an EthereumCommon instance, use it directly
26+
if (common instanceof EthereumCommon) {
27+
ethereumCommon = common;
28+
}
29+
// If common is provided as a plain object, convert it to EthereumCommon instance
30+
else if (common && typeof common === 'object') {
31+
try {
32+
const chainId = common.chain || common.chainId;
33+
const hardfork = common.hardfork || 'london';
34+
35+
if (chainId) {
36+
ethereumCommon = EthereumCommon.custom({
37+
name: this.getFullName(),
38+
chainId: chainId,
39+
defaultHardfork: hardfork,
40+
});
41+
}
42+
} catch (error) {
43+
ethereumCommon = undefined;
44+
}
45+
}
46+
return new EthLikeTransactionBuilder(coins.get(this.getBaseChain()), ethereumCommon);
1847
}
1948

2049
async recoveryBlockchainExplorerQuery(query: Record<string, string>): Promise<Record<string, unknown>> {

modules/sdk-coin-ethlike/test/unit/ethlikeCoin.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,4 +672,58 @@ describe('EthLikeCoin', function () {
672672
transaction.halfSigned?.should.have.property('recipients');
673673
});
674674
});
675+
676+
describe('Common parameter handling', function () {
677+
it('should handle plain object common parameter for BASE chain', function () {
678+
const baseCoin = bitgo.coin('baseeth') as EthLikeCoin;
679+
680+
const plainCommon = { chain: 8453, hardfork: 'london' };
681+
682+
const txBuilder = (baseCoin as any).getTransactionBuilder(plainCommon);
683+
txBuilder.should.be.an.instanceof(EthLikeTransactionBuilder);
684+
});
685+
686+
it('should handle plain object common parameter with chainId property', function () {
687+
const baseCoin = bitgo.coin('baseeth') as EthLikeCoin;
688+
689+
const plainCommon = { chainId: 8453, hardfork: 'london' };
690+
691+
const txBuilder = (baseCoin as any).getTransactionBuilder(plainCommon);
692+
txBuilder.should.be.an.instanceof(EthLikeTransactionBuilder);
693+
});
694+
695+
it('should handle EthereumCommon instance', function () {
696+
const baseCoin = bitgo.coin('baseeth') as EthLikeCoin;
697+
698+
const txBuilder = (baseCoin as any).getTransactionBuilder(baseChainCommon);
699+
txBuilder.should.be.an.instanceof(EthLikeTransactionBuilder);
700+
});
701+
702+
it('should require common parameter and throw error when undefined', function () {
703+
const baseCoin = bitgo.coin('baseeth') as EthLikeCoin;
704+
705+
(() => {
706+
(baseCoin as any).getTransactionBuilder(undefined);
707+
}).should.throw('Common must be provided for EthLikeTransactionBuilder');
708+
});
709+
710+
it('should convert plain object common to EthereumCommon with working gteHardfork method', function () {
711+
const baseCoin = bitgo.coin('baseeth') as EthLikeCoin;
712+
713+
const plainCommon = { chain: 8453, hardfork: 'london' };
714+
715+
const txBuilder = (baseCoin as any).getTransactionBuilder(plainCommon);
716+
txBuilder.should.be.an.instanceof(EthLikeTransactionBuilder);
717+
718+
const common = (txBuilder as any)._common;
719+
common.should.not.be.undefined;
720+
721+
common.should.have.property('gteHardfork');
722+
(typeof common.gteHardfork).should.equal('function');
723+
724+
const result = common.gteHardfork('homestead');
725+
(typeof result).should.equal('boolean');
726+
result.should.equal(true);
727+
});
728+
});
675729
});

0 commit comments

Comments
 (0)