Skip to content

Commit

Permalink
chore: cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Nov 29, 2024
1 parent 4c22ff9 commit 1e114a6
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 101 deletions.
6 changes: 3 additions & 3 deletions autonomi/examples/metamask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ const executeQuotePayments = async (sender, quotes, quotePayments) => {
quotePayments
);

// Form approve to transaction tokens calldata
// Form approve to spend tokens calldata
const approveCalldata = autonomi.getApproveToSpendTokensCalldata(
evmNetwork,
payForQuotesCalldata.approve_transactioner,
payForQuotesCalldata.approve_spender,
payForQuotesCalldata.approve_amount
);

console.log("Sending approve transaction..");

// Approve to transaction tokens
// Approve to spend tokens
let hash = await sendTransaction({
from: sender,
to: approveCalldata[1],
Expand Down
14 changes: 6 additions & 8 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ mod external_signer {
use crate::client::external_signer::encrypt_data;
use crate::client::payment::Receipt;
use crate::receipt_from_quotes_and_payments;
use sn_evm::external_signer::{
approve_to_transaction_tokens_calldata, pay_for_quotes_calldata,
};
use sn_evm::external_signer::{approve_to_spend_tokens_calldata, pay_for_quotes_calldata};
use sn_evm::EvmNetwork;
use sn_evm::QuotePayment;
use sn_evm::{Amount, PaymentQuote};
Expand Down Expand Up @@ -742,17 +740,17 @@ mod external_signer {
Ok(js_value)
}

/// Form approve to transaction tokens calldata.
/// Form approve to spend tokens calldata.
#[wasm_bindgen(js_name = getApproveToSpendTokensCalldata)]
pub fn get_approve_to_transaction_tokens_calldata(
pub fn get_approve_to_spend_tokens_calldata(
network: JsValue,
transactioner: JsValue,
spender: JsValue,
amount: JsValue,
) -> Result<JsValue, JsError> {
let network: EvmNetwork = serde_wasm_bindgen::from_value(network)?;
let transactioner: EvmAddress = serde_wasm_bindgen::from_value(transactioner)?;
let spender: EvmAddress = serde_wasm_bindgen::from_value(spender)?;
let amount: Amount = serde_wasm_bindgen::from_value(amount)?;
let calldata = approve_to_transaction_tokens_calldata(&network, transactioner, amount);
let calldata = approve_to_spend_tokens_calldata(&network, spender, amount);
let js_value = serde_wasm_bindgen::to_value(&calldata)?;
Ok(js_value)
}
Expand Down
19 changes: 9 additions & 10 deletions autonomi/tests/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,20 @@ async fn pay_for_content_addresses(
// Init an external wallet provider. In the webapp, this would be MetaMask for example
let provider = wallet.to_provider();

// Form approve to transaction tokens transaction data
let approve_calldata =
autonomi::client::external_signer::approve_to_transaction_tokens_calldata(
wallet.network(),
pay_for_quotes_calldata.approve_transactioner,
pay_for_quotes_calldata.approve_amount,
);

// Prepare approve to transaction tokens transaction
// Form approve to spend tokens transaction data
let approve_calldata = autonomi::client::external_signer::approve_to_spend_tokens_calldata(
wallet.network(),
pay_for_quotes_calldata.approve_spender,
pay_for_quotes_calldata.approve_amount,
);

// Prepare approve to spend tokens transaction
let transaction_request = provider
.transaction_request()
.with_to(approve_calldata.1)
.with_input(approve_calldata.0);

// Send approve to transaction tokens transaction
// Send approve to spend tokens transaction
let _tx_hash = provider
.send_transaction(transaction_request)
.await?
Expand Down
12 changes: 6 additions & 6 deletions evmlib/artifacts/AutonomiNetworkToken.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"inputs": [
{
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
},
{
Expand Down Expand Up @@ -135,7 +135,7 @@
"inputs": [
{
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
}
],
Expand Down Expand Up @@ -261,7 +261,7 @@
{
"indexed": true,
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
},
{
Expand Down Expand Up @@ -390,7 +390,7 @@
},
{
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
}
],
Expand All @@ -409,7 +409,7 @@
"inputs": [
{
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
},
{
Expand Down Expand Up @@ -776,7 +776,7 @@
},
{
"internalType": "address",
"name": "transactioner",
"name": "spender",
"type": "address"
},
{
Expand Down
30 changes: 13 additions & 17 deletions evmlib/src/contract/network_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ where
}

/// See how many tokens are approved to be spent.
pub async fn allowance(&self, owner: Address, transactioner: Address) -> Result<U256, Error> {
debug!("Getting allowance of owner: {owner} for transactioner: {transactioner}",);
pub async fn allowance(&self, owner: Address, spender: Address) -> Result<U256, Error> {
debug!("Getting allowance of owner: {owner} for spender: {spender}",);
let balance = self
.contract
.allowance(owner, transactioner)
.allowance(owner, spender)
.call()
.await
.inspect_err(|err| error!("Error getting allowance: {err:?}"))?
._0;
debug!("Allowance of owner: {owner} for transactioner: {transactioner} is: {balance}");
debug!("Allowance of owner: {owner} for spender: {spender} is: {balance}");
Ok(balance)
}

/// Approve transactioner to transaction a raw amount of tokens.
pub async fn approve(&self, transactioner: Address, value: U256) -> Result<TxHash, Error> {
debug!("Approving transactioner to transaction raw amt of tokens: {value}");
let (calldata, to) = self.approve_calldata(transactioner, value);
/// Approve spender to spend a raw amount of tokens.
pub async fn approve(&self, spender: Address, value: U256) -> Result<TxHash, Error> {
debug!("Approving spender to spend raw amt of tokens: {value}");
let (calldata, to) = self.approve_calldata(spender, value);

let transaction_request = self
.contract
Expand All @@ -108,13 +108,13 @@ where
.await
.inspect_err(|err| {
error!(
"Error approving transactioner {transactioner:?} to transaction raw amt of tokens {value}: {err:?}"
"Error approving spender {spender:?} to spend raw amt of tokens {value}: {err:?}"
)
})?;

let pending_tx_hash = *pending_tx_builder.tx_hash();

debug!("The approval from sender {transactioner:?} is pending with tx_hash: {pending_tx_hash:?}",);
debug!("The approval from sender {spender:?} is pending with tx_hash: {pending_tx_hash:?}",);

let tx_hash = pending_tx_builder.watch().await.inspect_err(|err| {
error!("Error watching approve tx with hash {pending_tx_hash:?}: {err:?}")
Expand All @@ -125,14 +125,10 @@ where
Ok(tx_hash)
}

/// Approve transactioner to transaction a raw amount of tokens.
/// Approve spender to spend a raw amount of tokens.
/// Returns the transaction calldata.
pub fn approve_calldata(&self, transactioner: Address, value: U256) -> (Calldata, Address) {
let calldata = self
.contract
.approve(transactioner, value)
.calldata()
.to_owned();
pub fn approve_calldata(&self, spender: Address, value: U256) -> (Calldata, Address) {
let calldata = self.contract.approve(spender, value).calldata().to_owned();
(calldata, *self.contract.address())
}

Expand Down
16 changes: 8 additions & 8 deletions evmlib/src/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ pub enum Error {
DataPaymentsContract(#[from] data_payments::error::Error),
}

/// Approve an address / smart contract to transaction this wallet's payment tokens.
/// Approve an address / smart contract to spend this wallet's payment tokens.
///
/// Returns the transaction calldata (input, to).
pub fn approve_to_transaction_tokens_calldata(
pub fn approve_to_spend_tokens_calldata(
network: &Network,
transactioner: Address,
spender: Address,
value: U256,
) -> (Calldata, Address) {
let provider = http_provider(network.rpc_url().clone());
let network_token = NetworkToken::new(*network.payment_token_address(), provider);
network_token.approve_calldata(transactioner, value)
network_token.approve_calldata(spender, value)
}

/// Transfer payment tokens from the supplied wallet to an address.
Expand All @@ -53,14 +53,14 @@ pub fn transfer_tokens_calldata(
pub struct PayForQuotesCalldataReturnType {
pub batched_calldata_map: HashMap<Calldata, Vec<QuoteHash>>,
pub to: Address,
pub approve_transactioner: Address,
pub approve_spender: Address,
pub approve_amount: Amount,
}

/// Use this wallet to pay for chunks in batched transfer transactions.
/// If the amount of transfers is more than one transaction can contain, the transfers will be split up over multiple transactions.
///
/// Returns PayForQuotesCalldataReturnType, containing calldata of the transaction batches along with the approval details for the transactioner.
/// Returns PayForQuotesCalldataReturnType, containing calldata of the transaction batches along with the approval details for the spender.
pub fn pay_for_quotes_calldata<T: IntoIterator<Item = QuotePayment>>(
network: &Network,
payments: T,
Expand All @@ -69,7 +69,7 @@ pub fn pay_for_quotes_calldata<T: IntoIterator<Item = QuotePayment>>(

let total_amount = payments.iter().map(|(_, _, amount)| amount).sum();

let approve_transactioner = *network.data_payments_address();
let approve_spender = *network.data_payments_address();
let approve_amount = total_amount;

let provider = http_provider(network.rpc_url().clone());
Expand All @@ -90,7 +90,7 @@ pub fn pay_for_quotes_calldata<T: IntoIterator<Item = QuotePayment>>(
Ok(PayForQuotesCalldataReturnType {
batched_calldata_map: calldata_map,
to: *data_payments.contract.address(),
approve_transactioner,
approve_spender,
approve_amount,
})
}
40 changes: 18 additions & 22 deletions evmlib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,18 @@ impl Wallet {
transfer_gas_tokens(self.wallet.clone(), &self.network, to, amount).await
}

/// See how many tokens of the owner may be spent by the transactioner.
pub async fn token_allowance(
&self,
transactioner: Address,
) -> Result<U256, network_token::Error> {
token_allowance(&self.network, self.address(), transactioner).await
/// See how many tokens of the owner may be spent by the spender.
pub async fn token_allowance(&self, spender: Address) -> Result<U256, network_token::Error> {
token_allowance(&self.network, self.address(), spender).await
}

/// Approve an address / smart contract to transaction this wallet's payment tokens.
pub async fn approve_to_transaction_tokens(
/// Approve an address / smart contract to spend this wallet's payment tokens.
pub async fn approve_to_spend_tokens(
&self,
transactioner: Address,
spender: Address,
amount: U256,
) -> Result<TxHash, network_token::Error> {
approve_to_transaction_tokens(self.wallet.clone(), &self.network, transactioner, amount)
.await
approve_to_spend_tokens(self.wallet.clone(), &self.network, spender, amount).await
}

/// Pays for a single quote. Returns transaction hash of the payment.
Expand Down Expand Up @@ -227,29 +223,29 @@ pub async fn balance_of_gas_tokens(
Ok(balance)
}

/// See how many tokens of the owner may be spent by the transactioner.
/// See how many tokens of the owner may be spent by the spender.
pub async fn token_allowance(
network: &Network,
owner: Address,
transactioner: Address,
spender: Address,
) -> Result<U256, network_token::Error> {
debug!("Getting allowance for owner: {owner} and transactioner: {transactioner}",);
debug!("Getting allowance for owner: {owner} and spender: {spender}",);
let provider = http_provider(network.rpc_url().clone());
let network_token = NetworkToken::new(*network.payment_token_address(), provider);
network_token.allowance(owner, transactioner).await
network_token.allowance(owner, spender).await
}

/// Approve an address / smart contract to transaction this wallet's payment tokens.
pub async fn approve_to_transaction_tokens(
/// Approve an address / smart contract to spend this wallet's payment tokens.
pub async fn approve_to_spend_tokens(
wallet: EthereumWallet,
network: &Network,
transactioner: Address,
spender: Address,
amount: U256,
) -> Result<TxHash, network_token::Error> {
debug!("Approving address/smart contract with {amount} tokens at address: {transactioner}",);
debug!("Approving address/smart contract with {amount} tokens at address: {spender}",);
let provider = http_provider_with_wallet(network.rpc_url().clone(), wallet);
let network_token = NetworkToken::new(*network.payment_token_address(), provider);
network_token.approve(transactioner, amount).await
network_token.approve(spender, amount).await
}

/// Transfer payment tokens from the supplied wallet to an address.
Expand Down Expand Up @@ -323,8 +319,8 @@ pub async fn pay_for_quotes<T: IntoIterator<Item = QuotePayment>>(

// TODO: Get rid of approvals altogether, by using permits or whatever..
if allowance < total_amount_to_be_paid {
// Approve the contract to transaction all the client's tokens.
approve_to_transaction_tokens(
// Approve the contract to spend all the client's tokens.
approve_to_spend_tokens(
wallet.clone(),
network,
*network.data_payments_address(),
Expand Down
8 changes: 4 additions & 4 deletions evmlib/tests/network_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ async fn test_approve() {
let account = wallet_address(network_token.contract.provider().wallet());

let transaction_value = U256::from(1);
let transactioner = PrivateKeySigner::random();
let spender = PrivateKeySigner::random();

// Approve for the transactioner to transaction a value from the funds of the owner (our default account).
// Approve for the spender to spend a value from the funds of the owner (our default account).
let approval_result = network_token
.approve(transactioner.address(), transaction_value)
.approve(spender.address(), transaction_value)
.await;

assert!(
Expand All @@ -86,7 +86,7 @@ async fn test_approve() {

let allowance = network_token
.contract
.allowance(account, transactioner.address())
.allowance(account, spender.address())
.call()
.await
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions node-launchpad/.config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"<Shift-q>": "Quit",
"<Q>": "Quit",
"<Ctrl-c>": "Quit",
"<Ctrl-z>": "Sutransaction" // Sutransaction the application
"<Ctrl-z>": "Suspend" // Suspend the application
},
"Options": {
"<s>": {"SwitchScene":"Status"},
Expand Down Expand Up @@ -67,7 +67,7 @@
"<Shift-q>": "Quit",
"<Q>": "Quit",
"<Ctrl-c>": "Quit",
"<Ctrl-z>": "Sutransaction" // Sutransaction the application
"<Ctrl-z>": "Suspend" // Suspend the application
},
"Help": {
"<s>": {"SwitchScene":"Status"},
Expand All @@ -82,7 +82,7 @@
"<Shift-q>": "Quit",
"<Q>": "Quit",
"<Ctrl-c>": "Quit",
"<Ctrl-z>": "Sutransaction" // Sutransaction the application
"<Ctrl-z>": "Suspend" // Suspend the application
}
}
}
Loading

0 comments on commit 1e114a6

Please sign in to comment.