Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-agarwal-coinbase committed Jan 17, 2025
1 parent 15eb0f3 commit 9400b75
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
116 changes: 67 additions & 49 deletions src/coinbase/address/wallet_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ export class WalletAddress extends Address {
* @returns A Promise that resolves to the deployed SmartContract object.
* @throws {APIError} If the API request to create a smart contract fails.
*/
public async deployToken({ name, symbol, totalSupply }: CreateERC20Options): Promise<SmartContract> {
public async deployToken({
name,
symbol,
totalSupply,
}: CreateERC20Options): Promise<SmartContract> {
if (!Coinbase.useServerSigner && !this.key) {
throw new Error("Cannot deploy ERC20 without private key loaded");
}
Expand Down Expand Up @@ -449,8 +453,8 @@ export class WalletAddress extends Address {

return smartContract;
}
/**

/**
* Deploys a custom contract.
*
* @param options - The options for creating the custom contract.
Expand All @@ -461,23 +465,33 @@ export class WalletAddress extends Address {
* @returns A Promise that resolves to the deployed SmartContract object.
* @throws {APIError} If the API request to create a smart contract fails.
*/
public async deployContract({ solidityVersion, solidityInputJson, contractName, constructorArgs }: CreateCustomContractOptions): Promise<SmartContract> {
if (!Coinbase.useServerSigner && !this.key) {
throw new Error("Cannot deploy custom contract without private key loaded");
}

const smartContract = await this.createCustomContract({ solidityVersion, solidityInputJson, contractName, constructorArgs });

if (Coinbase.useServerSigner) {
return smartContract;
}

await smartContract.sign(this.getSigner());
await smartContract.broadcast();

public async deployContract({
solidityVersion,
solidityInputJson,
contractName,
constructorArgs,
}: CreateCustomContractOptions): Promise<SmartContract> {
if (!Coinbase.useServerSigner && !this.key) {
throw new Error("Cannot deploy custom contract without private key loaded");
}

const smartContract = await this.createCustomContract({
solidityVersion,
solidityInputJson,
contractName,
constructorArgs,
});

if (Coinbase.useServerSigner) {
return smartContract;
}

await smartContract.sign(this.getSigner());
await smartContract.broadcast();

return smartContract;
}

/**
* Creates an ERC20 token contract.
*
Expand All @@ -489,7 +503,11 @@ export class WalletAddress extends Address {
* @returns {Promise<SmartContract>} A Promise that resolves to the created SmartContract.
* @throws {APIError} If the API request to create a smart contract fails.
*/
private async createERC20({ name, symbol, totalSupply }: CreateERC20Options): Promise<SmartContract> {
private async createERC20({
name,
symbol,
totalSupply,
}: CreateERC20Options): Promise<SmartContract> {
const resp = await Coinbase.apiClients.smartContract!.createSmartContract(
this.getWalletId(),
this.getId(),
Expand All @@ -515,7 +533,11 @@ export class WalletAddress extends Address {
* @returns A Promise that resolves to the deployed SmartContract object.
* @throws {APIError} If the private key is not loaded when not using server signer.
*/
private async createERC721({ name, symbol, baseURI }: CreateERC721Options): Promise<SmartContract> {
private async createERC721({
name,
symbol,
baseURI,
}: CreateERC721Options): Promise<SmartContract> {
const resp = await Coinbase.apiClients.smartContract!.createSmartContract(
this.getWalletId(),
this.getId(),
Expand Down Expand Up @@ -566,30 +588,32 @@ export class WalletAddress extends Address {
* @returns {Promise<SmartContract>} A Promise that resolves to the created SmartContract.
* @throws {APIError} If the API request to compile or subsequently create a smart contract fails.
*/
private async createCustomContract({ solidityVersion, solidityInputJson, contractName, constructorArgs }: CreateCustomContractOptions): Promise<SmartContract> {
const compileContractResp = await Coinbase.apiClients.smartContract!.compileSmartContract(
{
solidity_compiler_version: solidityVersion,
solidity_input_json: solidityInputJson,
contract_name: contractName,
},
);

const compiledContract = compileContractResp.data;
const compiledContractId = compiledContract.compiled_smart_contract_id;
private async createCustomContract({
solidityVersion,
solidityInputJson,
contractName,
constructorArgs,
}: CreateCustomContractOptions): Promise<SmartContract> {
const compileContractResp = await Coinbase.apiClients.smartContract!.compileSmartContract({
solidity_compiler_version: solidityVersion,
solidity_input_json: solidityInputJson,
contract_name: contractName,
});

const createContractResp = await Coinbase.apiClients.smartContract!.createSmartContract(
this.getWalletId(),
this.getId(),
{
type: SmartContractType.Custom,
options: JSON.stringify(constructorArgs),
compiled_smart_contract_id: compiledContractId,
},
);
return SmartContract.fromModel(createContractResp?.data);
}
const compiledContract = compileContractResp.data;
const compiledContractId = compiledContract.compiled_smart_contract_id;

const createContractResp = await Coinbase.apiClients.smartContract!.createSmartContract(
this.getWalletId(),
this.getId(),
{
type: SmartContractType.Custom,
options: JSON.stringify(constructorArgs),
compiled_smart_contract_id: compiledContractId,
},
);
return SmartContract.fromModel(createContractResp?.data);
}

/**
* Creates a contract invocation with the given data.
Expand Down Expand Up @@ -843,10 +867,7 @@ export class WalletAddress extends Address {
* @param options.assetId - The ID of the Asset to fund with. For Ether, eth, gwei, and wei are supported.
* @returns The created fund operation object
*/
public async fund({
amount,
assetId,
}: CreateFundOptions): Promise<FundOperation> {
public async fund({ amount, assetId }: CreateFundOptions): Promise<FundOperation> {
const normalizedAmount = new Decimal(amount.toString());

return FundOperation.create(
Expand All @@ -866,10 +887,7 @@ export class WalletAddress extends Address {
* @param options.assetId - The ID of the Asset to fund with. For Ether, eth, gwei, and wei are supported.
* @returns The fund quote object
*/
public async quoteFund({
amount,
assetId,
}: CreateQuoteOptions): Promise<FundQuote> {
public async quoteFund({ amount, assetId }: CreateQuoteOptions): Promise<FundQuote> {
const normalizedAmount = new Decimal(amount.toString());

return FundQuote.create(
Expand Down
2 changes: 1 addition & 1 deletion src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ export type CreateCustomContractOptions = {
solidityVersion: string;
solidityInputJson: string;
contractName: string;
constructorArgs: Record<string, any>
constructorArgs: Record<string, any>;
};

/**
Expand Down
10 changes: 7 additions & 3 deletions src/tests/authenticator_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe("Authenticator tests", () => {
});

describe("when a source version is provided", () => {
beforeAll(() => sourceVersion = "1.0.0");
afterAll(() => sourceVersion = undefined);
beforeAll(() => (sourceVersion = "1.0.0"));
afterAll(() => (sourceVersion = undefined));

it("includes the source version in the correlation context", async () => {
const config = await authenticator.authenticateRequest(VALID_CONFIG, true);
Expand All @@ -65,7 +65,11 @@ describe("Authenticator tests", () => {
});

it("invalid pem key should raise an InvalidAPIKeyFormat error", async () => {
const invalidAuthenticator = new CoinbaseAuthenticator("test-key", "-----BEGIN EC KEY-----\n", source);
const invalidAuthenticator = new CoinbaseAuthenticator(
"test-key",
"-----BEGIN EC KEY-----\n",
source,
);

expect(invalidAuthenticator.authenticateRequest(VALID_CONFIG)).rejects.toThrow();
});
Expand Down
4 changes: 2 additions & 2 deletions src/tests/smart_contract_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ describe("SmartContract", () => {
it("returns the same value as toString", () => {
expect(erc20SmartContract.toString()).toEqual(
`SmartContract{id: '${erc20SmartContract.getId()}', networkId: '${erc20SmartContract.getNetworkId()}', ` +
`contractAddress: '${erc20SmartContract.getContractAddress()}', deployerAddress: '${erc20SmartContract.getDeployerAddress()}', ` +
`type: '${erc20SmartContract.getType()}'}`,
`contractAddress: '${erc20SmartContract.getContractAddress()}', deployerAddress: '${erc20SmartContract.getDeployerAddress()}', ` +
`type: '${erc20SmartContract.getType()}'}`,
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ export const VALID_SMART_CONTRACT_CUSTOM_MODEL: SmartContractModel = {
transaction: {
network_id: Coinbase.networks.BaseSepolia,
from_address_id: "0xdeadbeef",
unsigned_payload: "7b2274797065223a22307832222c22636861696e4964223a2230783134613334222c226e6f6e6365223a22307830222c22746f223a22307861383261623835303466646562326461646161336234663037356539363762626533353036356239222c22676173223a22307865623338222c226761735072696365223a6e756c6c2c226d61785072696f72697479466565506572476173223a2230786634323430222c226d6178466565506572476173223a2230786634333638222c2276616c7565223a22307830222c22696e707574223a223078366136323738343230303030303030303030303030303030303030303030303034373564343164653761383132393862613236333138343939363830306362636161643733633062222c226163636573734c697374223a5b5d2c2276223a22307830222c2272223a22307830222c2273223a22307830222c2279506172697479223a22307830222c2268617368223a22307865333131636632303063643237326639313566656433323165663065376431653965353362393761346166623737336638653935646431343630653665326163227d",
unsigned_payload:
"7b2274797065223a22307832222c22636861696e4964223a2230783134613334222c226e6f6e6365223a22307830222c22746f223a22307861383261623835303466646562326461646161336234663037356539363762626533353036356239222c22676173223a22307865623338222c226761735072696365223a6e756c6c2c226d61785072696f72697479466565506572476173223a2230786634323430222c226d6178466565506572476173223a2230786634333638222c2276616c7565223a22307830222c22696e707574223a223078366136323738343230303030303030303030303030303030303030303030303034373564343164653761383132393862613236333138343939363830306362636161643733633062222c226163636573734c697374223a5b5d2c2276223a22307830222c2272223a22307830222c2273223a22307830222c2279506172697479223a22307830222c2268617368223a22307865333131636632303063643237326639313566656433323165663065376431653965353362393761346166623737336638653935646431343630653665326163227d",
status: TransactionStatusEnum.Pending,
},
};
Expand All @@ -426,7 +427,6 @@ export const VALID_SMART_CONTRACT_EXTERNAL_MODEL: SmartContractModel = {
abi: JSON.stringify("some-abi"),
};


const asset = Asset.fromModel({
asset_id: Coinbase.assets.Eth,
network_id: "base-sepolia",
Expand Down
4 changes: 3 additions & 1 deletion src/tests/webhook_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ describe("Webhook", () => {
it("should update the webhook address list only", async () => {
const webhook = Webhook.init(mockModel);

await webhook.update({ eventTypeFilter: { wallet_id: "test-wallet-id", addresses: ["0x1..", "0x2.."] } });
await webhook.update({
eventTypeFilter: { wallet_id: "test-wallet-id", addresses: ["0x1..", "0x2.."] },
});

expect(Coinbase.apiClients.webhook!.updateWebhook).toHaveBeenCalledWith("test-id", {
notification_uri: "https://example.com/callback",
Expand Down

0 comments on commit 9400b75

Please sign in to comment.