Skip to content

Commit

Permalink
Merge pull request #131 from coinbase/CCSTAK-732-2
Browse files Browse the repository at this point in the history
[CCSTAK-732] Adding conversion data to staking rewards
  • Loading branch information
shreifabdallah authored Aug 8, 2024
2 parents 78aea4d + eca8186 commit d0b08ae
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Coinbase Node.js SDK Changelog

## [0.0.15]

### Added

- USD value conversion details to the StakingReward object

## [0.0.14] - 2024-08-05

### Added
Expand Down
234 changes: 234 additions & 0 deletions src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ export interface AddressBalanceList {
*/
'total_count': number;
}
/**
*
* @export
* @interface AddressHistoricalBalanceList
*/
export interface AddressHistoricalBalanceList {
/**
*
* @type {Array<HistoricalBalance>}
* @memberof AddressHistoricalBalanceList
*/
'data': Array<HistoricalBalance>;
/**
* True if this list has another page of items after this one that can be fetched.
* @type {boolean}
* @memberof AddressHistoricalBalanceList
*/
'has_more': boolean;
/**
* The page token to be used to fetch the next page.
* @type {string}
* @memberof AddressHistoricalBalanceList
*/
'next_page': string;
}
/**
*
* @export
Expand Down Expand Up @@ -757,6 +782,37 @@ export interface GetStakingContextRequest {
*/
'options': { [key: string]: string; };
}
/**
* The balance of an asset onchain at a particular block
* @export
* @interface HistoricalBalance
*/
export interface HistoricalBalance {
/**
* The amount in the atomic units of the asset
* @type {string}
* @memberof HistoricalBalance
*/
'amount': string;
/**
* The hash of the block at which the balance was recorded
* @type {string}
* @memberof HistoricalBalance
*/
'block_hash': string;
/**
* The block height at which the balance was recorded
* @type {string}
* @memberof HistoricalBalance
*/
'block_height': string;
/**
*
* @type {Asset}
* @memberof HistoricalBalance
*/
'asset': Asset;
}
/**
* An error response from the Coinbase Developer Platform API
* @export
Expand Down Expand Up @@ -1236,6 +1292,12 @@ export interface StakingReward {
* @memberof StakingReward
*/
'format': StakingRewardFormat;
/**
*
* @type {StakingRewardUSDValue}
* @memberof StakingReward
*/
'usd_value': StakingRewardUSDValue;
}

export const StakingRewardStateEnum = {
Expand All @@ -1259,6 +1321,31 @@ export const StakingRewardFormat = {
export type StakingRewardFormat = typeof StakingRewardFormat[keyof typeof StakingRewardFormat];


/**
* The USD value of the reward
* @export
* @interface StakingRewardUSDValue
*/
export interface StakingRewardUSDValue {
/**
* The value of the reward in USD
* @type {string}
* @memberof StakingRewardUSDValue
*/
'amount': string;
/**
* The conversion price from native currency to USD
* @type {string}
* @memberof StakingRewardUSDValue
*/
'conversion_price': string;
/**
* The time of the conversion in UTC.
* @type {string}
* @memberof StakingRewardUSDValue
*/
'conversion_time': string;
}
/**
* A trade of an asset to another asset
* @export
Expand Down Expand Up @@ -1490,7 +1577,41 @@ export interface Transfer {
* @memberof Transfer
*/
'transaction': Transaction;
/**
* The unsigned payload of the transfer. This is the payload that needs to be signed by the sender.
* @type {string}
* @memberof Transfer
*/
'unsigned_payload'?: string;
/**
* The signed payload of the transfer. This is the payload that has been signed by the sender.
* @type {string}
* @memberof Transfer
*/
'signed_payload'?: string;
/**
* The hash of the transfer transaction
* @type {string}
* @memberof Transfer
*/
'transaction_hash'?: string;
/**
* The status of the transfer
* @type {string}
* @memberof Transfer
*/
'status'?: TransferStatusEnum;
}

export const TransferStatusEnum = {
Pending: 'pending',
Broadcast: 'broadcast',
Complete: 'complete',
Failed: 'failed'
} as const;

export type TransferStatusEnum = typeof TransferStatusEnum[keyof typeof TransferStatusEnum];

/**
*
* @export
Expand Down Expand Up @@ -2798,6 +2919,58 @@ export const ExternalAddressesApiAxiosParamCreator = function (configuration?: C



setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* List the historical balance of an asset in a specific address.
* @summary Get address balance history for asset
* @param {string} networkId The ID of the blockchain network
* @param {string} addressId The ID of the address to fetch the historical balance for.
* @param {string} assetId The symbol of the asset to fetch the historical balance for.
* @param {number} [limit] A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
* @param {string} [page] A cursor for pagination across multiple pages of results. Don\&#39;t include this parameter on the first call. Use the next_page value returned in a previous response to request subsequent results.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
listAddressHistoricalBalance: async (networkId: string, addressId: string, assetId: string, limit?: number, page?: string, options: RawAxiosRequestConfig = {}): Promise<RequestArgs> => {
// verify required parameter 'networkId' is not null or undefined
assertParamExists('listAddressHistoricalBalance', 'networkId', networkId)
// verify required parameter 'addressId' is not null or undefined
assertParamExists('listAddressHistoricalBalance', 'addressId', addressId)
// verify required parameter 'assetId' is not null or undefined
assertParamExists('listAddressHistoricalBalance', 'assetId', assetId)
const localVarPath = `/v1/networks/{network_id}/addresses/{address_id}/balance_history/{asset_id}`
.replace(`{${"network_id"}}`, encodeURIComponent(String(networkId)))
.replace(`{${"address_id"}}`, encodeURIComponent(String(addressId)))
.replace(`{${"asset_id"}}`, encodeURIComponent(String(assetId)));
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

if (limit !== undefined) {
localVarQueryParameter['limit'] = limit;
}

if (page !== undefined) {
localVarQueryParameter['page'] = page;
}



setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
Expand Down Expand Up @@ -2913,6 +3086,23 @@ export const ExternalAddressesApiFp = function(configuration?: Configuration) {
const localVarOperationServerBasePath = operationServerMap['ExternalAddressesApi.getExternalAddressBalance']?.[localVarOperationServerIndex]?.url;
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath);
},
/**
* List the historical balance of an asset in a specific address.
* @summary Get address balance history for asset
* @param {string} networkId The ID of the blockchain network
* @param {string} addressId The ID of the address to fetch the historical balance for.
* @param {string} assetId The symbol of the asset to fetch the historical balance for.
* @param {number} [limit] A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
* @param {string} [page] A cursor for pagination across multiple pages of results. Don\&#39;t include this parameter on the first call. Use the next_page value returned in a previous response to request subsequent results.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async listAddressHistoricalBalance(networkId: string, addressId: string, assetId: string, limit?: number, page?: string, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<AddressHistoricalBalanceList>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.listAddressHistoricalBalance(networkId, addressId, assetId, limit, page, options);
const localVarOperationServerIndex = configuration?.serverIndex ?? 0;
const localVarOperationServerBasePath = operationServerMap['ExternalAddressesApi.listAddressHistoricalBalance']?.[localVarOperationServerIndex]?.url;
return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath);
},
/**
* List all of the balances of an external address
* @summary Get the balances of an external address
Expand Down Expand Up @@ -2964,6 +3154,20 @@ export const ExternalAddressesApiFactory = function (configuration?: Configurati
getExternalAddressBalance(networkId: string, addressId: string, assetId: string, options?: any): AxiosPromise<Balance> {
return localVarFp.getExternalAddressBalance(networkId, addressId, assetId, options).then((request) => request(axios, basePath));
},
/**
* List the historical balance of an asset in a specific address.
* @summary Get address balance history for asset
* @param {string} networkId The ID of the blockchain network
* @param {string} addressId The ID of the address to fetch the historical balance for.
* @param {string} assetId The symbol of the asset to fetch the historical balance for.
* @param {number} [limit] A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
* @param {string} [page] A cursor for pagination across multiple pages of results. Don\&#39;t include this parameter on the first call. Use the next_page value returned in a previous response to request subsequent results.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
listAddressHistoricalBalance(networkId: string, addressId: string, assetId: string, limit?: number, page?: string, options?: any): AxiosPromise<AddressHistoricalBalanceList> {
return localVarFp.listAddressHistoricalBalance(networkId, addressId, assetId, limit, page, options).then((request) => request(axios, basePath));
},
/**
* List all of the balances of an external address
* @summary Get the balances of an external address
Expand Down Expand Up @@ -3008,6 +3212,20 @@ export interface ExternalAddressesApiInterface {
*/
getExternalAddressBalance(networkId: string, addressId: string, assetId: string, options?: RawAxiosRequestConfig): AxiosPromise<Balance>;

/**
* List the historical balance of an asset in a specific address.
* @summary Get address balance history for asset
* @param {string} networkId The ID of the blockchain network
* @param {string} addressId The ID of the address to fetch the historical balance for.
* @param {string} assetId The symbol of the asset to fetch the historical balance for.
* @param {number} [limit] A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
* @param {string} [page] A cursor for pagination across multiple pages of results. Don\&#39;t include this parameter on the first call. Use the next_page value returned in a previous response to request subsequent results.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof ExternalAddressesApiInterface
*/
listAddressHistoricalBalance(networkId: string, addressId: string, assetId: string, limit?: number, page?: string, options?: RawAxiosRequestConfig): AxiosPromise<AddressHistoricalBalanceList>;

/**
* List all of the balances of an external address
* @summary Get the balances of an external address
Expand Down Expand Up @@ -3054,6 +3272,22 @@ export class ExternalAddressesApi extends BaseAPI implements ExternalAddressesAp
return ExternalAddressesApiFp(this.configuration).getExternalAddressBalance(networkId, addressId, assetId, options).then((request) => request(this.axios, this.basePath));
}

/**
* List the historical balance of an asset in a specific address.
* @summary Get address balance history for asset
* @param {string} networkId The ID of the blockchain network
* @param {string} addressId The ID of the address to fetch the historical balance for.
* @param {string} assetId The symbol of the asset to fetch the historical balance for.
* @param {number} [limit] A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.
* @param {string} [page] A cursor for pagination across multiple pages of results. Don\&#39;t include this parameter on the first call. Use the next_page value returned in a previous response to request subsequent results.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof ExternalAddressesApi
*/
public listAddressHistoricalBalance(networkId: string, addressId: string, assetId: string, limit?: number, page?: string, options?: RawAxiosRequestConfig) {
return ExternalAddressesApiFp(this.configuration).listAddressHistoricalBalance(networkId, addressId, assetId, limit, page, options).then((request) => request(this.axios, this.basePath));
}

/**
* List all of the balances of an external address
* @summary Get the balances of an external address
Expand Down
29 changes: 28 additions & 1 deletion src/coinbase/staking_reward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,39 @@ export class StakingReward {
return this.model.address_id;
}

/**
* Returns the USD value of the StakingReward.
*
* @returns The USD value.
*/
public usdValue(): Amount {
return new Decimal(this.model.usd_value.amount).div(new Decimal("100"));
}

/**
* Returns the conversion price of the StakingReward in USD.
*
* @returns The conversion price.
*/
public conversionPrice(): Amount {
return new Decimal(this.model.usd_value.conversion_price);
}

/**
* Returns the time of calculating the conversion price.
*
* @returns The conversion time.
*/
public conversionTime(): Date {
return new Date(this.model.usd_value.conversion_time);
}

/**
* Print the Staking Reward as a string.
*
* @returns The string representation of the Staking Reward.
*/
public toString(): string {
return `StakingReward { date: '${this.date().toISOString()}' address: '${this.addressId()}' amount: '${this.amount().toString()}' }`;
return `StakingReward { date: '${this.date().toISOString()}' address: '${this.addressId()}' amount: '${this.amount().toString()}' usd_value: '${this.usdValue().toString()}' conversion_price: '${this.conversionPrice().toString()}' conversion_time: '${this.conversionTime().toISOString()}' }`;
}
}
15 changes: 15 additions & 0 deletions src/tests/external_address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,35 @@ describe("ExternalAddress", () => {
amount: "361",
state: StakingRewardStateEnum.Pending,
format: StakingRewardFormat.Usd,
usd_value: {
amount: "361",
conversion_price: "3000",
conversion_time: "2024-05-01T00:00:00Z",
},
},
{
address_id: address.getId(),
date: "2024-05-02",
amount: "203",
state: StakingRewardStateEnum.Pending,
format: StakingRewardFormat.Usd,
usd_value: {
amount: "203",
conversion_price: "3000",
conversion_time: "2024-05-02T00:00:00Z",
},
},
{
address_id: address.getId(),
date: "2024-05-03",
amount: "226",
state: StakingRewardStateEnum.Pending,
format: StakingRewardFormat.Usd,
usd_value: {
amount: "226",
conversion_price: "3000",
conversion_time: "2024-05-03T00:00:00Z",
},
},
],
has_more: false,
Expand Down
Loading

0 comments on commit d0b08ae

Please sign in to comment.