-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathaddress.ts
150 lines (144 loc) · 6.42 KB
/
address.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ContractRunner, ethers, InterfaceAbi } from 'ethers';
import { type Abi } from 'viem';
/**
* Represents the functions available in the Address precompile contract, facilitating interoperability between the EVM and Cosmos.
* @category Cosmos Interoperability
*/
export interface AddressPrecompileFunctions {
/**
* Retrieves the associated EVM address for a given Cosmos address.
* @param addr The Cosmos address for which to retrieve the associated EVM address.
* @returns A Promise resolving to an object containing the associated EVM address.
* @category Cosmos Interoperability
*/
getEvmAddr(addr: string): Promise<{ response: string }>;
/**
* Retrieves the associated Cosmos address for a given EVM address.
* @param addr The EVM address for which to retrieve the associated Cosmos address.
* @returns A Promise resolving to an object containing the associated Cosmos address.
* @category Cosmos Interoperability
*/
getSeiAddr(addr: string): Promise<{ response: string }>;
}
/** Represents the typed contract instance for the ADDRESS precompile contract.
* @category Cosmos Interoperability
* */
export type AddressPrecompileContract = ethers.Contract & AddressPrecompileFunctions;
/**
* The address of the Address precompile contract, which can be used for interoperability between the EVM and Cosmos.
* It can be used to get the associated EVM address for a given Cosmos address and vice versa.
*
* @example
* Wagmi: Use the `useReadContract` hook to read the associated Cosmos address for the connected account.
* ```tsx
* import { ADDRESS_PRECOMPILE_ADDRESS, ADDRESS_PRECOMPILE_ABI } from '@sei-js/evm';
* import { useReadContract } from 'wagmi';
*
* // Make sure your component is wrapped in a WagmiProvider
* const { address } = useAccount();
*
* const associatedSeiAddress = useReadContract({ abi: ADDRESS_PRECOMPILE_ABI, address: ADDRESS_PRECOMPILE_ADDRESS, functionName: 'getSeiAddr', args: [address] });
* ```
*
* @example
* ethers v6: Use the `ethers` library and precompiles to read the associated Cosmos address for the connected account.
* ```tsx
* import { ADDRESS_PRECOMPILE_ADDRESS } 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 addressPrecompileContract = getAddressPrecompileEthersV6Contract(ADDRESS_PRECOMPILE_ADDRESS, signer);
*
* const associatedSeiAddress = await addressPrecompileContract.getSeiAddr(accounts[0]);
* ```
*
* @category Cosmos Interoperability
*/
export const ADDRESS_PRECOMPILE_ADDRESS: `0x${string}` = '0x0000000000000000000000000000000000001004';
/**
* The ABI for the address precompile contract which can be used for interoperability between the EVM and Cosmos.
* It can be used to get the associated EVM address for a given Cosmos address and vice versa.
*
* @example
* Wagmi: Use the `useReadContract` hook to read the associated Cosmos address for the connected account.
* ```tsx
* import { ADDRESS_PRECOMPILE_ADDRESS, ADDRESS_PRECOMPILE_ABI } from '@sei-js/evm';
* import { useReadContract } from 'wagmi';
*
* // Make sure your component is wrapped in a WagmiProvider
* const { address } = useAccount();
*
* const associatedSeiAddress = useReadContract({ abi: ADDRESS_PRECOMPILE_ABI, address: ADDRESS_PRECOMPILE_ADDRESS, functionName: 'getSeiAddr', args: [address] });
* ```
*
* @category Cosmos Interoperability
*/
export const ADDRESS_PRECOMPILE_ABI: Abi = [
{
inputs: [{ internalType: 'string', name: 'addr', type: 'string' }],
name: 'getEvmAddr',
outputs: [{ internalType: 'address', name: 'response', type: 'address' }],
stateMutability: 'view',
type: 'function'
},
{
inputs: [{ internalType: 'address', name: 'addr', type: 'address' }],
name: 'getSeiAddr',
outputs: [{ internalType: 'string', name: 'response', type: 'string' }],
stateMutability: 'view',
type: 'function'
}
];
/**
* The ABI for the address precompile contract which can be used for interoperability between the EVM and Cosmos.
* It can be used to get the associated EVM address for a given Cosmos address and vice versa.
*
* @example
* ethers v6: Use the `ethers` library and precompiles to read the associated Cosmos address for the connected account.
* ```tsx
* import { ADDRESS_PRECOMPILE_ADDRESS, AddressPrecompileContract, ETHERS_ADDRESS_PRECOMPILE_ABI } 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 addressPrecompileContract = new ethers.Contract(ADDRESS_PRECOMPILE_ADDRESS, ETHERS_ADDRESS_PRECOMPILE_ABI, signer) as AddressPrecompileContract;
*
* const associatedSeiAddress = await addressPrecompileContract.getSeiAddr(accounts[0]);
* ```
*
* @category Cosmos Interoperability
*/
export const ETHERS_ADDRESS_PRECOMPILE_ABI = ADDRESS_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.
* It can be used to get the associated EVM address for a given Cosmos address and vice versa.
*
* @example
* ethers v6: Use the `ethers` library and precompiles to read the associated Cosmos address for the connected account.
* ```tsx
* import { getAddressPrecompileEthersV6Contract } 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 addressPrecompileContract = getAddressPrecompileEthersV6Contract(signer);
*
* const associatedSeiAddress = await addressPrecompileContract.connect().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 ADDRESS precompile contract.
* @category Cosmos Interoperability
*/
export function getAddressPrecompileEthersV6Contract(runner: ContractRunner): AddressPrecompileContract {
return new ethers.Contract(ADDRESS_PRECOMPILE_ADDRESS, ETHERS_ADDRESS_PRECOMPILE_ABI, runner) as AddressPrecompileContract;
}