Skip to content

Commit e8f9d9e

Browse files
authored
feat: eth staking support lido (#2936)
* fix: discover my dapps list * feat: add service contract for build evm transaction data * feat: support staking to lido
1 parent ee66083 commit e8f9d9e

File tree

8 files changed

+171
-17
lines changed

8 files changed

+171
-17
lines changed

packages/kit-bg/src/BackgroundApi.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,5 +395,15 @@ class BackgroundApi extends BackgroundApiBase implements IBackgroundApi {
395395
Object.defineProperty(this, 'serviceLimitOrder', { value });
396396
return value;
397397
}
398+
399+
get serviceContract() {
400+
const ServiceContract =
401+
require('./services/ServiceContract') as typeof import('./services/ServiceContract');
402+
const value = new ServiceContract.default({
403+
backgroundApi: this,
404+
});
405+
Object.defineProperty(this, 'serviceContract', { value });
406+
return value;
407+
}
398408
}
399409
export default BackgroundApi;

packages/kit-bg/src/BackgroundApiProxy.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type ServiceApp from './services/ServiceApp';
1616
import type ServiceBatchTransfer from './services/ServiceBatchTransfer';
1717
import type ServiceBootstrap from './services/ServiceBootstrap';
1818
import type ServiceCloudBackup from './services/ServiceCloudBackup';
19+
import type ServiceContract from './services/ServiceContract';
1920
import type ServiceCronJob from './services/ServiceCronJob';
2021
import type ServiceDapp from './services/ServiceDapp';
2122
import type ServiceDerivationPath from './services/ServiceDerivationPath';
@@ -176,6 +177,10 @@ class BackgroundApiProxy
176177
'serviceLimitOrder',
177178
) as ServiceLimitOrder;
178179

180+
serviceContract = this._createProxyService(
181+
'serviceContract',
182+
) as ServiceContract;
183+
179184
_createProxyService(name = 'ROOT') {
180185
if (this._serviceCreatedNames[name]) {
181186
throw new Error(`_createProxyService name duplicated. name=${name}`);

packages/kit-bg/src/IBackgroundApi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type ServiceApp from './services/ServiceApp';
1414
import type ServiceBatchTransfer from './services/ServiceBatchTransfer';
1515
import type ServiceBootstrap from './services/ServiceBootstrap';
1616
import type ServiceCloudBackup from './services/ServiceCloudBackup';
17+
import type ServiceContract from './services/ServiceContract';
1718
import type ServiceCronJob from './services/ServiceCronJob';
1819
import type ServiceDapp from './services/ServiceDapp';
1920
import type ServiceDerivationPath from './services/ServiceDerivationPath';
@@ -109,4 +110,5 @@ export interface IBackgroundApi extends IBackgroundApiBridge {
109110
serviceFiatPay: ServiceFiatPay;
110111
serviceAddressbook: ServiceAddressbook;
111112
serviceLimitOrder: ServiceLimitOrder;
113+
serviceContract: ServiceContract;
112114
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* eslint-disable @typescript-eslint/require-await */
2+
3+
import { Interface } from '@ethersproject/abi';
4+
5+
import {
6+
backgroundClass,
7+
backgroundMethod,
8+
} from '@onekeyhq/shared/src/background/backgroundDecorators';
9+
10+
import ServiceBase from './ServiceBase';
11+
12+
import type { Fragment, JsonFragment } from '@ethersproject/abi';
13+
14+
type EvmTransactionParams = {
15+
abi: string | readonly (string | Fragment | JsonFragment)[];
16+
method: string;
17+
params: any[];
18+
};
19+
20+
const lidoReferralAddress = '0xc1e92BD5d1aa6e5f5F299D0490BefD9D8E5a887a';
21+
22+
const LIDO_ABI = [
23+
{
24+
constant: false,
25+
inputs: [{ name: '_referral', type: 'address' }],
26+
name: 'submit',
27+
outputs: [{ name: '', type: 'uint256' }],
28+
payable: true,
29+
stateMutability: 'payable',
30+
type: 'function',
31+
},
32+
];
33+
34+
const WETH9_ABI = [
35+
{
36+
constant: false,
37+
inputs: [
38+
{
39+
'name': 'wad',
40+
'type': 'uint256',
41+
},
42+
],
43+
name: 'withdraw',
44+
outputs: [],
45+
payable: false,
46+
stateMutability: 'nonpayable',
47+
type: 'function',
48+
},
49+
{
50+
constant: false,
51+
inputs: [],
52+
name: 'deposit',
53+
outputs: [],
54+
payable: true,
55+
stateMutability: 'payable',
56+
type: 'function',
57+
},
58+
];
59+
60+
@backgroundClass()
61+
class ServiceContract extends ServiceBase {
62+
private async buildEvmTransaction({
63+
abi,
64+
method,
65+
params,
66+
}: EvmTransactionParams) {
67+
const inter = new Interface(abi);
68+
return inter.encodeFunctionData(inter.getFunction(method), params);
69+
}
70+
71+
@backgroundMethod()
72+
async buildWrapTransaction() {
73+
return this.buildEvmTransaction({
74+
abi: WETH9_ABI,
75+
method: 'deposit',
76+
params: [],
77+
});
78+
}
79+
80+
@backgroundMethod()
81+
async buildUnwrapTransaction(wad: number) {
82+
return this.buildEvmTransaction({
83+
abi: WETH9_ABI,
84+
method: 'withdraw',
85+
params: [wad],
86+
});
87+
}
88+
89+
@backgroundMethod()
90+
async buildLidoStakeTransaction() {
91+
return this.buildEvmTransaction({
92+
abi: LIDO_ABI,
93+
method: 'submit',
94+
params: [lidoReferralAddress],
95+
});
96+
}
97+
}
98+
99+
export default ServiceContract;

packages/kit-bg/src/services/ServiceStaking.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ import ServiceBase from './ServiceBase';
3333

3434
import type { AxiosResponse } from 'axios';
3535

36-
const TestnetContractAddress = '0xdCAe38cC28606e61B1e54D8b4b134588e4ca7Ab7';
37-
const MainnetContractAddress = '0xACBA4cFE7F30E64dA787c6Dc7Dc34f623570e758';
36+
const TestnetKeleContractAddress = '0xdCAe38cC28606e61B1e54D8b4b134588e4ca7Ab7';
37+
const MainnetKeleContractAddress = '0xACBA4cFE7F30E64dA787c6Dc7Dc34f623570e758';
38+
39+
const TestnetLidoContractAddress = '0x1643E812aE58766192Cf7D2Cf9567dF2C37e9B7F';
40+
const MainnetLidoContractAddress = '0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84';
3841

3942
export interface SerializableTransactionReceipt {
4043
to: string;
@@ -74,10 +77,20 @@ export default class ServiceStaking extends ServiceBase {
7477

7578
getKeleContractAddress(networkId: string): string {
7679
if (networkId === OnekeyNetwork.eth) {
77-
return MainnetContractAddress;
80+
return MainnetKeleContractAddress;
7881
}
7982
if (networkId === OnekeyNetwork.goerli) {
80-
return TestnetContractAddress;
83+
return TestnetKeleContractAddress;
84+
}
85+
throw new Error('Not supported network');
86+
}
87+
88+
getLidoContractAddress(networkId: string) {
89+
if (networkId === OnekeyNetwork.eth) {
90+
return MainnetLidoContractAddress;
91+
}
92+
if (networkId === OnekeyNetwork.goerli) {
93+
return TestnetLidoContractAddress;
8194
}
8295
throw new Error('Not supported network');
8396
}
@@ -105,6 +118,20 @@ export default class ServiceStaking extends ServiceBase {
105118
};
106119
}
107120

121+
@backgroundMethod()
122+
async buildTxForStakingETHtoLido(params: {
123+
value: string;
124+
networkId: string;
125+
}) {
126+
const { serviceContract } = this.backgroundApi;
127+
const data = await serviceContract.buildLidoStakeTransaction();
128+
return {
129+
data,
130+
to: this.getLidoContractAddress(params.networkId),
131+
value: params.value,
132+
};
133+
}
134+
108135
@backgroundMethod()
109136
async setAccountStakingActivity({
110137
networkId,

packages/kit/src/views/Discover/MyDAppList/desktop.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ const FavOrHisList = ({ isFav }: { isFav: boolean }) => {
141141
const cardWidth = boxWidth / numColumns;
142142

143143
const renderItem: ListRenderItem<MatchDAppItemType> = useCallback(
144-
({ item }) => <RenderItem cardWidth={cardWidth} item={item} isFav />,
145-
[cardWidth],
144+
({ item }) => (
145+
<RenderItem cardWidth={cardWidth} item={item} isFav={isFav} />
146+
),
147+
[cardWidth, isFav],
146148
);
147149

148150
const onLayout = useCallback((e: LayoutChangeEvent) => {

packages/kit/src/views/Staking/StakingETHNotes/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ export default function StakingETHNotes() {
4343
} catch {
4444
debugLogger.common.error('registerOnKele failed');
4545
}
46+
47+
const value = new BigNumber(10)
48+
.exponentiatedBy(18)
49+
.multipliedBy(params.amount)
50+
.toFixed(0);
51+
4652
const encodedTx =
4753
await backgroundApiProxy.serviceStaking.buildTxForStakingETHtoKele({
48-
value: new BigNumber(10)
49-
.exponentiatedBy(18)
50-
.multipliedBy(params.amount)
51-
.toFixed(0),
54+
value,
5255
networkId: params.networkId,
5356
});
5457
onClose();

packages/kit/src/views/Swap/components/TokenDisplay/index.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FC } from 'react';
22

33
import {
44
Box,
5+
Icon,
56
Image,
67
Token as TokenImage,
78
Typography,
@@ -48,13 +49,18 @@ export const TokenDisplay: FC<TokenDisplayProps> = ({ token }) => {
4849
<Image size="4" src={network?.logoURI} />
4950
</Box>
5051
</Box>
51-
<Box>
52-
<Typography.DisplayLarge color="text-default" fontSize={24}>
53-
{token.symbol}
54-
</Typography.DisplayLarge>
55-
<Typography.Body2 color="text-subdued">
56-
{network?.name ?? '-'}
57-
</Typography.Body2>
52+
<Box flexDirection="row" alignItems="center">
53+
<Box>
54+
<Typography.DisplayLarge color="text-default" fontSize={24}>
55+
{token.symbol}
56+
</Typography.DisplayLarge>
57+
<Typography.Body2 color="text-subdued">
58+
{network?.name ?? '-'}
59+
</Typography.Body2>
60+
</Box>
61+
<Box ml="1">
62+
<Icon size={12} name="ChevronDownSolid" />
63+
</Box>
5864
</Box>
5965
</Box>
6066
);

0 commit comments

Comments
 (0)