Skip to content

Commit

Permalink
Refactoring Coinbase.assetList usage (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
erdimaden authored May 25, 2024
1 parent 7955e87 commit af9b826
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ console.log(`Faucet transaction: ${faucetTransaction}`);
// Create a new Wallet to transfer funds to.
// Then, we can transfer 0.00001 ETH out of the Wallet to another Wallet.
const anotherWallet = await user.createWallet();
const transfer = await wallet.createTransfer(0.00001, Coinbase.assetList.Eth, anotherWallet);
const transfer = await wallet.createTransfer(0.00001, Coinbase.assets.Eth, anotherWallet);
```

### Re-Instantiating Wallets
Expand Down
18 changes: 9 additions & 9 deletions src/coinbase/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Address {
* Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported.
*
* @param amount - The amount of the Asset to send.
* @param assetId - The ID of the Asset to send. For Ether, Coinbase.assetList.Eth, Coinbase.assetList.Gwei, and Coinbase.assetList.Wei supported.
* @param assetId - The ID of the Asset to send. For Ether, Coinbase.assets.Eth, Coinbase.assets.Gwei, and Coinbase.assets.Wei supported.
* @param destination - The destination of the transfer. If a Wallet, sends to the Wallet's default address. If a String, interprets it as the address ID.
* @param intervalSeconds - The interval at which to poll the Network for Transfer status, in seconds.
* @param timeoutSeconds - The maximum amount of time to wait for the Transfer to complete, in seconds.
Expand Down Expand Up @@ -175,18 +175,18 @@ export class Address {
}

switch (assetId) {
case Coinbase.assetList.Eth:
case Coinbase.assets.Eth:
normalizedAmount = normalizedAmount.mul(WEI_PER_ETHER);
break;
case Coinbase.assetList.Gwei:
case Coinbase.assets.Gwei:
normalizedAmount = normalizedAmount.mul(WEI_PER_GWEI);
break;
case Coinbase.assetList.Wei:
case Coinbase.assets.Wei:
break;
case Coinbase.assetList.Weth:
case Coinbase.assets.Weth:
normalizedAmount = normalizedAmount.mul(WEI_PER_ETHER);
break;
case Coinbase.assetList.Usdc:
case Coinbase.assets.Usdc:
normalizedAmount = normalizedAmount.mul(ATOMIC_UNITS_PER_USDC);
break;
default:
Expand All @@ -197,9 +197,9 @@ export class Address {

const normalizedAssetId = ((): string => {
switch (assetId) {
case Coinbase.assetList.Gwei:
case Coinbase.assetList.Wei:
return Coinbase.assetList.Eth;
case Coinbase.assets.Gwei:
case Coinbase.assets.Wei:
return Coinbase.assets.Eth;
default:
return assetId;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/coinbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Coinbase {
*
* @constant
*/
static assetList = {
static assets = {
Eth: "eth",
Wei: "wei",
Gwei: "gwei",
Expand Down
18 changes: 9 additions & 9 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("Address", () => {

it("should return the correct list of balances", async () => {
const balances = await address.getBalances();
expect(balances.get(Coinbase.assetList.Eth)).toEqual(new Decimal(1));
expect(balances.get(Coinbase.assets.Eth)).toEqual(new Decimal(1));
expect(balances.get("usdc")).toEqual(new Decimal(5000));
expect(balances.get("weth")).toEqual(new Decimal(3));
expect(Coinbase.apiClients.address!.listAddressBalances).toHaveBeenCalledWith(
Expand All @@ -81,13 +81,13 @@ describe("Address", () => {
});

it("should return the correct ETH balance", async () => {
const ethBalance = await address.getBalance(Coinbase.assetList.Eth);
const ethBalance = await address.getBalance(Coinbase.assets.Eth);
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1));
expect(Coinbase.apiClients.address!.getAddressBalance).toHaveBeenCalledWith(
address.getWalletId(),
address.getId(),
Coinbase.assetList.Eth,
Coinbase.assets.Eth,
);
expect(Coinbase.apiClients.address!.getAddressBalance).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -228,7 +228,7 @@ describe("Address", () => {

const transfer = await address.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -245,7 +245,7 @@ describe("Address", () => {
await expect(
address.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -258,7 +258,7 @@ describe("Address", () => {
await expect(
addressWithoutKey.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -274,7 +274,7 @@ describe("Address", () => {
await expect(
address.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -294,7 +294,7 @@ describe("Address", () => {
await expect(
address.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -307,7 +307,7 @@ describe("Address", () => {
await expect(
address.createTransfer(
insufficientAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand Down
8 changes: 4 additions & 4 deletions src/coinbase/tests/balance_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("BalanceMap", () => {
describe(".fromBalances", () => {
const ethBalanceModel: BalanceModel = {
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
amount: ethAtomicAmount,
Expand All @@ -42,14 +42,14 @@ describe("BalanceMap", () => {
const balanceMap = BalanceMap.fromBalances(balances);

it("returns a new BalanceMap object with the correct balances", () => {
expect(balanceMap.get(Coinbase.assetList.Eth)).toEqual(ethAmount);
expect(balanceMap.get(Coinbase.assets.Eth)).toEqual(ethAmount);
expect(balanceMap.get("usdc")).toEqual(usdcAmount);
expect(balanceMap.get("weth")).toEqual(wethAmount);
});
});

describe(".add", () => {
const assetId = Coinbase.assetList.Eth;
const assetId = Coinbase.assets.Eth;
const balance = Balance.fromModelAndAssetId(
{
amount: ethAtomicAmount,
Expand All @@ -67,7 +67,7 @@ describe("BalanceMap", () => {
});

describe(".toString", () => {
const assetId = Coinbase.assetList.Eth;
const assetId = Coinbase.assets.Eth;
const balance = Balance.fromModelAndAssetId(
{
amount: ethAtomicAmount,
Expand Down
10 changes: 5 additions & 5 deletions src/coinbase/tests/balance_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Balance", () => {
const balanceModel: BalanceModel = {
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
};
Expand All @@ -21,25 +21,25 @@ describe("Balance", () => {
});

it("returns a new Balance object with the correct asset_id", () => {
expect(balance.assetId).toBe(Coinbase.assetList.Eth);
expect(balance.assetId).toBe(Coinbase.assets.Eth);
});
});

describe(".fromModelAndAssetId", () => {
const amount = new Decimal(1);
const balanceModel: BalanceModel = {
asset: { asset_id: Coinbase.assetList.Eth, network_id: Coinbase.networkList.BaseSepolia },
asset: { asset_id: Coinbase.assets.Eth, network_id: Coinbase.networkList.BaseSepolia },
amount: "1000000000000000000",
};

const balance = Balance.fromModelAndAssetId(balanceModel, Coinbase.assetList.Eth);
const balance = Balance.fromModelAndAssetId(balanceModel, Coinbase.assets.Eth);

it("returns a new Balance object with the correct amount", () => {
expect(balance.amount).toEqual(amount);
});

it("returns a new Balance object with the correct asset_id", () => {
expect(balance.assetId).toBe(Coinbase.assetList.Eth);
expect(balance.assetId).toBe(Coinbase.assets.Eth);
});
});
});
6 changes: 3 additions & 3 deletions src/coinbase/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ describe("User Class", () => {
await expect(
unhydratedWallet.createTransfer(
new Decimal("500000000000000000"),
Coinbase.assetList.Eth,
Coinbase.assets.Eth,
address1,
),
).rejects.toThrow(InternalError);
Expand All @@ -463,7 +463,7 @@ describe("User Class", () => {
const mockWalletBalance: BalanceModel = {
amount: "5000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
Expand Down Expand Up @@ -491,7 +491,7 @@ describe("User Class", () => {
expect(wallet.getAddress(addressListModel.data[0].address_id)?.getId()).toBe(
addressListModel.data[0].address_id,
);
const balance = await wallet.getBalance(Coinbase.assetList.Eth);
const balance = await wallet.getBalance(Coinbase.assets.Eth);
expect(balance).toEqual(new Decimal("5"));

const balanceMap = await wallet.getBalances();
Expand Down
6 changes: 3 additions & 3 deletions src/coinbase/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const VALID_TRANSFER_MODEL: TransferModel = {
wallet_id: walletId,
address_id: ethers.Wallet.createRandom().address,
destination: "0x4D9E4F3f4D1A8B5F4f7b1F5b5C7b8d6b2B3b1b0b",
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
amount: new Decimal(ethers.parseUnits("100", 18).toString()).toString(),
unsigned_payload:
"7b2274797065223a22307832222c22636861696e4964223a2230783134613334222c226e6f6e63" +
Expand All @@ -102,7 +102,7 @@ export const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
{
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
Expand Down Expand Up @@ -132,7 +132,7 @@ export const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
export const VALID_BALANCE_MODEL: BalanceModel = {
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
};
Expand Down
30 changes: 15 additions & 15 deletions src/coinbase/tests/wallet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("Wallet Class", () => {

const transfer = await wallet.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -122,7 +122,7 @@ describe("Wallet Class", () => {
await expect(
wallet.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -138,7 +138,7 @@ describe("Wallet Class", () => {
await expect(
wallet.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -158,7 +158,7 @@ describe("Wallet Class", () => {
await expect(
wallet.createTransfer(
weiAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand All @@ -171,7 +171,7 @@ describe("Wallet Class", () => {
await expect(
wallet.createTransfer(
insufficientAmount,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
destination,
intervalSeconds,
timeoutSeconds,
Expand Down Expand Up @@ -353,7 +353,7 @@ describe("Wallet Class", () => {
{
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
Expand Down Expand Up @@ -388,7 +388,7 @@ describe("Wallet Class", () => {
const mockWalletBalance: BalanceModel = {
amount: "5000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
asset_id: Coinbase.assets.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
Expand All @@ -397,43 +397,43 @@ describe("Wallet Class", () => {
});

it("should return the correct ETH balance", async () => {
const balanceMap = await wallet.getBalance(Coinbase.assetList.Eth);
const balanceMap = await wallet.getBalance(Coinbase.assets.Eth);
expect(balanceMap).toEqual(new Decimal(5));
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledWith(
walletId,
Coinbase.assetList.Eth,
Coinbase.assets.Eth,
);
});

it("should return the correct GWEI balance", async () => {
const balance = await wallet.getBalance(Coinbase.assetList.Gwei);
const balance = await wallet.getBalance(Coinbase.assets.Gwei);
expect(balance).toEqual(GWEI_PER_ETHER.mul(5));
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledWith(
walletId,
Coinbase.assetList.Gwei,
Coinbase.assets.Gwei,
);
});

it("should return the correct WEI balance", async () => {
const balance = await wallet.getBalance(Coinbase.assetList.Wei);
const balance = await wallet.getBalance(Coinbase.assets.Wei);
expect(balance).toEqual(WEI_PER_ETHER.mul(5));
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledWith(
walletId,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
);
});

it("should return 0 when the balance is not found", async () => {
Coinbase.apiClients.wallet!.getWalletBalance = mockReturnValue({});
const balance = await wallet.getBalance(Coinbase.assetList.Wei);
const balance = await wallet.getBalance(Coinbase.assets.Wei);
expect(balance).toEqual(new Decimal(0));
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.wallet!.getWalletBalance).toHaveBeenCalledWith(
walletId,
Coinbase.assetList.Wei,
Coinbase.assets.Wei,
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Transfer {
public getAmount(): Decimal {
const amount = new Decimal(this.model.amount);

if (this.getAssetId() === Coinbase.assetList.Eth) {
if (this.getAssetId() === Coinbase.assets.Eth) {
return amount.div(WEI_PER_ETHER);
}
return amount;
Expand Down

0 comments on commit af9b826

Please sign in to comment.