Skip to content

Commit

Permalink
Merge branch 'webapp-ca' of https://github.com/Koniverse/SubWallet-Ex…
Browse files Browse the repository at this point in the history
…tension into webapp-ca
  • Loading branch information
S2kael committed Sep 9, 2024
2 parents 084a061 + fa25601 commit 5b1802b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ export async function getERC20SpendingApprovalTx (spender: _Address, owner: _Add
maxPriorityFeePerGas: priority.maxPriorityFeePerGas?.toString()
} as TransactionConfig;
}

export function encodeSpendingApproval (spender: _Address, owner: _Address, contractAddress: _Address, evmApi: _EvmApi, amount = '115792089237316195423570985008687907853269984665640564039457584007913129639935'): string {
const tokenContract = getERC20Contract(contractAddress, evmApi);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
const approveCall = tokenContract.methods.approve(spender, amount); // TODO: need test

// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
return approveCall.encodeABI() as string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { BridgePluginParams } from 'klaster-sdk';
import { encodeFunctionData, Hex, parseAbi } from 'viem';

import { AcrossSuggestedFeeResp } from '../klaster';

export async function getAcrossSuggestedFee (data: BridgePluginParams): Promise<AcrossSuggestedFeeResp> {
const url = 'https://testnet.across.to/api/suggested-fees?' + new URLSearchParams({
originChainId: data.sourceChainId.toString(),
destinationChainId: data.destinationChainId.toString(),
inputToken: data.sourceToken,
outputToken: data.destinationToken,
amount: data.amount.toString()
}).toString();

console.log('url', url);

return await fetch(url, {
method: 'GET'
})
.then((res) => res.json()) as AcrossSuggestedFeeResp;
}

export function encodeAcrossCallData (data: BridgePluginParams, fees: AcrossSuggestedFeeResp): Hex {
// @ts-ignore
const abi = parseAbi([
'function depositV3(address depositor, address recipient, address inputToken, address outputToken, uint256 inputAmount, uint256 outputAmount, uint256 destinationChainId, address exclusiveRelayer, uint32 quoteTimestamp, uint32 fillDeadline, uint32 exclusivityDeadline, bytes calldata message) external'
]);
const outputAmount = data.amount - BigInt(fees.totalRelayFee.total);
const fillDeadline = Math.round(Date.now() / 1000) + 900;

const [srcAddress, destAddress] = data.account.getAddresses([data.sourceChainId, data.destinationChainId]);

if (!srcAddress || !destAddress) {
throw Error(`Can't fetch address from multichain account for ${data.sourceChainId} or ${data.destinationChainId}`);
}

// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return encodeFunctionData({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
abi: abi,
functionName: 'depositV3',
args: [
srcAddress,
destAddress,
data.sourceToken,
data.destinationToken,
data.amount,
outputAmount,
BigInt(data.destinationChainId),
fees.exclusiveRelayer,
parseInt(fees.timestamp),
fillDeadline,
parseInt(fees.exclusivityDeadline),
'0x'
]
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
import { _getContractAddressOfToken, _getEvmChainId } from '@subwallet/extension-base/services/chain-service/utils';
import { Abi } from 'abitype/src/abi';
import { batchTx, BiconomyV2AccountInitData, BridgePlugin, BridgePluginParams, buildItx, buildMultichainReadonlyClient, buildRpcInfo, buildTokenMapping, deployment, encodeApproveTx, initKlaster, klasterNodeHost, KlasterSDK, loadBicoV2Account, MultichainClient, MultichainTokenMapping, QuoteResponse, rawTx } from 'klaster-sdk';
import { encodeFunctionData, Hex, parseAbi } from 'viem';

import { encodeAcrossCallData, getAcrossSuggestedFee } from './helper/tx-encoder';

export interface AcrossSuggestedFeeResp {
totalRelayFee: {
Expand Down Expand Up @@ -33,42 +33,6 @@ export interface AcrossSuggestedFeeResp {
expectedFillTimeSec: string
}

function encodeAcrossCallData (data: BridgePluginParams, fees: AcrossSuggestedFeeResp): Hex {
// @ts-ignore
const abi = parseAbi([
'function depositV3(address depositor, address recipient, address inputToken, address outputToken, uint256 inputAmount, uint256 outputAmount, uint256 destinationChainId, address exclusiveRelayer, uint32 quoteTimestamp, uint32 fillDeadline, uint32 exclusivityDeadline, bytes calldata message) external'
]) as Abi;
const outputAmount = data.amount - BigInt(fees.totalRelayFee.total);
const fillDeadline = Math.round(Date.now() / 1000) + 300;

const [srcAddress, destAddress] = data.account.getAddresses([data.sourceChainId, data.destinationChainId]);

if (!srcAddress || !destAddress) {
throw Error(`Can't fetch address from multichain account for ${data.sourceChainId} or ${data.destinationChainId}`);
}

// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return encodeFunctionData({
abi: abi,
functionName: 'depositV3',
args: [
srcAddress,
destAddress,
data.sourceToken,
data.destinationToken,
data.amount,
outputAmount,
BigInt(data.destinationChainId),
fees.exclusiveRelayer,
parseInt(fees.timestamp),
fillDeadline,
parseInt(fees.exclusivityDeadline),
'0x'
]
});
}

export class KlasterService {
public sdk: KlasterSDK<BiconomyV2AccountInitData>;
private mcClient: MultichainClient;
Expand Down Expand Up @@ -98,27 +62,12 @@ export class KlasterService {
]);

this.bridgePlugin = async (data: BridgePluginParams) => {
const url = 'https://testnet.across.to/api/suggested-fees?' + new URLSearchParams({
originChainId: data.sourceChainId.toString(),
destinationChainId: data.destinationChainId.toString(),
inputToken: data.sourceToken,
outputToken: data.destinationToken,
amount: data.amount.toString()
}).toString();

console.log('url', url);

const feeResponse = await fetch(url, {
method: 'GET'
})
.then((res) => res.json()) as AcrossSuggestedFeeResp;

console.log(feeResponse);
const feeResponse = await getAcrossSuggestedFee(data);

const outputAmount = data.amount - BigInt(feeResponse.totalRelayFee.total);
const acrossApproveTx = encodeApproveTx({
tokenAddress: data.sourceToken,
amount: outputAmount,
amount: outputAmount * 10n,
recipient: feeResponse.spokePoolAddress
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export default class TransactionService {
});

emitter.on('send', (data: TransactionEventResponse) => {
console.log('onSend', data);
// this.onSend(data);
});

Expand Down Expand Up @@ -1286,7 +1287,8 @@ export default class TransactionService {
id,
errors: [],
warnings: [],
extrinsicHash: id
extrinsicHash: id,
eventLogs: []
};

this.state.requestService.addConfirmation(id, EXTENSION_REQUEST_URL, 'evmSignatureRequest', _payload)
Expand Down

0 comments on commit 5b1802b

Please sign in to comment.