Skip to content

Commit

Permalink
Merge branch 'main' into crowdfund
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy authored Sep 22, 2022
2 parents 24d93f5 + 873209d commit fc40640
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 27,129 deletions.
41 changes: 12 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ backed by smart contracts on Stellar.

1. Install the soroban-cli from https://soroban.stellar.org/docs/getting-started/setup#install-the-soroban-cli
2. Run `./initialize.sh` to load the contracts and initialize it.
- Note: this will create a `.soroban` sub-directory, to contain the sandbox
network data.
3. Run the backend with `soroban-cli serve`

### Frontend
Expand All @@ -17,8 +19,6 @@ Then, run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
Expand All @@ -43,30 +43,13 @@ Then via the web UI, users should be able to:
- See their deposit(s) appear on the page as the transactions are confirmed.
- "Live"-Update the page with the total amount with the new amount

## TODO

- [ ] Build the proper backend rpc server, and update instructions.
- [ ] Local env setup script/walkthrough
- [ ] Contract deployment walkthrough and/or tooling
- [ ] Contract tests
- [ ] Sending a txn to update data in the contract
- [ ] txnbuilding with new xdr
- [ ] Waiting for the txn success and showing new value
- how to poll/stream these changes?
- should just be submitting the txn to horizon, and waiting for the result,
then refreshing from the rpc node.
- The more interesting bit is if other users change the value. How do we make
it "multiplayer" efficiently?
- [ ] clean up the debris in the codebase
- [ ] clean up api provider thing, and setup process. can simplify a lot for us for now.
- [ ] dropdowns for network, and disconnect menu
- freigher can't disconnect yet, afaik
- [ ] strongly-typed contract rpc querying
- blocked on manifest format/generation
- [ ] generate a `useContract` hook, that gives a class with methods
- [ ] better scval formatting/parsing
- was struggling with u64s & hypers
- [ ] format scvals to string
- [ ] parse scvals from string
- [ ] nicer loading indicator
- [ ] nicer error handling
## Wallet Integration & Data Fetching

There is a `./wallet` directory, which contains a small library to connect to
the user's freighter wallet, as well as some React hooks to talk to a
soroban-rpc server (e.g. `soroban-cli serve`), to fetch data and send
transactions.

Data from contracts is fetched using the `useContractValue` hook in
`./wallet/hooks/useContractValue.tsx`. Transactions are submitted to the network
using the `useSendTransactions` hook in `./wallet/hooks/useSendTransaction.tsx`.
25 changes: 20 additions & 5 deletions contracts/crowdfund/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,38 @@ fn get_ledger_timestamp(e: &Env) -> u64 {
}

fn get_owner(e: &Env) -> Identifier {
e.contract_data().get_unchecked(DataKey::Owner).unwrap()
e.contract_data()
.get(DataKey::Owner)
.expect("not initialized")
.unwrap()
}

fn get_deadline(e: &Env) -> u64 {
e.contract_data().get_unchecked(DataKey::Deadline).unwrap()
e.contract_data()
.get(DataKey::Deadline)
.expect("not initialized")
.unwrap()
}

fn get_started(e: &Env) -> u64 {
e.contract_data().get_unchecked(DataKey::Started).unwrap()
e.contract_data()
.get(DataKey::Started)
.expect("not initialized")
.unwrap()
}

fn get_target_amount(e: &Env) -> BigInt {
e.contract_data().get_unchecked(DataKey::Target).unwrap()
e.contract_data()
.get(DataKey::Target)
.expect("not initialized")
.unwrap()
}

fn get_token(e: &Env) -> BytesN<32> {
e.contract_data().get_unchecked(DataKey::Token).unwrap()
e.contract_data()
.get(DataKey::Token)
.expect("not initialized")
.unwrap()
}

fn get_user_deposited(e: &Env, user: &Identifier) -> BigInt {
Expand Down
71 changes: 71 additions & 0 deletions convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import BigNumber from 'bignumber.js';
import * as SorobanSdk from 'soroban-sdk';
let xdr = SorobanSdk.xdr;

export function scvalToBigNumber(scval: SorobanSdk.xdr.ScVal | undefined): BigNumber {
let value = scval?.obj()?.bigInt() ?? xdr.ScBigInt.zero();
let sign = BigInt(1);
switch (value.switch()) {
case SorobanSdk.xdr.ScNumSign.zero():
return BigNumber(0);
case SorobanSdk.xdr.ScNumSign.positive():
sign = BigInt(1);
break;
case SorobanSdk.xdr.ScNumSign.negative():
sign = BigInt(-1);
break;
}

let b = BigInt(0);
for (let byte of value.magnitude()) {
b <<= BigInt(8);
b |= BigInt(byte);
};

return BigNumber((b * sign).toString());
}

// TODO: Not sure this handles negatives right
export function bigNumberToScBigInt(value: BigNumber): SorobanSdk.xdr.ScVal {
const b: bigint = BigInt(value.toFixed(0));
if (b == BigInt(0)) {
return xdr.ScVal.scvObject(xdr.ScObject.scoBigInt(xdr.ScBigInt.zero()));
}
const buf = bnToBuf(b);
if (b > BigInt(0)) {
return xdr.ScVal.scvObject(xdr.ScObject.scoBigInt(xdr.ScBigInt.positive(buf)));
} else {
return xdr.ScVal.scvObject(xdr.ScObject.scoBigInt(xdr.ScBigInt.negative(buf)));
}
}

export function bigintToBuf(bn: bigint): Buffer {
var hex = BigInt(bn).toString(16);
if (hex.length % 2) { hex = '0' + hex; }

var len = hex.length / 2;
var u8 = new Uint8Array(len);

var i = 0;
var j = 0;
while (i < len) {
u8[i] = parseInt(hex.slice(j, j+2), 16);
i += 1;
j += 2;
}

return Buffer.from(u8);
}

export function xdrUint64ToNumber(value: SorobanSdk.xdr.Uint64): number {
let b = 0;
b |= value.high;
b <<= 8;
b |= value.low;
return b;
}

export function scvalToString(value: SorobanSdk.xdr.ScVal): string | undefined {
return value.obj()?.bin().toString();
}

27 changes: 0 additions & 27 deletions jsonrpc/index.ts

This file was deleted.

Loading

0 comments on commit fc40640

Please sign in to comment.