Skip to content

Commit

Permalink
Merge pull request #4395 from BitGo/WIN-2643-fix-op-transfers
Browse files Browse the repository at this point in the history
fix(abstract-eth): fix opeth:op transfers
  • Loading branch information
gianchandania authored Apr 4, 2024
2 parents a496493 + 37a8f4f commit dce43d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
6 changes: 5 additions & 1 deletion modules/abstract-eth/src/abstractEthLikeNewCoins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,11 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
const transferBuilder = txBuilder.transfer() as TransferBuilder;

const network = this.getNetwork();
const token = getToken(params.tokenContractAddress as string, network as EthLikeNetwork)?.name as string;
const token = getToken(
params.tokenContractAddress as string,
network as EthLikeNetwork,
this.staticsCoin?.family as string
)?.name as string;

transferBuilder
.coin(token)
Expand Down
2 changes: 1 addition & 1 deletion modules/abstract-eth/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Transaction extends BaseTransaction {
const { to, amount, tokenContractAddress, signature } = decodeTransferData(txData.data);
let coinName: string;
if (tokenContractAddress) {
const token = getToken(tokenContractAddress, this._coinConfig.network);
const token = getToken(tokenContractAddress, this._coinConfig.network, this._coinConfig.family);
coinName = token ? token.name : UNSUPPORTED_COIN_NAME;
} else {
coinName = this._coinConfig.name;
Expand Down
14 changes: 12 additions & 2 deletions modules/abstract-eth/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,23 @@ export function getBufferedByteCode(methodId: string, rawData: string): Buffer {
* Get the statics coin object matching a given contract address if it exists
*
* @param tokenContractAddress The contract address to match against
* @param network - the coin network
* @param family - the coin family
* @returns statics BaseCoin object for the matching token
*/
export function getToken(tokenContractAddress: string, network: BaseNetwork): Readonly<BaseCoin> | undefined {
export function getToken(
tokenContractAddress: string,
network: BaseNetwork,
family: string
): Readonly<BaseCoin> | undefined {
// filter the coins array to find the token with the matching contract address, network and coin family
// coin family is needed to avoid causing issues when a token has same contract address on two different chains
const tokens = coins.filter((coin) => {
if (coin instanceof ContractAddressDefinedToken) {
return (
coin.network.type === network.type && coin.contractAddress.toLowerCase() === tokenContractAddress.toLowerCase()
coin.network.type === network.type &&
coin.family === family &&
coin.contractAddress.toLowerCase() === tokenContractAddress.toLowerCase()
);
}
return false;
Expand Down
19 changes: 17 additions & 2 deletions modules/sdk-coin-opeth/test/unit/opethToken.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import 'should';

import assert from 'assert';
import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test';
import { register } from '../../src';
import { BitGoAPI } from '@bitgo/sdk-api';
import { getToken } from '@bitgo/abstract-eth';

import { register } from '../../src';

describe('Opeth Token:', function () {
let bitgo: TestBitGoAPI;
let opethTokenCoin;
let opTokenCoin;
const tokenName = 'topeth:terc18dp';
const opToken = 'opeth:op';

before(function () {
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
register(bitgo);
bitgo.initializeTestVars();
opethTokenCoin = bitgo.coin(tokenName);
opTokenCoin = bitgo.coin(opToken);
});

it('should return constants', function () {
Expand All @@ -32,4 +37,14 @@ describe('Opeth Token:', function () {
const tokencoinBycontractAddress = bitgo.coin(opethTokenCoin.tokenContractAddress);
opethTokenCoin.should.deepEqual(tokencoinBycontractAddress);
});

it('should return only one token for optimism token contract address', function () {
const token = getToken(
'0x4200000000000000000000000000000000000042',
opTokenCoin.getNetwork(),
opTokenCoin.getFamily()
);
assert(token);
token.name.should.equal('opeth:op');
});
});

0 comments on commit dce43d7

Please sign in to comment.