Skip to content

Commit

Permalink
transfer examples for uniffi bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
zksemi committed Jun 25, 2024
1 parent e0e5bce commit 3b659ef
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ GO_FILES = 1_change_pubkey 2_withdraw 3_transfer 4_forced_exit 5_order_matching
RUN_GO_EXAMPLES = $(patsubst %, run_example_go_%, $(GO_FILES))
run_example_go: ${RUN_GO_EXAMPLES}

PY_FILES = 1_change_pubkey 2_withdraw 5_order_matching
PY_FILES = 1_change_pubkey 2_withdraw 3_transfer 5_order_matching
RUN_PYTHON_EXAMPLES = $(patsubst %, run_example_python_%, $(PY_FILES))
run_example_python: ${RUN_PYTHON_EXAMPLES}

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

CPP_FILES = 1_change_pubkey 2_withdraw 5_order_matching
CPP_FILES = 1_change_pubkey 2_withdraw 3_transfer 5_order_matching
RUN_CPP_EXAMPLES = $(patsubst %, run_example_cpp_%, $(CPP_FILES))
run_example_cpp: ${RUN_CPP_EXAMPLES}
10 changes: 2 additions & 8 deletions examples/Cpp/2_withdraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ int main() {
ZkLinkAddress to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9";
TokenId l2_source_token = 17;
TokenId l1_target_token = 17;
BigUint amount = "1234567899808787";
cout << "Original amount: " << amount << "\n";
amount = closest_packable_token_amount(amount);
cout << "Converted amount: " << amount << "\n";
BigUint fee = "10000567777";
cout << "Original fee: " << fee << "\n";
fee = closest_packable_fee_amount(fee);
cout << "Converted fee: " << fee << "\n";
BigUint amount = "1000000";
BigUint fee = "1000";
Nonce nonce = 1;
uint16_t withdraw_fee_ratio = 50;
TimeStamp timestamp = 1000000000;
Expand Down
42 changes: 42 additions & 0 deletions examples/Cpp/3_transfer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include "generated/zklink_sdk.hpp"

using namespace std;
using namespace zklink_sdk;

int main() {
string private_key = "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4";
AccountId account_id = 20;
ZkLinkAddress to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9";
SubAccountId from_sub_account_id = 1;
SubAccountId to_sub_account_id = 1;
TokenId token = 18;
BigUint amount = "1234567899808787";
cout << "Original amount: " << amount << "\n";
amount = closest_packable_token_amount(amount);
cout << "Converted amount: " << amount << "\n";
BigUint fee = "10000567777";
cout << "Original fee: " << fee << "\n";
fee = closest_packable_fee_amount(fee);
cout << "Converted fee: " << fee << "\n";
Nonce nonce = 1;
TimeStamp timestamp = 1693472232;
string token_sybmol = "DAI";

TransferBuilder builder = {
account_id,
to_address,
from_sub_account_id,
to_sub_account_id,
token,
amount,
fee,
nonce,
timestamp
};
shared_ptr<Transfer> tx = Transfer::init(builder);
shared_ptr<Signer> signer = Signer::init(private_key, L1SignerType::kEth{});
TxSignature signature = signer->sign_transfer(tx, token_sybmol, {}, {});
cout << signature.tx << "\n";
return 0;
}
10 changes: 2 additions & 8 deletions examples/Golang/2_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ func HighLevelWithdraw() {
toAddress := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
l2SourceToken := sdk.TokenId(17)
l1TargetToken := sdk.TokenId(17)
amount := *big.NewInt(1234567899808787)
fmt.Println("Original amount: ", amount)
amount = sdk.ClosestPackableTokenAmount(amount)
fmt.Println("Converted amount:s", amount)
fee := *big.NewInt(10000567777)
fmt.Println("Original fee: ", fee)
fee = sdk.ClosestPackableFeeAmount(fee)
fmt.Println("Converted fee: ", fee)
amount := *big.NewInt(1000000)
fee := *big.NewInt(1000)
nonce := sdk.Nonce(1)
withdrawFeeRatio := uint16(50)
timestamp := sdk.TimeStamp(1000000000)
Expand Down
28 changes: 18 additions & 10 deletions examples/Golang/3_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ type SubmiterSignature struct {
func HighLevelTransfer() {
privateKey := "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
address := sdk.ZkLinkAddress("0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9")
amount := *big.NewInt(1234567899808787)
fmt.Println("Original amount: ", amount)
amount = sdk.ClosestPackableTokenAmount(amount)
fmt.Println("Converted amount:s", amount)
fee := *big.NewInt(10000567777)
fmt.Println("Original fee: ", fee)
fee = sdk.ClosestPackableFeeAmount(fee)
fmt.Println("Converted fee: ", fee)
builder := sdk.TransferBuilder {
sdk.AccountId(20),
address,
sdk.SubAccountId(1),
sdk.SubAccountId(1),
sdk.TokenId(18),
*big.NewInt(100000),
*big.NewInt(100),
sdk.Nonce(1),
sdk.TimeStamp(1693472232),
AccountId: sdk.AccountId(20),
ToAddress: address,
FromSubAccountId: sdk.SubAccountId(1),
ToSubAccountId: sdk.SubAccountId(1),
Token: sdk.TokenId(18),
Amount: amount,
Fee: fee,
Nonce: sdk.Nonce(1),
Timestamp: sdk.TimeStamp(1693472232),
}
tokenSymbol := "DAI"
tx := sdk.NewTransfer(builder)
Expand Down Expand Up @@ -68,7 +76,7 @@ func HighLevelTransfer() {
fmt.Println("error rpc req: %s", err)
return
}
fmt.Println("ChangePubKey rpc request:", string(JsonTx))
fmt.Println("Transfer rpc request:", string(JsonTx))
// get the testnet url or main net url
zklinkUrl := sdk.ZklinkTestNetUrl()
response, err := http.Post(zklinkUrl, "application/json", bytes.NewBuffer(JsonTx))
Expand Down
14 changes: 2 additions & 12 deletions examples/Python/2_withdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,8 @@ def main():
to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9"
l2_source_token = 17
l1_target_token = 17
amount = "1234567899808787"
print("Original amount: " + amount)
assert not sdk.is_token_amount_packable(amount)
amount = sdk.closest_packable_token_amount(amount)
assert sdk.is_token_amount_packable(amount)
print("Converted amount: " + amount)
fee = "10000567777"
print("Original fee: " + fee)
assert not sdk.is_fee_amount_packable(fee)
fee = sdk.closest_packable_fee_amount(fee)
assert sdk.is_fee_amount_packable(fee)
print("Converted fee: " + fee)
amount = "1000000"
fee = "1000"
nonce = 1
withdraw_fee_ratio = 50
timestamp = 1000000000
Expand Down
44 changes: 44 additions & 0 deletions examples/Python/3_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import zklink_sdk as sdk

def main():
private_key = "0xbe725250b123a39dab5b7579334d5888987c72a58f4508062545fe6e08ca94f4"
account_id = 20
to_address = "0xAFAFf3aD1a0425D792432D9eCD1c3e26Ef2C42E9"
from_sub_account_id = 1
to_sub_account_id = 1
token = 18
amount = "1234567899808787"
print("Original amount: " + amount)
assert not sdk.is_token_amount_packable(amount)
amount = sdk.closest_packable_token_amount(amount)
assert sdk.is_token_amount_packable(amount)
print("Converted amount: " + amount)
fee = "10000567777"
print("Original fee: " + fee)
assert not sdk.is_fee_amount_packable(fee)
fee = sdk.closest_packable_fee_amount(fee)
assert sdk.is_fee_amount_packable(fee)
print("Converted fee: " + fee)
nonce = 1
timestamp = 1693472232
token_sybmol = "DAI"

builder = sdk.TransferBuilder(
account_id,
to_address,
from_sub_account_id,
to_sub_account_id,
token,
amount,
fee,
nonce,
timestamp
)
tx = sdk.Transfer(builder)
signer = sdk.Signer(private_key, sdk.L1SignerType.ETH())

tx_signature = signer.sign_transfer(tx, "USDT", None, None)
print(tx_signature)

if __name__ == "__main__":
main()

0 comments on commit 3b659ef

Please sign in to comment.