Skip to content

Commit dce43d7

Browse files
Merge pull request #4395 from BitGo/WIN-2643-fix-op-transfers
fix(abstract-eth): fix opeth:op transfers
2 parents a496493 + 37a8f4f commit dce43d7

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

modules/abstract-eth/src/abstractEthLikeNewCoins.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,11 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
17301730
const transferBuilder = txBuilder.transfer() as TransferBuilder;
17311731

17321732
const network = this.getNetwork();
1733-
const token = getToken(params.tokenContractAddress as string, network as EthLikeNetwork)?.name as string;
1733+
const token = getToken(
1734+
params.tokenContractAddress as string,
1735+
network as EthLikeNetwork,
1736+
this.staticsCoin?.family as string
1737+
)?.name as string;
17341738

17351739
transferBuilder
17361740
.coin(token)

modules/abstract-eth/src/lib/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class Transaction extends BaseTransaction {
101101
const { to, amount, tokenContractAddress, signature } = decodeTransferData(txData.data);
102102
let coinName: string;
103103
if (tokenContractAddress) {
104-
const token = getToken(tokenContractAddress, this._coinConfig.network);
104+
const token = getToken(tokenContractAddress, this._coinConfig.network, this._coinConfig.family);
105105
coinName = token ? token.name : UNSUPPORTED_COIN_NAME;
106106
} else {
107107
coinName = this._coinConfig.name;

modules/abstract-eth/src/lib/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,23 @@ export function getBufferedByteCode(methodId: string, rawData: string): Buffer {
638638
* Get the statics coin object matching a given contract address if it exists
639639
*
640640
* @param tokenContractAddress The contract address to match against
641+
* @param network - the coin network
642+
* @param family - the coin family
641643
* @returns statics BaseCoin object for the matching token
642644
*/
643-
export function getToken(tokenContractAddress: string, network: BaseNetwork): Readonly<BaseCoin> | undefined {
645+
export function getToken(
646+
tokenContractAddress: string,
647+
network: BaseNetwork,
648+
family: string
649+
): Readonly<BaseCoin> | undefined {
650+
// filter the coins array to find the token with the matching contract address, network and coin family
651+
// coin family is needed to avoid causing issues when a token has same contract address on two different chains
644652
const tokens = coins.filter((coin) => {
645653
if (coin instanceof ContractAddressDefinedToken) {
646654
return (
647-
coin.network.type === network.type && coin.contractAddress.toLowerCase() === tokenContractAddress.toLowerCase()
655+
coin.network.type === network.type &&
656+
coin.family === family &&
657+
coin.contractAddress.toLowerCase() === tokenContractAddress.toLowerCase()
648658
);
649659
}
650660
return false;

modules/sdk-coin-opeth/test/unit/opethToken.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import 'should';
2-
2+
import assert from 'assert';
33
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
4-
import { register } from '../../src';
54
import { BitGoAPI } from '@bitgo/sdk-api';
5+
import { getToken } from '@bitgo/abstract-eth';
6+
7+
import { register } from '../../src';
68

79
describe('Opeth Token:', function () {
810
let bitgo: TestBitGoAPI;
911
let opethTokenCoin;
12+
let opTokenCoin;
1013
const tokenName = 'topeth:terc18dp';
14+
const opToken = 'opeth:op';
1115

1216
before(function () {
1317
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
1418
register(bitgo);
1519
bitgo.initializeTestVars();
1620
opethTokenCoin = bitgo.coin(tokenName);
21+
opTokenCoin = bitgo.coin(opToken);
1722
});
1823

1924
it('should return constants', function () {
@@ -32,4 +37,14 @@ describe('Opeth Token:', function () {
3237
const tokencoinBycontractAddress = bitgo.coin(opethTokenCoin.tokenContractAddress);
3338
opethTokenCoin.should.deepEqual(tokencoinBycontractAddress);
3439
});
40+
41+
it('should return only one token for optimism token contract address', function () {
42+
const token = getToken(
43+
'0x4200000000000000000000000000000000000042',
44+
opTokenCoin.getNetwork(),
45+
opTokenCoin.getFamily()
46+
);
47+
assert(token);
48+
token.name.should.equal('opeth:op');
49+
});
3550
});

0 commit comments

Comments
 (0)