SDK Package
Install
yarn add blindbox-sdk
yarn upgrade blindbox-sdk
Browser with Metamask
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import { BigNumber, ethers, Signer } from "ethers";
import { Web3Provider } from "@ethersproject/providers";
import detectEthereumProvider from "@metamask/detect-provider";
import {
riverBoxProvider,
RiverBoxClient,
RiverBoxExClient,
ContractBuyResult,
ContractFuseResult,
ContractTokenDetail,
ContractDealDetail,
riverBoxExProvider,
} from "blindbox-sdk";
const RIVERBOX_CONTRACT_ADDRESS = "0x0f522004467899fcbde46d3dec324e8f3238db14";
const RIVERBOX_EX_CONTRACT_ADDRESS = "0xA54eB5997260aeE8BC6b572B7CCdb599bad27280";
function App() {
const [sym, setSym] = React.useState("");
React.useEffect(() => {
const fetchSym = async () => {
const winProvider: any = await detectEthereumProvider();
if (winProvider) {
const provider: Web3Provider = new ethers.providers.Web3Provider(
winProvider as any
);
const client: RiverBoxClient = riverBoxProvider(false);
const exClient: RiverBoxExClient = riverBoxExProvider(false);
client.connectProvider(RIVERBOX_CONTRACT_ADDRESS, provider);
exClient.connectProvider(RIVERBOX_EX_CONTRACT_ADDRESS, provider);
try {
console.log(await provider.getSigner().getAddress());
} catch (error) {
console.log(error);
}
try {
const sym = (await client.symbol()) + (await client.totalSupply());
setSym(sym);
console.log(sym);
} catch (error) {
console.log(error);
}
client.setWaitConfirmations(1);
await winProvider.request({ method: "eth_requestAccounts" });
console.log(await provider.getSigner().getAddress());
const signer: Signer = provider.getSigner();
try {
const currentPrice = await client.connectSigner(signer).boxPrice();
console.log("currentPrice:", currentPrice.toNumber() / 1e18, "ETH");
const ret: ContractBuyResult = await client
.connectSigner(signer)
.buy(BigNumber.from(10), { value: currentPrice.mul(10) });
console.log(ret);
const currentPaidBoxes = await client
.connectSigner(signer)
.paidBoxes(await signer.getAddress());
console.log("currentPaidBoxes:", currentPaidBoxes.toString());
const tokenId = ret.newTokenIds[0];
const postDealResult = await exClient.connectSigner(signer).postDeal(tokenId, currentPrice.mul(2))
console.log(postDealResult);
} catch (error) {
console.log(error);
}
}
};
fetchSym();
}, []);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React {sym}
</a>
</header>
</div>
);
}
export default App;
Node with Ethers Provider
JsonRpcProvider or WebSocketProvider or APIProviders:
import { riverBoxProvider, IClient } from 'blindbox-sdk';
import { WebSocketProvider } from '@ethersproject/providers';
import { ethers, Signer, Wallet } from 'ethers';
const CONTRACT_ADDRESS = '0x5FbDB2315678afecb367f032d93F642f64180aa3';
const PRIVATE_KEY =
'0xdf57089febbacf7ba0bc227dafbffa9fc08a93fdc68e1e42411a14efcf23656e';
const provider: WebSocketProvider = new ethers.providers.WebSocketProvider(
'http://localhost:8545'
);
const signer: Wallet = new Wallet(PRIVATE_KEY).connect(provider);
const client: IClient = riverBoxProvider(CONTRACT_ADDRESS, provider, signer);
const client = await client.symbol();
API Reference:
Interface
export interface IBuyResult {
transactionHash: string;
newTokenIds: BigNumber[];
}
export interface IFuseResult {
transactionHash: string;
newTokenId: BigNumber;
}
export interface IAwardedBoxEvent {
payer: string;
tokenId: BigNumber;
}
export interface IProductDetail {
locationId: BigNumber;
signature: BigNumber;
creationTime: BigNumber;
fused: BigNumber;
parts: BigNumber[];
}
export interface IClient {
connectProvider(address: string, provider: Provider): IClient;
connectSigner(signer: Signer): IClient;
setWaitConfirmations(num: number): void;
contract?(): RiverBox | undefined;
boxPrice(
): Promise<BigNumber>;
tokenDetails(
tokenId: BigNumber,
config?: PayableOverrides
): Promise<IProductDetail>;
verifyFusion(
tokenIds: BigNumber[],
config?: PayableOverrides
): Promise<boolean>;
tokenOfOwnerByIndexPagination(
owner: string,
startIndex: BigNumber,
length: BigNumber,
config?: PayableOverrides
): Promise<BigNumber[]>;
tokenOfIndexPagination(
startIndex: BigNumber,
length: BigNumber,
config?: PayableOverrides
): Promise<BigNumber[]>;
paidBoxes(
account: string,
config?: PayableOverrides
): Promise<BigNumber>;
buy(quality: BigNumber, config?: PayableOverrides): Promise<IBuyResult>;
fuse(tokenIds: BigNumber[], config?: PayableOverrides): Promise<IFuseResult>;
balanceOf(owner: string, config?: PayableOverrides): Promise<BigNumber>;
ownerOf(tokenId: BigNumber, config?: PayableOverrides): Promise<string>;
name(config?: PayableOverrides): Promise<string>;
symbol(config?: PayableOverrides): Promise<string>;
tokenURI(tokenId: BigNumber, config?: PayableOverrides): Promise<string>;
tokenOfOwnerByIndex(
owner: string,
index: BigNumber,
config?: PayableOverrides
): Promise<BigNumber>;
totalSupply(config?: PayableOverrides): Promise<BigNumber>;
tokenByIndex(index: BigNumber, config?: PayableOverrides): Promise<BigNumber>;
}
}
MockClient Example
Usage - (MockClient (sdk-mock.spec.ts) output shown by test console output)
(base) ➜ sdk git:(main) ✗ yarn run test
yarn run v1.22.10
$ mocha
RiverBox
view functions
====================================
{
locationId: BigNumber { _hex: '0x01', _isBigNumber: true },
signature: '1111111111111',
creationTime: BigNumber { _hex: '0x0179c141fdd4', _isBigNumber: true },
fused: BigNumber { _hex: '0x00', _isBigNumber: true },
parts: []
}
✓ test: tokenDetails(BigNumber.from(0))
====================================
Mock client: ids.length <= 1 -> false, ids.length > 1 -> true
false
✓ test: verifyFusion([BigNumber.from(0)])
====================================
Mock client: ids.length <= 1 -> false, ids.length > 1 -> true
true
✓ test: verifyFusion([BigNumber.from(0), BigNumber.from(1)])
====================================
[
BigNumber { _hex: '0x64', _isBigNumber: true },
BigNumber { _hex: '0x65', _isBigNumber: true },
BigNumber { _hex: '0x66', _isBigNumber: true },
BigNumber { _hex: '0x67', _isBigNumber: true },
BigNumber { _hex: '0x68', _isBigNumber: true }
]
✓ test: client.tokenOfOwnerByIndexPagination(FAKE_ACCOUNT, BigNumber.from(0), BigNumber.from(5))
====================================
[
BigNumber { _hex: '0x64', _isBigNumber: true },
BigNumber { _hex: '0x65', _isBigNumber: true },
BigNumber { _hex: '0x66', _isBigNumber: true },
BigNumber { _hex: '0x67', _isBigNumber: true },
BigNumber { _hex: '0x68', _isBigNumber: true }
]
✓ test: client.tokenOfIndexPagination(BigNumber.from(0), BigNumber.from(5))
====================================
BigNumber { _hex: '0x64', _isBigNumber: true }
✓ test: client.balanceOf(FAKE_ACCOUNT)
====================================
0x000000000000000000000000000000000000000000
✓ test: client.ownerOf(BigNumber.from(0))
====================================
RiverBox
✓ test: client.name()
====================================
RB
✓ test: client.symbol()
====================================
http://metadata.resolve/0
✓ test: client.tokenURI(BigNumber.from(0))
====================================
BigNumber { _hex: '0x6e', _isBigNumber: true }
✓ test: client.tokenOfOwnerByIndex(FAKE_ACCOUNT, BigNumber.from(10))
====================================
BigNumber { _hex: '0x64', _isBigNumber: true }
✓ test: client.totalSupply()
====================================
BigNumber { _hex: '0x6e', _isBigNumber: true }
✓ test: client.tokenByIndex(BigNumber.from(10))
====================================
✓ clean up
transactions
====================================
{
transactionHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
newTokenIds: [
BigNumber { _hex: '0xc8', _isBigNumber: true },
BigNumber { _hex: '0xc9', _isBigNumber: true },
BigNumber { _hex: '0xca', _isBigNumber: true }
]
}
✓ test: buy(BigNumber.from(3))
====================================
{
transactionHash: '0x1111111111111111111111111111111111111111111111111111111111111111',
newTokenId: BigNumber { _hex: '0xcb', _isBigNumber: true }
}
✓ test: fuse([BigNumber.from(0), BigNumber.from(1)])
====================================
✓ clean up
SDK-Package:
Build:
yarn run build
Test:
Start local hardhat node first and deploy the contract
CONTRACT_ADDRESS={ADDRESS} yarn run test