Skip to content

Commit

Permalink
Merge branch 'decentralized-stable' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
D4mph1r authored Oct 4, 2024
2 parents ee10267 + fe6626a commit 781ced2
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 62 deletions.
107 changes: 62 additions & 45 deletions src/contractsTypes/icp/bridge/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const idlFactory = ({ IDL }) => {
'destination_chain' : IDL.Text,
'token_id' : IDL.Nat,
'source_nft_contract_address' : IDL.Principal,
'sender_address' : IDL.Text,
'metadata_uri' : IDL.Text,
'nft_type' : IDL.Text,
'destination_user_address' : IDL.Text,
});
Expand All @@ -50,51 +52,66 @@ export const idlFactory = ({ IDL }) => {
'pending_rewards' : IDL.Nat64,
});
const XPBridge = IDL.Service({
acceptCycles: IDL.Func([], [], []),
add_validator: IDL.Func(
[AddValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
availableCycles: IDL.Func([], [IDL.Nat], ["query"]),
blacklist_validator: IDL.Func(
[BlacklistValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
claim_nft: IDL.Func(
[ClaimData, IDL.Vec(SignerAndSignature)],
[IDL.Text],
[],
),
claim_validator_rewards: IDL.Func([IDL.Text], [IDL.Nat64, IDL.Nat64], []),
encode_add_validator: IDL.Func(
[AddValidator],
[IDL.Vec(IDL.Nat8)],
["query"],
),
encode_blacklist_validator: IDL.Func(
[BlacklistValidator],
[IDL.Vec(IDL.Nat8)],
["query"],
),
encode_claim_data: IDL.Func([ClaimData], [IDL.Vec(IDL.Nat8)], ["query"]),
get_blacklisted_validators: IDL.Func(
[IDL.Text],
[IDL.Opt(IDL.Bool)],
["query"],
),
get_claimed_data: IDL.Func([IDL.Text], [IDL.Opt(ClaimedEvent)], ["query"]),
get_locked_data: IDL.Func([IDL.Text], [IDL.Opt(LockedEvent)], ["query"]),
get_validator: IDL.Func([IDL.Text], [IDL.Opt(Validator)], ["query"]),
get_validator_count: IDL.Func([], [IDL.Nat], ["query"]),
get_nonce: IDL.Func([], [IDL.Nat], ["query"]),
init: IDL.Func([], [], []),
lock_nft: IDL.Func(
[IDL.Principal, IDL.Nat, IDL.Text, IDL.Text, IDL.Text],
[IDL.Text],
[],
),
'acceptCycles' : IDL.Func([], [], []),
'add_validator' : IDL.Func(
[AddValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
'availableCycles' : IDL.Func([], [IDL.Nat], ['query']),
'blacklist_validator' : IDL.Func(
[BlacklistValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
'claim_nft' : IDL.Func(
[ClaimData, IDL.Vec(SignerAndSignature)],
[IDL.Text],
[],
),
'claim_validator_rewards' : IDL.Func(
[IDL.Text],
[IDL.Nat64, IDL.Nat64],
[],
),
'encode_add_validator' : IDL.Func(
[AddValidator],
[IDL.Vec(IDL.Nat8)],
['query'],
),
'encode_blacklist_validator' : IDL.Func(
[BlacklistValidator],
[IDL.Vec(IDL.Nat8)],
['query'],
),
'encode_claim_data' : IDL.Func([ClaimData], [IDL.Vec(IDL.Nat8)], ['query']),
'get_blacklisted_validators' : IDL.Func(
[IDL.Text],
[IDL.Opt(IDL.Bool)],
['query'],
),
'get_claim_nonce' : IDL.Func([], [IDL.Nat], ['query']),
'get_claimed_data' : IDL.Func(
[IDL.Text],
[IDL.Opt(ClaimedEvent)],
['query'],
),
'get_hash_from_claim_nonce' : IDL.Func(
[IDL.Nat],
[IDL.Opt(IDL.Text)],
['query'],
),
'get_hash_from_nonce' : IDL.Func([IDL.Nat], [IDL.Opt(IDL.Text)], ['query']),
'get_locked_data' : IDL.Func([IDL.Text], [IDL.Opt(LockedEvent)], ['query']),
'get_nonce' : IDL.Func([], [IDL.Nat], ['query']),
'get_validator' : IDL.Func([IDL.Text], [IDL.Opt(Validator)], ['query']),
'get_validator_count' : IDL.Func([], [IDL.Nat], ['query']),
'init' : IDL.Func([], [], []),
'lock_nft' : IDL.Func(
[IDL.Principal, IDL.Nat, IDL.Text, IDL.Text, IDL.Text],
[IDL.Text],
[],
),
});
return XPBridge;
};
Expand Down
25 changes: 15 additions & 10 deletions src/contractsTypes/icp/bridge/bridge.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface LockedEvent {
'destination_chain' : string,
'token_id' : bigint,
'source_nft_contract_address' : Principal,
'sender_address' : string,
'metadata_uri' : string,
'nft_type' : string,
'destination_user_address' : string,
}
Expand All @@ -59,16 +61,19 @@ export interface XPBridge {
encode_blacklist_validator: ActorMethod<
[BlacklistValidator],
Uint8Array | number[]
>;
encode_claim_data: ActorMethod<[ClaimData], Uint8Array | number[]>;
get_blacklisted_validators: ActorMethod<[string], [] | [boolean]>;
get_claimed_data: ActorMethod<[string], [] | [ClaimedEvent]>;
get_locked_data: ActorMethod<[string], [] | [LockedEvent]>;
get_validator: ActorMethod<[string], [] | [Validator]>;
get_validator_count: ActorMethod<[], bigint>;
get_nonce: ActorMethod<[], bigint>;
init: ActorMethod<[], undefined>;
lock_nft: ActorMethod<[Principal, bigint, string, string, string], string>;
>,
'encode_claim_data' : ActorMethod<[ClaimData], Uint8Array | number[]>,
'get_blacklisted_validators' : ActorMethod<[string], [] | [boolean]>,
'get_claim_nonce' : ActorMethod<[], bigint>,
'get_claimed_data' : ActorMethod<[string], [] | [ClaimedEvent]>,
'get_hash_from_claim_nonce' : ActorMethod<[bigint], [] | [string]>,
'get_hash_from_nonce' : ActorMethod<[bigint], [] | [string]>,
'get_locked_data' : ActorMethod<[string], [] | [LockedEvent]>,
'get_nonce' : ActorMethod<[], bigint>,
'get_validator' : ActorMethod<[string], [] | [Validator]>,
'get_validator_count' : ActorMethod<[], bigint>,
'init' : ActorMethod<[], undefined>,
'lock_nft' : ActorMethod<[Principal, bigint, string, string, string], string>,
}
export interface _SERVICE extends XPBridge {}
export declare const idlFactory: IDL.InterfaceFactory;
Expand Down
14 changes: 11 additions & 3 deletions src/factory/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export namespace ChainFactoryConfigs {
storage,
},
tezosParams: {
bridge: "KT1Fh6VH8BxA9xgJCn8hGZRxoo2vY3hrbqeV",
bridge: "KT1Gm6qaUmfuVnGJmfY46K6hiUzgFP8DLcke",
identifier: "TEZOS",
storage,
Tezos: new TezosToolkit("https://rpc.ghostnet.teztnets.com"),
Tezos: new TezosToolkit("https://ghostnet.ecadinfra.com"),
tzktApi: "https://api.ghostnet.tzkt.io/",
},
multiversxParams: {
Expand Down Expand Up @@ -178,12 +178,20 @@ export namespace ChainFactoryConfigs {
storage,
},
tezosParams: {
bridge: "KT1N3qtcfxzNMhdRMphvtDCKLEDwVLhZ8Cpq",
bridge: "KT1UMVUP3XBpPrMUwuC6DXjEcLgZykV7p1PW",
identifier: Chain.TEZOS,
storage,
Tezos: new TezosToolkit("https://mainnet.ecadinfra.com"),
tzktApi: "https://api.tzkt.io/",
},
icpParams: {
agent: new HttpAgent({
host: "https://ic0.app",
}),
bridge: Principal.fromText("6gfde-pqaaa-aaaal-al76q-cai"),
identifier: "ICP",
storage,
},
} satisfies Partial<TChainParams>;
}
}
18 changes: 18 additions & 0 deletions src/handlers/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ERC721Royalty__factory,
ERC1155Royalty__factory,
} from "../../contractsTypes/evm";
import { getPolygonGas } from "../gasFactories/polygon";
import { retryFn } from "../utils";

import { TEvmHandler, TEvmParams } from "./types";
Expand All @@ -21,6 +22,23 @@ export function evmHandler({
return Promise.resolve(isAddress(address));
},
async claimNft(wallet, claimData, sigs, extraArgs) {
if (identifier === "MATIC") {
const gas = await getPolygonGas();
const contract = Bridge__factory.connect(bridge, wallet);
const ret = await contract.claimNFT721(
claimData,
sigs.map((e) => e.signature),
{
...extraArgs,
...gas,
value: claimData.fee,
},
);
return {
ret: ret,
hash: () => ret.hash,
};
}
const contract = Bridge__factory.connect(bridge, wallet);
const ret = await contract.claimNFT721(
claimData,
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/evm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Overrides,
Provider,
Signer,
Wallet,
} from "ethers";
import { Bridge, BridgeStorage } from "../../contractsTypes/evm";

Expand Down Expand Up @@ -36,7 +37,7 @@ export type TEvmHandler = TNftChain<
PayableOverrides,
ContractTransactionResponse
> &
TApproveNFT<Signer, PayableOverrides, ContractTransactionResponse> &
TApproveNFT<Signer | Wallet, PayableOverrides, ContractTransactionResponse> &
DeployCollection<
Signer,
{
Expand Down
29 changes: 29 additions & 0 deletions src/handlers/gasFactories/polygon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import axios from "axios";
import {
GAS_LIMIT,
TGas,
convertToGwei,
getConstantGas,
getGas,
} from "./utils";

const getConstantGasForPolygon = (): TGas => getConstantGas(800);

const getGasFromGasStation = async (): Promise<TGas> => {
const response = await axios.get("https://gasstation.polygon.technology/v2");
console.log("polygon gas response", response.data);

const gas = convertToGwei(String(response?.data?.fast.maxFee));

return {
maxPriorityFeePerGas: gas,
maxFeePerGas: gas,
gasLimit: GAS_LIMIT,
};
};

export const getPolygonGas = async (): Promise<TGas> => {
const gas = await getGas(getGasFromGasStation, getConstantGasForPolygon);
console.log({ gas }, "gas in internal polygon");
return gas;
};
60 changes: 60 additions & 0 deletions src/handlers/gasFactories/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { BigNumberish as EthersBigNumber, ethers } from "ethers";

export type TGas = {
maxPriorityFeePerGas: EthersBigNumber;
maxFeePerGas: EthersBigNumber;
gasLimit: number;
};

type TArgs = [
getGasStationGas: () => Promise<TGas>,
getConstantGas: () => TGas,
];

type TGetGas = (...args: TArgs) => Promise<TGas>;

export const GAS_LIMIT = 5_000_000;

export type TCreateGasConfig = {
chainNonce: number;
gasToAdd: number;
constantGas: number;
gasStationEndpoint: string;
};

export const convertToGwei = (value: string): EthersBigNumber => {
return ethers.parseUnits(value, "gwei");
};

export const getConstantGas = (fee: number): TGas => {
const maxPriorityFeePerGas = convertToGwei(`${fee}`);
const maxFeePerGas = convertToGwei(`${fee}`);
return {
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit: GAS_LIMIT,
};
};

export const getGas: TGetGas = async (getGasStationGas, getConstantGas) => {
let retriesForGettingGas = 0;
while (retriesForGettingGas < 5) {
try {
return await getGasStationGas();
} catch (error) {
console.log("error when getting gas for", error);
if (retriesForGettingGas === 4) {
return getConstantGas();
}
await sleep(5_000);
retriesForGettingGas++;
}
}
return getConstantGas();
};

export function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
6 changes: 3 additions & 3 deletions src/handlers/icp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export async function icpHandler({
destinationChain,
to,
tokenId,
metadataUri,
metaDataUri,
) {
const bcWithSigner = await createBridgeActor(bridge, {
agent: signer,
Expand All @@ -275,7 +275,7 @@ export async function icpHandler({
tokenId,
destinationChain,
to,
metadataUri,
metaDataUri,
);
return {
hash: () => hash,
Expand All @@ -288,7 +288,7 @@ export async function icpHandler({
agent: signer,
});
await lc.icrc2_approve({
amount: BigInt(claimData.fee + 10_000n),
amount: BigInt(claimData.fee + 10_000n + 14638409n),
spender: {
owner: bridge,
subaccount: [],
Expand Down

0 comments on commit 781ced2

Please sign in to comment.