|
| 1 | +use anyhow::Result; |
| 2 | +use fil_token::blockstore::Blockstore; |
| 3 | +use fil_token::runtime::FvmRuntime; |
| 4 | +use fil_token::token::types::*; |
| 5 | +use fil_token::token::{Token, TokenHelper}; |
| 6 | +use fvm_ipld_encoding::{RawBytes, DAG_CBOR}; |
| 7 | +use fvm_sdk as sdk; |
| 8 | +use fvm_shared::bigint::bigint_ser::BigIntSer; |
| 9 | +use fvm_shared::econ::TokenAmount; |
| 10 | +use sdk::NO_DATA_BLOCK_ID; |
| 11 | + |
| 12 | +struct WfilToken { |
| 13 | + /// Default token helper impl |
| 14 | + util: TokenHelper<Blockstore, FvmRuntime>, |
| 15 | +} |
| 16 | + |
| 17 | +// TODO: Wrapper is unecessary? |
| 18 | +// Instead expose a |
| 19 | +impl Token for WfilToken { |
| 20 | + fn name(&self) -> String { |
| 21 | + String::from("Wrapped FIL") |
| 22 | + } |
| 23 | + |
| 24 | + fn symbol(&self) -> String { |
| 25 | + String::from("WFIL") |
| 26 | + } |
| 27 | + |
| 28 | + fn total_supply(&self) -> TokenAmount { |
| 29 | + self.util.total_supply() |
| 30 | + } |
| 31 | + |
| 32 | + fn balance_of( |
| 33 | + &self, |
| 34 | + params: fvm_shared::address::Address, |
| 35 | + ) -> Result<fvm_shared::econ::TokenAmount> { |
| 36 | + self.util.balance_of(params) |
| 37 | + } |
| 38 | + |
| 39 | + fn increase_allowance(&self, params: ChangeAllowanceParams) -> Result<AllowanceReturn> { |
| 40 | + self.util.increase_allowance(params) |
| 41 | + } |
| 42 | + |
| 43 | + fn decrease_allowance(&self, params: ChangeAllowanceParams) -> Result<AllowanceReturn> { |
| 44 | + self.util.decrease_allowance(params) |
| 45 | + } |
| 46 | + |
| 47 | + fn revoke_allowance(&self, params: RevokeAllowanceParams) -> Result<AllowanceReturn> { |
| 48 | + self.util.revoke_allowance(params) |
| 49 | + } |
| 50 | + |
| 51 | + fn allowance(&self, params: GetAllowanceParams) -> Result<AllowanceReturn> { |
| 52 | + self.util.allowance(params) |
| 53 | + } |
| 54 | + |
| 55 | + fn burn(&self, params: BurnParams) -> Result<BurnReturn> { |
| 56 | + self.util.burn(params) |
| 57 | + } |
| 58 | + |
| 59 | + fn transfer_from(&self, params: TransferParams) -> Result<TransferReturn> { |
| 60 | + self.util.transfer_from(params) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Placeholder invoke for testing |
| 65 | +#[no_mangle] |
| 66 | +pub fn invoke(params: u32) -> u32 { |
| 67 | + // Conduct method dispatch. Handle input parameters and return data. |
| 68 | + let method_num = sdk::message::method_number(); |
| 69 | + |
| 70 | + let token_actor = WfilToken { |
| 71 | + util: TokenHelper::new(Blockstore {}, FvmRuntime {}), |
| 72 | + }; |
| 73 | + |
| 74 | + //TODO: this internal dispatch can be pushed as a library function into the fil_token crate |
| 75 | + // - it should support a few different calling-conventions |
| 76 | + // - it should also handle deserialization of raw_params into the expected IPLD types |
| 77 | + let res = match method_num { |
| 78 | + // Actor constructor |
| 79 | + 1 => constructor(), |
| 80 | + // Standard token interface |
| 81 | + 2 => { |
| 82 | + token_actor.name(); |
| 83 | + // TODO: store and return CID |
| 84 | + NO_DATA_BLOCK_ID |
| 85 | + } |
| 86 | + 3 => { |
| 87 | + token_actor.symbol(); |
| 88 | + // TODO: store and return CID |
| 89 | + NO_DATA_BLOCK_ID |
| 90 | + } |
| 91 | + 4 => { |
| 92 | + token_actor.total_supply(); |
| 93 | + // TODO: store and return CID |
| 94 | + NO_DATA_BLOCK_ID |
| 95 | + } |
| 96 | + 5 => { |
| 97 | + // balance of |
| 98 | + let params = sdk::message::params_raw(params).unwrap().1; |
| 99 | + let params = RawBytes::new(params); |
| 100 | + let params = params.deserialize().unwrap(); |
| 101 | + let res = token_actor.balance_of(params).unwrap(); |
| 102 | + let res = RawBytes::new(fvm_ipld_encoding::to_vec(&BigIntSer(&res)).unwrap()); |
| 103 | + let cid = sdk::ipld::put_block(DAG_CBOR, res.bytes()).unwrap(); |
| 104 | + cid |
| 105 | + } |
| 106 | + 6 => { |
| 107 | + // increase allowance |
| 108 | + NO_DATA_BLOCK_ID |
| 109 | + } |
| 110 | + 7 => { |
| 111 | + // decrease allowance |
| 112 | + NO_DATA_BLOCK_ID |
| 113 | + } |
| 114 | + 8 => { |
| 115 | + // revoke_allowance |
| 116 | + NO_DATA_BLOCK_ID |
| 117 | + } |
| 118 | + 9 => { |
| 119 | + // allowance |
| 120 | + NO_DATA_BLOCK_ID |
| 121 | + } |
| 122 | + 10 => { |
| 123 | + // burn |
| 124 | + NO_DATA_BLOCK_ID |
| 125 | + } |
| 126 | + 11 => { |
| 127 | + // transfer_from |
| 128 | + NO_DATA_BLOCK_ID |
| 129 | + } |
| 130 | + // Custom actor interface |
| 131 | + 12 => { |
| 132 | + // Mint |
| 133 | + let params = MintParams { |
| 134 | + initial_holder: sdk::message::caller(), |
| 135 | + value: sdk::message::value_received(), |
| 136 | + }; |
| 137 | + let res = token_actor.util.mint(params).unwrap(); |
| 138 | + let res = RawBytes::new(fvm_ipld_encoding::to_vec(&res).unwrap()); |
| 139 | + let cid = sdk::ipld::put_block(DAG_CBOR, res.bytes()).unwrap(); |
| 140 | + cid |
| 141 | + } |
| 142 | + _ => { |
| 143 | + sdk::vm::abort( |
| 144 | + fvm_shared::error::ExitCode::USR_ILLEGAL_ARGUMENT.value(), |
| 145 | + Some("Unknown method number"), |
| 146 | + ); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + res |
| 151 | +} |
| 152 | + |
| 153 | +fn constructor() -> u32 { |
| 154 | + 0_u32 |
| 155 | +} |
0 commit comments