-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { StrKey, scValToNative, xdr } from "stellar-sdk"; | ||
|
||
export const scValByType = (scVal: xdr.ScVal) => { | ||
switch (scVal.switch()) { | ||
case xdr.ScValType.scvAddress(): { | ||
const address = scVal.address(); | ||
const addressType = address.switch(); | ||
if (addressType.name === "scAddressTypeAccount") { | ||
return StrKey.encodeEd25519PublicKey(address.accountId().ed25519()); | ||
} | ||
return StrKey.encodeContract(address.contractId()); | ||
} | ||
|
||
case xdr.ScValType.scvBool(): { | ||
return scVal.b(); | ||
} | ||
|
||
case xdr.ScValType.scvBytes(): { | ||
return JSON.stringify(scVal.bytes().toJSON().data); | ||
} | ||
|
||
case xdr.ScValType.scvContractInstance(): { | ||
const instance = scVal.instance(); | ||
return instance.executable().wasmHash()?.toString("base64"); | ||
} | ||
|
||
case xdr.ScValType.scvError(): { | ||
const error = scVal.error(); | ||
return error.value(); | ||
} | ||
|
||
case xdr.ScValType.scvTimepoint(): | ||
case xdr.ScValType.scvDuration(): | ||
case xdr.ScValType.scvI128(): | ||
case xdr.ScValType.scvI256(): | ||
case xdr.ScValType.scvI32(): | ||
case xdr.ScValType.scvI64(): | ||
case xdr.ScValType.scvU128(): | ||
case xdr.ScValType.scvU256(): | ||
case xdr.ScValType.scvU32(): | ||
case xdr.ScValType.scvU64(): { | ||
return scValToNative(scVal).toString(); | ||
} | ||
|
||
case xdr.ScValType.scvLedgerKeyNonce(): | ||
case xdr.ScValType.scvLedgerKeyContractInstance(): { | ||
if (scVal.switch().name === "scvLedgerKeyNonce") { | ||
const val = scVal.nonceKey().nonce(); | ||
return val.toString(); | ||
} | ||
return scVal.value(); | ||
} | ||
|
||
case xdr.ScValType.scvVec(): | ||
case xdr.ScValType.scvMap(): { | ||
return JSON.stringify( | ||
scValToNative(scVal), | ||
(_, val) => (typeof val === "bigint" ? val.toString() : val), | ||
2, | ||
); | ||
} | ||
|
||
case xdr.ScValType.scvString(): | ||
case xdr.ScValType.scvSymbol(): { | ||
const native = scValToNative(scVal); | ||
if (native.constructor === "Uint8Array") { | ||
return native.toString(); | ||
} | ||
return native; | ||
} | ||
|
||
case xdr.ScValType.scvVoid(): { | ||
return null; | ||
} | ||
|
||
default: | ||
return null; | ||
} | ||
}; |