@myria/airdrop-js
Version:
Airdrop in L1 with claim based approach
479 lines (478 loc) • 21.2 kB
TypeScript
/**
* Set of single functions in our airdrop-js
* to let consumers pick and use with their strategies
* @module Transaction/Core
*/
export { saveSnapshot, setMerkleRoot } from 'thirdweb/extensions/airdrop';
import { EstimateGasOptions, PreparedTransaction, SendTransactionOptions, ThirdwebClient, ThirdwebContract } from 'thirdweb';
import { TransactionReceipt } from 'thirdweb/transaction';
import { Account, Address, CreateRpcClientOptions, ExtraGasOptions, GasFeeInfo, GenerateMerkleTreeInfo, RetryOptions, SupportingChain, WhiteListItem } from '../type';
/**
* Returns an RPC request that can be used to make JSON-RPC requests
*
* @param {ThirdwebClient} client - Thirdweb client.
* @param {CreateRpcClientOptions} options - Options to create RpcClient.
*/
export declare const getRpcClientByChain: (client: ThirdwebClient, options: CreateRpcClientOptions) => import("viem").EIP1193RequestFn<[{
Method: "web3_clientVersion";
Parameters?: undefined;
ReturnType: string;
}, {
Method: "web3_sha3";
Parameters: [data: `0x${string}`];
ReturnType: string;
}, {
Method: "net_listening";
Parameters?: undefined;
ReturnType: boolean;
}, {
Method: "net_peerCount";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "net_version";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_blobBaseFee";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_blockNumber";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_call";
Parameters: [transaction: import("viem").ExactPartial<import("viem").RpcTransactionRequest>] | [transaction: import("viem").ExactPartial<import("viem").RpcTransactionRequest>, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier] | [transaction: import("viem").ExactPartial<import("viem").RpcTransactionRequest>, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier, stateOverrideSet: import("viem").RpcStateOverride];
ReturnType: `0x${string}`;
}, {
Method: "eth_chainId";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_coinbase";
Parameters?: undefined;
ReturnType: string;
}, {
Method: "eth_estimateGas";
Parameters: [transaction: import("viem").RpcTransactionRequest] | [transaction: import("viem").RpcTransactionRequest, block: `0x${string}` | import("viem").BlockTag] | [transaction: import("viem").RpcTransactionRequest, block: `0x${string}` | import("viem").BlockTag, stateOverride: import("viem").RpcStateOverride];
ReturnType: `0x${string}`;
}, {
Method: "eth_feeHistory";
Parameters: [blockCount: `0x${string}`, newestBlock: `0x${string}` | import("viem").BlockTag, rewardPercentiles: number[] | undefined];
ReturnType: import("viem").RpcFeeHistory;
}, {
Method: "eth_gasPrice";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_getBalance";
Parameters: [address: string, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier];
ReturnType: `0x${string}`;
}, {
Method: "eth_getBlockByHash";
Parameters: [hash: `0x${string}`, includeTransactionObjects: boolean];
ReturnType: import("viem").RpcBlock | null;
}, {
Method: "eth_getBlockByNumber";
Parameters: [block: `0x${string}` | import("viem").BlockTag, includeTransactionObjects: boolean];
ReturnType: import("viem").RpcBlock | null;
}, {
Method: "eth_getBlockTransactionCountByHash";
Parameters: [hash: `0x${string}`];
ReturnType: `0x${string}`;
}, {
Method: "eth_getBlockTransactionCountByNumber";
Parameters: [block: `0x${string}` | import("viem").BlockTag];
ReturnType: `0x${string}`;
}, {
Method: "eth_getCode";
Parameters: [address: string, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier];
ReturnType: `0x${string}`;
}, {
Method: "eth_getFilterChanges";
Parameters: [filterId: `0x${string}`];
ReturnType: import("viem").RpcLog[] | `0x${string}`[];
}, {
Method: "eth_getFilterLogs";
Parameters: [filterId: `0x${string}`];
ReturnType: import("viem").RpcLog[];
}, {
Method: "eth_getLogs";
Parameters: [{
address?: string | string[] | undefined;
topics?: import("viem").LogTopic[] | undefined;
} & ({
fromBlock?: `0x${string}` | import("viem").BlockTag | undefined;
toBlock?: `0x${string}` | import("viem").BlockTag | undefined;
blockHash?: undefined;
} | {
fromBlock?: undefined;
toBlock?: undefined;
blockHash?: `0x${string}` | undefined;
})];
ReturnType: import("viem").RpcLog[];
}, {
Method: "eth_getProof";
Parameters: [address: string, storageKeys: `0x${string}`[], block: `0x${string}` | import("viem").BlockTag];
ReturnType: import("viem").RpcProof;
}, {
Method: "eth_getStorageAt";
Parameters: [address: string, index: `0x${string}`, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier];
ReturnType: `0x${string}`;
}, {
Method: "eth_getTransactionByBlockHashAndIndex";
Parameters: [hash: `0x${string}`, index: `0x${string}`];
ReturnType: import("viem").RpcTransaction | null;
}, {
Method: "eth_getTransactionByBlockNumberAndIndex";
Parameters: [block: `0x${string}` | import("viem").BlockTag, index: `0x${string}`];
ReturnType: import("viem").RpcTransaction | null;
}, {
Method: "eth_getTransactionByHash";
Parameters: [hash: `0x${string}`];
ReturnType: import("viem").RpcTransaction | null;
}, {
Method: "eth_getTransactionCount";
Parameters: [address: string, block: `0x${string}` | import("viem").BlockTag | import("viem").RpcBlockIdentifier];
ReturnType: `0x${string}`;
}, {
Method: "eth_getTransactionReceipt";
Parameters: [hash: `0x${string}`];
ReturnType: import("viem").RpcTransactionReceipt | null;
}, {
Method: "eth_getUncleByBlockHashAndIndex";
Parameters: [hash: `0x${string}`, index: `0x${string}`];
ReturnType: import("viem").RpcUncle | null;
}, {
Method: "eth_getUncleByBlockNumberAndIndex";
Parameters: [block: `0x${string}` | import("viem").BlockTag, index: `0x${string}`];
ReturnType: import("viem").RpcUncle | null;
}, {
Method: "eth_getUncleCountByBlockHash";
Parameters: [hash: `0x${string}`];
ReturnType: `0x${string}`;
}, {
Method: "eth_getUncleCountByBlockNumber";
Parameters: [block: `0x${string}` | import("viem").BlockTag];
ReturnType: `0x${string}`;
}, {
Method: "eth_maxPriorityFeePerGas";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_newBlockFilter";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_newFilter";
Parameters: [filter: {
fromBlock?: `0x${string}` | import("viem").BlockTag | undefined;
toBlock?: `0x${string}` | import("viem").BlockTag | undefined;
address?: string | string[] | undefined;
topics?: import("viem").LogTopic[] | undefined;
}];
ReturnType: `0x${string}`;
}, {
Method: "eth_newPendingTransactionFilter";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_protocolVersion";
Parameters?: undefined;
ReturnType: string;
}, {
Method: "eth_sendRawTransaction";
Parameters: [signedTransaction: `0x${string}`];
ReturnType: `0x${string}`;
}, {
Method: "eth_uninstallFilter";
Parameters: [filterId: `0x${string}`];
ReturnType: boolean;
}, {
Method: "eth_accounts";
Parameters?: undefined;
ReturnType: string[];
}, {
Method: "eth_chainId";
Parameters?: undefined;
ReturnType: `0x${string}`;
}, {
Method: "eth_estimateGas";
Parameters: [transaction: import("viem").RpcTransactionRequest] | [transaction: import("viem").RpcTransactionRequest, block: `0x${string}` | import("viem").BlockTag] | [transaction: import("viem").RpcTransactionRequest, block: `0x${string}` | import("viem").BlockTag, stateOverride: import("viem").RpcStateOverride];
ReturnType: `0x${string}`;
}, {
Method: "eth_requestAccounts";
Parameters?: undefined;
ReturnType: string[];
}, {
Method: "eth_sendTransaction";
Parameters: [transaction: import("viem").RpcTransactionRequest];
ReturnType: `0x${string}`;
}, {
Method: "eth_sendRawTransaction";
Parameters: [signedTransaction: `0x${string}`];
ReturnType: `0x${string}`;
}, {
Method: "eth_sign";
Parameters: [address: string, data: `0x${string}`];
ReturnType: `0x${string}`;
}, {
Method: "eth_signTransaction";
Parameters: [request: import("viem").RpcTransactionRequest];
ReturnType: `0x${string}`;
}, {
Method: "eth_signTypedData_v4";
Parameters: [address: string, message: string];
ReturnType: `0x${string}`;
}, {
Method: "eth_syncing";
Parameters?: undefined;
ReturnType: false | import("viem").NetworkSync;
}, {
Method: "personal_sign";
Parameters: [data: `0x${string}`, address: string];
ReturnType: `0x${string}`;
}, {
Method: "wallet_addEthereumChain";
Parameters: [chain: import("viem").AddEthereumChainParameter];
ReturnType: null;
}, {
Method: "wallet_getCallsStatus";
Parameters?: [string] | undefined;
ReturnType: import("viem").WalletGetCallsStatusReturnType<`0x${string}`, `0x${string}`>;
}, {
Method: "wallet_getCapabilities";
Parameters?: [string] | undefined;
ReturnType: {
[x: `0x${string}`]: import("viem").WalletCapabilities;
};
}, {
Method: "wallet_getPermissions";
Parameters?: undefined;
ReturnType: import("viem").WalletPermission[];
}, {
Method: "wallet_grantPermissions";
Parameters?: [import("viem").WalletGrantPermissionsParameters] | undefined;
ReturnType: {
expiry: number;
factory?: `0x${string}` | undefined;
factoryData?: string | undefined;
grantedPermissions: readonly {
data: unknown;
policies: readonly {
data: unknown;
type: string;
}[];
required?: boolean | undefined;
type: string;
}[];
permissionsContext: string;
signerData?: {
userOpBuilder?: `0x${string}` | undefined;
submitToAddress?: `0x${string}` | undefined;
} | undefined;
};
}, {
Method: "wallet_requestPermissions";
Parameters: [permissions: {
eth_accounts: Record<string, any>;
}];
ReturnType: import("viem").WalletPermission[];
}, {
Method: "wallet_revokePermissions";
Parameters: [permissions: {
eth_accounts: Record<string, any>;
}];
ReturnType: null;
}, {
Method: "wallet_sendCalls";
Parameters?: import("viem").WalletSendCallsParameters<import("viem").WalletCapabilities, `0x${string}`, `0x${string}`> | undefined;
ReturnType: string;
}, {
Method: "wallet_showCallsStatus";
Parameters?: [string] | undefined;
ReturnType: void;
}, {
Method: "wallet_switchEthereumChain";
Parameters: [chain: {
chainId: string;
}];
ReturnType: null;
}, {
Method: "wallet_watchAsset";
Parameters: import("viem").WatchAssetParams;
ReturnType: boolean;
}]>;
/**
* This callback type is called `transactionCallback` and is displayed as a global symbol.
*
* @callback transactionCallback
* @param {TransactionReceipt} result - The transaction receipt received from submitting.
*/
/**
* Calculate gas fee info need to paid to submit a transaction
* @see {@link https://ethereum.org/en/developers/docs/gas|Ethereum's gas} or @see {@link https://support.metamask.io/transactions-and-gas/gas-fees/user-guide-gas/|Metamask's gas}
* @see {@link https://etherscan.io/gastracker|Gas Tracker}
*
* @param {EstimateGasOptions} options - The options for estimating gas.
* @param {boolean} isLogResult - Whether to log the result or not. Default true.
* @returns {Promise<GasFeeInfo>} Promise object represents the gas fee info to perform an on-chain transaction
* @throws An error if the account is missed.
*/
export declare const getGasFeeInfo: (options: EstimateGasOptions, extraGasOptions?: ExtraGasOptions, isLogResult?: boolean) => Promise<GasFeeInfo>;
/**
* Retrieves the transaction count (nonce) for a given Ethereum address.
*
* @see {@link https://ethereum.org/en/developers/docs/gas} for GAS AND FEES.
* @see {@link https://etherscan.io/gastracker|Gas Tracker}
*
* @param {Address} address - The Ethereum address of Sender.
* @param {ThirdwebClient} client - Thirdweb client.
* @param {CreateRpcClientOptions} options - Options to create RpcClient.
* @param {boolean} isLogResult - Whether to log the result or not. Default true.
* @returns {Promise<number>} Promise object represents the next transaction nonce
*/
export declare const getNextNonce: (address: Address, client: ThirdwebClient, options: CreateRpcClientOptions, isLogResult?: boolean) => Promise<number>;
/**
* Reads owner of a smart contract.
*
* @param {ThirdwebContract} contract - The Thirdweb contract.
* @returns {Promise<string>} A promise that resolves with the result of the owner ethereum address.
*/
export declare const getOwnerOfContract: (contract: ThirdwebContract) => Promise<string>;
/**
* Creates a Thirdweb contract by combining the Thirdweb client and contract options.
*
* @param {Address} address - The ethereum smart contract address.
* @param {ThirdwebClient} client - Thirdweb client.
* @param {SupportingChain} selectingChain - The selecting chain.
* @returns The Thirdweb contract.
*/
export declare const getThirdwebContract: (address: Address, client: ThirdwebClient, selectingChain: SupportingChain) => Readonly<import("thirdweb").ContractOptions<[]>>;
/**
* End-User connects wallet to trigger Claim from Client side
*
* @param {Address} tokenAddress - The token address to claim.
* @param {Account} account - The Account represent as sender @see {@link https://ethereum.org/en/glossary/#account|Account's Ethereum}.
* @param {ThirdwebContract} airdropContract - The airdrop Thirdweb contract.
* @param {transactionCallback} callback - The callback that handles the post-submit state.
* @param {boolean} isLogResult - Whether to log the result or not. Default true.
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the wallet is not connected.
* @example
* ```ts
* import { claimAirdropToken } from "./index";
*
* const transactionReceipt = await claimAirdropToken(
* tokenAddress,
* account,
* airdropContract
* );
* ```
*/
export declare const claimAirdropToken: (tokenAddress: Address, account: Account, airdropContract: ThirdwebContract, callback?: ((result: TransactionReceipt) => void) | undefined, isLogResult?: boolean) => Promise<TransactionReceipt>;
/**
* Token contract owner approve airdrop contract address as spender with amount
*
* @param {Address} spender - The airdrop smart contract address as spender.
* @param {number} amount - The total airdrop amount in ether format.
* @param {Account} account - The Account represent as sender @see {@link https://ethereum.org/en/glossary/#account|Account's Ethereum}.
* @param {ThirdwebContract} tokenContract - The token Thirdweb contract to airdrop
* @param {transactionCallback} callback - The callback that handles the post-submit state.
* @param {boolean} isLogResult - Whether to log the result or not. Default true.
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the amount <= 0.
* @throws An error if the wallet is not connected.
* @example
* ```ts
* import { approveAirdropAsSpender } from "./index";
*
* const transactionReceipt = await approveAirdropAsSpender(
* spender,
* amount,
* account,
* tokenContract
* );
* ```
*/
export declare const approveAirdropAsSpender: (spender: Address, amount: number, account: Account, tokenContract: ThirdwebContract, callback?: ((result: TransactionReceipt) => void) | undefined, isLogResult?: boolean) => Promise<TransactionReceipt>;
/**
* Generate merkle tree info for a whitelist
*
* @param {WhiteListItem[]} whitelist - The list of items is available for airdrop.
* @param {ThirdwebContract} airdropContract - The Airdrop Thirdweb contract.
* @param {Address} tokenAddress - The token address to claim.
* @param {boolean} isLogResult - Whether to log the result or not. Default true.
* @returns {Promise<GenerateMerkleTreeInfo>} A promise that resolves to the generated info.
* @throws An error if the wallet is zero.
* @example
* ```ts
* import { generateMerkleTreeForWhitelist } from "./index";
*
* const generateMerkleTreeInfo = await generateMerkleTreeForWhitelist(
* whitelist,
* airdropContract,
* tokenAddress,
* tokenContract
* );
* ```
*/
export declare const generateMerkleTreeInfoERC20ForWhitelist: (whitelist: WhiteListItem[], airdropContract: ThirdwebContract, tokenAddress: Address, isLogResult?: boolean) => Promise<GenerateMerkleTreeInfo>;
/**
* Airdrop's owner saved merkleRoot to on-chain.
*
* @remarks
* MUST execute saveSnapshotByOwner first. Order master
*
* @param {Account} account - The Account represent as sender @see {@link https://ethereum.org/en/glossary/#account|Account's Ethereum}.
* @param {ThirdwebContract} airdropContract - The airdrop Thirdweb contract.
* @param {Address} tokenAddress - The token address to claim.
* @param {string} merkleRoot - The generated merkleRoot from whitelist @see {@link generateMerkleTreeInfoERC20ForWhitelist|Generate merkleRoot}
* @param {RetryOptions} retryOptions - The configuration on retry
* @param {ExtraGasOptions} extraGasOptions - The extra gas options bidding for your transaction to be included in the next block.
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the wallet is not connected.
*/
export declare const saveMerkleRootByOwner: (account: Account, airdropContract: ThirdwebContract, tokenAddress: string, merkleRoot: string, retryOptions?: RetryOptions, extraGasOptions?: ExtraGasOptions) => Promise<TransactionReceipt>;
/**
* Airdrop's owner saved snapshotUri to on-chain.
*
* @param {Account} account - The Account represent as sender @see {@link https://ethereum.org/en/glossary/#account|Account's Ethereum}.
* @param {ThirdwebContract} airdropContract - The airdrop Thirdweb contract.
* @param {string} merkleRoot - The generated merkleRoot from whitelist @see {@link generateMerkleTreeInfoERC20ForWhitelist|Generate merkleRoot}
* @param {string} snapshotUri - The generated snapshotUri from whitelist @see {@link generateMerkleTreeInfoERC20ForWhitelist|Generate snapshotUri}
* @param {RetryOptions} retryOptions - The configuration on retry
* @param {ExtraGasOptions} extraGasOptions - The extra gas options bidding for your transaction to be included in the next block.
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the wallet is not connected.
*/
export declare const saveSnapshotByOwner: (account: Account, airdropContract: ThirdwebContract, merkleRoot: string, snapshotUri: string, retryOptions?: RetryOptions, extraGasOptions?: ExtraGasOptions) => Promise<TransactionReceipt>;
/**
* Sends a transaction using the provided wallet.
* @param {SendTransactionOptions} options - The options for sending the transaction.
* @param {number} maxBlocksWaitTime - The maximum of blocks to wait for confirmation before considering success.
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the wallet is not connected.
* @example
* ```ts
* import { sendAndConfirmTransaction } from "./index";
*
* const transactionReceipt = await sendAndConfirmTransaction(
* options,
* maxBlocksWaitTime
* );
* ```
*/
export declare function sendTransactionAndWaitForReceipt(options: SendTransactionOptions, maxBlocksWaitTime?: number): Promise<TransactionReceipt>;
/**
* Retry on preparing the gas fee, and nonce, and send a transaction using the provided wallet.
*
* @param {PreparedTransaction} transaction - The raw transaction to submit
* @param {Account} account - The Account represent as sender @see {@link https://ethereum.org/en/glossary/#account|Account's Ethereum}.
* @param {ExtraGasOptions} extraGasOptions - The extra gas options bidding for your transaction to be included in the next block. Otherwise, Use default our sdk {@link Type | Default Variables}
* @param {RetryOptions} retryOptions - The configuration on retry
* @returns {Promise<TransactionReceipt>} A promise that resolves to the confirmed transaction receipt.
* @throws An error if the wallet is not connected.
*/
export declare function retryPrepareAndSubmitRawTransaction(transaction: PreparedTransaction<any>, account: Account, retryOptions?: RetryOptions, extraGasOptions?: ExtraGasOptions): Promise<TransactionReceipt>;