Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
feat(PTP-403): create SDK read implementation
Browse files Browse the repository at this point in the history
* feat(PTP-403): sdk read implementation

* fix(PTP-403): update Auth class

* fix(PTP-403): update SDK
  • Loading branch information
VGau committed May 25, 2022
1 parent dbb32bc commit d25c668
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 564 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"object-curly-newline": "off",
"import/extensions": ["error", "ignorePackages", {
"js": "never"
}]
}],
"arrow-parens": "off"
}
}
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
PROJECT_ID: ${{ secrets.PROJECT_ID }}
SECRET_ID: ${{ secrets.SECRET_ID }}
RPC_URL: ${{ secrets.RPC_URL }}
PUBLIC_ADDRESS: ${{ secrets.PUBLIC_ADDRESS }}

release:
runs-on: ubuntu-latest
Expand Down
101 changes: 0 additions & 101 deletions e2e/createContract.test.js

This file was deleted.

100 changes: 68 additions & 32 deletions e2e/readContract.test.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,96 @@
import { config as loadEnv } from 'dotenv';
import { ExternallyOwnedAccount } from '../lib/NFT/externallyOwnedAccount';
import SDK from '../lib/SDK/SDK';
import Auth from '../lib/Auth/Auth';

loadEnv();

describe('E2E Test: Basic NFT (read)', () => {
describe('E2E Test: SDK (read)', () => {
jest.setTimeout(120 * 1000);
let externallyOwnedAccount;
let sdk;

beforeAll(async () => {
// create the apiKey
const apiKey = Buffer.from(`${process.env.PROJECT_ID}:${process.env.SECRET_ID}`).toString(
'base64',
);

externallyOwnedAccount = new ExternallyOwnedAccount({
beforeAll(() => {
const auth = new Auth({
privateKey: process.env.PRIVATE_KEY,
apiKey,
projectId: process.env.PROJECT_ID,
secretId: process.env.SECRET_ID,
rpcUrl: process.env.RPC_URL,
chainId: 4,
});

sdk = new SDK(auth);
});

describe('As an account I should be able to get the contract metadata', () => {
it('should return the contract metadata', async () => {
const contractMetadata = await sdk.getContractMetadata(
'0xE26a682fa90322eC48eB9F3FA66E8961D799177C',
);
const expectedContractMetadata = { name: 'testContract', symbol: 'TST', tokenType: 'ERC721' };
expect(contractMetadata).toStrictEqual(expectedContractMetadata);
});
});

describe('As an account I should be able to get the list of NFTs by address', () => {
it('should return list of NFTs by address', async () => {
const nfts = await externallyOwnedAccount.getNFTs(
'0xF69c1883b098d621FC58a42E673C4bF6a6483fFf',
);
expect(nfts.assets.length).not.toBe(null);
const nfts = await sdk.getNFTs(process.env.PUBLIC_ADDRESS);
expect(nfts.assets.length).toBeGreaterThan(0);
expect(nfts.assets[0]).not.toHaveProperty('metadata');
});
});

describe('As an account I should be able to get the contract by address', () => {
it('should return a contract abstraction by address', async () => {
const contract = await externallyOwnedAccount.getContractAbstraction(
'0xE26a682fa90322eC48eB9F3FA66E8961D799177C',
);
expect(Object.keys(contract)).toEqual(['deploy', 'mint', 'getSymbol', 'getNFTs']);
describe('As an account I should be able to get the list of NFTs by collection', () => {
it('should return list of NFTs by collection', async () => {
const nfts = await sdk.getNFTsForCollection('0xE26a682fa90322eC48eB9F3FA66E8961D799177C');
expect(nfts.assets.length).toBeGreaterThan(0);
});
});

describe('As an account I should be able to get the collection symbol using the contract abstraction', () => {
it('should return the collection symbol', async () => {
const contract = await externallyOwnedAccount.getContractAbstraction(
describe('As an account I should be able to get the token metadata', () => {
it('should return token metadata', async () => {
const tokenMetadata = await sdk.getTokenMetadata(
'0xE26a682fa90322eC48eB9F3FA66E8961D799177C',
1,
);
expect(await contract.getSymbol()).toEqual('TST');
const expectedTokenMetadata = {
contract: '0xe26a682fa90322ec48eb9f3fa66e8961d799177c',
tokenId: '1',
name: '',
description: '',
image: '',
};

expect(tokenMetadata).toStrictEqual(expectedTokenMetadata);
});
});

describe('As contract account I should be able to get the list of nfts that i created', () => {
it('should return list of nfts for given contract', async () => {
const contract = await externallyOwnedAccount.getContractAbstraction(
'0xE26a682fa90322eC48eB9F3FA66E8961D799177C',
);
const nfts = await contract.getNFTs();
expect(nfts.assets.length).not.toBe(null);
describe('As an account I should be able to get the account ETH balance', () => {
it('should return account ETH balance', async () => {
const ethBalance = await sdk.getEthBalance(process.env.PUBLIC_ADDRESS);
expect(ethBalance).toEqual(expect.any(Number));
});
});

describe('As an account I should be able to get the account ERC20 balances', () => {
it('should return account ERC20 balances', async () => {
const erc20Balance = await sdk.getERC20Balances(process.env.PUBLIC_ADDRESS);

const expectedERC20Balance = {
account: process.env.PUBLIC_ADDRESS,
assets: expect.arrayContaining([
{
balance: expect.any(Number),
contract: '0x0000000000000000000000000000000000000000',
decimals: 18,
name: 'Ethereum',
rawBalance: expect.any(String),
symbol: 'ETH',
},
]),
network: 'Ethereum',
type: 'ERC20',
};

expect(erc20Balance).toStrictEqual(expectedERC20Balance);
});
});
});
43 changes: 24 additions & 19 deletions lib/Auth/index.js → lib/Auth/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@
* MIT Licensed
*/
import { ethers } from 'ethers';
import { availableChains, getChainName } from './availableChains';

export default class Auth {
#privateKey = null;
#privateKey;

#projectId = null;
#projectId;

#secretId = null;
#secretId;

#rpcUrl = null;
#rpcUrl;

#provider = null;
#provider;

#chainId = null;
#chainId;

constructor({ privateKey, projectId, secretId, rpcUrl, chainId }) {
if (!privateKey) throw new Error('[Auth.constructor] privateKey is missing!');
if (!projectId) throw new Error('[Auth.constructor] projectId is missing!');
if (!secretId) throw new Error('[Auth.constructor] secretId is missing!');
if (!chainId) throw new Error('[Auth.constructor] chainId is missing!');
if (!availableChains.includes(chainId)) {
throw new Error(`[Auth.constructor] chainId: ${chainId} is not supported!`);
}

this.#rpcUrl = rpcUrl;

if (rpcUrl) {
this.#rpcUrl = rpcUrl;
if (!this.#rpcUrl) {
this.#rpcUrl = `https://${getChainName(chainId)}.infura.io/v3/${this.#projectId}`;
}

this.#privateKey = privateKey;
this.#projectId = projectId;
this.#secretId = secretId;
this.#chainId = chainId;
// eslint-disable-next-line new-cap
this.#provider = new ethers.providers.getDefaultProvider(this.#rpcUrl);
}

getChainId() {
Expand All @@ -53,24 +61,21 @@ export default class Auth {
}

getSigner() {
if (!this.#provider) throw new Error('[Auth.getSigner] You need to set a provider');
return new ethers.Wallet(this.#privateKey, this.#provider);
}

getProvider(injectedProvider) {
if (!injectedProvider && !this.#rpcUrl) {
getProvider() {
return this.#provider;
}

setInjectedProvider(injectedProvider) {
if (!injectedProvider) {
throw new Error(
'[Auth.getProvider] You need to pass an rpcUrl to the constructor or pass an injected provider to this function!',
'[Auth.setInjectedProvider] You need to pass an injected provider to this function!',
);
}

if (injectedProvider) {
this.#provider = new ethers.providers.Web3Provider(injectedProvider);
return this.#provider;
}

// eslint-disable-next-line new-cap
this.#provider = new ethers.providers.getDefaultProvider(this.#rpcUrl);
this.#provider = new ethers.providers.Web3Provider(injectedProvider);
return this.#provider;
}
}
19 changes: 19 additions & 0 deletions lib/Auth/availableChains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Chains = {
mainnet: 1,
goerli: 5,
rinkeby: 4,
ropsten: 3,
};

const chainsName = {
1: 'mainnet',
5: 'goerli',
4: 'rinkeby',
3: 'ropsten',
};

const availableChains = [Chains.mainnet, Chains.goerli, Chains.rinkeby, Chains.ropsten];

const getChainName = chainId => chainsName[chainId];

export { availableChains, Chains, getChainName };
14 changes: 0 additions & 14 deletions lib/Client/consensysClient.js

This file was deleted.

22 changes: 0 additions & 22 deletions lib/NFT/SDK.js

This file was deleted.

Loading

0 comments on commit d25c668

Please sign in to comment.