-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Proposal] Transfer '<<' operator #4
Comments
Cool! How do the balance checks happen? What happens if the sender balance is insufficient? |
You can see the details with some comments in this WAS playgound: So in future AS it looks like: class Account {
constructor(
owner: Address,
public balance: U64,
) {
this.owner = owner;
}
static get(address: Address): Account { ... }
// [Currently]
@operator("<<")
static transferTo(quantity: U64, account: Account): U64 {
assert(this.balance > quantity); // check balance
account.balance -= quantity.value;
return quantity;
}
/* Unfortunately we cannot do this now and in future
@operator("<<")
static transferTo(account: Account, quantity: U64): Account {
account.balance += quantity.value;
return account;
}
*/
// but in future we could do something like that
// <---- NOT SUPPORT YET!
@operator("<<")
static transferTo<
Q extends Amount | Account,
T extends Amount | Account
>(a: Q, b: T): Q {
// Handle "quantity << Account.get(from)" case
if (a instanceof Amount && b instanceof Account) {
assert(b.balance >= a); // check balance
b.balance -= a;
return <Q>a;
}
// Handle "Account.get(to) << quantity" case
if (a instanceof Account && b instanceof Amount) {
a.balance += b;
return <Q>a;
}
throw new TypeError("Unsupported generic type combination");
return <Q>0;
}
// ---->
} Currently we can't overload methods with same name so for now we need |
Thanks, great idea, I didn't know this sort of operator overloading was even possible with TS. |
Not in TS. Operator overloading valid only for AssemblyScript. It's pretty useful for math as well. You can investigate how this using for bignum (like u128, i128, u256 and etc) types for example. |
@MaxGraey proposal from lrettig/ewasm-as-wrc20#1:
For EOS smart contracts I proposed this high-level transfer operator:
WDYT about this convention also for ewasm smart contracts?
The text was updated successfully, but these errors were encountered: