Skip to content

Commit

Permalink
feat: support ton example
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 committed Aug 29, 2024
1 parent a4d78f9 commit accecd4
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/example/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const registeredChains: IChainInfo[] = [
id: uuid.v4(),
name: 'Alephium',
href: '/alephium',
icon: 'https://uni.onekey-asset.com/static/chain/alephium.png',
icon: 'https://uni.onekey-asset.com/static/chain/alph.png',
},
{
id: uuid.v4(),
Expand Down
8 changes: 8 additions & 0 deletions packages/example/components/ApiActuator/ApiPayload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ function ApiExecute({
[dispatch],
);

const handleSetValidateResult = useCallback(
(newResult: string) => {
dispatch({ type: 'SET_VALIDATE_RESULT', payload: newResult });
},
[dispatch],
);

const handleExecute = useCallback(async () => {
setLoading(true);
handleSetResult('Calling...');
handleSetValidateResult('');

try {
// @ts-expect-error
Expand Down
15 changes: 14 additions & 1 deletion packages/example/components/chains/alephium/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useWallet,
useBalance,
} from '@alephium/web3-react';
import { verifySignedMessage } from '@alephium/web3';

export function Example() {
const wallet = useWallet();
Expand Down Expand Up @@ -94,6 +95,18 @@ export function Example() {
onExecute={async (request: string) => {
return wallet.signer.signMessage(JSON.parse(request));
}}
onValidate={async (request: string, response: string) => {
const params = JSON.parse(request);
const signature = JSON.parse(response).signature;

return verifySignedMessage(
params.message,
params.messageHasher,
wallet.account.publicKey,
signature,
params.signerKeyType,
).toString();
}}
/>
</ApiGroup>

Expand All @@ -104,7 +117,7 @@ export function Example() {

export default function App() {
return (
<AlephiumWalletProvider network="testnet" theme="retro">
<AlephiumWalletProvider network="mainnet" theme="retro">
<Example />
</AlephiumWalletProvider>
);
Expand Down
58 changes: 53 additions & 5 deletions packages/example/components/chains/alephium/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ONE_ALPH, DUST_AMOUNT } from '@alephium/web3'
import { ONE_ALPH, DUST_AMOUNT } from '@alephium/web3';

export default {
signAndSubmitTransferTx: (from: string, to: string) => [
Expand All @@ -16,15 +16,62 @@ export default {
}),
},
{
id: 'signAndSubmitTransferTx-token-638b022292ea665dc9c946eec02ef9602926dc0e6db17143baebce898e34a302',
name: 'Token',
id: 'signAndSubmitTransferTx-token-722954d9067c5a5ad532746a024f2a9d7a18ed9b90e27d0a3a504962160b5600',
name: 'Token-USDC-MAIN',
value: JSON.stringify({
signerAddress: from,
destinations: [
{
address: to,
amount: DUST_AMOUNT.toString(),
tokens: [{ id: '638b022292ea665dc9c946eec02ef9602926dc0e6db17143baebce898e34a302', amount: '10' }],
tokens: [
{
id: '722954d9067c5a5ad532746a024f2a9d7a18ed9b90e27d0a3a504962160b5600',
amount: '10',
},
],
},
],
}),
},
{
id: 'signAndSubmitTransferTx-token-556d9582463fe44fbd108aedc9f409f69086dc78d994b88ea6c9e65f8bf98e00',
name: 'Token-USDT-MAIN',
value: JSON.stringify({
signerAddress: from,
destinations: [
{
address: to,
amount: DUST_AMOUNT.toString(),
tokens: [
{
id: '556d9582463fe44fbd108aedc9f409f69086dc78d994b88ea6c9e65f8bf98e00',
amount: '10',
},
],
},
],
}),
},
{
id: 'signAndSubmitTransferTx-Multiple-Native-and-Token-USDT-MAIN',
name: 'Multiple Native andToken-USDT-MAIN',
value: JSON.stringify({
signerAddress: from,
destinations: [
{
address: to,
amount: DUST_AMOUNT.toString(),
tokens: [
{
id: '556d9582463fe44fbd108aedc9f409f69086dc78d994b88ea6c9e65f8bf98e00',
amount: '10',
},
],
},
{
address: to,
amount: ONE_ALPH.toString(),
},
],
}),
Expand All @@ -49,7 +96,8 @@ export default {
name: 'call test contract',
value: JSON.stringify({
signerAddress: from,
bytecode: '01010300000007b413c40de0b6b3a7640000a20c0c1440206c3b1f6262ffad9a4cb1e78f03f17f3593837505a69edbc18a59cf23c1f1c4020100',
bytecode:
'01010300000007b413c40de0b6b3a7640000a20c0c1440206c3b1f6262ffad9a4cb1e78f03f17f3593837505a69edbc18a59cf23c1f1c4020100',
}),
},
];
Expand Down
27 changes: 16 additions & 11 deletions packages/example/components/chains/scdo/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { IKnownWallet } from '../../../components/connect/types';
import DappList from '../../../components/DAppList';
import params from './params';

// https://demo.scdo.org/
export default function Example() {
const walletsRef = useRef<IProviderInfo[]>([
{
Expand Down Expand Up @@ -80,35 +81,39 @@ export default function Example() {
<ApiPayload
title="scdo_getBalance"
description="获取账户余额"
presupposeParams={[
{
id: 'scdo_getBalance',
name: 'getBalance',
value: account?.address,
},
]}
onExecute={async (request: string) => {
const res = await provider?.request<string[]>({
method: 'scdo_getBalance',
params: [account?.address ?? '', '', -1],
params: [request ?? '', '', -1],
});
return JSON.stringify(res);
console.log('scdo_getBalance', res);

return res;
}}
/>
</ApiGroup>

<ApiGroup title="Sign Message">
<ApiPayload
title="scdo_signMessage"
description="获取账户余额"
description="签署消息"
onExecute={async (request: string) => {
return await provider?.request<string>({
method: 'scdo_signMessage',
params: [request],
});
}}
/>

<ApiPayload
title="scdo_signMessage"
description="签署消息"
onExecute={async (request: string) => {
onValidate={async (request: string, response: string) => {
return await provider?.request<string>({
method: 'scdo_signMessage',
params: [request],
method: 'scdo_ecRecover',
params: [request, response],
});
}}
/>
Expand Down
1 change: 1 addition & 0 deletions packages/example/components/chains/scdo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type methodType =
| 'scdo_signTransaction'
| 'scdo_sendTransaction'
| 'scdo_signMessage'
| 'scdo_ecRecover'
| 'scdo_estimateGas';
export interface IProviderApi {
request<T>({ method, params }: { method: methodType; params?: Array<unknown> }): Promise<T>;
Expand Down
4 changes: 2 additions & 2 deletions packages/example/components/chains/ton/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function Example() {
title="sendTransaction"
description="转账普通 Native"
allowCallWithoutProvider={!!userFriendlyAddress}
presupposeParams={params.sendTransaction(rawAddress)}
presupposeParams={params.sendTransaction(userFriendlyAddress || '')}
onExecute={async (request: string) => {
const res = await tonConnectUI?.sendTransaction(JSON.parse(request));
return JSON.stringify(res);
Expand All @@ -48,7 +48,7 @@ export function Example() {
title="sendTransaction"
description="带评论的转账普通 Native"
allowCallWithoutProvider={!!userFriendlyAddress}
presupposeParams={params.sendTransactionWithBody(rawAddress)}
presupposeParams={params.sendTransactionWithBody(userFriendlyAddress || '')}
onExecute={async (request: string) => {
const res = await tonConnectUI?.sendTransaction(JSON.parse(request));
return JSON.stringify(res);
Expand Down
52 changes: 46 additions & 6 deletions packages/example/components/chains/ton/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,61 @@ export default {
],
}),
},
{
id: 'sendTransaction-native-two',
name: 'Native',
value: JSON.stringify({
messages: [
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
],
}),
},
{
id: 'sendTransaction-native-four',
name: 'Native',
value: JSON.stringify({
messages: [
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
{
address: to, // destination address
amount: '20000000', //Toncoin in nanotons
},
],
}),
},
],
sendTransactionWithBody: (to: string) => {
return [
{
id: 'sendTransaction-native',
name: 'Native with body',
value: JSON.stringify({
'validUntil': Math.floor(Date.now() / 1000) + 360,
'messages': [
validUntil: Math.floor(Date.now() / 1000) + 360,
messages: [
{
'address': to,
'amount': '5000000',
'stateInit':
address: to,
amount: '5000000',
stateInit:
'te6cckEBBAEAOgACATQCAQAAART/APSkE/S88sgLAwBI0wHQ0wMBcbCRW+D6QDBwgBDIywVYzxYh+gLLagHPFsmAQPsAlxCarA==',
'payload': 'te6ccsEBAQEADAAMABQAAAAASGVsbG8hCaTc/g==',
payload: 'te6ccsEBAQEADAAMABQAAAAASGVsbG8hCaTc/g==',
},
],
}),
Expand Down

0 comments on commit accecd4

Please sign in to comment.