Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jetton wallet support #276

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/flutter_nekoton_bridge/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ targets:
- lib/nekoton/external/storage.dart
- lib/nekoton/core/ton_wallet/ton_wallet.dart
- lib/nekoton/core/token_wallet/token_wallet.dart
- lib/nekoton/core/jetton_wallet/jetton_wallet.dart
- lib/nekoton/core/generic_contract/generic_contract.dart
options:
formatted: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_nekoton_bridge/flutter_nekoton_bridge.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'package:http/http.dart' as http;

import '../timeout_utils.dart';

class HttpClient implements JrpcConnectionHttpClient {
@override
Future<String> post({
required String endpoint,
required Map<String, String> headers,
required String data,
}) async {
final response = await http.post(
Uri.parse(endpoint),
headers: headers,
body: data,
);

return response.body;
}

@override
void dispose() {}
}

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

const name = 'TON';
const networkGroup = 'ton';
const endpoint = 'https://jrpc-ton.broxus.com';

const address = Address(
address:
'0:6ca35273892588b4c5f4ae898dc1983eec9662dffebeacdbe82103a1d1dcac60');
const usdtTokenRoot = Address(
address:
'0:b113a994b5024a16719f69139328eb759596c38a25f59028b146fecdc3621dfe');
const tokenWallet = Address(
address:
'0:2de4b60d57c1b6de29557922139212ee44c2eac5c5fd0cef51c73611e27b1a00');

const jrpcSettings = JrpcNetworkSettings(endpoint: endpoint);
late JrpcTransport transport;

setUp(() async {
// This setup thing SHOULD NOT be removed or altered because it used in integration tests
setupLogger(
level: LogLevel.Trace,
mobileLogger: false,
logHandler: (logEntry) => debugPrint(
'FromLib: ${logEntry.level} ${logEntry.tag} ${logEntry.msg} (lib_time=${logEntry.timeMillis})',
),
);

runApp(Container());

await initRustToDartCaller();

final connection = await JrpcConnection.create(
client: HttpClient(),
settings: jrpcSettings,
name: name,
group: networkGroup,
);
transport = await JrpcTransport.create(jrpcConnection: connection);
});

group('JettonWallet test', () {
testWidgets('JettonWallet subscribe', (WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final wallet = await JettonWallet.subscribe(
transport: transport,
owner: address,
rootTokenContract: usdtTokenRoot,
);

expect(wallet, isNotNull);
expect(wallet.owner, address);
expect(wallet.tokenAddress, tokenWallet);
expect(wallet.rootTokenContract, usdtTokenRoot);
expect(wallet.contractState.balance, isNot(BigInt.zero));
});

// TODO(komarov): wait for nekoton fix
// testWidgets('JettonWallet estimateMinAttachedAmount',
// (WidgetTester tester) async {
// await tester.pumpAndSettleWithTimeout();

// const destination = Address(
// address:
// '0:4ae0972605f7425d46fa368e588098aa087129fc1d91b3a5f47a18f8d45f10d3',
// );
// final wallet = await JettonWallet.subscribe(
// transport: transport,
// owner: address,
// rootTokenContract: usdtTokenRoot,
// );

// final amount = await wallet.estimateMinAttachedAmount(
// destination: destination,
// amount: BigInt.parse('10000'),
// callbackValue: BigInt.one,
// remainingGasTo: address,
// );

// expect(amount, isNotNull);
// expect(amount.isValidInt, isTrue);
// });

testWidgets('JettonWallet prepareTransfer', (WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final wallet = await JettonWallet.subscribe(
transport: transport,
owner: address,
rootTokenContract: usdtTokenRoot,
);

final message = await wallet.prepareTransfer(
destination: usdtTokenRoot,
amount: BigInt.parse('10000'),
callbackValue: BigInt.one,
remainingGasTo: address,
);

expect(message, isNotNull);
});

testWidgets('JettonWallet getJettonWalletDetails',
(WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final details = await JettonWallet.getJettonWalletDetails(
transport: transport,
address: tokenWallet,
);

expect(details.item1.balance, isNot(BigInt.zero));
expect(details.item1.ownerAddress, address);
expect(details.item1.rootAddress, usdtTokenRoot);
expect(
details.item2.adminAddress,
const Address(
address:
'0:6440fe3c69410383963945173c4b11479bf0b9b4d7090e58777bda581c2f9998'),
);
});

testWidgets('JettonWallet getTokenRootDetailsFromJettonWallet',
(WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final details = await JettonWallet.getJettonRootDetailsFromJettonWallet(
transport: transport,
address: tokenWallet,
);

expect(details.item1, usdtTokenRoot);
expect(
details.item2.adminAddress,
const Address(
address:
'0:6440fe3c69410383963945173c4b11479bf0b9b4d7090e58777bda581c2f9998'),
);
});

testWidgets('JettonWallet getTokenRootDetails',
(WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final details = await JettonWallet.getJettonRootDetails(
transport: transport,
tokenRoot: usdtTokenRoot,
);

expect(
details.adminAddress,
const Address(
address:
'0:6440fe3c69410383963945173c4b11479bf0b9b4d7090e58777bda581c2f9998'),
);
});

testWidgets('JettonWallet refresh', (WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

final wallet = await JettonWallet.subscribe(
transport: transport,
owner: address,
rootTokenContract: usdtTokenRoot,
);

expect(wallet, isNotNull);
expect(wallet.owner, address);
expect(wallet.tokenAddress, tokenWallet);
expect(wallet.rootTokenContract, usdtTokenRoot);
expect(wallet.contractState.balance, isNot(BigInt.zero));

final fut = expectLater(wallet.fieldUpdatesStream, emits(null));
await wallet.refresh();
await fut;

expect(wallet, isNotNull);
expect(wallet.owner, address);
expect(wallet.tokenAddress, tokenWallet);
expect(wallet.rootTokenContract, usdtTokenRoot);
expect(wallet.contractState.balance, isNot(BigInt.zero));
});

testWidgets(
'JettonWallet subscribing new instance after disposing old one',
(WidgetTester tester) async {
await tester.pumpAndSettleWithTimeout();

for (var i = 0; i < 10; i++) {
final completer = Completer<void>();

// if wallet will not create instance for 5 seconds, then some bug here
final delaying = Future.delayed(const Duration(seconds: 5), () {
if (!completer.isCompleted) {
throw Exception('Resubscribe timeout at $i iteration');
}
});

final wallet = await JettonWallet.subscribe(
transport: transport,
owner: address,
rootTokenContract: usdtTokenRoot,
);

expect(wallet, isNotNull);
expect(wallet.owner, address);
expect(wallet.tokenAddress, tokenWallet);
expect(wallet.rootTokenContract, usdtTokenRoot);
expect(wallet.contractState.balance, isNot(BigInt.zero));

wallet.dispose();

completer.complete();
await delaying;
}
},
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
Loading
Loading