abi Wallet {
#[storage(read, write), payable]
fn receive_funds();
#[storage(read, write)]
fn send_funds(amount_to_send: u64, recipient_address: Address);
}
use std::{
asset::transfer_to_address,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
};
use wallet_abi::Wallet;
const OWNER_ADDRESS = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861);
storage {
balance: u64 = 0,
}
impl Wallet for Contract {
#[storage(read, write), payable]
fn receive_funds() {
if msg_asset_id() == BASE_ASSET_ID {
// If we received `BASE_ASSET_ID` then keep track of the balance.
// Otherwise, we're receiving other native assets and don't care
// about our balance of coins.
storage.balance.write(storage.balance.read() + msg_amount());
}
}
#[storage(read, write)]
fn send_funds(amount_to_send: u64, recipient_address: Address) {
let sender = msg_sender().unwrap();
match sender {
Identity::Address(addr) => assert(addr == OWNER_ADDRESS),
_ => revert(0),
};
let current_balance = storage.balance.read();
assert(current_balance >= amount_to_send);
storage.balance.write(current_balance - amount_to_send);
// Note: `transfer_to_address()` is not a call and thus not an
// interaction. Regardless, this code conforms to
// checks-effects-interactions to avoid re-entrancy.
transfer_to_address(recipient_address, BASE_ASSET_ID, amount_to_send);
}
}```
I've been trying to learn sway via examples given in the docs but stuck at this error. Am i making any syntactical error or missing out on something?
In Sway, each file must contain a single library
, predicate
, script
or contract
. In the code you provided, you are combining a library
, without specifying it as the context, in the same file as a contract
without specifying it and then trying to import the library.
This example combines the ABI interface in the library with the contract correctly, in a single file:
contract;
use std::{
asset::transfer_to_address,
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
};
const OWNER_ADDRESS = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861);
abi Wallet {
#[storage(read, write), payable]
fn receive_funds();
#[storage(read, write)]
fn send_funds(amount_to_send: u64, recipient_address: Address);
}
storage {
balance: u64 = 0,
}
impl Wallet for Contract {
#[storage(read, write), payable]
fn receive_funds() {
if msg_asset_id() == BASE_ASSET_ID {
// If we received `BASE_ASSET_ID` then keep track of the balance.
// Otherwise, we're receiving other native assets and don't care
// about our balance of coins.
storage.balance.write(storage.balance.read() + msg_amount());
}
}
#[storage(read, write)]
fn send_funds(amount_to_send: u64, recipient_address: Address) {
let sender = msg_sender().unwrap();
match sender {
Identity::Address(addr) => assert(addr == OWNER_ADDRESS),
_ => revert(0),
};
let current_balance = storage.balance.read();
assert(current_balance >= amount_to_send);
storage.balance.write(current_balance - amount_to_send);
// Note: `transfer_to_address()` is not a call and thus not an
// interaction. Regardless, this code conforms to
// checks-effects-interactions to avoid re-entrancy.
transfer_to_address(recipient_address, BASE_ASSET_ID, amount_to_send);
}
}
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.