Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk-coin-avaxc): add avaxc support for evm ccr #4977

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
public 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
80 changes: 64 additions & 16 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is changed, please keep an eye on avaxc once this goes out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure.

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 @@ -1117,16 +1128,6 @@ export class AvaxC extends BaseCoin {
return await this.bitgo.get(this.url('/tx/fee')).query(query).result();
}

/**
* Gets the hop digest for the user to sign. This is validated in the HSM to prove that the user requested this tx
* @param paramsArr The parameters to hash together for the digest
*/
static getHopDigest(paramsArr: string[]): Buffer {
const hash = Keccak('keccak256');
hash.update([AvaxC.hopTransactionSalt, ...paramsArr].join('$'));
return hash.digest();
}

/**
* Calculate tx hash like evm from tx hex.
* @param {string} tx
Expand Down Expand Up @@ -1180,4 +1181,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: ');
venkateshv1266 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
3 changes: 3 additions & 0 deletions modules/sdk-coin-avaxc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"include": ["src/**/*", "test/**/*"],
"exclude": ["node_modules"],
"references": [
{
"path": "../abstract-eth"
},
{
"path": "../sdk-api"
},
Expand Down
Loading
Loading