Skip to content

Commit 3b659ef

Browse files
committed
transfer examples for uniffi bindings
1 parent e0e5bce commit 3b659ef

File tree

7 files changed

+112
-40
lines changed

7 files changed

+112
-40
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ GO_FILES = 1_change_pubkey 2_withdraw 3_transfer 4_forced_exit 5_order_matching
171171
RUN_GO_EXAMPLES = $(patsubst %, run_example_go_%, $(GO_FILES))
172172
run_example_go: ${RUN_GO_EXAMPLES}
173173

174-
PY_FILES = 1_change_pubkey 2_withdraw 5_order_matching
174+
PY_FILES = 1_change_pubkey 2_withdraw 3_transfer 5_order_matching
175175
RUN_PYTHON_EXAMPLES = $(patsubst %, run_example_python_%, $(PY_FILES))
176176
run_example_python: ${RUN_PYTHON_EXAMPLES}
177177

178178
JS_FILES = 1_change_pubkey 2_auto_deleveraging 3_update_global_var 4_contract_matching 5_liquidation 6_funding
179179
RUN_JS_EXAMPLES = $(patsubst %, run_example_js_%, $(JS_FILES))
180180
run_example_js: ${RUN_JS_EXAMPLES}
181181

182-
CPP_FILES = 1_change_pubkey 2_withdraw 5_order_matching
182+
CPP_FILES = 1_change_pubkey 2_withdraw 3_transfer 5_order_matching
183183
RUN_CPP_EXAMPLES = $(patsubst %, run_example_cpp_%, $(CPP_FILES))
184184
run_example_cpp: ${RUN_CPP_EXAMPLES}

examples/Cpp/2_withdraw.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ int main() {
1212
ZkLinkAddress to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9";
1313
TokenId l2_source_token = 17;
1414
TokenId l1_target_token = 17;
15-
BigUint amount = "1234567899808787";
16-
cout << "Original amount: " << amount << "\n";
17-
amount = closest_packable_token_amount(amount);
18-
cout << "Converted amount: " << amount << "\n";
19-
BigUint fee = "10000567777";
20-
cout << "Original fee: " << fee << "\n";
21-
fee = closest_packable_fee_amount(fee);
22-
cout << "Converted fee: " << fee << "\n";
15+
BigUint amount = "1000000";
16+
BigUint fee = "1000";
2317
Nonce nonce = 1;
2418
uint16_t withdraw_fee_ratio = 50;
2519
TimeStamp timestamp = 1000000000;

examples/Cpp/3_transfer.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include "generated/zklink_sdk.hpp"
3+
4+
using namespace std;
5+
using namespace zklink_sdk;
6+
7+
int main() {
8+
string private_key = "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4";
9+
AccountId account_id = 20;
10+
ZkLinkAddress to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9";
11+
SubAccountId from_sub_account_id = 1;
12+
SubAccountId to_sub_account_id = 1;
13+
TokenId token = 18;
14+
BigUint amount = "1234567899808787";
15+
cout << "Original amount: " << amount << "\n";
16+
amount = closest_packable_token_amount(amount);
17+
cout << "Converted amount: " << amount << "\n";
18+
BigUint fee = "10000567777";
19+
cout << "Original fee: " << fee << "\n";
20+
fee = closest_packable_fee_amount(fee);
21+
cout << "Converted fee: " << fee << "\n";
22+
Nonce nonce = 1;
23+
TimeStamp timestamp = 1693472232;
24+
string token_sybmol = "DAI";
25+
26+
TransferBuilder builder = {
27+
account_id,
28+
to_address,
29+
from_sub_account_id,
30+
to_sub_account_id,
31+
token,
32+
amount,
33+
fee,
34+
nonce,
35+
timestamp
36+
};
37+
shared_ptr<Transfer> tx = Transfer::init(builder);
38+
shared_ptr<Signer> signer = Signer::init(private_key, L1SignerType::kEth{});
39+
TxSignature signature = signer->sign_transfer(tx, token_sybmol, {}, {});
40+
cout << signature.tx << "\n";
41+
return 0;
42+
}

examples/Golang/2_withdraw.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,8 @@ func HighLevelWithdraw() {
2929
toAddress := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
3030
l2SourceToken := sdk.TokenId(17)
3131
l1TargetToken := sdk.TokenId(17)
32-
amount := *big.NewInt(1234567899808787)
33-
fmt.Println("Original amount: ", amount)
34-
amount = sdk.ClosestPackableTokenAmount(amount)
35-
fmt.Println("Converted amount:s", amount)
36-
fee := *big.NewInt(10000567777)
37-
fmt.Println("Original fee: ", fee)
38-
fee = sdk.ClosestPackableFeeAmount(fee)
39-
fmt.Println("Converted fee: ", fee)
32+
amount := *big.NewInt(1000000)
33+
fee := *big.NewInt(1000)
4034
nonce := sdk.Nonce(1)
4135
withdrawFeeRatio := uint16(50)
4236
timestamp := sdk.TimeStamp(1000000000)

examples/Golang/3_transfer.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@ type SubmiterSignature struct {
2626
func HighLevelTransfer() {
2727
privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
2828
address := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
29+
amount := *big.NewInt(1234567899808787)
30+
fmt.Println("Original amount: ", amount)
31+
amount = sdk.ClosestPackableTokenAmount(amount)
32+
fmt.Println("Converted amount:s", amount)
33+
fee := *big.NewInt(10000567777)
34+
fmt.Println("Original fee: ", fee)
35+
fee = sdk.ClosestPackableFeeAmount(fee)
36+
fmt.Println("Converted fee: ", fee)
2937
builder := sdk.TransferBuilder {
30-
sdk.AccountId(20),
31-
address,
32-
sdk.SubAccountId(1),
33-
sdk.SubAccountId(1),
34-
sdk.TokenId(18),
35-
*big.NewInt(100000),
36-
*big.NewInt(100),
37-
sdk.Nonce(1),
38-
sdk.TimeStamp(1693472232),
38+
AccountId: sdk.AccountId(20),
39+
ToAddress: address,
40+
FromSubAccountId: sdk.SubAccountId(1),
41+
ToSubAccountId: sdk.SubAccountId(1),
42+
Token: sdk.TokenId(18),
43+
Amount: amount,
44+
Fee: fee,
45+
Nonce: sdk.Nonce(1),
46+
Timestamp: sdk.TimeStamp(1693472232),
3947
}
4048
tokenSymbol := "DAI"
4149
tx := sdk.NewTransfer(builder)
@@ -68,7 +76,7 @@ func HighLevelTransfer() {
6876
fmt.Println("error rpc req: %s", err)
6977
return
7078
}
71-
fmt.Println("ChangePubKey rpc request:", string(JsonTx))
79+
fmt.Println("Transfer rpc request:", string(JsonTx))
7280
// get the testnet url or main net url
7381
zklinkUrl := sdk.ZklinkTestNetUrl()
7482
response, err := http.Post(zklinkUrl, "application/json", bytes.NewBuffer(JsonTx))

examples/Python/2_withdraw.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@ def main():
88
to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9"
99
l2_source_token = 17
1010
l1_target_token = 17
11-
amount = "1234567899808787"
12-
print("Original amount: " + amount)
13-
assert not sdk.is_token_amount_packable(amount)
14-
amount = sdk.closest_packable_token_amount(amount)
15-
assert sdk.is_token_amount_packable(amount)
16-
print("Converted amount: " + amount)
17-
fee = "10000567777"
18-
print("Original fee: " + fee)
19-
assert not sdk.is_fee_amount_packable(fee)
20-
fee = sdk.closest_packable_fee_amount(fee)
21-
assert sdk.is_fee_amount_packable(fee)
22-
print("Converted fee: " + fee)
11+
amount = "1000000"
12+
fee = "1000"
2313
nonce = 1
2414
withdraw_fee_ratio = 50
2515
timestamp = 1000000000

examples/Python/3_transfer.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import zklink_sdk as sdk
2+
3+
def main():
4+
private_key = "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
5+
account_id = 20
6+
to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9"
7+
from_sub_account_id = 1
8+
to_sub_account_id = 1
9+
token = 18
10+
amount = "1234567899808787"
11+
print("Original amount: " + amount)
12+
assert not sdk.is_token_amount_packable(amount)
13+
amount = sdk.closest_packable_token_amount(amount)
14+
assert sdk.is_token_amount_packable(amount)
15+
print("Converted amount: " + amount)
16+
fee = "10000567777"
17+
print("Original fee: " + fee)
18+
assert not sdk.is_fee_amount_packable(fee)
19+
fee = sdk.closest_packable_fee_amount(fee)
20+
assert sdk.is_fee_amount_packable(fee)
21+
print("Converted fee: " + fee)
22+
nonce = 1
23+
timestamp = 1693472232
24+
token_sybmol = "DAI"
25+
26+
builder = sdk.TransferBuilder(
27+
account_id,
28+
to_address,
29+
from_sub_account_id,
30+
to_sub_account_id,
31+
token,
32+
amount,
33+
fee,
34+
nonce,
35+
timestamp
36+
)
37+
tx = sdk.Transfer(builder)
38+
signer = sdk.Signer(private_key, sdk.L1SignerType.ETH())
39+
40+
tx_signature = signer.sign_transfer(tx, "USDT", None, None)
41+
print(tx_signature)
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)