Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
chore: add examples folder (#195)
Browse files Browse the repository at this point in the history
* chore: add examples folder

[DX-1308]
  • Loading branch information
zatlite authored Oct 25, 2022
1 parent 50a5920 commit 827da94
Show file tree
Hide file tree
Showing 18 changed files with 1,138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PRIVATE_KEY=
STARK_PRIVATE_KEY=
ALCHEMY_API_KEY=
120 changes: 120 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<div align="center">
<a href="https://www.immutable.com">
<img width="150" src="https://assets-global.website-files.com/5f7eec37ff782e797edabe11/5f8d36771ffcf8c91b03e7f4_dark.svg">
</a>
<br>
<br>
</div>

---

# Examples
This folder contains code examples for usage of `@imtbl/core-sdk`

# How to run the sample code

```sh
git clone https://github.com/immutable/imx-core-sdk.git

cd imx-core-sdk/examples

yarn install
```

Create a `.env` file similar to [.env.example](./.env.example)

```sh
cp .env.example .env
```

Once you have saved the necessary values in your `.env` file, follow the instructions below to run.

## Deposit Eth

Update [depositEth.ts](./depositEth.ts#L14) with your deposit amount then run

```sh
yarn deposit-eth
```

## Prepare Eth Withdrawal

Update [createEthWithdrawal.ts](./createEthWithdrawal.ts#L12) with your withdrawal amount then run

```sh
yarn create-eth-withdrawal
```


## Complete Eth Withdrawal

Check [completeEthWithdrawal.ts](./completeEthWithdrawal.ts#L12) then run

```sh
yarn complete-eth-withdrawal
```

## Create project

Update [createProject.ts](./createProject.ts#L9) with your project details then run

```sh
yarn create-project
```

## Create collection

Update [createCollection.ts](./createCollection.ts#L15) with your collection details then run

```sh
yarn create-collection
```

## Mint

Update [mint.ts](./mint.ts#L9) with your minting details then run

```sh
yarn mint
```

## Create order

Update [createOrder.ts](./createOrder.ts#L9) with your order details then run

```sh
yarn create-order
```

## Create trade

Update [createTrade.ts](./createTrade.ts#L9) with your trade details then run

```sh
yarn create-trade
```

## Transfer Nfts

Update [transferNfts.ts](./transferNfts.ts#L13) with your transfer details then run

```sh
yarn transfer-nfts
```

## Transfer Eth

Update [transferEth.ts](./transferEth.ts#L10) with your transfer details then run

```sh
yarn transfer-eth
```

## Transfer ERC20

Update [transferErc20.ts](./transferErc20.ts#L10) with your transfer details then run

```sh
yarn transfer-erc20
```

25 changes: 25 additions & 0 deletions examples/completeEthWithdrawal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Config, ImmutableX } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
try {
const walletConnection = await generateWalletConnection('goerli');

const client = new ImmutableX(Config.SANDBOX);

const starkPublicKey = await walletConnection.starkSigner.getAddress();

const completeWithdrawalResponse = await client.completeWithdrawal(
walletConnection.ethSigner,
starkPublicKey,
{
type: 'ETH',
},
);

console.log('completeWithdrawalResponse', completeWithdrawalResponse);
} catch (err) {
console.error(err);
process.exit(1);
}
})();
39 changes: 39 additions & 0 deletions examples/createCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { AlchemyProvider } from '@ethersproject/providers';
import { Wallet } from '@ethersproject/wallet';
import { ImmutableX, Config, CreateCollectionRequest } from '@imtbl/core-sdk';
import { requireEnvironmentVariable } from './libs/utils';

(async () => {
const privateKey = requireEnvironmentVariable('PRIVATE_KEY');
const alchemyKey = requireEnvironmentVariable('ALCHEMY_API_KEY');
const wallet = new Wallet(privateKey);
const provider = new AlchemyProvider('goerli', alchemyKey);
const signer = wallet.connect(provider);

const imxClient = new ImmutableX(Config.SANDBOX);

const createCollectionParams: CreateCollectionRequest = {
contract_address: '',
name: '',
owner_public_key: wallet.publicKey,
project_id: 0,
};

try {
const createCollectionResponse = await imxClient.createCollection(
signer,
createCollectionParams,
);

console.log('createCollectionResponse', JSON.stringify(createCollectionResponse));

const getCollectionResponse = await imxClient.getCollection({
address: createCollectionResponse.address,
});

console.log('getCollectionResponse', JSON.stringify(getCollectionResponse));
} catch (error) {
console.error(error);
process.exit(1);
}
})();
25 changes: 25 additions & 0 deletions examples/createEthWithdrawal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Config, ImmutableX, TokenAmount } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
try {
const walletConnection = await generateWalletConnection('goerli');

const client = new ImmutableX(Config.SANDBOX);

const tokenAmount: TokenAmount = {
type: 'ETH',
amount: '',
};

const createWithdrawalResponse = await client.prepareWithdrawal(
walletConnection,
tokenAmount,
);

console.log('createWithdrawalResponse', createWithdrawalResponse);
} catch (err) {
console.error(err);
process.exit(1);
}
})();
32 changes: 32 additions & 0 deletions examples/createOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ImmutableX, Config, UnsignedOrderRequest } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
const walletConnection = await generateWalletConnection('goerli');

const imxClient = new ImmutableX(Config.SANDBOX);

const orderParams: UnsignedOrderRequest = {
buy: {
type: 'ETH',
amount: '',
},
sell: {
type: 'ERC721',
tokenAddress: '',
tokenId: '',
},
};

try {
const orderResponse = await imxClient.createOrder(
walletConnection,
orderParams,
);

console.log('orderResponse', orderResponse);
} catch (error) {
console.error(error);
process.exit(1);
}
})();
33 changes: 33 additions & 0 deletions examples/createProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ImmutableX, Config, CreateProjectRequest } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
const walletConnection = await generateWalletConnection('goerli');

const imxClient = new ImmutableX(Config.SANDBOX);

const projectParams: CreateProjectRequest = {
company_name: '',
contact_email: '',
name: '',
};

try {
const createProjectResponse = await imxClient.createProject(
walletConnection.ethSigner,
projectParams,
);

console.log('createProjectResponse', JSON.stringify(createProjectResponse));

const getProjectResponse = await imxClient.getProject(
walletConnection.ethSigner,
createProjectResponse.id.toString(),
);

console.log('getProjectResponse', JSON.stringify(getProjectResponse));
} catch (error) {
console.error(error);
process.exit(1);
}
})();
25 changes: 25 additions & 0 deletions examples/createTrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ImmutableX, Config, GetSignableTradeRequest } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
const walletConnection = await generateWalletConnection('goerli');

const imxClient = new ImmutableX(Config.SANDBOX);

const tradeParams: GetSignableTradeRequest = {
order_id: 0,
user: '',
};

try {
const tradeResponse = await imxClient.createTrade(
walletConnection,
tradeParams,
);

console.log('tradeResponse', tradeResponse);
} catch (error) {
console.error(error);
process.exit(1);
}
})();
22 changes: 22 additions & 0 deletions examples/depositEth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Config, ImmutableX } from '@imtbl/core-sdk';
import { generateWalletConnection } from './libs/walletConnection';

(async () => {
try {
const walletConnection = await generateWalletConnection('goerli');

// IMX class client
const client = new ImmutableX(Config.SANDBOX);

// Deposit ETH
const depositResponse = await client.deposit(walletConnection.ethSigner, {
type: 'ETH',
amount: '',
});

console.log('depositResponse', depositResponse);
} catch (err) {
console.error(err);
process.exit(1);
}
})();
22 changes: 22 additions & 0 deletions examples/libs/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function getEnv(
name: string,
defaultValue: string | undefined = undefined,
): string {
const value = process.env[name];

if (value !== undefined) {
return value;
}
if (defaultValue !== undefined) {
return defaultValue;
}
throw new Error(`Environment variable '${name}' not set`);
}

export function requireEnvironmentVariable(key: string): string {
const value = getEnv(key);
if (!value) {
throw new Error(`Please ensure a value exists for ${key}`);
}
return value;
}
29 changes: 29 additions & 0 deletions examples/libs/walletConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AlchemyProvider } from '@ethersproject/providers';
import { Wallet } from '@ethersproject/wallet';
import { createStarkSigner, WalletConnection } from '@imtbl/core-sdk';
import { requireEnvironmentVariable } from './utils';

/**
* Generate a ethSigner/starkSigner object from a private key.
*/
export const generateWalletConnection = async (
ethNetwork: string,
): Promise<WalletConnection> => {
const userPrivateKey = requireEnvironmentVariable('PRIVATE_KEY');
const userStarkKey = requireEnvironmentVariable('STARK_PRIVATE_KEY')
const alchemyKey = requireEnvironmentVariable('ALCHEMY_API_KEY');

// connect provider
const provider = new AlchemyProvider(ethNetwork, alchemyKey);

// L1 credentials
const ethSigner = new Wallet(userPrivateKey).connect(provider);

// L2 credentials
const starkSigner = createStarkSigner(userStarkKey);

return {
ethSigner,
starkSigner,
};
};
Loading

0 comments on commit 827da94

Please sign in to comment.