Skip to content

Commit

Permalink
Refactor the rest of the precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
besated committed Jul 4, 2024
1 parent 04c1c75 commit f90ef2e
Show file tree
Hide file tree
Showing 41 changed files with 1,479 additions and 1,428 deletions.
27 changes: 27 additions & 0 deletions packages/evm/src/ethers/addressPrecompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,35 @@ export interface AddressPrecompileFunctions {
* */
type AddressPrecompileContract = Contract & AddressPrecompileFunctions;

/**
* The ABI for the address precompile contract, which can be used to create an Ethers contract.
*
* @category Cosmos Interoperability
*/
export const ETHERS_ADDRESS_PRECOMPILE_ABI = ADDRESS_PRECOMPILE_ABI as InterfaceAbi;

/**
* Creates and returns an Ethers contract instance with the provided signer.
*
* @example
* ```tsx
* import { getAddressPrecompileEthersV6Contract } from '@sei-js/evm/ethers';
* import { ethers } from 'ethers';
*
* const provider = new ethers.BrowserProvider(window.ethereum); // or any other provider
* const signer = await provider.getSigner();
*
* const accounts = await provider.send('eth_requestAccounts', []);
*
* const addressPrecompileContract = getAddressPrecompileEthersV6Contract(signer);
*
* const seiAddr = await addressPrecompileContract.getSeiAddr(accounts[0]);
* ```
*
* @param runner a [Provider](https://docs.ethers.org/v6/api/providers/) (read-only) or ethers.Signer to use with the contract.
* @returns The typed contract instance allowing interaction with the precompile contract.
* @category Cosmos Interoperability
*/
export function getAddressPrecompileEthersV6Contract(runner: ContractRunner): AddressPrecompileContract {
return new Contract(ADDRESS_PRECOMPILE_ADDRESS, ETHERS_ADDRESS_PRECOMPILE_ABI, runner) as AddressPrecompileContract;
}
102 changes: 102 additions & 0 deletions packages/evm/src/ethers/bankPrecompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { BigNumberish, Contract, ContractRunner, InterfaceAbi } from 'ethers';
import { BANK_PRECOMPILE_ABI, BANK_PRECOMPILE_ADDRESS } from '../precompiles';

/**
* Represents the functions available in the Bank precompile contract,
* facilitating interoperability between the EVM and Cosmos.
* @category Cosmos Interoperability
*/
export interface BankPrecompileFunctions {
/**
* Retrieves the balance of the specified account for the given denomination.
* @param acc The account address for which to retrieve the balance.
* @param denom The denomination of the balance to retrieve.
* @returns A Promise resolving to an object containing the balance amount.
* @category Cosmos Interoperability
*/
balance(acc: string, denom: string): Promise<{ amount: BigNumberish }>;
/**
* Retrieves the number of decimal places for the specified denomination.
* @param denom The denomination for which to retrieve the number of decimal places.
* @returns A Promise resolving to an object containing the number of decimal places.
* @category Cosmos Interoperability
*/
decimals(denom: string): Promise<{ response: BigNumberish }>;
/**
* Retrieves the name of the specified denomination.
* @param denom The denomination for which to retrieve the name.
* @returns A Promise resolving to an object containing the denomination name.
* @category Cosmos Interoperability
*/
name(denom: string): Promise<{ response: string }>;
/**
* Sends tokens from one address to another.
* @param fromAddress The sender's address.
* @param toAddress The recipient's address.
* @param denom The denomination of the tokens to send.
* @param amount The amount of tokens to send.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
send(fromAddress: string, toAddress: string, denom: string, amount: number): Promise<{ success: boolean }>;
/**
* Retrieves the total supply of tokens for the specified denomination.
* @param denom The denomination for which to retrieve the total supply.
* @returns A Promise resolving to an object containing the total supply.
* @category Cosmos Interoperability
*/
supply(denom: string): Promise<{ response: BigNumberish }>;
/**
* Retrieves the symbol of the specified denomination.
* @param denom The denomination for which to retrieve the symbol.
* @returns A Promise resolving to an object containing the denomination symbol.
* @category Cosmos Interoperability
*/
symbol(denom: string): Promise<{ response: string }>;
/**
* Sends native tokens to a specified address.
* @param toNativeAddress The recipient's native address.
* @param value The amount of native tokens to send.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
sendNative(toNativeAddress: string, value: BigNumberish): Promise<{ success: boolean }>;
}

/** Represents the typed contract instance for the BANK precompile contract.
* @category Cosmos Interoperability
* */
export type BankPrecompileContract = Contract & BankPrecompileFunctions;

/**
* The Ethers ABI for the bank precompile contract.
*
* @category Cosmos Interoperability
*/
export const ETHERS_BANK_PRECOMPILE_ABI = BANK_PRECOMPILE_ABI as InterfaceAbi;

/**
* Creates and returns an Ethers contract instance with the provided signer.
*
* @example
* ```tsx
* import { getBankPrecompileEthersV6Contract } from '@sei-js/evm';
* import { ethers } from 'ethers';
*
* const provider = new ethers.BrowserProvider(window.ethereum); // or any other provider
* const signer = await provider.getSigner();
*
* const accounts = await provider.send('eth_requestAccounts', []);
*
* const bankPrecompileContract = getBankPrecompileEthersV6Contract(signer);
*
* const balance = await bankPrecompileContract.balance(accounts[0], 'usei');
* ```
*
* @param runner a [Provider](https://docs.ethers.org/v6/api/providers/) (read-only) or ethers.Signer to use with the contract.
* @returns The typed contract instance allowing interaction with the precompile contract.
* @category Cosmos Interoperability
*/
export const getBankPrecompileEthersV6Contract = (runner: ContractRunner): BankPrecompileContract => {
return new Contract(BANK_PRECOMPILE_ADDRESS, ETHERS_BANK_PRECOMPILE_ABI, runner) as BankPrecompileContract;
};
81 changes: 81 additions & 0 deletions packages/evm/src/ethers/distributionPrecompile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Contract, ContractRunner, InterfaceAbi } from 'ethers';
import { DISTRIBUTION_PRECOMPILE_ADDRESS, DISTRIBUTION_PRECOMPILE_ABI } from '../precompiles';

/**
* Represents the functions available in the Distribution precompile contract,
* facilitating interoperability between the EVM and Cosmos.
* @category Cosmos Interoperability
*/
export interface DistributionPrecompileFunctions {
/**
* Sets the withdrawal address for rewards.
* @param withdrawAddress The address to set as the withdrawal address.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
setWithdrawAddress(withdrawAddress: string): Promise<{ success: boolean }>;
/**
* Withdraws delegation rewards for a given validator.
* @param validator The validator for which to withdraw delegation rewards.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
withdrawDelegationRewards(validator: string): Promise<{ success: boolean }>;
/**
* Withdraws delegation rewards for a given validators.
* @param validators The validators for which to withdraw delegation rewards.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
withdrawMultipleDelegationRewards(validators: string[]): Promise<{ success: boolean }>;
}

/** Represents the typed contract instance for the DISTRIBUTION precompile contract.
* @category Cosmos Interoperability
* */
export type DistributionPrecompileContract = Contract & DistributionPrecompileFunctions;

/**
* The ABI for the distribution precompile contract, which can be used for interoperability between the EVM and Cosmos.
*
* @example
* ethers v6: Use the `ethers` library and precompiles to set the withdrawal address for rewards for the connected account.
* ```tsx
* import { DISTRIBUTION_PRECOMPILE_ADDRESS, ETHERS_DISTRIBUTION_PRECOMPILE_ABI, DistributionPrecompileContract } from '@sei-js/evm';
* import { ethers } from 'ethers';
*
* const provider = new ethers.BrowserProvider(); // or any other provider
* const signer = provider.getSigner();
*
* const contract = new ethers.Contract(DISTRIBUTION_PRECOMPILE_ADDRESS, ETHERS_DISTRIBUTION_PRECOMPILE_ABI, signer) as DistributionPrecompileContract;
*
* const response = await contract.setWithdrawAddress('0xADDRESS');
* ```
*
* @category Cosmos Interoperability
*/
export const ETHERS_DISTRIBUTION_PRECOMPILE_ABI = DISTRIBUTION_PRECOMPILE_ABI as InterfaceAbi;

/**
* Creates and returns an ethers v6 contract instance with the provided signer, for use in interoperability between the EVM and Cosmos.
*
* @example
* ethers v6: Use the `ethers` library and precompiles to set the withdrawal address for rewards for the connected account.
* ```tsx
* import { getDistributionPrecompileEthersV6Contract } from '@sei-js/evm';
* import { ethers } from 'ethers';
*
* const provider = new ethers.BrowserProvider(); // or any other provider
* const signer = provider.getSigner();
*
* const contract = getDistributionPrecompileEthersV6Contract(signer);
*
* const response = await contract.setWithdrawAddress('0xADDRESS');
* ```
* @param runner a [Provider](https://docs.ethers.org/v6/api/providers/) (read-only) or ethers.Signer to use with the contract.
* @returns The typed contract instance allowing interaction with the precompile contract.
* @category Cosmos Interoperability
*/
export const getDistributionPrecompileEthersV6Contract = (runner: ContractRunner): DistributionPrecompileContract => {
return new Contract(DISTRIBUTION_PRECOMPILE_ADDRESS, ETHERS_DISTRIBUTION_PRECOMPILE_ABI, runner) as DistributionPrecompileContract;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContractRunner, ethers, InterfaceAbi } from 'ethers';
import { Abi } from 'viem';
import { BigNumberish, Contract, ContractRunner, InterfaceAbi } from 'ethers';
import { GOVERNANCE_PRECOMPILE_ABI, GOVERNANCE_PRECOMPILE_ADDRESS } from '../precompiles';

/**
* Represents the functions available in the Governance precompile contract,
Expand All @@ -13,94 +13,21 @@ export interface GovernancePrecompileFunctions {
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
deposit(proposalID: ethers.BigNumberish): Promise<{ success: boolean }>;
deposit(proposalID: BigNumberish): Promise<{ success: boolean }>;
/**
* Votes on a governance proposal.
* @param proposalID The ID of the proposal to vote on.
* @param option The option to vote for.
* @returns A Promise resolving to an object indicating the success of the transaction.
* @category Cosmos Interoperability
*/
vote(proposalID: ethers.BigNumberish, option: ethers.BigNumberish): Promise<{ success: boolean }>;
vote(proposalID: BigNumberish, option: BigNumberish): Promise<{ success: boolean }>;
}

/** Represents the typed contract instance for the GOVERNANCE precompile contract.
* @category Cosmos Interoperability
* */
export type GovernancePrecompileContract = ethers.Contract & GovernancePrecompileFunctions;

/**
* The address of the GOVERNANCE precompile contract, which can be used for interoperability between the EVM and Cosmos.
*
* @example
* Wagmi
* ```tsx
* import { GOVERNANCE_PRECOMPILE_ADDRESS, GOVERNANCE_PRECOMPILE_ABI } from '@sei-js/evm';
* import { useReadContract } from 'wagmi';
**
* const depositResponse = useContractFunction(
* GOVERNANCE_PRECOMPILE_ADDRESS,
* GOVERNANCE_PRECOMPILE_ABI,
* 'deposit'
* );
* ```
*
* @example
* ethers v6
* ```tsx
* import { getGovernancePrecompileEthersV6Contract, GOVERNANCE_PRECOMPILE_ADDRESS, parseSei } from '@sei-js/evm';
* import { ethers } from 'ethers';
*
* const provider = new ethers.BrowserProvider(); // or any other provider
* const signer = provider.getSigner();
*
* const governancePrecompileContract = getGovernancePrecompileEthersV6Contract(GOVERNANCE_PRECOMPILE_ADDRESS, signer);
*
* //Surround with try/catch for detailed errors
* const depositResponse = await governancePrecompileContract.connect(signer).deposit('1', { value: parseSei(1) });
* ```
*
* @category Cosmos Interoperability
* */
export const GOVERNANCE_PRECOMPILE_ADDRESS: `0x${string}` = '0x0000000000000000000000000000000000001006';

/**
* The ABI for the governance precompile contract, which can be used for interoperability between the EVM and Cosmos.
*
* @example
* Wagmi
* ```tsx
* import { GOVERNANCE_PRECOMPILE_ADDRESS, GOVERNANCE_PRECOMPILE_ABI } from '@sei-js/evm';
* import { useContractFunction } from '@wagmi/core';
*
* const depositResponse = useContractFunction(
* GOVERNANCE_PRECOMPILE_ADDRESS,
* GOVERNANCE_PRECOMPILE_ABI,
* 'deposit'
* );
* ```
*
* @category Cosmos Interoperability
* */
export const GOVERNANCE_PRECOMPILE_ABI: Abi = [
{
inputs: [{ internalType: 'uint64', name: 'proposalID', type: 'uint64' }],
name: 'deposit',
outputs: [{ internalType: 'bool', name: 'success', type: 'bool' }],
stateMutability: 'payable',
type: 'function'
},
{
inputs: [
{ internalType: 'uint64', name: 'proposalID', type: 'uint64' },
{ internalType: 'int32', name: 'option', type: 'int32' }
],
name: 'vote',
outputs: [{ internalType: 'bool', name: 'success', type: 'bool' }],
stateMutability: 'nonpayable',
type: 'function'
}
];
export type GovernancePrecompileContract = Contract & GovernancePrecompileFunctions;

/**
* The ABI for the governance precompile contract, which can be used for interoperability between the EVM and Cosmos.
Expand Down Expand Up @@ -145,5 +72,5 @@ export const ETHERS_GOVERNANCE_PRECOMPILE_ABI = GOVERNANCE_PRECOMPILE_ABI as Int
* @category Cosmos Interoperability
*/
export const getGovernancePrecompileEthersV6Contract = (runner: ContractRunner) => {
return new ethers.Contract(GOVERNANCE_PRECOMPILE_ADDRESS, ETHERS_GOVERNANCE_PRECOMPILE_ABI, runner) as GovernancePrecompileContract;
return new Contract(GOVERNANCE_PRECOMPILE_ADDRESS, ETHERS_GOVERNANCE_PRECOMPILE_ABI, runner) as GovernancePrecompileContract;
};
Loading

0 comments on commit f90ef2e

Please sign in to comment.