@skalenetwork/ima-js
Version:
Simple TS/JS library to interact with SKALE IMA
135 lines (134 loc) • 7.04 kB
JavaScript
/**
* @license
* SKALE ima-js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import MainnetChain from './MainnetChain';
import SChain from './SChain';
import * as constants from './constants';
export { default as MainnetChain } from './MainnetChain';
export { default as SChain } from './SChain';
export * as helper from './helper';
export { default as TimeoutException } from './exceptions/TimeoutException';
export { ERC_ABIS } from './contracts/tokens';
export class IMA {
mainnet;
schain;
constructor(mainnetProvider, sChainProvider, mainnetAbi, sChainAbi) {
this.mainnet = new MainnetChain(mainnetProvider, mainnetAbi);
this.schain = new SChain(sChainProvider, sChainAbi);
}
addERC20Token(tokenName, mainnetContract, sChainContact) {
this.mainnet.erc20.addToken(tokenName, mainnetContract);
this.schain.erc20.addToken(tokenName, sChainContact);
}
addERC721Token(tokenName, mainnetContract, sChainContact) {
this.mainnet.erc721.addToken(tokenName, mainnetContract);
this.schain.erc721.addToken(tokenName, sChainContact);
}
addERC721WithMetadataToken(tokenName, mainnetContract, sChainContact) {
this.mainnet.erc721meta.addToken(tokenName, mainnetContract);
this.schain.erc721meta.addToken(tokenName, sChainContact);
}
addERC1155Token(tokenName, mainnetContract, sChainContact) {
this.mainnet.erc1155.addToken(tokenName, mainnetContract);
this.schain.erc1155.addToken(tokenName, sChainContact);
}
async depositERC20(chainName, tokenName, amount, opts) {
return await this.mainnet.erc20.deposit(chainName, tokenName, amount, opts);
}
async withdrawERC20(tokenName, amount, opts) {
const tokenContract = this.mainnet.erc20.tokens[tokenName];
const tokenContractAddress = await tokenContract.getAddress();
return await this.schain.erc20.withdraw(tokenContractAddress, amount, opts);
}
async depositERC721(chainName, tokenName, tokenId, opts) {
return await this.mainnet.erc721.deposit(chainName, tokenName, tokenId, opts);
}
async withdrawERC721(tokenName, tokenId, opts) {
const tokenContract = this.mainnet.erc721.tokens[tokenName];
const tokenContractAddress = await tokenContract.getAddress();
return await this.schain.erc721.withdraw(tokenContractAddress, tokenId, opts);
}
async depositERC721WithMetadata(chainName, tokenName, tokenId, opts) {
return await this.mainnet.erc721meta.deposit(chainName, tokenName, tokenId, opts);
}
async withdrawERC721Meta(tokenName, tokenId, opts) {
const tokenContract = this.mainnet.erc721meta.tokens[tokenName];
const tokenContractAddress = await tokenContract.getAddress();
return await this.schain.erc721meta.withdraw(tokenContractAddress, tokenId, opts);
}
async depositERC1155(chainName, tokenName, tokenIds, amounts, opts) {
return await this.mainnet.erc1155.deposit(chainName, tokenName, tokenIds, amounts, opts);
}
async withdrawERC1155(tokenName, tokenIds, amounts, opts) {
const tokenContract = this.mainnet.erc1155.tokens[tokenName];
const tokenContractAddress = await tokenContract.getAddress();
return await this.schain.erc1155.withdraw(tokenContractAddress, tokenIds, amounts, opts);
}
// todo: move to .admin or .owner namespace
async linkERC20Token(chainName, originChainName, erc20OnMainnet, erc20OnSchain, opts) {
const isERC20AddedMainnet = await this.mainnet.erc20.isTokenAdded(chainName, erc20OnMainnet);
if (!isERC20AddedMainnet) {
await this.mainnet.erc20.addTokenByOwner(chainName, erc20OnMainnet, opts);
}
const tokenCloneAddress = await this.schain.erc20.getTokenCloneAddress(erc20OnMainnet);
if (tokenCloneAddress === constants.ZERO_ADDRESS) {
await this.schain.erc20.addTokenByOwner(originChainName, erc20OnMainnet, erc20OnSchain, opts);
}
}
async linkERC721Token(chainName, originChainName, erc721OnMainnet, erc721OnSchain, opts) {
const isERC721AddedMainnet = await this.mainnet.erc721.isTokenAdded(chainName, erc721OnMainnet);
if (!isERC721AddedMainnet) {
await this.mainnet.erc721.addTokenByOwner(chainName, erc721OnMainnet, opts);
}
const tokenCloneAddress = await this.schain.erc721.getTokenCloneAddress(erc721OnMainnet);
if (tokenCloneAddress === constants.ZERO_ADDRESS) {
await this.schain.erc721.addTokenByOwner(originChainName, erc721OnMainnet, erc721OnSchain, opts);
}
}
async linkERC721TokenWithMetadata(chainName, originChainName, erc721OnMainnet, erc721OnSchain, opts) {
const isERC721AddedMainnet = await this.mainnet.erc721meta.isTokenAdded(chainName, erc721OnMainnet);
if (!isERC721AddedMainnet) {
await this.mainnet.erc721meta.addTokenByOwner(chainName, erc721OnMainnet, opts);
}
const tokenCloneAddress = await this.schain.erc721meta.getTokenCloneAddress(erc721OnMainnet);
if (tokenCloneAddress === constants.ZERO_ADDRESS) {
await this.schain.erc721meta.addTokenByOwner(originChainName, erc721OnMainnet, erc721OnSchain, opts);
}
}
async linkERC1155Token(chainName, originChainName, erc1155OnMainnet, erc1155OnSchain, opts) {
const isERC1155AddedMainnet = await this.mainnet.erc1155.isTokenAdded(chainName, erc1155OnMainnet);
if (!isERC1155AddedMainnet) {
await this.mainnet.erc1155.addTokenByOwner(chainName, erc1155OnMainnet, opts);
}
const tokenCloneAddress = await this.schain.erc1155.getTokenCloneAddress(erc1155OnMainnet);
if (tokenCloneAddress === constants.ZERO_ADDRESS) {
await this.schain.erc1155.addTokenByOwner(originChainName, erc1155OnMainnet, erc1155OnSchain, opts);
}
}
async connectSchain(chainName, opts) {
const contractAddresses = [
this.schain.tokenManagerLinker.address,
this.schain.communityLocker.address,
this.schain.eth.address,
this.schain.erc20.address,
this.schain.erc721.address,
this.schain.erc721meta.address,
this.schain.erc1155.address
];
return await this.mainnet.linker.connectSchain(chainName, contractAddresses, opts);
}
}