Skip to content

Commit

Permalink
v3Bridge evm claim
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAdoniev committed Nov 8, 2023
1 parent a268fbf commit 7c1d9f1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export namespace Chain {
export enum v3_ChainId {
BSC = "BSC",
POLYGON = "MATIC",
ETHEREUM = "ETH",
DEFAULT = "",
}

Expand Down Expand Up @@ -329,7 +330,7 @@ CHAIN_INFO.set(Chain.BSC, {
CHAIN_INFO.set(Chain.ETHEREUM, {
name: "Ethereum",
nonce: 5,
v3_chainId: v3_ChainId.DEFAULT,
v3_chainId: v3_ChainId.ETHEREUM,
currency: SupportedCurrency.ETH,
currencySymbol: SupportedCurrencyName.ETH,
chainId: 5,
Expand Down
1 change: 1 addition & 0 deletions src/factory/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export namespace ChainFactoryConfigs {
erc1155Minter: "0xE90105827d04522e52AdfA6BF695730E5706C0C2",
erc721Minter: "0x90d38996B210D45bDF2FD54d091C6061dff0dA9F",
minter_addr: "0x04a5f9158829Cae5a0a549954AdEaBD47BbB3d2d",
v3_bridge: "0x7935f469298002c0Fb11F853B415B08aDCaC9Cf7",
nonce: Chain.ETHEREUM,
feeMargin,
},
Expand Down
43 changes: 36 additions & 7 deletions src/factory/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Chain, CHAIN_INFO, ChainType } from "../consts";
import { ElrondParams } from "../helpers/elrond";
import { TronParams } from "../helpers/tron";
import { Web3Params, getClaimFee } from "../helpers/evm/web3";
import { Web3Params } from "../helpers/evm/web3";

export * from "./utils";
export * from "./factories";
Expand Down Expand Up @@ -71,6 +71,7 @@ import {
InferNativeNft,
InferSigner,
ParamMap,
V3_ChainId,
} from "../type-utils";
import {
getDefaultContract,
Expand All @@ -79,6 +80,8 @@ import {
} from "./utils";

import { CasperParams } from "../helpers/casper/casper";
import { BridgeStorage__factory } from "xpnet-web3-contracts/dist/v3/factories/contracts/BridgeStorage__factory";
import { BridgeStorage } from "xpnet-web3-contracts/dist/v3";

export type FullChain<Signer, RawNft, Resp> = TransferNftForeign<
Signer,
Expand Down Expand Up @@ -321,7 +324,8 @@ export type ChainFactory = {
): Promise<Resp | undefined>;

estimateClaimFee(
fromChain: FullChain<never, unknown, unknown>
fromChain: FullChain<never, unknown, unknown>,
storageContract: BridgeStorage | undefined
): Promise<string>;
};

Expand Down Expand Up @@ -449,6 +453,26 @@ function mapNonceToParams(chainParams: Partial<ChainParams>): ParamMap {
* @param chainParams: {@link ChainParams} Contains the details for all the chains to mint and transfer NFTs between them.
* @returns {ChainFactory}: A factory object that can be used to mint and transfer NFTs between chains.
*/

export const getStorageContract = (config: AppConfig) => {
const provider = new ethers.providers.JsonRpcProvider(config.storegeNetwork);
const storageContract = BridgeStorage__factory.connect(
config.storageContract,
provider
);

return storageContract;
};

export const getClaimFee = async (
toChain: V3_ChainId,
storageContract: BridgeStorage
) => {
const fee = await storageContract.chainFee(toChain);

return String(fee);
};

export function ChainFactory(
appConfig: AppConfig,
chainParams: Partial<ChainParams>
Expand Down Expand Up @@ -737,11 +761,15 @@ export function ChainFactory(
}

const estimateClaimFee = async (
fromChain: FullChain<never, unknown, unknown>
fromChain: FullChain<never, unknown, unknown>,
storageContract: BridgeStorage | undefined
) => {
if (!storageContract) {
storageContract = getStorageContract(appConfig);
}
const v3_chainId = CHAIN_INFO.get(fromChain.getNonce())!.v3_chainId;
if (!v3_chainId) return "0";
return await getClaimFee(v3_chainId, appConfig);
return await getClaimFee(v3_chainId, storageContract);
};

return {
Expand Down Expand Up @@ -1120,9 +1148,10 @@ export function ChainFactory(
return await from.lockNFT(signer, toChain.v3_chainId, nft, receiver);
},
async claimNFT(from, to, txHash, signer, fee) {
const fromChain = CHAIN_INFO.get(from.getNonce())!;
if (!fee) fee = await estimateClaimFee(from);
return await to.claimV3NFT(signer, fromChain.v3_chainId, txHash, fee);
const storageContract = getStorageContract(appConfig);
if (!fee) fee = await estimateClaimFee(from, storageContract);

return await to.claimV3NFT(signer, from, txHash, storageContract, fee);
},
estimateClaimFee,
};
Expand Down
26 changes: 24 additions & 2 deletions src/helpers/chain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import BigNumber from "bignumber.js";
import { ethers } from "ethers";
import { ChainNonce, V3_ChainId } from "../type-utils";
import { FullChain } from "../factory";
import { BridgeStorage } from "xpnet-web3-contracts/dist/v3";

/**
* NFT Info
Expand Down Expand Up @@ -64,12 +66,32 @@ export interface LockNFT<Signer, RawNft, Resp> {
): Promise<Resp | undefined>;
}

export type ClaimData = {
tokenId: string;
destinationChain: V3_ChainId;
destinationUserAddress: string;
sourceNftContractAddress: string;
tokenAmount: string;
nftType: "singular" | "multiple";
sourceChain: V3_ChainId;
royalty: string;
royaltyReceiver: string;
metadata: string;
name: string;
symbol: string;
};

export interface GetClaimData<BridgeType> {
getClaimData(hash: string, bridge: BridgeType): Promise<ClaimData>;
}

export interface ClaimV3NFT<Signer, Resp> {
claimV3NFT(
sender: Signer,
fromChain: V3_ChainId,
fromChain: FullChain<never, unknown, unknown>,
txHash: string,
fee?: string
storageContract: BridgeStorage,
fee: string
): Promise<Resp | undefined>;
}

Expand Down
Loading

0 comments on commit 7c1d9f1

Please sign in to comment.