Tokens
Transfer Tokens
A contract that takes a payment in tokens from a user.
Showcases how to transfer tokens and how to use macro $THIS_ADDRESS to obtain address of the contract.
contract test {
import Runtime;
import Token;
public paySomething(from:address, quantity:number)
{
Runtime.expect(Runtime.isWitness(from), "witness failed");
local price: number = 10;
price *= quantity;
local thisAddr:address = $THIS_ADDRESS;
Token.transfer(from, thisAddr, "SOUL", price);
// Deliver the purchased item or record the purchase here.
}
}Token Flags
There are also some built-in enums, like TokenFlags.\
contract test {
import Runtime;
import Token;
public paySomething(from:address, amount:number, symbol:string)
{
Runtime.expect(Runtime.isWitness(from), "witness failed");
local flags:TokenFlags = Token.getFlags(symbol);
if (flags.isSet(TokenFlags.Fungible)) {
local thisAddr:address = $THIS_ADDRESS;
Token.transfer(from, thisAddr, "SOUL", amount);
}
}
}