Skip to content

Commit

Permalink
feat(sdk-coin-avaxc): add avaxc support for evm ccr
Browse files Browse the repository at this point in the history
TICKET: COIN-1708
  • Loading branch information
venkateshv1266 committed Oct 2, 2024
1 parent 8e228f6 commit 1d60679
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 81 deletions.
2 changes: 1 addition & 1 deletion modules/abstract-eth/src/abstractEthLikeNewCoins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
* @param {string[]} paramsArr - The parameters to hash together for the digest
* @returns {Buffer}
*/
private static getHopDigest(paramsArr: string[]): Buffer {
protected static getHopDigest(paramsArr: string[]): Buffer {
const hash = Keccak('keccak256');
hash.update([AbstractEthLikeNewCoins.hopTransactionSalt, ...paramsArr].join('$'));
return hash.digest();
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-avaxc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
]
},
"dependencies": {
"@bitgo/abstract-eth": "^22.1.2",
"@bitgo/sdk-coin-avaxp": "^5.0.37",
"@bitgo/sdk-coin-eth": "^24.2.27",
"@bitgo/sdk-core": "^28.7.0",
Expand Down
70 changes: 64 additions & 6 deletions modules/sdk-coin-avaxc/src/avaxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
CoinFamily,
coins,
ethGasConfigs,
BaseNetwork,
EthereumNetwork,
} from '@bitgo/statics';
import {
BaseCoin,
Expand All @@ -33,6 +33,7 @@ import {
VerifyAddressOptions,
} from '@bitgo/sdk-core';
import {
AbstractEthLikeNewCoins,
GetSendMethodArgsOptions,
optionalDeps,
RecoverOptions,
Expand Down Expand Up @@ -62,14 +63,20 @@ import {
VerifyAvaxcTransactionOptions,
} from './iface';
import { AvaxpLib } from '@bitgo/sdk-coin-avaxp';
import { SignTransactionOptions } from '@bitgo/abstract-eth';

export class AvaxC extends BaseCoin {
/** COIN-1708 : Avaxc is added for CCR in WRW,
* hence adding the feature for AbstractEthLikeNewCoins
* Super class changed from BaseCoin to AbstractEthLikeNewCoins
* @since Sept 2024
*/
export class AvaxC extends AbstractEthLikeNewCoins {
static hopTransactionSalt = 'bitgoHopAddressRequestSalt';

protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;

protected constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
super(bitgo);
super(bitgo, staticsCoin);

if (!staticsCoin) {
throw new Error('missing required constructor parameter staticsCoin');
Expand All @@ -94,8 +101,8 @@ export class AvaxC extends BaseCoin {
* Method to return the coin's network object
* @returns {BaseNetwork}
*/
getNetwork(): BaseNetwork {
return this._staticsCoin.network;
getNetwork(): EthereumNetwork {
return this._staticsCoin.network as EthereumNetwork;
}

/**
Expand Down Expand Up @@ -545,6 +552,10 @@ export class AvaxC extends BaseCoin {
* @returns {Promise<RecoveryInfo>} - recovery tx info
*/
async recover(params: RecoverOptions): Promise<RecoveryInfo | OfflineVaultTxInfo> {
if (params.bitgoFeeAddress) {
return (await this.recoverEthLikeforEvmBasedRecovery(params)) as RecoveryInfo | OfflineVaultTxInfo;
}

if (_.isUndefined(params.userKey)) {
throw new Error('missing userKey');
}
Expand Down Expand Up @@ -990,7 +1001,7 @@ export class AvaxC extends BaseCoin {
* Assemble half-sign prebuilt transaction
* @param params
*/
async signTransaction(params: AvaxSignTransactionOptions): Promise<SignedTransaction> {
async signTransaction(params: AvaxSignTransactionOptions | SignTransactionOptions): Promise<SignedTransaction> {
// Normally the SDK provides the first signature for an AVAXC tx,
// but for unsigned sweep recoveries it can provide the second and final one.
if (params.isLastSignature) {
Expand Down Expand Up @@ -1180,4 +1191,51 @@ export class AvaxC extends BaseCoin {
getAvaxP(): string {
return this.getChain().toString() === 'avaxc' ? 'avaxp' : 'tavaxp';
}

/**
* Fetch the gas price from the explorer
*/
async getGasPriceFromExternalAPI(): Promise<BN> {
try {
const res = await this.recoveryBlockchainExplorerQuery({
jsonrpc: '2.0',
method: 'eth_gasPrice',
id: 1,
});
const gasPrice = new BN(res.result.slice(2), 16);
console.log(` Got gas price: ${gasPrice}`);
return gasPrice;
} catch (e) {
throw new Error('Failed to get gas price');
}
}

/**
* Fetch the gas limit from the explorer
* @param from
* @param to
* @param data
*/
async getGasLimitFromExternalAPI(from: string, to: string, data: string): Promise<BN> {
try {
const res = await this.recoveryBlockchainExplorerQuery({
jsonrpc: '2.0',
method: 'eth_estimateGas',
params: [
{
from,
to,
data,
},
'latest',
],
id: 1,
});
const gasLimit = new BN(res.result.slice(2), 16);
console.log(`Got gas limit: ${gasLimit}`);
return gasLimit;
} catch (e) {
throw new Error('Failed to get gas limit: ');
}
}
}
Loading

0 comments on commit 1d60679

Please sign in to comment.